MCPcopy Create free account
hub / github.com/PyImageSearch/imutils / align

Method align

imutils/face_utils/facealigner.py:23–82  ·  view source on GitHub ↗
(self, image, gray, rect)

Source from the content-addressed store, hash-verified

21 self.desiredFaceHeight = self.desiredFaceWidth
22
23 def align(self, image, gray, rect):
24 # convert the landmark (x, y)-coordinates to a NumPy array
25 shape = self.predictor(gray, rect)
26 shape = shape_to_np(shape)
27
28 #simple hack ;)
29 if (len(shape)==68):
30 # extract the left and right eye (x, y)-coordinates
31 (lStart, lEnd) = FACIAL_LANDMARKS_68_IDXS["left_eye"]
32 (rStart, rEnd) = FACIAL_LANDMARKS_68_IDXS["right_eye"]
33 else:
34 (lStart, lEnd) = FACIAL_LANDMARKS_5_IDXS["left_eye"]
35 (rStart, rEnd) = FACIAL_LANDMARKS_5_IDXS["right_eye"]
36
37 leftEyePts = shape[lStart:lEnd]
38 rightEyePts = shape[rStart:rEnd]
39
40 # compute the center of mass for each eye
41 leftEyeCenter = leftEyePts.mean(axis=0).astype("int")
42 rightEyeCenter = rightEyePts.mean(axis=0).astype("int")
43
44 # compute the angle between the eye centroids
45 dY = rightEyeCenter[1] - leftEyeCenter[1]
46 dX = rightEyeCenter[0] - leftEyeCenter[0]
47 angle = np.degrees(np.arctan2(dY, dX)) - 180
48
49 # compute the desired right eye x-coordinate based on the
50 # desired x-coordinate of the left eye
51 desiredRightEyeX = 1.0 - self.desiredLeftEye[0]
52
53 # determine the scale of the new resulting image by taking
54 # the ratio of the distance between eyes in the *current*
55 # image to the ratio of distance between eyes in the
56 # *desired* image
57 dist = np.sqrt((dX ** 2) + (dY ** 2))
58 desiredDist = (desiredRightEyeX - self.desiredLeftEye[0])
59 desiredDist *= self.desiredFaceWidth
60 scale = desiredDist / dist
61
62 # compute center (x, y)-coordinates (i.e., the median point)
63 # between the two eyes in the input image
64 eyesCenter = ((leftEyeCenter[0] + rightEyeCenter[0]) // 2,
65 (leftEyeCenter[1] + rightEyeCenter[1]) // 2)
66
67 # grab the rotation matrix for rotating and scaling the face
68 M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)
69
70 # update the translation component of the matrix
71 tX = self.desiredFaceWidth * 0.5
72 tY = self.desiredFaceHeight * self.desiredLeftEye[1]
73 M[0, 2] += (tX - eyesCenter[0])
74 M[1, 2] += (tY - eyesCenter[1])
75
76 # apply the affine transformation
77 (w, h) = (self.desiredFaceWidth, self.desiredFaceHeight)
78 output = cv2.warpAffine(image, M, (w, h),
79 flags=cv2.INTER_CUBIC)
80

Callers

nothing calls this directly

Calls 1

shape_to_npFunction · 0.85

Tested by

no test coverage detected