forked from millerhooks/close2mocap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distance_utils.py
83 lines (64 loc) · 2.08 KB
/
distance_utils.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
import bpy
import numpy as np
import cv2
def get_mag(v1, v2, flat=False):
''' returns the mag between two vectors'''
if flat:
v1 = v1[:, :2]
v2 = v2[:, :2]
v3 = (v1 - v2)
v3 = v3 ** 2
mag = np.sqrt(np.sum(v3, axis=1))
print('mag', mag[0])
return mag
def norm_arr(distance, scale):
'''normalise range'''
# scale distance
distance = distance / scale
max = np.max(distance)
min = np.min(distance)
distance = (distance - min) /(max - min)
return distance
def create_empty(name, data):
'''create empty with mocap data'''
bpy.ops.mesh.primitive_cube_add()
bpy.context.active_object.name = name
obj = bpy.context.active_object
for i, d in enumerate(data):
obj.location[0] = d
obj.keyframe_insert(data_path="location", frame=i)
def nothing(x):
pass
def get_shape(landmarks, verts_idx, mask, kx, ky):
''' use the face verts to create a shape for the mask'''
points = [landmarks[idx] for idx in verts_idx]
points = np.array(points)[:, :2]
points[:, 0] *= mask.shape[1]
points[:, 1] *= mask.shape[0]
points = points.astype(int)
mask = cv2.fillConvexPoly(mask, points, 255)
kernel = np.ones((kx, ky), np.uint8)
mask = cv2.erode(mask, kernel)
return mask
def find_eye_centre(contours, idx):
''' finds the centre of the contour'''
x = 0
y = 0
print(contours[idx])
points = len(contours[idx])
for kp in contours[idx]:
x = x+kp[0][0]
y = y+kp[0][1]
x = int(x/points)
y = int(y/points)
return (x, y)
def find_iris(img, img_o):
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
if len(contours) > 0:
#draw cirlce on eye
eye_loc = find_eye_centre(contours, 0)
cv2.circle(img_o, (eye_loc[0], eye_loc[1]), 1, (0, 0, 255), 2)
else:
eye_loc = (0, 0)
return eye_loc