-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnano.py
283 lines (233 loc) · 11.2 KB
/
nano.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/usr/bin/env python3
from pathlib import Path
import cv2
import numpy as np
import depthai as dai
import east
import blobconverter
from gps_function import *
class HostSeqSync:
def __init__(self):
self.imfFrames = []
def add_msg(self, msg):
self.imfFrames.append(msg)
def get_msg(self, target_seq):
for i, imgFrame in enumerate(self.imfFrames):
if target_seq == imgFrame.getSequenceNum():
self.imfFrames = self.imfFrames[i:]
break
return self.imfFrames[0]
pipeline = dai.Pipeline()
version = "2021.2"
pipeline.setOpenVINOVersion(version=dai.OpenVINO.Version.VERSION_2021_2)
colorCam = pipeline.create(dai.node.ColorCamera)
colorCam.setPreviewSize(256, 256)
colorCam.setVideoSize(1024, 1024) # 4 times larger in both axis
colorCam.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
colorCam.setInterleaved(False)
colorCam.setBoardSocket(dai.CameraBoardSocket.RGB)
colorCam.setFps(10)
controlIn = pipeline.create(dai.node.XLinkIn)
controlIn.setStreamName('control')
controlIn.out.link(colorCam.inputControl)
cam_xout = pipeline.create(dai.node.XLinkOut)
cam_xout.setStreamName('video')
colorCam.video.link(cam_xout.input)
# ---------------------------------------
# 1st stage NN - text-detection
# ---------------------------------------
nn = pipeline.create(dai.node.NeuralNetwork)
nn.setBlobPath(blobconverter.from_zoo(name="east_text_detection_256x256",zoo_type="depthai",shaves=6, version=version))
colorCam.preview.link(nn.input)
nn_xout = pipeline.create(dai.node.XLinkOut)
nn_xout.setStreamName('detections')
nn.out.link(nn_xout.input)
# ---------------------------------------
# 2nd stage NN - text-recognition-0012
# ---------------------------------------
manip = pipeline.create(dai.node.ImageManip)
manip.setWaitForConfigInput(True)
manip_img = pipeline.create(dai.node.XLinkIn)
manip_img.setStreamName('manip_img')
manip_img.out.link(manip.inputImage)
manip_cfg = pipeline.create(dai.node.XLinkIn)
manip_cfg.setStreamName('manip_cfg')
manip_cfg.out.link(manip.inputConfig)
manip_xout = pipeline.create(dai.node.XLinkOut)
manip_xout.setStreamName('manip_out')
nn2 = pipeline.create(dai.node.NeuralNetwork)
nn2.setBlobPath(blobconverter.from_zoo(name="text-recognition-0012", shaves=6, version=version))
nn2.setNumInferenceThreads(2)
manip.out.link(nn2.input)
manip.out.link(manip_xout.input)
nn2_xout = pipeline.create(dai.node.XLinkOut)
nn2_xout.setStreamName("recognitions")
nn2.out.link(nn2_xout.input)
def to_tensor_result(packet):
return {
name: np.array(packet.getLayerFp16(name))
for name in [tensor.name for tensor in packet.getRaw().tensors]
}
def to_planar(frame):
return frame.transpose(2, 0, 1).flatten()
def mainFunc_cam():
with dai.Device(pipeline) as device:
q_vid = device.getOutputQueue("video", 4, blocking=False)
# This should be set to block, but would get to some extreme queuing/latency!
q_det = device.getOutputQueue("detections", 4, blocking=False)
q_rec = device.getOutputQueue("recognitions", 4, blocking=True)
q_manip_img = device.getInputQueue("manip_img")
q_manip_cfg = device.getInputQueue("manip_cfg")
q_manip_out = device.getOutputQueue("manip_out", 4, blocking=False)
controlQueue = device.getInputQueue('control')
frame = None
cropped_stacked = None
rotated_rectangles = []
rec_pushed = 0
rec_received = 0
host_sync = HostSeqSync()
class CTCCodec(object):
""" Convert between text-label and text-index """
def __init__(self, characters):
# characters (str): set of the possible characters.
dict_character = list(characters)
self.dict = {}
for i, char in enumerate(dict_character):
self.dict[char] = i + 1
self.characters = dict_character
#print(self.characters)
#input()
def decode(self, preds):
""" convert text-index into text-label. """
texts = []
index = 0
# Select max probabilty (greedy decoding) then decode index to character
preds = preds.astype(np.float16)
preds_index = np.argmax(preds, 2)
preds_index = preds_index.transpose(1, 0)
preds_index_reshape = preds_index.reshape(-1)
preds_sizes = np.array([preds_index.shape[1]] * preds_index.shape[0])
for l in preds_sizes:
t = preds_index_reshape[index:index + l]
# NOTE: t might be zero size
if t.shape[0] == 0:
continue
char_list = []
for i in range(l):
# removing repeated characters and blank.
if not (i > 0 and t[i - 1] == t[i]):
if self.characters[t[i]] != '#':
char_list.append(self.characters[t[i]])
text = ''.join(char_list)
texts.append(text)
index += l
return texts
characters = '0123456789abcdefghijklmnopqrstuvwxyz#'
codec = CTCCodec(characters)
ctrl = dai.CameraControl()
ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.CONTINUOUS_VIDEO)
ctrl.setAutoFocusTrigger()
controlQueue.send(ctrl)
while True:
vid_in = q_vid.tryGet()
if vid_in is not None:
host_sync.add_msg(vid_in)
# Multiple recognition results may be available, read until queue is empty
while True:
in_rec = q_rec.tryGet()
if in_rec is None:
break
rec_data = bboxes = np.array(in_rec.getFirstLayerFp16()).reshape(30,1,37)
decoded_text = codec.decode(rec_data)[0]
pos = rotated_rectangles[rec_received]
print("{:2}: {:20}".format(rec_received, decoded_text),
"center({:3},{:3}) size({:3},{:3}) angle{:5.1f} deg".format(
int(pos[0][0]), int(pos[0][1]), pos[1][0], pos[1][1], pos[2]))
# Pass decoded_text to gps function
gps(decoded_text)
# if gps = False, break out of function
if not gps(decoded_text):
break
# Draw the text on the right side of 'cropped_stacked' - placeholder
if cropped_stacked is not None:
cv2.putText(cropped_stacked, decoded_text,
(120 + 10 , 32 * rec_received + 24),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 2)
cv2.imshow('cropped_stacked', cropped_stacked)
rec_received += 1
if cv2.waitKey(1) == ord('q'):
break
if rec_received >= rec_pushed:
in_det = q_det.tryGet()
if in_det is not None:
frame = host_sync.get_msg(in_det.getSequenceNum()).getCvFrame().copy()
scores, geom1, geom2 = to_tensor_result(in_det).values()
scores = np.reshape(scores, (1, 1, 64, 64))
geom1 = np.reshape(geom1, (1, 4, 64, 64))
geom2 = np.reshape(geom2, (1, 1, 64, 64))
bboxes, confs, angles = east.decode_predictions(scores, geom1, geom2)
boxes, angles = east.non_max_suppression(np.array(bboxes), probs=confs, angles=np.array(angles))
rotated_rectangles = [
east.get_cv_rotated_rect(bbox, angle * -1)
for (bbox, angle) in zip(boxes, angles)
]
rec_received = 0
rec_pushed = len(rotated_rectangles)
if rec_pushed:
print("====== Pushing for recognition, count:", rec_pushed)
cropped_stacked = None
for idx, rotated_rect in enumerate(rotated_rectangles):
# Detections are done on 256x256 frames, we are sending back 1024x1024
# That's why we multiply center and size values by 4
rotated_rect[0][0] = rotated_rect[0][0] * 4
rotated_rect[0][1] = rotated_rect[0][1] * 4
rotated_rect[1][0] = rotated_rect[1][0] * 4
rotated_rect[1][1] = rotated_rect[1][1] * 4
# Draw detection crop area on input frame
points = np.int0(cv2.boxPoints(rotated_rect))
print(rotated_rect)
cv2.polylines(frame, [points], isClosed=True, color=(255, 0, 0), thickness=1, lineType=cv2.LINE_8)
# TODO make it work taking args like in OpenCV:
# rr = ((256, 256), (128, 64), 30)
rr = dai.RotatedRect()
rr.center.x = rotated_rect[0][0]
rr.center.y = rotated_rect[0][1]
rr.size.width = rotated_rect[1][0]
rr.size.height = rotated_rect[1][1]
rr.angle = rotated_rect[2]
cfg = dai.ImageManipConfig()
cfg.setCropRotatedRect(rr, False)
cfg.setResize(120, 32)
# Send frame and config to device
if idx == 0:
w,h,c = frame.shape
imgFrame = dai.ImgFrame()
imgFrame.setData(to_planar(frame))
imgFrame.setType(dai.ImgFrame.Type.BGR888p)
imgFrame.setWidth(w)
imgFrame.setHeight(h)
q_manip_img.send(imgFrame)
else:
cfg.setReusePreviousImage(True)
q_manip_cfg.send(cfg)
# Get manipulated image from the device
transformed = q_manip_out.get().getCvFrame()
rec_placeholder_img = np.zeros((32, 200, 3), np.uint8)
transformed = np.hstack((transformed, rec_placeholder_img))
if cropped_stacked is None:
cropped_stacked = transformed
else:
cropped_stacked = np.vstack((cropped_stacked, transformed))
if cropped_stacked is not None:
cv2.imshow('cropped_stacked', cropped_stacked)
if frame is not None:
cv2.imshow('frame', frame)
key = cv2.waitKey(1)
if key == ord('q'):
break
elif key == ord('t'):
print("Autofocus trigger (and disable continuous)")
ctrl = dai.CameraControl()
ctrl.setAutoFocusMode(dai.CameraControl.AutoFocusMode.AUTO)
ctrl.setAutoFocusTrigger()
controlQueue.send(ctrl)