-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect_vid.py
89 lines (79 loc) · 2.98 KB
/
detect_vid.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
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 26 00:21:24 2020
@author: hso
This .py-file follows the structure found in https://debuggercafe.com/faster-rcnn-object-detection-with-pytorch/
## Detect food in video/live webcam - given pretrained model ##
- run in terminal: "python detect_vid.py" (for live/webcam)
· for video: "python detect_vid.py --input input/video2.mp4"
"""
import torchvision
import cv2
import torch
import argparse
import time
import detect_utils
from PIL import Image
# construct the argument parser
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help='path to input video')
parser.add_argument('-m', '--min-size', dest='min_size', default=800,
help='minimum input size for the FasterRCNN network')
args = vars(parser.parse_args())
# download or load the model from disk
#model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True,
#min_size=args['min_size'])
model = torch.load("model_epoch18.pth",map_location=torch.device('cpu'))
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
#cap = cv2.VideoCapture(0) Uncomment for webcam //live
cap = cv2.VideoCapture(args['input'])
if (cap.isOpened() == False):
print('Error while trying to read video. Please check path again')
# get the frame width and height
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# define codec and create VideoWriter object
out = cv2.VideoWriter("predict.mp4",
cv2.VideoWriter_fourcc(*'mp4v'), 30,
(frame_width, frame_height))
frame_count = 0 # to count total frames
total_fps = 0 # to get the final frames per second
# load the model onto the computation device
model = model.eval().to(device)
# read until end of video
while(cap.isOpened()):
# capture each frame of the video
ret, frame = cap.read()
if ret == True:
# get the start time
start_time = time.time()
with torch.no_grad():
# get predictions for the current frame
boxes, classes, labels = detect_utils.predict(frame, model, device, 0.8)
# draw boxes and show current frame on screen
image = detect_utils.draw_boxes(boxes, classes, labels, frame)
# get the end time
end_time = time.time()
# get the fps
fps = 1 / (end_time - start_time)
# add fps to total fps
total_fps += fps
# increment frame count
frame_count += 1
# press `q` to exit
wait_time = max(1, int(fps/4))
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) ##
cv2.imshow('image', image)
out.write(image)
if cv2.waitKey(wait_time) & 0xFF == ord('q'):
break
else:
break
# release VideoCapture()
cap.release()
# close all frames and video windows
cv2.destroyAllWindows()
# calculate and print the average FPS
avg_fps = total_fps / frame_count
print(f"Average FPS: {avg_fps:.3f}")