/
part2.py
153 lines (121 loc) · 4.89 KB
/
part2.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
import xml.etree.ElementTree as ET
from sklearn.cluster import KMeans
from scipy.spatial import distance
def createData(filename):
tree = ET.parse(filename)
root = tree.getroot()
def parseData(data):
parsed_data = data.split()
for d in range(len(parsed_data)):
headpose_data.append([float(parsed_data[0]), float(parsed_data[1]), float(parsed_data[2])])
def parseAndAddArrayData(data):
# Data is currently [x1, x2, x3, ... y1, y2, y3, ...]
parsed_data = data.split()
for j in range(len(parsed_data) / 2):
n = j + len(parsed_data) / 2
feature_data[i].append((float(parsed_data[j]), float(parsed_data[n])))
# Set up data
feature_data = []
headpose_data = []
i = 0
for frame in root.findall('frame'):
feature_data.append([])
for landmark in frame.findall('landmarks'):
for feature in landmark.findall('data'):
parseAndAddArrayData(feature.text)
for pose in frame.findall('headpose'):
for coord in pose.findall('data'):
parseData(coord.text)
i += 1
return feature_data, headpose_data
def distance(p1, p2):
if len(p1) == 2:
return ((( float(p2[0]) - float(p1[0]) )**2) + (( float(p2[1]) - float(p1[1]) )**2))**0.5
return ((( float(p2[0]) - float(p1[0]) )**2) + (( float(p2[1]) - float(p1[1]) )**2) + ( float(p2[2]) - float(p1[2]) )**2)**0.5
def plotData():
import numpy as np
import matplotlib.pyplot as plt
# Dataset to numpy array
data = np.array(training_data[0])[:,0:2]
# Plot
N = len(data)
labels = ['{0}'.format(i) for i in range(N)]
plt.subplots_adjust(bottom = 0.1)
plt.scatter(
data[:, 0], data[:, 1], marker = 'o',
cmap = plt.get_cmap('Spectral'))
for label, x, y in zip(labels, data[:, 0], data[:, 1]):
plt.annotate(
label,
xy = (x, y), xytext = (-20, 20),
textcoords = 'offset points', ha = 'right', va = 'bottom',
bbox = dict(boxstyle = 'round,pad=0.5', fc = 'yellow', alpha = 0.5),
arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3,rad=0'))
plt.show()
def calculate_asymmetry(frame, training_data, headpose_data):
# These are the pairs from left to right features across the face
# All of these following numbers are 1 less than their documentation, because they are indices in the data array
pairs = [[4, 5], [3, 6], [2, 7], [1, 8], [0, 9], # Eyebrows
[19, 28], [20, 27], [21, 26], [22, 25], [23, 30], [24, 29], # Eyes
[14, 18], [24, 29], # Nose
[31, 37], [32, 36], [33, 35], [42, 38], [41, 39], [41, 39], [43, 45], [46, 48]] # Lips
middle = [10, 11, 12, 13, 16, 34, 40, 44, 47]
result_val = 0
for i in range(len(training_data)):
frame = i
for pair in pairs:
mid_feature = 12
p1 = training_data[frame][pair[0]]
# print p1
#p1 = mapPoint(headpose_data[frame], p1)
p2 = training_data[frame][pair[1]]
#p2 = mapPoint(headpose_data[frame], p2)
mid_point = training_data[frame][mid_feature]
#mid_point = mapPoint(headpose_data[frame], mid_point)
dis1 = distance(p1, mid_point)
dis2 = distance(p2, mid_point)
result_val += abs(dis1 - dis2)
return result_val
# Map 2D points into 3D space with 3D vector
def mapPoint(vector, point):
mappedPoint = []
for v in vector:
tot = 0
for c in point:
tot += v * c
mappedPoint.append(tot)
return mappedPoint
# training_data = createData('../eye_move_lr.MP4_intraface_data.xml')
training_collection = []
headpose_collection = []
training1, headpose1 = createData('data/scare_fake.MP4_intraface_data.xml')
training2, headpose2 = createData('data/scare_fake[2].MP4_intraface_data.xml')
training3, headpose3 = createData('data/scare_fake[3].MP4_intraface_data.xml')
training21, headpose21 = createData('data/scare_real.MP4_intraface_data.xml')
training22, headpose22 = createData('data/scare_real[2].MP4_intraface_data.xml')
training23, headpose23 = createData('data/scare_real[3].MP4_intraface_data.xml')
training_collection.extend((training1, training2, training3, training21, training22))
headpose_collection.extend((headpose1, headpose2, headpose3, headpose21, headpose22))
cumulative_vals = []
for v in range(len(training_collection)):
cumulative_vals.append([calculate_asymmetry(0, training_collection[v], headpose_collection[v])])
print cumulative_vals
estimator = KMeans(n_clusters=2)
estimator.fit(cumulative_vals)
labels = estimator.labels_
print labels
testing1, testing_headpose1 = createData('data/scare_fake[4].MP4_intraface_data.xml')
testing21, testing_headpose21 = createData('data/scare_real[4].MP4_intraface_data.xml')
# test_data = createData('../social.MP4_intraface_data.xml')
# # test_data = createData('../face_move_left_right.MP4_intraface_data.xml')
# estimator = KMeans(n_clusters=3)
# estimator.fit(training_data)
# labels = estimator.labels_
# print labels
# ans = estimator.predict(test_data)
# print ans
# for i in range(len(ans)):
# print ans[i],
### Forward = 2. Left (to camera) = 0. Right = 1
# for i in range(len(labels)):
# print labels[i], ":", training_data[i]