norfair
repository·master·Indexed 25 days ago
https://github.com/tryolabs/norfairA lightweight Python library for adding real-time multi-object tracking to any detector. Norfair converts detector outputs into coordinate-based detections and supports camera movement estimation via translation and homographies to stabilize tracking. It can be integrated with various detectors including YOLO, Detectron2, MMDetection, AlphaPose, and OpenPose, and supports Re-identification (ReID) using appearance embeddings to maintain tracks during occlusions.
What's inside norfair
- Norfair is a modular object tracking library designed to be easily integrated with various state-of-the-art (SOTA) object detectors. Unlike trackers that are hardcoded to specific detection formats (like bounding boxes), Norfair can work with any detector by supporting a variable number of points per detection. It also allows for heavy customization through user-defined distance functions.
Understand the Detectron2 tracking implementation
masterThe Detectron2 demo implementation tracks objects by using a single point per detection. Specifically, it calculates the centroid of the bounding boxes returned by the Detectron2 object detector to serve as the tracking point.Understand YOLOv5 tracking logic
masterThe YOLOv5 demo tracks objects by utilizing either a single point or two points per detection. Specifically, it uses either the centroid of the detection or the two corners of the bounding boxes returned by YOLOv5 to perform tracking.Track multiple classes with a single Tracker instance
masterThe keypoints and bounding boxes demo demonstrates Norfair's capability to use a singleTrackerinstance to manage and track objects belonging to different classes simultaneously. It also showcases the ability to visualize both keypoints and bounding boxes for various object types within the same tracking session.Use OpenPose demo to extrapolate detections through skipped frames
masterThe OpenPose demo demonstrates how to speed up inference by making a detector skip frames and using Norfair to extrapolate detections during those skipped frames.
In this specific implementation, the detector skips 1 out of every 2 frames, which can make video processing up to 2 times faster because the computational overhead of Norfair is negligible compared to the cost of deep neural network inference.
Understand ReID logic in Norfair
masterRe-identification (ReID) in Norfair is used when spatial distance matching fails (specifically when
hit_counteris0).Workflow:
- When spatial matching fails, trackers with
hit_counter <= 0attempt to calculate ReID distances. - This continues until
reid_hit_counterreaches0, at which point the tracker is removed. - If a ReID distance match is found, the trackers are merged.
In the provided demo, the embedding is the color of the box (with noise), and the distance function is a histogram comparison between embeddings from
unmatched_init_trackersanddead_objectsagainst those frommatched_not_init_trackers.- When spatial matching fails, trackers with
Run the SAHI and Norfair detection and tracking demo
masterThis demo demonstrates how to combine Norfair with SAHI (Slicing Aided Hyper Inference) and YOLOv5x to perform detection and tracking on small objects.
To run the demo, you must use the provided Docker environment:
- Build and start the Docker container using the
./run.shscript. - Inside the container, execute the demo script by passing a video file as an argument:
python demo.py <video>.mp4.
The process will generate an output video named
output.mp4containing the tracking results.- Build and start the Docker container using the
Add tracking to a detector with Norfair
masterTo use Norfair, you need to convert your detector's output into a list of
norfair.Detectionobjects. Norfair then uses aTrackerto match these detections over time using a distance function (e.g.,"euclidean").Basic workflow:
- Initialize a
Videoobject for frame iteration. - Initialize a
Trackerwith adistance_functionanddistance_threshold. - In a loop, pass detections to
tracker.update(). - Use
draw_tracked_objectsto visualize the results.
import cv2 import numpy as np from detectron2.config import get_cfg from detectron2.engine import DefaultPredictor from norfair import Detection, Tracker, Video, draw_tracked_objects # Set up Detectron2 object detector cfg = get_cfg() cfg.merge_from_file("demos/faster_rcnn_R_50_FPN_3x.yaml") cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 cfg.MODEL.WEIGHTS = "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" detector = DefaultPredictor(cfg) # Norfair video = Video(input_path="video.mp4") tracker = Tracker(distance_function="euclidean", distance_threshold=20) for frame in video: detections = detector(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) detections = [Detection(p) for p in detections['instances'].pred_boxes.get_centers().cpu().numpy()] tracked_objects = tracker.update(detections=detections) draw_tracked_objects(frame, tracked_objects) video.write(frame)- Initialize a
Estimate camera movement in Norfair
masterNorfair can estimate camera movement to stabilize object tracking and calculate trajectories in a fixed reference frame. This is particularly useful when camera motion causes erratic apparent object movement.
There are two primary methods for estimating movement:
- Translation: Works for camera pans and tilts. It helps maintain object IDs when the camera moves horizontally or vertically.
- Homographies: A more robust method that works with any camera movement, including pan, tilt, rotation, movement in any direction, and zoom.
Note: Estimation works best with a static background. Highly chaotic scenes with significant movement may reduce accuracy, though incorrect estimation will not negatively impact the tracking itself.
Run the Moving Camera Demo
masterTo run the moving camera demonstration, follow these steps:
- Build and run the Docker container using the provided script:
./run_gpu.sh - Copy your target video file into the
srcfolder. - Inside the container, execute the demo script with your video file:
python demo.py <video>.mp4
To see all available configuration options, run:
python demo.py --help./run_gpu.sh # After copying video to src/ python demo.py <video>.mp4- Build and run the Docker container using the provided script:
Run the MMDetection tracking example using Docker
masterTo run the MMDetection tracking demo, follow these steps:
- Build and start the GPU-enabled Docker container using the provided script:
./run_gpu.sh - Place the video file you wish to process into the
srcfolder. - Inside the running container, execute the demo script by passing your video file as an argument:
python demo.py <video>.mp4
You can view all available command-line options by running
python demo.py --helpinside the container../run_gpu.sh python demo.py <video>.mp4- Build and start the GPU-enabled Docker container using the provided script:
Run the ReID Demo using Docker
masterTo run the Re-identification (ReID) demonstration, which shows how Norfair uses appearance embeddings to maintain tracks during occlusions, follow these steps:
- Build and run the Docker container using the provided script:
./run.sh. - Inside the container, execute the demo script:
python demo.py.
This process generates two video files:
demo.avi: The original simulation video.output.mp4: The result of the tracking with ReID applied.
./run.sh # Inside the container: python demo.py- Build and run the Docker container using the provided script: