-
Notifications
You must be signed in to change notification settings - Fork 13
/
Face_tracking01.py
363 lines (281 loc) · 11.5 KB
/
Face_tracking01.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
"""
Face_tracking01
Python program for realtime face tracking of a Universal Robot (tested with UR5cb)
see here for a demonstration: https://youtu.be/HHb-5dZoPFQ
Created by Robin Godwyll
License: GPL v3 https://www.gnu.org/licenses/gpl-3.0.en.html
"""
import URBasic
import math
import numpy as np
import sys
import cv2
import time
import imutils
from imutils.video import VideoStream
import math3d as m3d
"""SETTINGS AND VARIABLES ________________________________________________________________"""
RASPBERRY_BOOL = False
# If this is run on a linux system, a picamera will be used.
# If you are using a linux system, with a webcam instead of a raspberry pi delete the following if-statement
if sys.platform == "linux":
import picamera
from picamera.array import PiRGBArray
RASPBERRY_BOOL = True
ROBOT_IP = '192.168.178.120'
ACCELERATION = 0.9 # Robot acceleration value
VELOCITY = 0.8 # Robot speed value
robot_startposition = (math.radians(-218),
math.radians(-63),
math.radians(-93),
math.radians(-20),
math.radians(88),
math.radians(0))
# Path to the face-detection model:
pretrained_model = cv2.dnn.readNetFromCaffe("MODELS/deploy.prototxt.txt", "MODELS/res10_300x300_ssd_iter_140000.caffemodel")
video_resolution = (700, 400) # resolution the video capture will be resized to, smaller sizes can speed up detection
video_midpoint = (int(video_resolution[0]/2),
int(video_resolution[1]/2))
video_asp_ratio = video_resolution[0] / video_resolution[1] # Aspect ration of each frame
video_viewangle_hor = math.radians(25) # Camera FOV (field of fiew) angle in radians in horizontal direction
#video_viewangle_vert = video_viewangle_hor / video_asp_ratio # Camera FOV (field of fiew) angle in radians in vertical direction
m_per_pixel = 00.00009 # Variable which scales the robot movement from pixels to meters.
max_x = 0.2
max_y = 0.2
hor_rot_max = math.radians(50)
vert_rot_max = math.radians(25)
vs = VideoStream(src= 0 ,
usePiCamera= RASPBERRY_BOOL,
resolution=video_resolution,
framerate = 13,
meter_mode = "backlit",
exposure_mode ="auto",
shutter_speed = 8900,
exposure_compensation = 2,
rotation = 0).start()
time.sleep(0.2)
"""FUNCTIONS _____________________________________________________________________________"""
def find_faces_dnn(image):
"""
Finds human faces in the frame captured by the camera and returns the positions
uses the pretrained model located at pretrained_model
Input:
image: frame captured by the camera
Return Values:
face_centers: list of center positions of all detected faces
list of lists with 2 values (x and y)
frame: new frame resized with boxes and probabilities drawn around all faces
"""
frame = image
frame = imutils.resize(frame, width= video_resolution[0])
# grab the frame dimensions and convert it to a blob
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0,
(300, 300), (104.0, 177.0, 123.0))
# pass the blob through the network and obtain the detections and predictions
pretrained_model.setInput(blob)
# the following line handles the actual face detection
# it is the most computationally intensive part of the entire program
# TODO: find a quicker face detection model
detections = pretrained_model.forward()
face_centers = []
# loop over the detections
for i in range(0, detections.shape[2]):
# extract the confidence (i.e., probability) associated with the prediction
confidence = detections[0, 0, i, 2]
# filter out weak detections by ensuring the `confidence` is
# greater than the minimum confidence
if confidence < 0.4:
continue
# compute the (x, y)-coordinates of the bounding box for the object
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
(startX, startY, endX, endY) = box.astype("int")
# draw the bounding box of the face along with the associated probability
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
face_center = (int(startX + (endX - startX) / 2), int(startY + (endY - startY) / 2))
position_from_center = (face_center[0] - video_midpoint[0], face_center[1] - video_midpoint[1])
face_centers.append(position_from_center)
cv2.rectangle(frame, (startX, startY), (endX, endY),
(0, 0, 255), 2)
cv2.putText(frame, text, (startX, y),
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
#cv2.putText(frame, str(position_from_center), face_center, 0, 1, (0, 200, 0))
cv2.line(frame, video_midpoint, face_center, (0, 200, 0), 5)
cv2.circle(frame, face_center, 4, (0, 200, 0), 3)
return face_centers, frame
def show_frame(frame):
cv2.imshow('RobotCamera', frame)
k = cv2.waitKey(6) & 0xff
"""def convert_rpy(angles):
# This is very stupid:
# For some reason this doesnt work if exactly one value = 0
# the following simply make it a very small value if that happens
# I do not understand the math behind this well enough to create a better solution
zeros = 0
zero_pos = None
for i,ang in enumerate(angles):
if ang == 0 :
zeros += 1
zero_pos = i
if zeros == 1:
#logging.debug("rotation value" + str(zero_pos+1) +"is 0 a small value is added")
angles[zero_pos] = 1e-6
roll = angles[0]
pitch = angles[1]
yaw = angles[2]
# print ("roll = ", roll)
# print ("pitch = ", pitch)
# print ("yaw = ", yaw)
# print ("")
for ang in angles:
# print(ang % np.pi)
pass
if roll == pitch == yaw:
if roll % np.pi == 0:
rotation_vec = [0, 0, 0]
return rotation_vec
yawMatrix = np.matrix([
[math.cos(yaw), -math.sin(yaw), 0],
[math.sin(yaw), math.cos(yaw), 0],
[0, 0, 1]
])
# print("yawmatrix")
# print(yawMatrix)
pitchMatrix = np.matrix([
[math.cos(pitch), 0, math.sin(pitch)],
[0, 1, 0],
[-math.sin(pitch), 0, math.cos(pitch)]
])
# print("pitchmatrix")
# print(pitchMatrix)
rollMatrix = np.matrix([
[1, 0, 0],
[0, math.cos(roll), -math.sin(roll)],
[0, math.sin(roll), math.cos(roll)]
])
# print("rollmatrix")
# print(rollMatrix)
R = yawMatrix * pitchMatrix * rollMatrix
# print("R")
# print(R)
theta = math.acos(((R[0, 0] + R[1, 1] + R[2, 2]) - 1) / 2)
# print("theta = ",theta)
multi = 1 / (2 * math.sin(theta))
# print("multi = ", multi)
rx = multi * (R[2, 1] - R[1, 2]) * theta
ry = multi * (R[0, 2] - R[2, 0]) * theta
rz = multi * (R[1, 0] - R[0, 1]) * theta
rotation_vec = [rx,ry,rz]
# print(rx, ry, rz)
return rotation_vec
"""
def check_max_xy(xy_coord):
"""
Checks if the face is outside of the predefined maximum values on the lookaraound plane
Inputs:
xy_coord: list of 2 values: x and y value of the face in the lookaround plane.
These values will be evaluated against max_x and max_y
Return Value:
x_y: new x and y values
if the values were within the maximum values (max_x and max_y) these are the same as the input.
if one or both of the input values were over the maximum, the maximum will be returned instead
"""
x_y = [0, 0]
#print("xy before conversion: ", xy_coord)
if -max_x <= xy_coord[0] <= max_x:
# checks if the resulting position would be outside of max_x
x_y[0] = xy_coord[0]
elif -max_x > xy_coord[0]:
x_y[0] = -max_x
elif max_x < xy_coord[0]:
x_y[0] = max_x
else:
raise Exception(" x is wrong somehow:", xy_coord[0], -max_x, max_x)
if -max_y <= xy_coord[1] <= max_y:
# checks if the resulting position would be outside of max_y
x_y[1] = xy_coord[1]
elif -max_y > xy_coord[1]:
x_y[1] = -max_y
elif max_y < xy_coord[1]:
x_y[1] = max_y
else:
raise Exception(" y is wrong somehow", xy_coord[1], max_y)
#print("xy after conversion: ", x_y)
return x_y
def set_lookorigin():
"""
Creates a new coordinate system at the current robot tcp position.
This coordinate system is the basis of the face following.
It describes the midpoint of the plane in which the robot follows faces.
Return Value:
orig: math3D Transform Object
characterises location and rotation of the new coordinate system in reference to the base coordinate system
"""
position = robot.get_actual_tcp_pose()
orig = m3d.Transform(position)
return orig
def move_to_face(list_of_facepos,robot_pos):
"""
Function that moves the robot to the position of the face
Inputs:
list_of_facepos: a list of face positions captured by the camera, only the first face will be used
robot_pos: position of the robot in 2D - coordinates
Return Value:
prev_robot_pos: 2D robot position the robot will move to. The basis for the next call to this funtion as robot_pos
"""
face_from_center = list(list_of_facepos[0]) # TODO: find way of making the selected face persistent
prev_robot_pos = robot_pos
scaled_face_pos = [c * m_per_pixel for c in face_from_center]
robot_target_xy = [a + b for a, b in zip(prev_robot_pos, scaled_face_pos)]
# print("..", robot_target_xy)
robot_target_xy = check_max_xy(robot_target_xy)
prev_robot_pos = robot_target_xy
x = robot_target_xy[0]
y = robot_target_xy[1]
z = 0
xyz_coords = m3d.Vector(x, y, z)
x_pos_perc = x / max_x
y_pos_perc = y / max_y
x_rot = x_pos_perc * hor_rot_max
y_rot = y_pos_perc * vert_rot_max * -1
tcp_rotation_rpy = [y_rot, x_rot, 0]
# tcp_rotation_rvec = convert_rpy(tcp_rotation_rpy)
tcp_orient = m3d.Orientation.new_euler(tcp_rotation_rpy, encoding='xyz')
position_vec_coords = m3d.Transform(tcp_orient, xyz_coords)
oriented_xyz = origin * position_vec_coords
oriented_xyz_coord = oriented_xyz.get_pose_vector()
coordinates = oriented_xyz_coord
qnear = robot.get_actual_joint_positions()
next_pose = coordinates
robot.set_realtime_pose(next_pose)
return prev_robot_pos
"""FACE TRACKING LOOP ____________________________________________________________________"""
# initialise robot with URBasic
print("initialising robot")
robotModel = URBasic.robotModel.RobotModel()
robot = URBasic.urScriptExt.UrScriptExt(host=ROBOT_IP,robotModel=robotModel)
robot.reset_error()
print("robot initialised")
time.sleep(1)
# Move Robot to the midpoint of the lookplane
robot.movej(q=robot_startposition, a= ACCELERATION, v= VELOCITY )
robot_position = [0,0]
origin = set_lookorigin()
robot.init_realtime_control() # starts the realtime control loop on the Universal-Robot Controller
time.sleep(1) # just a short wait to make sure everything is initialised
try:
print("starting loop")
while True:
frame = vs.read()
face_positions, new_frame = find_faces_dnn(frame)
show_frame(new_frame)
if len(face_positions) > 0:
robot_position = move_to_face(face_positions,robot_position)
print("exiting loop")
except KeyboardInterrupt:
print("closing robot connection")
# Remember to always close the robot connection, otherwise it is not possible to reconnect
robot.close()
except:
robot.close()