Skip to content

Commit 374e31a

Browse files
committed
[feat]: full body, hand, face detection added, frame added to response
1 parent 2eb203a commit 374e31a

File tree

3 files changed

+235
-115
lines changed

3 files changed

+235
-115
lines changed

Backend/mocap.py

+114-86
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
"""
4-
Created on Fri Sep 27 16:42:52 2019
5-
6-
@author: James Wu
7-
"""
8-
91
import cv2
102
import numpy as np
113
import dlib
124
import time
135
import math
146
import sys
15-
167
import socket
178

9+
#@citation: James Wu, OpenVHead
10+
1811
detector = dlib.get_frontal_face_detector()
1912
predictor = dlib.shape_predictor("./data/shape_predictor_68_face_landmarks.dat")
2013
POINTS_NUM_LANDMARK = 68
@@ -217,101 +210,134 @@ def get_quaternion(rotation_vector):
217210
return round(w,4), round(x,4), round(y,4), round(z,4)
218211

219212

213+
214+
215+
#get process based on the input frame
216+
def get_frame_facial_mocap(img,frame):
217+
size = img.shape
218+
219+
if size[0] > 700:
220+
h = size[0] / 3
221+
w = size[1] / 3
222+
img = cv2.resize(img, (int(w), int(h)), interpolation=cv2.INTER_CUBIC)
223+
size = img.shape
224+
225+
ret, landmark_shape = get_image_points(img)
226+
if ret != 0:
227+
print('ERROR: get_image_points failed')
228+
return None
229+
230+
# Compute feature parameters of facial expressions (eyes, mouth)
231+
landmarks_orig = landmarks_to_np(landmark_shape) # convert format
232+
233+
leftEyeWid, rightEyewid, mouthWid, mouthLen = get_feature_parameters(landmarks_orig)
234+
parameters_str = 'leftEyeWid:{}, rightEyewid:{}, mouthWid:{}, mouthLen:{}'.format(leftEyeWid, rightEyewid,
235+
mouthWid, mouthLen)
236+
print(parameters_str)
237+
238+
try:
239+
json_data = {
240+
'leftEyeWid': leftEyeWid,
241+
'rightEyeWid': rightEyewid,
242+
'mouthWid': mouthWid,
243+
'mouthLen': mouthLen,
244+
'frame': frame
245+
}
246+
return json_data
247+
except:
248+
return None
249+
250+
220251
#calculating facial expression
221252
def face_mocap_video(video_path, debug=False):
222-
detector = dlib.get_frontal_face_detector()
223-
predictor = dlib.shape_predictor("./data/shape_predictor_68_face_landmarks.dat")
224-
POINTS_NUM_LANDMARK = 68
225-
226-
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # CLAHE Object (for Adaptive histogram equalization)
227-
228-
boxPoints3D = np.array(([500., 500., 500.],
229-
[-500., 500., 500.],
230-
[-500., -500., 500.],
231-
[500., -500., 500.],
232-
[500., 500., -500.],
233-
[-500., 500., -500.],
234-
[-500., -500., -500.],
235-
[500., -500., -500.]))
236-
boxPoints2D = np.zeros((1, 1, 8, 2))
237-
# parameters for mean filter
238-
windowlen_1 = 5
239-
queue3D_points = np.zeros((windowlen_1, POINTS_NUM_LANDMARK, 2))
240-
241-
windowlen_2 = 5
242-
queue1D = np.zeros(windowlen_2)
243-
244-
# pamameters for kalman filter
245-
XX = 0
246-
PP = 0.01
253+
cap = cv2.VideoCapture(video_path)
254+
frame = 0
255+
while (cap.isOpened()):
256+
# Read Image
257+
ret, img = cap.read()
258+
# current_frame
259+
frame = cap.get(cv2.CAP_PROP_POS_FRAMES)
260+
261+
if ret != True:
262+
print('read frame failed')
263+
# continue
264+
break
265+
result = get_frame_facial_mocap(img,frame)
266+
if result is not None:
267+
yield result
268+
cap.release()
269+
270+
271+
272+
273+
def face_mocap_pose_video(video_path, debug=False):
274+
275+
247276
# initialize kalman object
248277
KalmanX = KalmanObject(POINTS_NUM_LANDMARK, 1, 10) # Tune Q, R to change landmarks_x sensitivity
249278
KalmanY = KalmanObject(POINTS_NUM_LANDMARK, 1, 10) # Tune Q, R to change landmarks_y sensitivity
250279
uu_ = np.zeros((POINTS_NUM_LANDMARK))
251280
# initialize PARAMETERS
252281
landmarks = np.zeros((POINTS_NUM_LANDMARK, 2))
253282

