-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
442 lines (387 loc) · 20.7 KB
/
run.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import os,sys
import cv2
import numpy as np
import depthai as dai
from pathlib import Path
import pdb
import marshal
import time
import argparse
import pickle
import torch
import multiprocessing
import rospy
from std_msgs.msg import String
import shutil
FILE_DIR = Path(__file__).parent
# sys.path.append(f"{FILE_DIR}/controller")
# from move_and_grip import receiver
sys.path.append(f'{FILE_DIR}/depthai_blazepose')
from BlazeposeRenderer import BlazeposeRenderer
from BlazeposeDepthaiEdge_module_outside import BlazeposeDepthaiModule
sys.path.append(f'{FILE_DIR}/traj_intention')
from predict import IntentionPredictor
from Dataset import INTENTION_LIST
def get_distance(detection):
return (detection.spatialCoordinates.x**2+detection.spatialCoordinates.y**2+detection.spatialCoordinates.z**2)**0.5
def camera_to_world(X, R= np.array([0.14070565, -0.15007018, -0.7552408, 0.62232804], dtype=np.float32),\
t=0):
return qrot(np.tile(R, (*X.shape[:-1], 1)), X) + t
def qrot(q, v):
"""
Rotate vector(s) v about the rotation described by 四元数quaternion(s) q.
Expects a tensor of shape (*, 4) for q and a tensor of shape (*, 3) for v,
where * denotes any number of dimensions.
Returns a tensor of shape (*, 3).
"""
assert q.shape[-1] == 4
assert v.shape[-1] == 3
assert q.shape[:-1] == v.shape[:-1]
qvec = q[..., 1:]
uv = np.cross(qvec, v, len(q.shape) - 1)
uuv = np.cross(qvec, uv, len(q.shape) - 1)
return (v + 2 * (q[..., :1] * uv + uuv))
def get_intention(index):
for key,value in INTENTION_LIST.items():
if value == index:
return key
return "no_action"
def send_intention_to_ros(intention):
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('intention', anonymous=True)
rospy.loginfo(intention)
pub.publish(intention)
def intention_sender(args):
show = args.show
task = args.task
syncNN = args.syncNN
frame_size = args.seq_len
send_window = args.send_window
video = args.video
ROOT_DIR = f'{FILE_DIR}/human_traj/{task[:-3]}'
if not os.path.exists(ROOT_DIR):
os.mkdir(ROOT_DIR)
pipeline = dai.Pipeline()
# define sources
camRGB = pipeline.createColorCamera()
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
stereo = pipeline.create(dai.node.StereoDepth)
spatialDetectionNetwork = pipeline.create(dai.node.MobileNetSpatialDetectionNetwork)
pose_manip = pipeline.createImageManip()
# define in/outputs
xoutDetection = pipeline.create(dai.node.XLinkOut)
xoutDetection.setStreamName("Detection")
xoutRGB = pipeline.create(dai.node.XLinkOut)
xoutRGB.setStreamName("RGB")
xoutBlazepose = pipeline.create(dai.node.XLinkOut)
xoutBlazepose.setStreamName("Blazepose")
xinFrame = pipeline.create(dai.node.XLinkIn)
xinFrame.setStreamName("MaskedFrame")
# define properties
internal_fps = 30
DET_INPUT_SIZE = (300,300)
det_manip = pipeline.createImageManip()
det_manip.initialConfig.setResize(DET_INPUT_SIZE[0], DET_INPUT_SIZE[1])
det_manip.initialConfig.setKeepAspectRatio(False)
camRGB.setBoardSocket(dai.CameraBoardSocket.CAM_A)
camRGB.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P)
camRGB.setInterleaved(False)
camRGB.setFps(internal_fps)
monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
monoLeft.setCamera("left")
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_400_P)
monoRight.setCamera("right")
stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
# Align depth map to the perspective of RGB camera, on which inference is done
stereo.setDepthAlign(dai.CameraBoardSocket.CAM_A)
stereo.setSubpixel(False)
stereo.setLeftRightCheck(True)
stereo.setConfidenceThreshold(230)
stereo.setOutputSize(monoLeft.getResolutionWidth(), monoLeft.getResolutionHeight())
spatialDetectionNetwork.setBlobPath(detection_nnPath)
spatialDetectionNetwork.setConfidenceThreshold(0.5)
spatialDetectionNetwork.input.setBlocking(False)
spatialDetectionNetwork.setBoundingBoxScaleFactor(0.5)
spatialDetectionNetwork.setDepthLowerThreshold(100)
spatialDetectionNetwork.setDepthUpperThreshold(5000)
# Linking
monoLeft.out.link(stereo.left)
monoRight.out.link(stereo.right)
camRGB.preview.link(det_manip.inputImage)
det_manip.out.link(spatialDetectionNetwork.input)
spatialDetectionNetwork.out.link(xoutDetection.input)
stereo.depth.link(spatialDetectionNetwork.inputDepth)
if syncNN:
spatialDetectionNetwork.passthrough.link(xoutRGB.input)
else:
camRGB.preview.link(xoutRGB.input)
# set blazepose module
xyz = True
internal_frame_height = 450
no_pos_estimate = False
blazepose_model = BlazeposeDepthaiModule(input_src="rgb",
pd_model=None,
lm_model=None,
smoothing=True,
xyz=xyz,
crop=False,
internal_fps=internal_fps,
internal_frame_height=internal_frame_height,
force_detection=False,
stats=True,
trace=False,
no_pos_estimate=no_pos_estimate)
img_w,img_h = blazepose_model.set_pipeline(pipeline,camRGB,stereo,xinFrame,xoutBlazepose)
show_3d = False
renderer = BlazeposeRenderer(
blazepose_model,
show_3d=show_3d,
output=None)
device = dai.Device()
device.startPipeline(pipeline)
calib_data = device.readCalibration()
calib_lens_pos = calib_data.getLensPosition(dai.CameraBoardSocket.CAM_A)
print(f"RGB calibration lens position: {calib_lens_pos}")
camRGB.initialControl.setManualFocus(calib_lens_pos)
qDetection = device.getOutputQueue("Detection")
qRgb = device.getOutputQueue("RGB")
qBlazepose = device.getOutputQueue("Blazepose")
inQ = device.getInputQueue("MaskedFrame")
counter = 0
fps = 0
startTime = time.monotonic()
color = (255, 255, 255)
traj = []
# 定义编解码器并创建VideoWriter对象
if video:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
pose_fps = 8
video_out = cv2.VideoWriter(f'{ROOT_DIR}/{task}_camera_out.mp4', fourcc, pose_fps, (img_w,img_h))
frame_count = 0
traj_queue = []
intention_queue = []
righthand_queue = []
lefthand_queue = []
Predictor = IntentionPredictor(model_type=args.model_type)
old_upperbody = 0
old_intention = None
if os.path.exists(f'{ROOT_DIR}/images{task[-3:]}'):
# If it exists, remove the entire directory and its contents
shutil.rmtree(f'{ROOT_DIR}/images{task[-3:]}')
os.makedirs(f'{ROOT_DIR}/images{task[-3:]}')
while True:
frame = qRgb.get().getCvFrame()
if qDetection.has():
detections = qDetection.get()
detections = detections.detections
height = frame.shape[0]
width = frame.shape[1]
nearest_dist = np.inf
nearest_person = None
send_flag = 0
for detection in detections:
if detection.label == 15 and detection.confidence > 0.6 and \
get_distance(detection) < 2500: # TODO
send_flag = 1
if detection.spatialCoordinates.z < nearest_dist:
nearest_dist = detection.spatialCoordinates.z
nearest_person = detection
if send_flag == 0:
# print(f'detection{counter}')
for detection in detections:
if detection.label == 15 and detection.confidence > 0.6:
x1 = int(detection.xmin * width)
x2 = int(detection.xmax * width)
y1 = int(detection.ymin * height)
y2 = int(detection.ymax * height)
label = 'person'
cv2.putText(frame, str(label), (x1 + 10, y1 + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.putText(frame, "{:.2f}".format(detection.confidence*100), (x1 + 10, y1 + 35), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.putText(frame, f"X: {int(detection.spatialCoordinates.x)} mm", (x1 + 10, y1 + 50), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.putText(frame, f"Y: {int(detection.spatialCoordinates.y)} mm", (x1 + 10, y1 + 65), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.putText(frame, f"Z: {int(detection.spatialCoordinates.z)} mm", (x1 + 10, y1 + 80), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
cv2.rectangle(frame, (x1, y1), (x2, y2), (255, 0, 0), cv2.FONT_HERSHEY_SIMPLEX)
cv2.putText(frame, "NN fps: {:.2f}".format(fps), (2, frame.shape[0] - 4), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
if show:
cv2.imshow("preview", frame)
if cv2.waitKey(1) == ord('q'):
break
elif send_flag == 1: # send to pose estimation model
assert nearest_person
mask = np.zeros(frame.shape[:2], dtype="uint8")
# pdb.set_trace()
x1 = int(max((nearest_person.xmin-0.05),0) * width)
x2 = int(min((nearest_person.xmax+0.05),1) * width)
y1 = int(max((nearest_person.ymin-0.05),0) * height)
y2 = int(min((nearest_person.ymax+0.05),1) * height)
cv2.rectangle(mask, (x1,y1), (x2,y2), 255, -1)
masked_frame = cv2.bitwise_and(frame, frame, mask=mask)
img = dai.ImgFrame()
img.setData(masked_frame.transpose(2,0,1).flatten())
img.setTimestamp(time.monotonic())
img.setWidth(img_w)
img.setHeight(img_h)
img.setType(dai.RawImgFrame.Type.BGR888p)
inQ.send(img)
if qBlazepose.has():
res = marshal.loads(qBlazepose.get().getData())
body = blazepose_model.inference(res)
if body:
upperbody = np.concatenate((body.landmarks[11:25,:],body.landmarks[0:1,:]),axis=0)
body.landmarks = upperbody
masked_frame = renderer.draw(masked_frame, body)
# print(f'blazepose{counter}')
cv2.putText(masked_frame, "NN fps: {:.2f}".format(fps), (2, frame.shape[0] - 4), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
# save trajectory
if body:
try:
frame_count += 1
cv2.putText(masked_frame, "frame: {:.2f}".format(frame_count), (2, frame.shape[0] - 20), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
landmarks = body.landmarks_world
righthand = landmarks[16]
rightelbow = landmarks[14]
rightshoulder = landmarks[12]
lefthand = landmarks[15]
# upperbody = np.concatenate((landmarks[11:25,:],landmarks[0:1,:]),axis=0)
upperbody = body.landmarks
if len(traj_queue) < frame_size:
traj_queue.append(upperbody)
else:
traj_queue.pop(0)
traj_queue.append(upperbody)
assert len(traj_queue) <= frame_size, "the length of accumulated traj is longer than intention prediction frame size!"
# intention prediction based on learning
intention = None
if len(traj_queue)==frame_size: # send to intention prediction module
poses = np.array(traj_queue)
poses_norm = 2*(poses-poses.min())/(poses.max()-poses.min())
poses_world = camera_to_world(poses_norm)
poses_world[:, :, 2] -= np.min(poses_world[:, :, 2])
inputs = torch.tensor(poses_world.reshape(1,frame_size,-1)).float()
# pdb.set_trace()
pred_traj,pred_intention = Predictor.predict(inputs,args.restrict)
intention = get_intention(pred_intention)
# cv2.putText(masked_frame, f"intention:{intention}", (2, frame.shape[0] - 52), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
# intention prediction based on naive coordinates changes
# if righthand[0] < -0.5:
# intention = "get long tubes"
# elif righthand[0] > 0.3:
# intention = "get short tubes"
# elif len(traj_queue)==frame_size:
# change = 0
# for i in range(frame_size-1):
# change += abs(traj_queue[i+1]-traj_queue[i]).sum()
# if change < 1:
# intention = "waiting"
# else:
# intention = ""
# else:
# intention = ""
# old_upperbody = upperbody
righthand_pose = body.xyz/10 + righthand*100
righthand_xpose = body.xyz[0]/10 + righthand[0]*100
lefthand_pose = body.xyz/10 + lefthand*100
lefthand_xpose = body.xyz[0]/10 + lefthand[0]*100
if intention:
if len(intention_queue) < send_window:
if len(intention_queue) == 0:
intention_queue.append(intention)
righthand_queue.append(righthand_xpose)
lefthand_queue.append(lefthand_xpose)
else:
if intention == intention_queue[-1]:
intention_queue.append(intention)
righthand_queue.append(righthand_xpose)
lefthand_queue.append(lefthand_xpose)
else:
intention_queue = []
righthand_queue = []
lefthand_queue = []
old_intention = "no_action"
else:
if intention != old_intention and intention == intention_queue[-1] and intention != "no_action":
if intention == "get_connectors":
if args.outer_restrict == 'working_area':
# if righthand_xpose < -40:
if min(righthand_queue) < -33:
send_intention_to_ros(intention)
old_intention = intention
else:
send_intention_to_ros(intention)
old_intention = intention
elif intention == "get_screws":
if args.outer_restrict == 'working_area':
# if righthand_xpose > 100 or lefthand_xpose > 100:
if max(righthand_queue) > 80 or max(lefthand_queue) > 80:
send_intention_to_ros(intention)
old_intention = intention
else:
send_intention_to_ros(intention)
old_intention = intention
elif intention == "get_wheels":
if args.outer_restrict == 'working_area':
if max(righthand_queue) > 100 or max(lefthand_queue) > 100:
send_intention_to_ros(intention)
old_intention = intention
else:
send_intention_to_ros(intention)
old_intention = intention
else:
old_intention = intention
intention_queue = []
cv2.putText(masked_frame, f"intention:{intention}", (2, frame.shape[0] - 68), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
cv2.putText(masked_frame, "right hand x: {:.2f}, y: {:.2f}, z: {:.2f}".format(righthand_pose[0],righthand_pose[1],righthand_pose[2]), (2, frame.shape[0] - 52), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
cv2.putText(masked_frame, "left hand x: {:.2f}, y: {:.2f}, z: {:.2f}".format(lefthand_pose[0],lefthand_pose[1],lefthand_pose[2]), (2, frame.shape[0] - 36), cv2.FONT_HERSHEY_TRIPLEX, 0.4, (255,255,255))
traj.append(body)
if not video:
cv2.imwrite(f'{ROOT_DIR}/images{task[-3:]}/{frame_count}.png',masked_frame)
else:
video_out.write(masked_frame)
except:
pass
if show:
cv2.imshow("preview", masked_frame)
if cv2.waitKey(1) == ord('q'):
break
else:
continue
else:
continue
counter+=1
current_time = time.monotonic()
if (current_time - startTime) > 1 :
fps = counter / (current_time - startTime)
counter = 0
startTime = current_time
file = open(f'{ROOT_DIR}/{task}.pkl', 'wb')
pickle.dump(traj, file)
file.close()
if video:
video_out.release()
device.close()
if __name__ == '__main__':
# path
parentDir = Path(__file__).parent
detection_nnPath = str((parentDir / Path('./models/mobilenet-ssd_openvino_2021.4_6shave.blob')).resolve().absolute())
parser = argparse.ArgumentParser()
parser.add_argument('--syncNN', action="store_true",
help="sync the output of detection network with the input of pose estimation network")
parser.add_argument('--show', action="store_true",
help="show real time camera video")
parser.add_argument('--task', default="test",
help="name for traj.pkl and camera video")
parser.add_argument('--seq_len', default=5,
help="input frame window")
parser.add_argument('--send_window', default=3,
help="send if intention is consecutively recognized in send_window")
parser.add_argument('--video', action="store_true",
help="save video, else save images")
parser.add_argument('--restrict', type=str,default="ood",
help='four options:[no,working_area,ood,all]')
parser.add_argument('--outer_restrict',type=str,default="working_area",
help='outer restriction')
parser.add_argument('--model_type',type=str,default="final_intention",
help='two options:[final_intention,final_traj]')
args = parser.parse_args()
intention_sender(args)