-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (53 loc) · 2.16 KB
/
utils.py
File metadata and controls
56 lines (53 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import cv2
def annotateVideo(path, outputPath, predictions, bboxes, fps=None):
"""
Overlay predictions on the original video for better understanding of the model output.
Args:
path (str): Path toe the original video.
outputPath (str): Path to the output video.
predictions (dict): A dictionary mapping actor IDs to their predicted labels.
Example: {1: "Running", 2: "Walking", 3: "Standing"}
bboxes (dict): A dictionary of bounding boxes for each frame and actor
Example: {0: {1: (x1, y1, x2, y2), 2: (x1, y1, x2, y2), ...}, 1: {...}, ...}
fps (int): Frames per second for the output video (default: None).
"""
cap = cv2.VideoCapture(path)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
originalFPS = cap.get(cv2.CAP_PROP_FPS)
fps = fps if fps else originalFPS
print(f'Original video dimensions: {width}x{height} @ {originalFPS} FPS')
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f'Total frames: {frameCount}')
#Videowriter setup
FOURCC = cv2.VideoWriter_fourcc(*'mp4v')
output = cv2.VideoWriter(outputPath, FOURCC, fps, (width, height))
frameIdx = 0
writtenFrames = 0 #counts successfully written frames.
while cap.isOpened():
ret, frame = cap.read()
if not ret:
print('End of video.')
break
#Overlay predictions on frame
if frameIdx in bboxes:
for actorID, bbox in bboxes[frameIdx].items():
x1, y1, x2, y2 = bbox
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
label = predictions.get(actorID, "Unknown")
cv2.putText(
frame,
f'Actor {actorID}: {label}',
(x1, y1 - 10),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
2,
)
output.write(frame)
writtenFrames += 1
frameIdx += 1
cap.release()
output.release()
cv2.destroyAllWindows()
print(f'Annotated video saved to {outputPath}')