forked from pytorch/serve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.py
204 lines (158 loc) · 5.65 KB
/
request.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
import argparse
import base64
import json
import time
from collections import deque
from concurrent.futures import as_completed
from threading import Thread
import cv2
import requests
from requests_futures.sessions import FuturesSession
def read_frames(args):
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
# If Reading a camera, we convert to int
try:
device = int(args.input)
except:
device = args.input
cap = cv2.VideoCapture(device)
# Check if video opened successfully
if cap.isOpened() == False:
print("Error opening video stream or file")
frame_cnt = 0
# Read until video is completed
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Encode the frame into byte data
data = cv2.imencode(".jpg", frame)[1].tobytes()
queue.append(data)
frame_cnt += 1
# For videos, add a sleep so that we read at 30 FPS
if not isinstance(device, 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()
def send_frames(payload, snd_cnt, session):
if args.client_batching:
snd_cnt += len(payload)
payload = json.dumps(payload)
response = requests.post(api, data=payload, headers=headers)
else:
snd_cnt += 1
response = session.post(api, data=payload)
return (response, snd_cnt)
def calculate_fps(start_time, snd_cnt):
end_time = time.time()
if args.client_batching:
fps = 1.0 * args.batch_size / (end_time - start_time)
else:
fps = 1.0 / (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):
# Initialize variables
count, exit_cnt, snd_cnt, log_cnt = 0, 0, 0, 20
payload, futures = {}, []
start_time = time.time()
fps = 0
session = FuturesSession()
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
if args.client_batching:
# Batch the frames into a dict payload
while queue and count < args.batch_size:
data = queue.popleft()
im_b64 = base64.b64encode(data).decode("utf8")
payload[str(count)] = im_b64
count += 1
if count >= args.batch_size:
response, snd_cnt = send_frames(payload, snd_cnt, session)
if snd_cnt % log_cnt == 0:
# Calculate FPS
fps = calculate_fps(start_time, snd_cnt)
# Printing the response
print(response.content.decode("UTF-8"))
# Reset for next batch
start_time = time.time()
payload = {}
count = 0
else:
# If queue is not empty, send one frame at a time
if queue:
payload = queue.popleft()
response, snd_cnt = send_frames(payload, snd_cnt, session)
futures.append(response)
if snd_cnt % log_cnt == 0:
# Calculate FPS
fps = calculate_fps(start_time, snd_cnt)
# Printing the response
for response in list(as_completed(futures))[-4:]:
print(response.result().content.decode("utf-8"))
# Cleaning up futures in case futures becomes too large
del futures[:log_cnt]
# Reset for next batch
start_time = time.time()
payload = None
# Sleep for 10 ms before trying to send next batch of frames
time.sleep(args.sleep)
# Send any remaining frames
_, snd_cnt = send_frames(payload, snd_cnt, session)
print(
"With Batch Size {}, FPS at frame number {} is {:.1f}".format(
args.batch_size, snd_cnt, fps
)
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--batch_size",
help="Batch frames on TorchServe side for inference",
type=int,
default=4,
)
parser.add_argument(
"--input",
help="Path to video file or device id",
default="examples/image_classifier/near_real_time_video/data/sample_video.mp4",
)
parser.add_argument(
"--client-batching",
help="To use client side batching methodology",
action="store_true",
)
parser.add_argument(
"--sleep",
help="Sleep between 2 subsequent requests in seconds",
type=float,
default=0.01,
)
args = parser.parse_args()
# Read frames are placed here and then processed
queue = deque([])
api = "http://localhost:8080/predictions/resnet-18"
headers = {"Content-type": "application/json", "Accept": "text/plain"}
thread1 = Thread(target=read_frames, args=(args,))
thread2 = Thread(target=batch_and_send_frames, args=(args,))
thread1.start()
thread2.start()