-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetect_cam.py
More file actions
68 lines (52 loc) · 1.77 KB
/
Copy pathdetect_cam.py
File metadata and controls
68 lines (52 loc) · 1.77 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
import sys
import time
from PIL import Image, ImageDraw
from utils import *
from darknet import Darknet
import cv2
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(0)
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.5, 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)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
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')