-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_face_detect.py
267 lines (184 loc) · 6.88 KB
/
request_face_detect.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import argparse
import base64
import json
import time
import concurrent.futures
from threading import Thread
import numpy as np
import queue as q
import cv2
import requests
from collections import deque
import threading
def read_frames(cam, queue):
# Check if video opened successfully
cam_id = cam['id']
cap = cam['cap']
print('Cam ID: ', cam_id)
if cap.isOpened() == False:
print("Error opening video stream or file")
return
frame_cnt = 0
# Read until video is completed
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Preprocess the frame
# image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = cv2.resize(frame, (640, 640), interpolation = cv2.INTER_LINEAR)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Encode the frame into byte data
data = cv2.imencode(".jpg", image)[1].tobytes()
queue.append({'id': cam_id,
'size': frame.shape,
'frame': frame_cnt,
'image': data})
frame_cnt += 1
# For videos, add a sleep so that we read at 30 FPS
if not isinstance(cam_id, int):
time.sleep(1.0 / 30)
# Break the loop
else:
break
print("Done reading {} frames".format(frame_cnt))
# When everything done, release the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
return
def spawn_camera(device):
if isinstance(device, int):
device = [device]
for i in range(len(device)):
try:
# print(device[i])
device[i] = int(device[i])
except:
pass
cams = [{'id': cam,
'cap': cv2.VideoCapture(cam)}
for cam in device]
return cams
def convert_arg(args):
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
try:
device = int(args.input)
except:
try:
device = args.input.split(',')
except:
device = args.input
# print(device, type(device))
cams = spawn_camera(device)
# threads = [Thread(target=read_frames, args=(cam,)) for cam in cams]
# [t.start() for t in threads]
# [t.join() for t in threads]
return cams
def send_frames(payload, snd_cnt):
api = "http://127.0.0.1:8080/predictions/face-client"
snd_cnt += len(payload)
payload = json.dumps(payload)
response = requests.post(api, data=payload, headers=headers)
return (response, snd_cnt)
def calculate_fps(start_time, snd_cnt):
end_time = time.time()
fps = 1.0 * args.batch_size / (end_time - start_time)
print(
"With Batch Size {}, FPS at frame number {} is {:.1f}".format(
args.batch_size, snd_cnt, fps
)
)
return fps
def batch_and_send_frames(args, queue):
# Initialize variables
count, exit_cnt, snd_cnt, log_cnt = 0, 0, 0, 20
payload, futures = {}, []
start_time = time.time()
fps = 0
while True:
# Exit condition for the while loop. Need a better logic
if len(queue) == 0:
exit_cnt += 1
# By trial and error, 1000 seemed to work best
if exit_cnt >= 1000:
print(
"Length of queue is {} , snd_cnt is {}".format(len(queue), snd_cnt)
)
break
# Batch the frames into a dict payload
while queue and count < args.batch_size:
data = queue.popleft()
img = data['image']
im_b64 = base64.b64encode(img).decode("utf8")
payload[str(count)] = {'id': data['id'],
'size': data['size'],
'frame': data['frame'],
'image': im_b64}
count += 1
if count >= args.batch_size:
response, snd_cnt = send_frames(payload, snd_cnt)
results = json.loads(response.content.decode("UTF-8"))
print(results)
# Reset for next batch
start_time = time.time()
payload = {}
count = 0
# Sleep for 10 ms before trying to send next batch of frames
time.sleep(args.sleep)
return
def read_and_send(cam):
"""
Parallelized function.
"""
# Read frames are placed here and then processed
queue = deque([])
# Start the read_frames worker
read_worker_thread = threading.Thread(target=read_frames, args=(cam, queue))
read_worker_thread.daemon = True
read_worker_thread.start()
# Start the batch_and_send_frames worker
batch_and_send_thread = threading.Thread(target=batch_and_send_frames, args=(args, queue))
batch_and_send_thread.daemon = True
batch_and_send_thread.start()
# # Start the send_keypoints worker
# send_keypoints_thread = threading.Thread(target=send_keypoints, args=(keypoints_queue,))
# send_keypoints_thread.daemon = True
# send_keypoints_thread.start()
batch_and_send_thread.join()
read_worker_thread.join()
# send_keypoints_thread.join()
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument(
"--batch_size",
help="Batch frames on TorchServe side for inference",
type=int,
default=1,
)
parser.add_argument(
"--input",
help="Path to video file or device id",
default=r"videos\cut.mp4",
)
parser.add_argument(
"--sleep",
help="Sleep between 2 subsequent requests in seconds",
type=float,
default=0.01,
)
start_time = time.time()
args = parser.parse_args()
headers = {"Content-type": "application/json", "Accept": "text/plain"}
cams = convert_arg(args)
print('Cams: ', cams)
# Start the read and send process parallelly use thread
threads = [Thread(target=read_and_send, args=(cam,)) for cam in cams]
[t.start() for t in threads]
[t.join() for t in threads]
print('Time: ', time.time() - start_time)
except Exception as e:
print(e)
pass