Created
May 4, 2024 13:51
-
-
Save blafasel42/1f19c1058aa691bc2549235b6788f59a to your computer and use it in GitHub Desktop.
draw bboxes on a video
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is a Python program that uses OpenCV to generate a video with bounding boxes and lines for the driven trajectories on top of the original video footage: | |
``` | |
import cv2 | |
import numpy as np | |
# Load the MOT challenge data file | |
mot_data = [] | |
with open('mot_data.txt', 'r') as f: | |
for line in f: | |
frame, id, x, y, w, h, _, _, _, _ = line.strip().split(',') | |
mot_data.append({ | |
'frame': int(frame), | |
'id': int(id), | |
'x': int(float(x)), | |
'y': int(float(y)), | |
'w': int(float(w)), | |
'h': int(float(h)) | |
}) | |
# Load the video capture | |
cap = cv2.VideoCapture('input_video.mp4') | |
# Get the video properties | |
fps = cap.get(cv2.CAP_PROP_FPS) | |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
# Create a video writer | |
fourcc = cv2.VideoWriter_fourcc(*'XVID') | |
out = cv2.VideoWriter('output_video.avi', fourcc, fps, (width, height)) | |
# Create a dictionary to store the trajectories | |
trajectories = {} | |
# Iterate through the frames | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
break | |
# Get the current frame number | |
frame_num = int(cap.get(cv2.CAP_PROP_POS_FRAMES)) | |
# Draw bounding boxes and trajectories | |
for detection in mot_data: | |
if detection['frame'] == frame_num: | |
x, y, w, h = detection['x'], detection['y'], detection['w'], detection['h'] | |
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) | |
# Add the detection to the trajectory | |
if detection['id'] not in trajectories: | |
trajectories[detection['id']] = [] | |
trajectories[detection['id']].append((x, y)) | |
# Draw trajectories | |
for trajectory in trajectories.values(): | |
for i in range(1, len(trajectory)): | |
cv2.line(frame, trajectory[i-1], trajectory[i], (0, 0, 255), 2) | |
# Write the output frame | |
out.write(frame) | |
# Release the video capture and writer | |
cap.release() | |
out.release() | |
``` | |
This program assumes that the MOT challenge data file is in the same format as the MOT16 dataset, with each line representing a detection in the format `frame,id,x,y,w,h,...`. The program loads the video capture, reads the MOT challenge data file, and iterates through the frames. For each frame, it draws bounding boxes around the detected objects and connects the detections to form trajectories. The output video is written to a file named `output_video.avi`. | |
Note that this program assumes that the video and MOT challenge data file are in the same directory as the Python script. You may need to modify the file paths and names to match your specific use case. Additionally, you can customize the appearance of the bounding boxes and trajectories by modifying the `cv2.rectangle` and `cv2.line` function calls. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment