-
Notifications
You must be signed in to change notification settings - Fork 31
/
high_speed.py
181 lines (148 loc) · 5.2 KB
/
high_speed.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
import argparse
import configparser
import os
import queue
import threading
import time
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
import chainer
import cv2
import numpy as np
from PIL import Image
from predict import get_feature, get_humans_by_feature, draw_humans, create_model, load_config
from utils import parse_size
QUEUE_SIZE = 5
"""
Bonus script
If you have good USB camera which gets image as well as 60 FPS,
this script will be helpful for realtime inference
"""
class Capture(threading.Thread):
def __init__(self, cap, insize):
super(Capture, self).__init__()
self.cap = cap
self.insize = insize
self.stop_event = threading.Event()
self.queue = queue.Queue(QUEUE_SIZE)
self.name = 'Capture'
def run(self):
while not self.stop_event.is_set():
try:
ret_val, image = self.cap.read()
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, self.insize)
self.queue.put(image, timeout=1)
except queue.Full:
pass
def get(self):
return self.queue.get(timeout=1)
def stop(self):
logger.info('{} will stop'.format(self.name))
self.stop_event.set()
class Predictor(threading.Thread):
def __init__(self, model, cap):
super(Predictor, self).__init__()
self.cap = cap
self.model = model
self.stop_event = threading.Event()
self.queue = queue.Queue(QUEUE_SIZE)
self.name = 'Predictor'
def run(self):
while not self.stop_event.is_set():
try:
image = self.cap.get()
with chainer.using_config('autotune', True), \
chainer.using_config('use_ideep', 'auto'):
feature_map = get_feature(self.model, image.transpose(2, 0, 1).astype(np.float32))
self.queue.put((image, feature_map), timeout=1)
except queue.Full:
pass
except queue.Empty:
pass
def get(self):
return self.queue.get(timeout=1)
def stop(self):
logger.info('{} will stop'.format(self.name))
self.stop_event.set()
def high_speed(args):
config = load_config(args)
dataset_type = config.get('dataset', 'type')
detection_thresh = config.getfloat('predict', 'detection_thresh')
min_num_keypoints = config.getint('predict', 'min_num_keypoints')
model = create_model(args, config)
if os.path.exists('mask.png'):
mask = Image.open('mask.png')
mask = mask.resize((200, 200))
else:
mask = None
cap = cv2.VideoCapture(0)
if cap.isOpened() is False:
print('Error opening video stream or file')
exit(1)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
cap.set(cv2.CAP_PROP_FPS, 60)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
logger.info('camera will capture {} FPS'.format(cap.get(cv2.CAP_PROP_FPS)))
capture = Capture(cap, model.insize)
predictor = Predictor(model=model, cap=capture)
capture.start()
predictor.start()
fps_time = 0
degree = 0
main_event = threading.Event()
try:
while not main_event.is_set() and cap.isOpened():
degree += 5
degree = degree % 360
try:
image, feature_map = predictor.get()
humans = get_humans_by_feature(
model,
feature_map,
detection_thresh,
min_num_keypoints
)
except queue.Empty:
continue
except Exception:
break
pilImg = Image.fromarray(image)
pilImg = draw_humans(
model.keypoint_names,
model.edges,
pilImg,
humans,
mask=mask.rotate(degree) if mask else None,
visbbox=config.getboolean('predict', 'visbbox'),
)
img_with_humans = cv2.cvtColor(np.asarray(pilImg), cv2.COLOR_RGB2BGR)
msg = 'GPU ON' if chainer.backends.cuda.available else 'GPU OFF'
msg += ' ' + config.get('model_param', 'model_name')
cv2.putText(img_with_humans, 'FPS: %f' % (1.0 / (time.time() - fps_time)),
(10, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
img_with_humans = cv2.resize(img_with_humans, (3 * model.insize[0], 3 * model.insize[1]))
cv2.imshow('Pose Proposal Network' + msg, img_with_humans)
fps_time = time.time()
# press Esc to exit
if cv2.waitKey(1) == 27:
main_event.set()
except Exception as e:
print(e)
except KeyboardInterrupt:
main_event.set()
capture.stop()
predictor.stop()
capture.join()
predictor.join()
def parse_arguments():
parser = argparse.ArgumentParser()
parser.add_argument('model', help='path/to/model', type=str)
return parser.parse_args()
def main():
args = parse_arguments()
high_speed(args)
if __name__ == '__main__':
main()