levan92 / deep_sort_realtime

A really more real-time adaptation of deep sort
MIT License
167 stars 51 forks source link

It seems more like a question than a problem. Real-time processing is not achieved. #47

Closed DongWan0285 closed 1 year ago

DongWan0285 commented 1 year ago

Hi, First of all, thank you for sharing the code. I am trying to use this library in ROS. Below is my code, but real-time processing is not achieved.

from deep_sort_realtime.deepsort_tracker import DeepSort import numpy as np from datetime import datetime

today = datetime.now().date() os.environ["OMP_NUM_THREADS"] = "1" os.environ["OPENBLAS_NUM_THREADS"] = "1" os.environ["MKL_NUM_THREADS"] = "1" os.environ["VECLIB_MAXIMUM_THREADS"] = "1" os.environ["NUMEXPR_NUM_THREADS"] = "1"

class ImageProcessor: def init(self, max_age, embedder): self.bridge = CvBridge() self.tracker = DeepSort(max_age=max_age, embedder=embedder) self.current_frame = None # Variable to store the most recent frame

    # Initialize publishers
    #self.pub = rospy.Publisher('online_targets', String, queue_size=10)
    self.pub_image = rospy.Publisher('processed_image', Image, queue_size=1)

    # Initialize subscribers
    rospy.Subscriber("/darknet_ros/bounding_boxes", BoundingBoxes, self.bounding_box_callback)
    rospy.Subscriber("/normalized_flipped_image", Image, self.image_callback)

def image_callback(self, data):
    try:
        cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
        print(e)
    self.current_frame = cv_image  # Update the current frame

def bounding_box_callback(self, data):
    # Transform bounding box data to the required format
    dets = []

    for i, box in enumerate(data.bounding_boxes):
        width = box.xmax - box.xmin
        height = box.ymax - box.ymin
        det = [(box.xmin, box.ymin, width, height), box.probability, box.id]
        dets.append(det)

    # Provide the current frame to the update_tracks method
    online_targets = self.tracker.update_tracks(dets, frame=self.current_frame)

    for track in online_targets:
        if not track.is_confirmed():
            continue
        track_id = track.track_id

        ltrb = track.to_ltrb().astype(int)  # Convert to integer
        cv2.rectangle(self.current_frame, (ltrb[0], ltrb[1]), (ltrb[2], ltrb[3]), (0, 255, 0), 2)  # Draw bounding box
        cv2.putText(self.current_frame, str(track_id), (ltrb[0], ltrb[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2)  # Put track_id on top of bounding box

    # Convert the image with bounding boxes to a ROS image message and publish
    ros_image = self.bridge.cv2_to_imgmsg(self.current_frame, "bgr8")
    self.pub_image.publish(ros_image)

    # Here you might need to convert online_targets to a suitable type for publishing
    #self.pub.publish(online_targets)

def main(max_age,embedder): rospy.init_node('image_processor', anonymous=True) ip = ImageProcessor(max_age, embedder) try: rospy.spin() except KeyboardInterrupt: print("Shutting down")

if name == 'main':

Initialize your max_age here

max_age = 30
embedder = "torchreid" #clip_ViT-B/16, torchreid
main(max_age, embedder)
levan92 commented 1 year ago

It's hard to say why real-time processing is not achieved as it varies from case to case. Try to do a speed analysis to see where your chokepoint is at, most likely the detection inference step. This repo simply provides a library to fit multi-object tracking into a video analytics pipeline designed to take in real-time video streams.