-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolo.py
214 lines (175 loc) · 5.66 KB
/
yolo.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import numpy as np
import argparse
import cv2 as cv
import subprocess
import time
import os
from yolo_utils import infer_image, show_image
import requests
from bs4 import BeautifulSoup
FLAGS = []
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--model-path',
type=str,
default='./yolov3-coco/',
help='The directory where the model weights and \
configuration files are.')
parser.add_argument('-w', '--weights',
type=str,
default='./yolov3-coco/yolov3.weights',
help='Path to the file which contains the weights \
for YOLOv3.')
parser.add_argument('-cfg', '--config',
type=str,
default='./yolov3-coco/yolov3.cfg',
help='Path to the configuration file for the YOLOv3 model.')
parser.add_argument('-i', '--image-path',
type=str,
help='The path to the image file')
parser.add_argument('-v', '--video-path',
type=str,
help='The path to the video file')
parser.add_argument('-vo', '--video-output-path',
type=str,
default='./output.avi',
help='The path of the output video file')
parser.add_argument('-l', '--labels',
type=str,
default='./yolov3-coco/coco-labels',
help='Path to the file having the \
labels in a new-line seperated way.')
parser.add_argument('--host',
type=str,
default="http://127.0.0.1:8000/up",
help='Show the time taken to infer each image.')
parser.add_argument('-c', '--confidence',
type=float,
default=0.5,
help='The model will reject boundaries which has a \
probabiity less than the confidence value. \
default: 0.5')
parser.add_argument('-th', '--threshold',
type=float,
default=0.3,
help='The threshold to use when applying the \
Non-Max Suppresion')
parser.add_argument('--download-model',
type=bool,
default=False,
help='Set to True, if the model weights and configurations \
are not present on your local machine.')
parser.add_argument('-t', '--show-time',
type=bool,
default=False,
help='Show the time taken to infer each image.')
FLAGS, unparsed = parser.parse_known_args()
# Download the YOLOv3 models if needed
if FLAGS.download_model:
subprocess.call(['./yolov3-coco/get_model.sh'])
# Get the labels
labels = open(FLAGS.labels).read().strip().split('\n')
# Intializing colors to represent each label uniquely
colors = np.random.randint(0, 255, size=(len(labels), 3), dtype='uint8')
# Load the weights and configutation to form the pretrained YOLOv3 model
net = cv.dnn.readNetFromDarknet(FLAGS.config, FLAGS.weights)
# Get the output layer names of the model
layer_names = net.getLayerNames()
layer_names = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
url = FLAGS.host
r = requests.get(url) #url is create form
soup = BeautifulSoup(r.text, 'html.parser')
links = soup.find_all('input')
token = links[0].get('value')
print(token)
# print(soup.find_all('input'))
# print()
jar = requests.cookies.RequestsCookieJar()
jar.set('XSRF-TOKEN', r.cookies['XSRF-TOKEN'])
jar.set('laravel_session', r.cookies['laravel_session'])
data = {
"_token": token
}
# If both image and video files are given then raise error
if FLAGS.image_path is None and FLAGS.video_path is None:
print ('Neither path to an image or path to video provided')
print ('Starting Inference on Webcam')
# Do inference with given image
if FLAGS.image_path:
# Read the image
try:
img = cv.imread(FLAGS.image_path)
height, width = img.shape[:2]
except:
raise 'Image cannot be loaded!\n\
Please check the path provided!'
finally:
img, _, _, _, _ = infer_image(net, layer_names, height, width, img, colors, labels, FLAGS)
cv.imwrite('result/result.jpg', img)
show_image(img)
elif FLAGS.video_path:
# Read the video
try:
vid = cv.VideoCapture(FLAGS.video_path)
height, width = None, None
writer = None
except:
raise 'Video cannot be loaded!\n\
Please check the path provided!'
finally:
while True:
grabbed, frame = vid.read()
# Checking if the complete video is read
if not grabbed:
break
if width is None or height is None:
height, width = frame.shape[:2]
frame, _, _, _, _ = infer_image(net, layer_names, height, width, frame, colors, labels, FLAGS)
if writer is None:
# Initialize the video writer
fourcc = cv.VideoWriter_fourcc(*"MJPG")
writer = cv.VideoWriter(FLAGS.video_output_path, fourcc, 30,
(frame.shape[1], frame.shape[0]), True)
writer.write(frame)
print ("[INFO] Cleaning up...")
writer.release()
vid.release()
else:
# Infer real-time on webcam
count = 0
current = 0
delta = 0
previous = 0
# tick = 0
vid = cv.VideoCapture(0)
while True:
current = time.time()
delta += current - previous
previous = current
_, frame = vid.read()
height, width = frame.shape[:2]
if delta > 1:
delta = 0
# tick += 1
if count == 0:
frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
height, width, frame, colors, labels, FLAGS)
count += 1
else:
frame, boxes, confidences, classids, idxs = infer_image(net, layer_names, \
height, width, frame, colors, labels, FLAGS, boxes, confidences, classids, idxs, infer=False)
count = (count + 1) % 6
# print(idxs)
# print(classids)
if 0 in classids:
print('exec')
filename = f'cap.jpg'
cv.imwrite(filename, frame)
img = open(filename, 'rb')
files = {'img': img}
upload = requests.post(url, files=files, data=data, cookies=jar, verify=False)
# cv.imshow('webcam', frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv.destroyAllWindows()