Path: blob/master/FacialAttractiveness/source/generateFeatures.py
3143 views
import math1import numpy2import itertools34def facialRatio(points):5x1 = points[0];6y1 = points[1];7x2 = points[2];8y2 = points[3];9x3 = points[4];10y3 = points[5];11x4 = points[6];12y4 = points[7];1314dist1 = math.sqrt((x1-x2)**2 + (y1-y2)**2)15dist2 = math.sqrt((x3-x4)**2 + (y3-y4)**2)1617ratio = dist1/dist21819return ratio202122def generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, allLandmarkCoordinates):23size = allLandmarkCoordinates.shape24allFeatures = numpy.zeros((size[0], len(pointIndices1)))25for x in range(0, size[0]):26landmarkCoordinates = allLandmarkCoordinates[x, :]27ratios = [];28for i in range(0, len(pointIndices1)):29x1 = landmarkCoordinates[2*(pointIndices1[i]-1)]30y1 = landmarkCoordinates[2*pointIndices1[i] - 1]31x2 = landmarkCoordinates[2*(pointIndices2[i]-1)]32y2 = landmarkCoordinates[2*pointIndices2[i] - 1]3334x3 = landmarkCoordinates[2*(pointIndices3[i]-1)]35y3 = landmarkCoordinates[2*pointIndices3[i] - 1]36x4 = landmarkCoordinates[2*(pointIndices4[i]-1)]37y4 = landmarkCoordinates[2*pointIndices4[i] - 1]3839points = [x1, y1, x2, y2, x3, y3, x4, y4]40ratios.append(facialRatio(points))41allFeatures[x, :] = numpy.asarray(ratios)42return allFeatures434445def generateAllFeatures(allLandmarkCoordinates):46a = [18, 22, 23, 27, 37, 40, 43, 46, 28, 32, 34, 36, 5, 9, 13, 49, 55, 52, 58]47combinations = itertools.combinations(a, 4)48i = 049pointIndices1 = [];50pointIndices2 = [];51pointIndices3 = [];52pointIndices4 = [];5354for combination in combinations:55pointIndices1.append(combination[0])56pointIndices2.append(combination[1])57pointIndices3.append(combination[2])58pointIndices4.append(combination[3])59i = i+160pointIndices1.append(combination[0])61pointIndices2.append(combination[2])62pointIndices3.append(combination[1])63pointIndices4.append(combination[3])64i = i+165pointIndices1.append(combination[0])66pointIndices2.append(combination[3])67pointIndices3.append(combination[1])68pointIndices4.append(combination[2])69i = i+17071return generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, allLandmarkCoordinates)7273landmarks = numpy.loadtxt('landmarks.txt', delimiter=',', usecols=range(136))7475featuresALL = generateAllFeatures(landmarks)76numpy.savetxt('features_ALL.txt', featuresALL, delimiter=',', fmt = '%.04f')7778#pointIndices1 = [20, 20, 45, 45]79#pointIndices2 = [58, 9, 58, 58]80#pointIndices3 = [5, 7, 5, 32]81#pointIndices4 = [13, 13, 11, 36]82#features = generateFeatures(pointIndices1, pointIndices2, pointIndices3, pointIndices4, landmarks)83848586