Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/FacialAttractiveness/source/generateFeatures.py
3143 views
1
import math
2
import numpy
3
import itertools
4
5
def facialRatio(points):
6
x1 = points[0];
7
y1 = points[1];
8
x2 = points[2];
9
y2 = points[3];
10
x3 = points[4];
11
y3 = points[5];
12
x4 = points[6];
13
y4 = points[7];
14
15
dist1 = math.sqrt((x1-x2)**2 + (y1-y2)**2)
16
dist2 = math.sqrt((x3-x4)**2 + (y3-y4)**2)
17
18
ratio = dist1/dist2
19
20
return ratio
21
22
23
def generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, allLandmarkCoordinates):
24
size = allLandmarkCoordinates.shape
25
allFeatures = numpy.zeros((size[0], len(pointIndices1)))
26
for x in range(0, size[0]):
27
landmarkCoordinates = allLandmarkCoordinates[x, :]
28
ratios = [];
29
for i in range(0, len(pointIndices1)):
30
x1 = landmarkCoordinates[2*(pointIndices1[i]-1)]
31
y1 = landmarkCoordinates[2*pointIndices1[i] - 1]
32
x2 = landmarkCoordinates[2*(pointIndices2[i]-1)]
33
y2 = landmarkCoordinates[2*pointIndices2[i] - 1]
34
35
x3 = landmarkCoordinates[2*(pointIndices3[i]-1)]
36
y3 = landmarkCoordinates[2*pointIndices3[i] - 1]
37
x4 = landmarkCoordinates[2*(pointIndices4[i]-1)]
38
y4 = landmarkCoordinates[2*pointIndices4[i] - 1]
39
40
points = [x1, y1, x2, y2, x3, y3, x4, y4]
41
ratios.append(facialRatio(points))
42
allFeatures[x, :] = numpy.asarray(ratios)
43
return allFeatures
44
45
46
def generateAllFeatures(allLandmarkCoordinates):
47
a = [18, 22, 23, 27, 37, 40, 43, 46, 28, 32, 34, 36, 5, 9, 13, 49, 55, 52, 58]
48
combinations = itertools.combinations(a, 4)
49
i = 0
50
pointIndices1 = [];
51
pointIndices2 = [];
52
pointIndices3 = [];
53
pointIndices4 = [];
54
55
for combination in combinations:
56
pointIndices1.append(combination[0])
57
pointIndices2.append(combination[1])
58
pointIndices3.append(combination[2])
59
pointIndices4.append(combination[3])
60
i = i+1
61
pointIndices1.append(combination[0])
62
pointIndices2.append(combination[2])
63
pointIndices3.append(combination[1])
64
pointIndices4.append(combination[3])
65
i = i+1
66
pointIndices1.append(combination[0])
67
pointIndices2.append(combination[3])
68
pointIndices3.append(combination[1])
69
pointIndices4.append(combination[2])
70
i = i+1
71
72
return generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, allLandmarkCoordinates)
73
74
landmarks = numpy.loadtxt('landmarks.txt', delimiter=',', usecols=range(136))
75
76
featuresALL = generateAllFeatures(landmarks)
77
numpy.savetxt('features_ALL.txt', featuresALL, delimiter=',', fmt = '%.04f')
78
79
#pointIndices1 = [20, 20, 45, 45]
80
#pointIndices2 = [58, 9, 58, 58]
81
#pointIndices3 = [5, 7, 5, 32]
82
#pointIndices4 = [13, 13, 11, 36]
83
#features = generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, landmarks)
84
85
86