-
Notifications
You must be signed in to change notification settings - Fork 1
/
realsense_detect_skeleton.py
229 lines (186 loc) · 7.32 KB
/
realsense_detect_skeleton.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
import math
from time import time, sleep
from json import dumps
from datetime import datetime
import numpy as np
import cv2
from oscpy.client import OSCClient
import pyrealsense2 as rs
client = OSCClient(b'localhost', 8003)
kernel_size = 5
threshold = 0.1
in_width = 160
in_height = 160
MEAN = 0.3
SCALE = 1/255
MODE = "COCO"
CALC = "cpu"
if MODE == "COCO":
protoFile = "pose/coco/pose_deploy_linevec.prototxt"
weightsFile = "pose/coco/pose_iter_440000.caffemodel"
num_points = 18
POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],[6,7],[1,8],[8,9],[9,10],
[1,11],[11,12],[12,13],[0,14],[0,15],[14,16],[15,17]]
elif MODE == "MPI" :
protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
weightsFile = "pose/mpi/pose_iter_160000.caffemodel"
num_points = 15
POSE_PAIRS = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14],
[14,8], [8,9], [9,10], [14,11], [11,12], [12,13] ]
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
if CALC == "cpu":
net.setPreferableBackend(cv2.dnn.DNN_TARGET_CPU)
print("Using CPU device")
elif CALC == "gpu":
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
print("Using GPU device")
def save_json(fichier, data):
with open(fichier, "w") as fd:
fd.write(dumps(data))
fd.close()
def get_blobFromImage(frame):
"""
blobFromImage ( InputArray image,
double scalefactor = 1.0,
const Size & size = Size(),
const Scalar & mean = Scalar(),
bool swapRB = false,
bool crop = false,
int ddepth = CV_32F )
inpBlob = cv2.dnn.blobFromImage(frame, 1.0/255, (in_width, in_height),
(0, 0, 0), swapRB=False, crop=False, ddepth=cv2.CV_32F)
"""
inpBlob = cv2.dnn.blobFromImage(frame,
scalefactor=SCALE,
size=(in_width, in_height),
mean=MEAN, # in (mean-R, mean-G, mean-B) order
swapRB=True,
crop = False,
ddepth = cv2.CV_32F)
return inpBlob
pipeline = rs.pipeline()
config = rs.config()
pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()
device_product_line = str(device.get_info(rs.camera_info.product_line))
if device_product_line == 'L500':
config.enable_stream(rs.stream.color, 960, 540, rs.format.bgr8, 30)
else:
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
pipeline.start(config)
align = rs.align(rs.stream.color)
unaligned_frames = pipeline.wait_for_frames()
frames = align.process(unaligned_frames)
depth = frames.get_depth_frame()
depth_intrinsic = depth.profile.as_video_stream_profile().intrinsics
t0 = time()
n = 0
data = [] # Pour enregistrement d'un json
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
try:
while True:
unaligned_frames = pipeline.wait_for_frames()
frames = align.process(unaligned_frames)
color_frame = frames.get_color_frame()
depth_frame = frames.get_depth_frame()
if not color_frame or not depth_frame:
continue
depth = np.asanyarray(depth_frame.get_data())
frame = np.asanyarray(color_frame.get_data())
frameWidth = frame.shape[1]
frameHeight = frame.shape[0]
inpBlob = get_blobFromImage(frame)
net.setInput(inpBlob)
output = net.forward()
H = output.shape[2]
W = output.shape[3]
# Pour ajouter tous les points en 2D et 3D, y compris None
points2D = []
points3D = []
for num_point in range(num_points):
# confidence map of corresponding body's part.
probMap = output[0, num_point, :, :]
# Find global maxima of the probMap.
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
# Scale the point to fit on the original image
x = int(((frameWidth * point[0]) / W) + 0.5)
y = int(((frameHeight * point[1]) / H) + 0.5)
if prob > threshold : # 0.1
points2D.append([x, y])
kernel = []
x_min = max(x - kernel_size, 0) # mini à 0
x_max = max(x + kernel_size, 0)
y_min = max(y - kernel_size, 0)
y_max = max(y + kernel_size, 0)
for u in range(x_min, x_max):
for v in range(y_min, y_max):
kernel.append(depth_frame.get_distance(u, v))
# Equivaut à median si 50
median = np.percentile(np.array(kernel), 50)
pt = None
point_with_deph = None
if median >= 0.05:
# DepthIntrinsics, InputPixelAsFloat, DistanceToTargetInDepthScale)
# Coordonnées du point dans un repère centré sur la caméra
# 3D coordinate space with origin = Camera
point_with_deph = rs.rs2_deproject_pixel_to_point(
depth_intrinsic,
[x, y],
median)
if point_with_deph:
points3D.append(point_with_deph)
else:
points3D.append(None)
else:
points2D.append(None)
points3D.append(None)
# #print("3", points3D)
# Envoi du point en OSC en 3D
# Liste de n°body puis toutes les coordonnées sans liste de 3
# oscpy n'envoie pas de liste de listes
bodyId = 110 # TODO récupérer le vrai nums de body
msg = []
for point in points3D:
if point:
for i in range(3):
# Envoi en int
msg.append(int(point[i]*1000))
# Si pas de point ajout arbitraire de 3 fois -1 pour avoir toujours
# 3*18 valeurs dans la liste
else:
msg.extend((-1000000, -1000000, -1000000)) # tuple ou list
# N° body à la fin
msg.append(bodyId)
data.append(msg)
client.send_message(b'/points', msg)
# Draw articulation 2D
for point in points2D:
if point:
cv2.circle(frame, (point[0], point[1]), 4, (0, 255, 255),
thickness=2)
# Draw Skeleton
for pair in POSE_PAIRS:
if points2D[pair[0]] and points2D[pair[1]]:
p1 = tuple(points2D[pair[0]])
p2 = tuple(points2D[pair[1]])
cv2.line(frame, p1, p2, (0, 255, 0), 2)
cv2.imshow('RealSense', frame)
n += 1
t = time()
if t - t0 > 10:
print("FPS =", round(n/10, 1))
t0 = t
n = 0
if cv2.waitKey(1) == 27:
break
cv2.destroyAllWindows()
finally:
pipeline.stop()
sleep(1)
dt_now = datetime.now()
dt = dt_now.strftime("%Y_%m_%d_%H_%M")
fichier = f"./blender_osc/scripts/cap_{dt}.json"
save_json(fichier, data)