Skip to content

Commit 7b2a90a

Browse files
committed
Real-time yolo
1 parent 239097c commit 7b2a90a

File tree

2 files changed

+309
-0
lines changed

2 files changed

+309
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import cv2
2+
from darkflow.net.build import TFNet
3+
import numpy as np
4+
import time #how fast is the processing
5+
6+
options = {
7+
'model': 'cfg/yolo.cfg', #model in cfg folder with yolo model
8+
'load': 'bin/yolo.weights', #loading weights from bin folder downloaded from darknet
9+
'threshold': 0.2, #confidence factor to draw bounding box, > 0.2 to show in image, low = tons of boxes which is not good
10+
'gpu': 1.0 #uses GPU to render images
11+
}
12+
13+
tfnet = TFNet(options) #object called tfnet to run options
14+
#displays info in command window like trained, layer description, output size
15+
16+
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)] # list of random colors for variations to boxes, array with 3 elements long
17+
# for in range(10) = sicne we want few entries; we wanna go from 0 t0 255
18+
19+
#0 for webcam 1, diff no for multiple cams
20+
capture = cv2.VideoCapture(0)
21+
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920) #full hd
22+
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
23+
24+
while True:
25+
stime = time.time() #start time
26+
ret, frame = capture.read() # ret is true while playing, false when ends; frame is actual frame; reading from capture device
27+
if ret:
28+
results = tfnet.return_predict(frame) # return_predict operation predicts whats in the video frame; passing the frame
29+
30+
# define bounding box and display label on the frame
31+
#looping over all items, all results, adding box for each thing it detects; loops over all predictions
32+
for color, result in zip(colors, results): # zip makes a list and then each item is gonna be a tuple with one color and one result
33+
#create tuples with two corners x,y in it
34+
tl = (result['topleft']['x'], result['topleft']['y']) #take first element as top left from result
35+
br = (result['bottomright']['x'], result['bottomright']['y']) #bottom right
36+
label = result['label']
37+
confidence = result['confidence']
38+
text = '{}: {:.0f}%'.format(label, confidence * 100) # string, float with 0 decimals; format with label, conf and mul with 100 to get %
39+
40+
#redefine frame with cv2 rectangle, pass frame first, then tl,br,color we want, line width
41+
frame = cv2.rectangle(frame, tl, br, color, 5)
42+
#add label by redifing frame with cv2 puttext; pass img first, text=label, font, font size, color, line width
43+
frame = cv2.putText( #puttext on the frame
44+
frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 0), 2)
45+
cv2.imshow('frame', frame) #display frame
46+
print('FPS {:.1f}'.format(1 / (time.time() - stime))) #.1f = one decimal place, calculation
47+
if cv2.waitKey(1) & 0xFF == ord('q'): #boilder plate code, if hit q key it ends
48+
break
49+
50+
capture.release()
51+
cv2.destroyAllWindows()
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
[net]
2+
# Testing
3+
batch=1
4+
subdivisions=1
5+
# Training
6+
# batch=64
7+
# subdivisions=8
8+
width=608
9+
height=608
10+
channels=3
11+
momentum=0.9
12+
decay=0.0005
13+
angle=0
14+
saturation = 1.5
15+
exposure = 1.5
16+
hue=.1
17+
18+
learning_rate=0.001
19+
burn_in=1000
20+
max_batches = 500200
21+
policy=steps
22+
steps=400000,450000
23+
scales=.1,.1
24+
25+
[convolutional]
26+
batch_normalize=1
27+
filters=32
28+
size=3
29+
stride=1
30+
pad=1
31+
activation=leaky
32+
33+
[maxpool]
34+
size=2
35+
stride=2
36+
37+
[convolutional]
38+
batch_normalize=1
39+
filters=64
40+
size=3
41+
stride=1
42+
pad=1
43+
activation=leaky
44+
45+
[maxpool]
46+
size=2
47+
stride=2
48+
49+
[convolutional]
50+
batch_normalize=1
51+
filters=128
52+
size=3
53+
stride=1
54+
pad=1
55+
activation=leaky
56+
57+
[convolutional]
58+
batch_normalize=1
59+
filters=64
60+
size=1
61+
stride=1
62+
pad=1
63+
activation=leaky
64+
65+
[convolutional]
66+
batch_normalize=1
67+
filters=128
68+
size=3
69+
stride=1
70+
pad=1
71+
activation=leaky
72+
73+
[maxpool]
74+
size=2
75+
stride=2
76+
77+
[convolutional]
78+
batch_normalize=1
79+
filters=256
80+
size=3
81+
stride=1
82+
pad=1
83+
activation=leaky
84+
85+
[convolutional]
86+
batch_normalize=1
87+
filters=128
88+
size=1
89+
stride=1
90+
pad=1
91+
activation=leaky
92+
93+
[convolutional]
94+
batch_normalize=1
95+
filters=256
96+
size=3
97+
stride=1
98+
pad=1
99+
activation=leaky
100+
101+
[maxpool]
102+
size=2
103+
stride=2
104+
105+
[convolutional]
106+
batch_normalize=1
107+
filters=512
108+
size=3
109+
stride=1
110+
pad=1
111+
activation=leaky
112+
113+
[convolutional]
114+
batch_normalize=1
115+
filters=256
116+
size=1
117+
stride=1
118+
pad=1
119+
activation=leaky
120+
121+
[convolutional]
122+
batch_normalize=1
123+
filters=512
124+
size=3
125+
stride=1
126+
pad=1
127+
activation=leaky
128+
129+
[convolutional]
130+
batch_normalize=1
131+
filters=256
132+
size=1
133+
stride=1
134+
pad=1
135+
activation=leaky
136+
137+
[convolutional]
138+
batch_normalize=1
139+
filters=512
140+
size=3
141+
stride=1
142+
pad=1
143+
activation=leaky
144+
145+
[maxpool]
146+
size=2
147+
stride=2
148+
149+
[convolutional]
150+
batch_normalize=1
151+
filters=1024
152+
size=3
153+
stride=1
154+
pad=1
155+
activation=leaky
156+
157+
[convolutional]
158+
batch_normalize=1
159+
filters=512
160+
size=1
161+
stride=1
162+
pad=1
163+
activation=leaky
164+
165+
[convolutional]
166+
batch_normalize=1
167+
filters=1024
168+
size=3
169+
stride=1
170+
pad=1
171+
activation=leaky
172+
173+
[convolutional]
174+
batch_normalize=1
175+
filters=512
176+
size=1
177+
stride=1
178+
pad=1
179+
activation=leaky
180+
181+
[convolutional]
182+
batch_normalize=1
183+
filters=1024
184+
size=3
185+
stride=1
186+
pad=1
187+
activation=leaky
188+
189+
190+
#######
191+
192+
[convolutional]
193+
batch_normalize=1
194+
size=3
195+
stride=1
196+
pad=1
197+
filters=1024
198+
activation=leaky
199+
200+
[convolutional]
201+
batch_normalize=1
202+
size=3
203+
stride=1
204+
pad=1
205+
filters=1024
206+
activation=leaky
207+
208+
[route]
209+
layers=-9
210+
211+
[convolutional]
212+
batch_normalize=1
213+
size=1
214+
stride=1
215+
pad=1
216+
filters=64
217+
activation=leaky
218+
219+
[reorg]
220+
stride=2
221+
222+
[route]
223+
layers=-1,-4
224+
225+
[convolutional]
226+
batch_normalize=1
227+
size=3
228+
stride=1
229+
pad=1
230+
filters=1024
231+
activation=leaky
232+
233+
[convolutional]
234+
size=1
235+
stride=1
236+
pad=1
237+
filters=425
238+
activation=linear
239+
240+
241+
[region]
242+
anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828
243+
bias_match=1
244+
classes=80
245+
coords=4
246+
num=5
247+
softmax=1
248+
jitter=.3
249+
rescore=1
250+
251+
object_scale=5
252+
noobject_scale=1
253+
class_scale=1
254+
coord_scale=1
255+
256+
absolute=1
257+
thresh = .1
258+
random=1

0 commit comments

Comments
 (0)