-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_benchmark.py
76 lines (67 loc) · 2.46 KB
/
video_benchmark.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
'''
This file is used to run a model on each frame of a video
as a means to test a model's detection capabilities in the
real world.
It takes the video input as the first argument, and video output
as the second argument...
python3 video_benchmark.py video_in/IMG_2203_2.MOV video_out/demo1.mkv
'''
import sys
import os
import torch
import cv2
from grip.detectorfour import GripPipeline
model = torch.hub.load('ultralytics/yolov5', 'custom', path='models/best.pt', force_reload=True)
model.cpu()
detector = GripPipeline()
import re
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(data, key=alphanum_key)
def main():
# Initializing necessary variables
video_input = sys.argv[1]
video_output = sys.argv[2]
vidcap = cv2.VideoCapture(video_input)
success, image = vidcap.read()
wd = os.getcwd()
temp_path = os.path.join(wd, "temp")
count = 0
while success:
# Process a frame
frame = image
# Run the frame through the GRIP pipeline
detector.process(frame)
frame = detector.mask_output
# Predict and draw bounding boxes from the
# predicitons made by the YOLO model
pred = model(frame)
for row in pred.pandas().xyxy[0].iterrows():
pt1 = (int(row[1]["xmin"]), int(row[1]["ymin"]))
pt2 = (int(row[1]["xmax"]), int(row[1]["ymax"]))
color = (255, 0, 0)
thickness = 2
image = cv2.rectangle(image, pt1, pt2, color, thickness)
# Frame process end
# Save frame as JPEG file
cv2.imwrite(os.path.join(temp_path, "frame%d.jpg" % count), image)
count = count + 1
success, image = vidcap.read()
# Join all of the processed frames into a video
images = [img for img in os.listdir(temp_path + "/")]
images = sorted_alphanumeric(images)[1:]
frame = cv2.imread(os.path.join(temp_path, images[0]))
height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
video = cv2.VideoWriter(video_output, fourcc, 30, (width, height))
for image in images:
video.write(cv2.imread(os.path.join(temp_path, image)))
cv2.destroyAllWindows()
video.release()
# Remove all processed frames
dir = temp_path
for f in os.listdir(dir):
os.remove(os.path.join(dir, f))
if __name__ == "__main__":
main()