-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolocode.py
51 lines (32 loc) · 1.04 KB
/
yolocode.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
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
whT = 320
classesFile = 'coco.names'
classNames = []
with open(classesFile,'rt') as f:
classNames = f.read().rstrip('\n').split('\n')
#print(classNames)
#print(len(classNames))
modelConfiguration = 'yolov3-tiny.cfg'
modelWeights = 'yolov3-tiny.weights'
net = cv2.dnn.readNetFromDarknet(modelConfiguration,modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
while True:
success,img = cap.read()
if not success:
break
blob = cv2.dnn.blobFromImage(img,1/255,(whT,whT),[0,0,0],1,crop=False)
net.setInput(blob)
layerNames = net.getLayerNames()
# print(layerNames)
outputNames = [layerNames[i-1] for i in net.getUnconnectedOutLayers()]
#print(outputNames)
outputs = net.forward(outputNames)
print(type(outputs))
cv2.imshow('output',img)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
cap.release()
cv2.destroyAllWindows()