-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_video.py
More file actions
81 lines (61 loc) · 2.14 KB
/
Copy pathdetect_video.py
File metadata and controls
81 lines (61 loc) · 2.14 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
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
import sys
import time
from PIL import Image, ImageDraw
from utils import *
from darknet import Darknet
import cv2
from tqdm import tqdm
def detect(cfgfile, weightfile):
m = Darknet(cfgfile)
m.print_network()
m.load_weights(weightfile)
print('Loading weights from %s... Done!' % (weightfile))
if m.num_classes == 20:
namesfile = 'data/voc.names'
elif m.num_classes == 80:
namesfile = 'data/coco.names'
else:
namesfile = 'data/names'
use_cuda = 1
if use_cuda:
m.cuda()
cap = cv2.VideoCapture('D:/dev/offline_UNLABELED/skateboard/skateboard_full/skateboard_full_title.mov')
frame_number = 0
pbar = tqdm(total=cap.get(cv2.CAP_PROP_FRAME_COUNT))
while cap.isOpened():
ret, frame = cap.read()
img = frame
sized = cv2.resize(img, (m.width, m.height))
sized = cv2.cvtColor(sized, cv2.COLOR_BGR2RGB)
# Simen: niet nodig om dit in loop te doen?
# for i in range(2):
start = time.time()
boxes = do_detect(m, sized, 0.75, 0.4, use_cuda)
finish = time.time()
# if i == 1:
# print('Predicted in %f seconds.' % (finish-start))
class_names = load_class_names(namesfile)
img = plot_boxes(Image.fromarray(img), boxes, class_names=class_names)
img = np.array(img)
# cv2.imshow('Result', img)
# cv2.namedWindow('Result', cv2.WINDOW_NORMAL)
# cv2.setWindowProperty('Result', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
# cv2.imshow('Result', img)
cv2.imwrite('out/{}.png'.format(str(frame_number).zfill(5)), img, )
# pil_img = Image.fromarray(img)
# pil_img.save('out/{}.png'.format(str(frame_number).zfill(5)))
pbar.update(1)
frame_number += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
pbar.close()
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
if len(sys.argv) == 3:
cfgfile = sys.argv[1]
weightfile = sys.argv[2]
detect(cfgfile, weightfile)
else:
print('Usage: ')
print(' python detect.py cfgfile weightfile')