Created
Jul 3, 2020 5:36 AM
Tags
Python SDK
Overview
A people tracker which can keep a track of detected people and can re-identify them after occlusions or if they move outside the video frames and reappear later. This guide shows how to use it and includes a demo video
Steps
- Installing dependencies and the python wheel
# copy wheel url from
# https://github.com/getchui/offline_sdk/releases
# and run
# pip install [wheel_url]
# for example for Linux:
pip install https://github.com/getchui/offline_sdk/releases/download/0.6.6/trueface-0.6.6-cp37-cp37m-linux_x86_64.whl
2. Download Models
- Download and unzip the tracker models which include a demo video file test_video.mp4
reidentification/
|--reidentify.py
|--reid.weights
|--track_detector.weights
|--test_video.mp4
- Create a file called reidentify.py inside the re-identify folder the zip file just created.
3. Import SDK and video stream classes
from trueface.reid_tracker import VideoTracker
import cv2
4. Initialize VideoTracker instance
vd = VideoTracker(detector_weight_path='./track_detector.weights',
reid_weight_path='./reid.weights',
license='<your sdk token from creds.json>')
5. Start Video Capture session
video_source = './test_video.mp4' #The path where your video file resides
#Use video_source = 0 if you want to run tracker on your webcam
cap = VideoCapture(video_source)
6. Create main loop
frame_id = 0
while cap.isOpened():
frame_id += 1
ret, img = cap.read()
if ret:
# We do not need to process every frame for tracking
if not frame_id % 3:
ids, box, class_conf = vd.run(img)
# Print ids so you can visually verify detected ids
print(ids)
# If person is detected, draw bounding box around the detection
if len(ids) != 0:
img = vd.draw_box(image=img, bbox=box, identities=ids)
# Show output videostream
cv2.imshow('Trueface.ai', img)
if cv2.waitKey(33) == ord('q'):
break
else:
break
Full Code
from trueface.reid_tracker import VideoTracker
import cv2
vd = VideoTracker(detector_weight_path='./track_detector.weights',
reid_weight_path='./reid.weights',
license='<your sdk token from creds.json>')
video_source = './test_video.mp4' #The path where your video file resides
#Use video_source = 0 if you want to run tracker on your webcam
cap = VideoCapture(video_source)
frame_id = 0
while cap.isOpened():
frame_id += 1
ret, img = cap.read()
if ret:
# We do not need to process every frame for tracking
if not frame_id % 3:
ids, box, class_conf = vd.run(img)
# Print ids so you can visually verify detected ids
print(ids)
# If person is detected, draw bounding box around the detection
if len(ids) != 0:
img = vd.draw_box(image=img, bbox=box, identities=ids)
# Show output videostream
cv2.imshow('Trueface.ai', img)
if cv2.waitKey(33) == ord('q'):
break
else:
break
cap.release()