cjm-byte-track
A standalone Python implementation of the ByteTrack multi-object tracker based on the official implementation.
A standalone Python implementation of the ByteTrack multi-object tracker based on the official implementation.
Install
pip install cjm_byte_track
Tutorial:
- Real-Time Object Tracking with YOLOX and ByteTrack: Learn how to track objects across video frames with YOLOX and ByteTrack.
How to use
# Import ByteTrack package
from cjm_byte_track.core import BYTETracker
from cjm_byte_track.matching import match_detections_with_tracks
# Initialize a ByteTracker object
= BYTETracker(track_thresh=0.25, track_buffer=30, match_thresh=0.8, frame_rate=frame_fps)
tracker
with tqdm(total=frames, desc="Processing frames") as pbar:
while video_capture.isOpened():
= video_capture.read()
ret, frame if ret:
# Prepare an input image for inference
= prepare_image_for_inference(frame, test_sz, max_stride)
rgb_img, input_dims, offsets, min_img_scale, input_img
# Convert the existing input image to NumPy format
= np.array(input_img, dtype=np.float32).transpose((2, 0, 1))[None]/255
input_tensor_np
# Start performance counter`m
= time.perf_counter()
start_time
# Run inference
= session.run(None, {"input": input_tensor_np})[0]
outputs
# Process the model output
= process_outputs(outputs, input_tensor_np.shape[input_dim_slice], bbox_conf_thresh)
proposals
# Apply non-max suppression to the proposals with the specified threshold
= nms_sorted_boxes(calc_iou(proposals[:, :-2]), iou_thresh)
proposal_indices = proposals[proposal_indices]
proposals
= (proposals[:,:4]+[*offsets, 0, 0])*min_img_scale
bbox_list = [class_names[int(idx)] for idx in proposals[:,4]]
label_list = proposals[:,5]
probs_list
# Update tracker with detections.
= [-1]*len(bbox_list)
track_ids
# Convert to tlbr format
= bbox_list.copy()
tlbr_boxes 2:4] += tlbr_boxes[:, :2]
tlbr_boxes[:,
# Update tracker with detections
= tracker.update(
tracks =np.concatenate([tlbr_boxes, probs_list[:, np.newaxis]], axis=1),
output_results=rgb_img.size,
img_info=rgb_img.size)
img_size= match_detections_with_tracks(tlbr_boxes=tlbr_boxes, track_ids=track_ids, tracks=tracks)
track_ids
# End performance counter
= time.perf_counter()
end_time # Calculate the combined FPS for object detection and tracking
= 1 / (end_time - start_time)
fps # Display the frame rate in the progress bar
=fps)
pbar.set_postfix(fps
# Filter object detections based on tracking results
= zip(*[(bbox, label, prob, track_id)
bbox_list, label_list, probs_list, track_ids for bbox, label, prob, track_id
in zip(bbox_list, label_list, probs_list, track_ids) if track_id != -1])
# Annotate the current frame with bounding boxes and tracking IDs
= draw_bboxes_pil(
annotated_img =rgb_img,
image=bbox_list,
boxes=[f"{track_id}-{label}" for track_id, label in zip(track_ids, label_list)],
labels=probs_list,
probs=[int_colors[class_names.index(i)] for i in label_list],
colors=font_file,
font
)= cv2.cvtColor(np.array(annotated_img), cv2.COLOR_RGB2BGR)
annotated_frame
video_writer.write(annotated_frame)1)
pbar.update(else:
break
video_capture.release() video_writer.release()