Skip to content

Commit d24c491

Browse files
committed
add convert_98_to_68_landmarks
1 parent d020096 commit d24c491

1 file changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import numpy as np
2+
3+
4+
def load_txt_file(file_path):
5+
"""Load data or string from txt file."""
6+
7+
with open(file_path, 'r') as cfile:
8+
content = cfile.readlines()
9+
cfile.close()
10+
content = [x.strip() for x in content]
11+
num_lines = len(content)
12+
return content, num_lines
13+
14+
15+
def anno_parser(anno_path, num_pts, line_offset=0):
16+
"""Parse the annotation.
17+
Args:
18+
anno_path: path of anno file (suffix .txt)
19+
num_pts: number of landmarks.
20+
line_offset: first point starts, default: 0.
21+
22+
Returns:
23+
pts: num_pts x 2 (x, y)
24+
"""
25+
26+
data, _ = load_txt_file(anno_path)
27+
n_points = num_pts
28+
# read points coordinate.
29+
pts = np.zeros((n_points, 2), dtype='float32')
30+
for point_index in range(n_points):
31+
try:
32+
pts_list = data[point_index + line_offset].split(',')
33+
pts[point_index, 0] = float(pts_list[0])
34+
pts[point_index, 1] = float(pts_list[1])
35+
except ValueError:
36+
print(f'Error in loading points in {anno_path}')
37+
return pts
38+
39+
40+
def landmark_98_to_68(landmark_98):
41+
"""Transfer 98 landmark positions to 68 landmark positions.
42+
Args:
43+
landmark_98(numpy array): Polar coordinates of 98 landmarks, (98, 2)
44+
Returns:
45+
landmark_68(numpy array): Polar coordinates of 98 landmarks, (68, 2)
46+
"""
47+
48+
landmark_68 = np.zeros((68, 2), dtype='float32')
49+
# cheek
50+
for i in range(0, 33):
51+
if i % 2 == 0:
52+
landmark_68[int(i / 2), :] = landmark_98[i, :]
53+
# nose
54+
for i in range(51, 60):
55+
landmark_68[i - 24, :] = landmark_98[i, :]
56+
# mouth
57+
for i in range(76, 96):
58+
landmark_68[i - 28, :] = landmark_98[i, :]
59+
# left eyebrow
60+
landmark_68[17, :] = landmark_98[33, :]
61+
landmark_68[18, :] = (landmark_98[34, :] + landmark_98[41, :]) / 2
62+
landmark_68[19, :] = (landmark_98[35, :] + landmark_98[40, :]) / 2
63+
landmark_68[20, :] = (landmark_98[36, :] + landmark_98[39, :]) / 2
64+
landmark_68[21, :] = (landmark_98[37, :] + landmark_98[38, :]) / 2
65+
# right eyebrow
66+
landmark_68[22, :] = (landmark_98[42, :] + landmark_98[50, :]) / 2
67+
landmark_68[23, :] = (landmark_98[43, :] + landmark_98[49, :]) / 2
68+
landmark_68[24, :] = (landmark_98[44, :] + landmark_98[48, :]) / 2
69+
landmark_68[25, :] = (landmark_98[45, :] + landmark_98[47, :]) / 2
70+
landmark_68[26, :] = landmark_98[46, :]
71+
# left eye
72+
LUT_landmark_68_left_eye = [36, 37, 38, 39, 40, 41]
73+
LUT_landmark_98_left_eye = [60, 61, 63, 64, 65, 67]
74+
for idx, landmark_98_index in enumerate(LUT_landmark_98_left_eye):
75+
landmark_68[LUT_landmark_68_left_eye[idx], :] = landmark_98[landmark_98_index, :]
76+
# right eye
77+
LUT_landmark_68_right_eye = [42, 43, 44, 45, 46, 47]
78+
LUT_landmark_98_right_eye = [68, 69, 71, 72, 73, 75]
79+
for idx, landmark_98_index in enumerate(LUT_landmark_98_right_eye):
80+
landmark_68[LUT_landmark_68_right_eye[idx], :] = landmark_98[landmark_98_index, :]
81+
82+
return landmark_68

0 commit comments

Comments
 (0)