-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathteleop.py
244 lines (184 loc) · 7.83 KB
/
teleop.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Attribution: Part of this code is based on
https://github.com/Kazuhito00/mediapipe-python-sample/blob/main/sample_pose.py
(Apache 2.0 Licensed)
"""
import copy
import argparse
import cv2 as cv
import numpy as np
import mediapipe as mp
import matplotlib.pyplot as plt
from time import time, sleep
from utils import CvFpsCalc
from utils import KeypointsToAngles
from utils import SocketSend
from utils import SocketReceiveSignal
from utils import calc_bounding_rect, draw_landmarks, plot_world_landmarks, draw_bounding_rect
keypointsToAngles = KeypointsToAngles()
angle_trace = []
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0)
parser.add_argument("--width", help='cap width', type=int, default=960)
parser.add_argument("--height", help='cap height', type=int, default=540)
parser.add_argument("--video", type=str, default="")
parser.add_argument("--fps", type=int, default=10)
parser.add_argument("--model_complexity",
help='model_complexity(0,1(default),2)',
type=int,
default=1)
parser.add_argument("--min_detection_confidence",
help='min_detection_confidence',
type=float,
default=0.5)
parser.add_argument("--min_tracking_confidence",
help='min_tracking_confidence',
type=float,
default=0.5)
parser.add_argument('--use_brect', action='store_true')
parser.add_argument('--plot_world_landmark', action='store_true')
parser.add_argument('--plot_angle_trace', action='store_true')
parser.add_argument('--enable_teleop', action='store_true')
args = parser.parse_args()
return args
def main():
global angle_trace
args = get_args()
cap_device = args.device
cap_width = args.width
cap_height = args.height
model_complexity = args.model_complexity
min_detection_confidence = args.min_detection_confidence
min_tracking_confidence = args.min_tracking_confidence
use_brect = args.use_brect
plot_world_landmark = args.plot_world_landmark
plot_angle_trace = args.plot_angle_trace
enable_teleop = args.enable_teleop
video = args.video
fps = args.fps
if len(video) == 0:
cap = cv.VideoCapture(cap_device)
else:
cap = cv.VideoCapture(video)
cap.set(cv.CAP_PROP_FRAME_WIDTH, cap_width)
cap.set(cv.CAP_PROP_FRAME_HEIGHT, cap_height)
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(
model_complexity=model_complexity,
enable_segmentation=False,
min_detection_confidence=min_detection_confidence,
min_tracking_confidence=min_tracking_confidence,
)
if enable_teleop:
# Initialize socket to send keypoints
ss = SocketSend()
sr = SocketReceiveSignal()
cvFpsCalc = CvFpsCalc(buffer_len=10)
if plot_world_landmark:
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
fig.subplots_adjust(left=0.0, right=1, bottom=0, top=1)
while True:
start = time()
display_fps = cvFpsCalc.get()
ret, image = cap.read()
if not ret:
break
image = cv.flip(image, 1)
debug_image = copy.deepcopy(image)
image = cv.cvtColor(image, cv.COLOR_BGR2RGB)
results = pose.process(image)
if results.pose_landmarks is not None:
brect = calc_bounding_rect(debug_image, results.pose_landmarks)
debug_image = draw_landmarks(debug_image, results.pose_landmarks)
debug_image = draw_bounding_rect(use_brect, debug_image, brect)
if plot_world_landmark:
if results.pose_world_landmarks is not None:
plot_world_landmarks(plt, ax, results.pose_world_landmarks)
if results.pose_world_landmarks is not None:
cv.putText(debug_image, "TRACKING", (10, 90),
cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2, cv.LINE_AA)
if not do_teleop(results.pose_world_landmarks):
# limit reached
cv.putText(debug_image, "LIMIT", (10, 140),
cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255), 2, cv.LINE_AA)
if enable_teleop:
socket_stream_landmarks(ss, results.pose_world_landmarks)
cv.putText(debug_image, "FPS:" + str(display_fps), (10, 30),
cv.FONT_HERSHEY_SIMPLEX, 1.0, (0, 145, 255), 2, cv.LINE_AA)
key = cv.waitKey(1)
if key == 27: # ESC
break
cv.imshow('Pepper Teleop', debug_image)
if (len(video)>0):
sleep(max(1./fps - (time() - start), 0))
if plot_angle_trace:
angle_labels = ["LShoulderPitch","LShoulderRoll", "LElbowYaw", "LElbowRoll", "RShoulderPitch","RShoulderRoll", "RElbowYaw", "RElbowRoll", "HipPitch"]
angle_trace = np.array(angle_trace)
lines = plt.plot(angle_trace[:,:])
plt.legend(iter(lines), angle_labels[:])
plt.show()
cap.release()
cv.destroyAllWindows()
def socket_stream_landmarks(ss, landmarks):
p = []
for index, landmark in enumerate(landmarks.landmark):
p.append([landmark.x, landmark.y, landmark.z])
# p = np.array(p)
pNeck = (0.5 * (np.array(p[11]) + np.array(p[12]))).tolist()
pMidHip = (0.5 * (np.array(p[23]) + np.array(p[24]))).tolist()
wp_dict = {}
wp_dict['0'] = p[0] # Nose
wp_dict['1'] = pNeck # Neck
wp_dict['2'] = p[11] # LShoulder
wp_dict['3'] = p[13] # LElbow
wp_dict['4'] = p[15] # LWrist
wp_dict['5'] = p[12] # RShoulder
wp_dict['6'] = p[14] # RElbow
wp_dict['7'] = p[16] # RWrist
wp_dict['8'] = pMidHip # MidHip
# print(wp_dict)
ss.send(wp_dict)
def checkLim(val, limits):
return val < limits[0] or val > limits[1]
def do_teleop(landmarks):
global angle_trace
p = []
for index, landmark in enumerate(landmarks.landmark):
p.append([landmark.x, landmark.y, landmark.z])
p = np.array(p)
limitsLShoulderPitch = [-2.0857, 2.0857]
limitsRShoulderPitch = [-2.0857, 2.0857]
limitsLShoulderRoll = [ 0.0087, 1.5620]
limitsRShoulderRoll = [-1.5620,-0.0087]
limitsLElbowYaw = [-2.0857, 2.0857]
limitsRElbowYaw = [-2.0857, 2.0857]
limitsLElbowRoll = [-1.5620,-0.0087]
limitsRElbowRoll = [ 0.0087, 1.5620]
limitsHipPitch = [-1.0385, 1.0385]
pNeck = (0.5 * (np.array(p[11]) + np.array(p[12]))).tolist()
pMidHip = (0.5 * (np.array(p[23]) + np.array(p[24]))).tolist()
LShoulderPitch, LShoulderRoll = keypointsToAngles.obtain_LShoulderPitchRoll_angles(pNeck, p[11], p[13], pMidHip)
RShoulderPitch, RShoulderRoll = keypointsToAngles.obtain_RShoulderPitchRoll_angles(pNeck, p[12], p[14], pMidHip)
LElbowYaw, LElbowRoll = keypointsToAngles.obtain_LElbowYawRoll_angle(pNeck, p[11], p[13], p[15])
RElbowYaw, RElbowRoll = keypointsToAngles.obtain_RElbowYawRoll_angle(pNeck, p[12], p[14], p[16])
HipPitch = keypointsToAngles.obtain_HipPitch_angles(pMidHip, pNeck) # This is switched, why?
angles = [LShoulderPitch,LShoulderRoll, LElbowYaw, LElbowRoll, RShoulderPitch,RShoulderRoll, RElbowYaw, RElbowRoll, HipPitch]
print(angles)
angle_trace.append(angles)
if (checkLim(LShoulderPitch, limitsLShoulderPitch) or
checkLim(RShoulderPitch, limitsRShoulderPitch) or
checkLim(LShoulderRoll, limitsLShoulderRoll) or
checkLim(RShoulderRoll, limitsRShoulderRoll) or
checkLim(LElbowYaw, limitsLElbowYaw) or
checkLim(RElbowYaw, limitsRElbowYaw) or
checkLim(LElbowRoll, limitsLElbowRoll) or
checkLim(RElbowRoll, limitsRElbowRoll)):
return False
else:
return True
if __name__ == '__main__':
main()