-
Notifications
You must be signed in to change notification settings - Fork 0
/
v.py
58 lines (49 loc) · 1.58 KB
/
v.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
# -*- coding: utf-8 -*-
"""
@Author: zzn
@Modified by Quandong-Zhang
@Date: 2019-11-12 11:04:36
@Last Modified by: Quandong-Zhang
@Last Modified time: 2022-8-28 17:11:32
"""
import os
import sys
import cv2
import numpy as np
import torch
from PIL import Image
from predictor import Predictor
def main(video_path,img_path,video_name):
cap = cv2.VideoCapture(video_path)
if (cap.isOpened() == False):
print('Unable to read')
fps = int(cap.get(5))
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
out = cv2.VideoWriter('demo/detect_video_{}.avi'.format(video_name), cv2.VideoWriter_fourcc(
'M', 'P', '4', '2'), fps, (frame_width, frame_height))
device = 'cuda' if torch.cuda.is_available() else 'cpu'
predictor = Predictor(device=device)
while(True):
ret, frame = cap.read()
if ret == True:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame)
x = predictor.process_img(img)
predictions = predictor.predict(x)
img = predictor.display_boxes(img, predictions,img_path)
save_frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
out.write(save_frame)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
try:
video_path = sys.argv[1]
img_path = sys.argv[2]
video_name = os.path.basename(video_path)
except:
print('Please enter a video path and a file path')
main(video_path,img_path,video_name)