├── 1.mp4 ├── 2.mp4 ├── README.md ├── helper.py ├── object_detection.py ├── object_detection_tracking.py ├── output.mp4 └── yolov8n.pt /1.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python-dontrepeatyourself/Real-Time-Object-Tracking-with-DeepSORT-and-YOLOv8/657ab8eaebea118e5cee8c5b6ed9656e3aa03886/1.mp4 -------------------------------------------------------------------------------- /2.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python-dontrepeatyourself/Real-Time-Object-Tracking-with-DeepSORT-and-YOLOv8/657ab8eaebea118e5cee8c5b6ed9656e3aa03886/2.mp4 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Real-time Object Tracking with DeepSORT and YOLOv8 2 | -------------------------------------------------------------------------------- /helper.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | def create_video_writer(video_cap, output_filename): 4 | 5 | # grab the width, height, and fps of the frames in the video stream. 6 | frame_width = int(video_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 7 | frame_height = int(video_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 8 | fps = int(video_cap.get(cv2.CAP_PROP_FPS)) 9 | 10 | # initialize the FourCC and a video writer object 11 | fourcc = cv2.VideoWriter_fourcc(*'MP4V') 12 | writer = cv2.VideoWriter(output_filename, fourcc, fps, 13 | (frame_width, frame_height)) 14 | 15 | return writer -------------------------------------------------------------------------------- /object_detection.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | from ultralytics import YOLO 3 | import cv2 4 | from helper import create_video_writer 5 | 6 | 7 | # define some constants 8 | CONFIDENCE_THRESHOLD = 0.8 9 | GREEN = (0, 255, 0) 10 | 11 | # initialize the video capture object 12 | video_cap = cv2.VideoCapture("2.mp4") 13 | # initialize the video writer object 14 | writer = create_video_writer(video_cap, "output.mp4") 15 | 16 | # load the pre-trained YOLOv8n model 17 | model = YOLO("yolov8n.pt") 18 | 19 | 20 | while True: 21 | # start time to compute the fps 22 | start = datetime.datetime.now() 23 | 24 | ret, frame = video_cap.read() 25 | 26 | # if there are no more frames to process, break out of the loop 27 | if not ret: 28 | break 29 | 30 | # run the YOLO model on the frame 31 | detections = model(frame)[0] 32 | 33 | # loop over the detections 34 | for data in detections.boxes.data.tolist(): 35 | # extract the confidence (i.e., probability) associated with the detection 36 | confidence = data[4] 37 | 38 | # filter out weak detections by ensuring the 39 | # confidence is greater than the minimum confidence 40 | if float(confidence) < CONFIDENCE_THRESHOLD: 41 | continue 42 | 43 | # if the confidence is greater than the minimum confidence, 44 | # draw the bounding box on the frame 45 | xmin, ymin, xmax, ymax = int(data[0]), int(data[1]), int(data[2]), int(data[3]) 46 | cv2.rectangle(frame, (xmin, ymin) , (xmax, ymax), GREEN, 2) 47 | 48 | # end time to compute the fps 49 | end = datetime.datetime.now() 50 | # show the time it took to process 1 frame 51 | total = (end - start).total_seconds() 52 | print(f"Time to process 1 frame: {total * 1000:.0f} milliseconds") 53 | 54 | # calculate the frame per second and draw it on the frame 55 | fps = f"FPS: {1 / total:.2f}" 56 | cv2.putText(frame, fps, (50, 50), 57 | cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 8) 58 | 59 | # show the frame to our screen 60 | cv2.imshow("Frame", frame) 61 | writer.write(frame) 62 | if cv2.waitKey(1) == ord("q"): 63 | break 64 | 65 | video_cap.release() 66 | writer.release() 67 | cv2.destroyAllWindows() 68 | -------------------------------------------------------------------------------- /object_detection_tracking.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | from ultralytics import YOLO 3 | import cv2 4 | from helper import create_video_writer 5 | from deep_sort_realtime.deepsort_tracker import DeepSort 6 | 7 | 8 | CONFIDENCE_THRESHOLD = 0.8 9 | GREEN = (0, 255, 0) 10 | WHITE = (255, 255, 255) 11 | 12 | # initialize the video capture object 13 | video_cap = cv2.VideoCapture("2.mp4") 14 | # initialize the video writer object 15 | writer = create_video_writer(video_cap, "output.mp4") 16 | 17 | # load the pre-trained YOLOv8n model 18 | model = YOLO("yolov8n.pt") 19 | tracker = DeepSort(max_age=50) 20 | 21 | 22 | while True: 23 | start = datetime.datetime.now() 24 | 25 | ret, frame = video_cap.read() 26 | 27 | if not ret: 28 | break 29 | 30 | # run the YOLO model on the frame 31 | detections = model(frame)[0] 32 | 33 | # initialize the list of bounding boxes and confidences 34 | results = [] 35 | 36 | ###################################### 37 | # DETECTION 38 | ###################################### 39 | 40 | # loop over the detections 41 | for data in detections.boxes.data.tolist(): 42 | # extract the confidence (i.e., probability) associated with the prediction 43 | confidence = data[4] 44 | 45 | # filter out weak detections by ensuring the 46 | # confidence is greater than the minimum confidence 47 | if float(confidence) < CONFIDENCE_THRESHOLD: 48 | continue 49 | 50 | # if the confidence is greater than the minimum confidence, 51 | # get the bounding box and the class id 52 | xmin, ymin, xmax, ymax = int(data[0]), int(data[1]), int(data[2]), int(data[3]) 53 | class_id = int(data[5]) 54 | # add the bounding box (x, y, w, h), confidence and class id to the results list 55 | results.append([[xmin, ymin, xmax - xmin, ymax - ymin], confidence, class_id]) 56 | 57 | ###################################### 58 | # TRACKING 59 | ###################################### 60 | 61 | # update the tracker with the new detections 62 | tracks = tracker.update_tracks(results, frame=frame) 63 | # loop over the tracks 64 | for track in tracks: 65 | # if the track is not confirmed, ignore it 66 | if not track.is_confirmed(): 67 | continue 68 | 69 | # get the track id and the bounding box 70 | track_id = track.track_id 71 | ltrb = track.to_ltrb() 72 | 73 | xmin, ymin, xmax, ymax = int(ltrb[0]), int( 74 | ltrb[1]), int(ltrb[2]), int(ltrb[3]) 75 | # draw the bounding box and the track id 76 | cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), GREEN, 2) 77 | cv2.rectangle(frame, (xmin, ymin - 20), (xmin + 20, ymin), GREEN, -1) 78 | cv2.putText(frame, str(track_id), (xmin + 5, ymin - 8), 79 | cv2.FONT_HERSHEY_SIMPLEX, 0.5, WHITE, 2) 80 | 81 | # end time to compute the fps 82 | end = datetime.datetime.now() 83 | # show the time it took to process 1 frame 84 | print(f"Time to process 1 frame: {(end - start).total_seconds() * 1000:.0f} milliseconds") 85 | # calculate the frame per second and draw it on the frame 86 | fps = f"FPS: {1 / (end - start).total_seconds():.2f}" 87 | cv2.putText(frame, fps, (50, 50), 88 | cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255), 8) 89 | 90 | # show the frame to our screen 91 | cv2.imshow("Frame", frame) 92 | writer.write(frame) 93 | if cv2.waitKey(1) == ord("q"): 94 | break 95 | 96 | video_cap.release() 97 | writer.release() 98 | cv2.destroyAllWindows() 99 | -------------------------------------------------------------------------------- /output.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python-dontrepeatyourself/Real-Time-Object-Tracking-with-DeepSORT-and-YOLOv8/657ab8eaebea118e5cee8c5b6ed9656e3aa03886/output.mp4 -------------------------------------------------------------------------------- /yolov8n.pt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/python-dontrepeatyourself/Real-Time-Object-Tracking-with-DeepSORT-and-YOLOv8/657ab8eaebea118e5cee8c5b6ed9656e3aa03886/yolov8n.pt --------------------------------------------------------------------------------