254-
test_data = [0]
255-
test_time = [0]
256-
257-
# initialize kalman object
258-
KalmanX = KalmanObject(POINTS_NUM_LANDMARK, 1,10) # Tune Q, R to change landmarks_x sensitivity
259-
KalmanY = KalmanObject(POINTS_NUM_LANDMARK, 1,10) # Tune Q, R to change landmarks_y sensitivity
260-
uu_ = np.zeros((POINTS_NUM_LANDMARK))
261-
# initialize PARAMETERS
262-
landmarks = np.zeros((POINTS_NUM_LANDMARK,2))
263-
cap = cv2.VideoCapture(video_path)
264-
frame = 0
283+
open_time = time.time()
284+
cap = cv2.VideoCapture(0)
265285
while (cap.isOpened()):
286+
start_time = time.time()
287+
266288
# Read Image
267-
try:
268-
269-
frame += 1
270-
ret, img = cap.read()
271-
if ret != True:
272-
print('read frame failed')
273-
# continue
274-
break
289+
ret, img = cap.read()
290+
if ret != True:
291+
print('read frame failed')
292+
# continue
293+
break
294+
size = img.shape
295+
296+
if size[0] > 700:
297+
h = size[0] / 3
298+
w = size[1] / 3
299+
img = cv2.resize(img, (int(w), int(h)), interpolation=cv2.INTER_CUBIC)
275300
size = img.shape
276301

277-
if size[0] > 700:
278-
h = size[0] / 3
279-
w = size[1] / 3
280-
img = cv2.resize(img, (int(w), int(h)), interpolation=cv2.INTER_CUBIC)
281-
size = img.shape
302+
# img = cv2.normalize(img,dst=None,alpha=350,beta=10,norm_type=cv2.NORM_MINMAX)
303+
ret, landmark_shape = get_image_points(img)
304+
if ret != 0:
305+
print('ERROR: get_image_points failed')
306+
continue
307+
308+
# Compute feature parameters of facial expressions (eyes, mouth)
309+
landmarks_orig = landmarks_to_np(landmark_shape) # convert format
310+
311+
# Apply kalman filter to landmarks FOR POSE ESTIMATION
312+
KalmanX.kalman_update(uu_, landmarks_orig[:, 0])
313+
KalmanY.kalman_update(uu_, landmarks_orig[:, 1])
314+
landmarks[:, 0] = KalmanX.xx.astype(np.int32)
315+
landmarks[:, 1] = KalmanY.xx.astype(np.int32)
316+
317+
landmarks = mean_filter_for_landmarks(landmarks) # Apply smooth filter to landmarks FOR POSE ESTIMATION
318+
leftEyeWid, rightEyewid, mouthWid, mouthLen = get_feature_parameters(landmarks_orig)
282319

283-
# img = cv2.normalize(img,dst=None,alpha=350,beta=10,norm_type=cv2.NORM_MINMAX)
284-
ret, landmark_shape = get_image_points(img)
285-
if ret != 0:
286-
print('ERROR: get_image_points failed')
287-
continue
288320

289-
# Compute feature parameters of facial expressions (eyes, mouth)
290-
landmarks_orig = landmarks_to_np(landmark_shape) # convert format
291321

292-
leftEyeWid, rightEyewid, mouthWid, mouthLen = get_feature_parameters(landmarks_orig)
293-
parameters_str = 'leftEyeWid:{}, rightEyewid:{}, mouthWid:{}, mouthLen:{}'.format(leftEyeWid, rightEyewid,
294-
mouthWid, mouthLen)
295-
print(parameters_str)
322+
# Five feature points for pose estimation
323+
# image_points = np.vstack((landmarks[30],landmarks[8],landmarks[36],landmarks[45],landmarks[48],landmarks[54]))
324+
image_points = np.vstack(
325+
(landmarks[30], landmarks[8], landmarks[36], landmarks[45], landmarks[1], landmarks[15]))
296326

327+
ret, rotation_vector, translation_vector, camera_matrix, dist_coeffs = get_pose_estimation(size, image_points)
328+
if ret != True:
329+
print('ERROR: get_pose_estimation failed')
330+
continue
331+
used_time = time.time() - start_time
332+
print("used_time:{} sec".format(round(used_time, 3)))
297333

334+
# Convert rotation_vector to quaternion
335+
w, x, y, z = get_quaternion(rotation_vector)
336+
quaternion_str = 'w:{}, x:{}, y:{}, z:{}'.format(w, x, y, z)
337+
print(quaternion_str)
298338

299-
try:
300-
json_data = {
301-
'leftEyeWid': leftEyeWid,
302-
'rightEyeWid': rightEyewid,
303-
'mouthWid': mouthWid,
304-
'mouthLen': mouthLen,
305-
'frame': frame
306-
}
307-
yield json_data
308-
except:
309-
print("\n yielding problem!.\n")
310339

311-
except:
312-
print("\n calculation prblem!.\n")
313340

314-
continue
315341
# ============================================================================
316342
# For visualization only (below)
317343
# ============================================================================
@@ -354,7 +380,9 @@ def face_mocap_video(video_path, debug=False):
354380

355381
if (cv2.waitKey(5) & 0xFF) == 27: # Press ESC to quit
356382
break
383+
cap.release()
357384

358-
# client.close() # Socket disconnect
359-
# cap.release()
360-
# cv2.destroyAllWindows()
385+
# if __name__ == '__main__':
386+
# print("dd")
387+
# for i in face_mocap_video(video_path="C:/Danial/Projects/Danial/DigiHuman/Assets/video/onegai_darling.mp4"):
388+
# continue

0 commit comments

Comments
 (0)