Comments (4)
Hello @kenifxyz, a YOLOv8 demo would indeed be beneficial. For now, I'm providing you with code to make Norfair works for your model.
As you pointed out, it's necessary to modify the function that transforms YOLO detections into Norfair detections. The code below adjusts this function to accommodate the new model.
import logging
from typing import List
import numpy as np
import torch
from ultralytics import YOLO
from norfair import Detection, Tracker, Video, draw_boxes
# Disable logging from the ultralytics package
logger_name = "ultralytics"
logging.getLogger(logger_name).setLevel(logging.CRITICAL)
def yolov8_detections_to_norfair_detections(
yolo_detections: torch.tensor, track_points: str = "bbox" # bbox or centroid
) -> List[Detection]:
"""convert yolo_detections to Norfair's detections"""
norfair_detections: List[Detection] = []
if track_points == "centroid":
for object in yolo_detections:
detections_as_xywh = object.boxes.xywh
scores = object.boxes.conf
classes = object.boxes.cls
for detection_as_xywh, score, class_id in zip(detections_as_xywh, scores, classes):
centroid = np.array([detection_as_xywh[0].item(), detection_as_xywh[1].item()])
scores = np.array([score.item()])
norfair_detections.append(
Detection(points=centroid, scores=scores, label=int(class_id))
)
elif track_points == "bbox":
for object in yolo_detections:
detections_as_xyxy = object.boxes.xyxy
scores = object.boxes.conf
classes = object.boxes.cls
for detection_as_xyxy, score, class_id in zip(detections_as_xyxy, scores, classes):
bbox = np.array(
[
[detection_as_xyxy[0].item(), detection_as_xyxy[1].item()],
[detection_as_xyxy[2].item(), detection_as_xyxy[3].item()],
]
)
scores = np.array([score, score])
norfair_detections.append(
Detection(points=bbox, scores=scores, label=int(class_id))
)
return norfair_detections
video = Video(input_path=<video-path>)
tracker = Tracker(distance_function="iou", distance_threshold=0.7)
model = YOLO(<model-path>)
for frame in video:
yolo_detections = model(frame)
detections = yolov8_detections_to_norfair_detections(yolo_detections)
tracked_objects = tracker.update(detections=detections)
frame = draw_boxes(frame, tracked_objects)
video.write(frame)
If you have any further questions, please don't hesitate to ask.
from norfair.
Hey @DiegoFernandezC, that works perfectly! Thanks so much!
from norfair.
This works great. Able to do with yolov8. But if in case you come up with this error, "Canβt convert CUDA tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first", you can solve this by adding this before line 44,
score = score.cpu().numpy()
from norfair.
If I may add something useful to this discussion, I found this snippet of code super handy but kind of sluggish. Therefore, I optimized a bit the function yolov8_detections_to_norfair_detections with a few (tiny) changes. Just for reference, it went from around 2fps rate to more than 5fps. Again thanks for your support @DiegoFernandezC and hope someone else will find this code helpful.
def yolov8_detections_to_norfair_detections(
yolo_detections: torch.tensor, track_points: str = "bbox" # bbox or centroid
) -> List[Detection]:
"""convert yolo_detections to Norfair's detections"""
norfair_detections: List[Detection] = []
if track_points == "centroid":
for object in yolo_detections:
detections_as_xywh = object.boxes.xywh
scores = object.boxes.conf
classes = object.boxes.cls
norfair_detections += [
Detection(
points=np.array([detection_as_xywh[0].item(), detection_as_xywh[1].item()]),
scores=np.array([score.item()]),
label=int(class_id)
)
for detection_as_xywh, score, class_id in zip(detections_as_xywh, scores, classes)
]
elif track_points == "bbox":
for object in yolo_detections:
detections_as_xyxy = object.boxes.xyxy
scores = object.boxes.conf
classes = object.boxes.cls
norfair_detections += [
Detection(
points=np.array([
[detection_as_xyxy[0].item(), detection_as_xyxy[1].item()],
[detection_as_xyxy[2].item(), detection_as_xyxy[3].item()],
]),
scores=np.array([score, score]),
label=int(class_id)
)
for detection_as_xyxy, score, class_id in zip(detections_as_xyxy, scores, classes)
]
return norfair_detections
from norfair.
Related Issues (20)
- MotionEstimator with Reid HOT 2
- Reid in case of occlusion/object going out and coming back in the frame HOT 12
- error: (-28:Unknown error code -28) The input arrays should have at least 4 corresponding point sets to calculate Homography in function 'findHomography' HOT 6
- How to get object/track id and their bbox coordinates for a video? HOT 2
- Question about skip_period HOT 2
- Skipped frames on avi videos
- Suggestions to make the processing faster HOT 5
- Typing issues (example: `Detection.embedding` is `Unknown | None`)
- object ids in tracked_objects skipped a number? HOT 9
- Detection confidence when drawing tracked_objects' boxes? HOT 5
- Any tips on how to use Norfair for re-identification accross cameras HOT 1
- Ask a question about object tracking HOT 3
- Configure logging behavior? HOT 2
- The class of object being tracked changes over time HOT 3
- FixedCamera crashes with an inappropriate point array shape
- Detection age not always set
- Detection age not updated after merging tracked objects
- What are the tracked objects? HOT 6
- Object Placement using Homography HOT 2
- Tracking Algorithm
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from norfair.