def DetectEyes(Image):
Theta = 0
rows, cols = Image.shape
glass = glass_cas.detectMultiScale(Image) # This ditects the eyes
for (sx, sy, sw, sh) in glass:
if glass.shape[0] == 2: # The Image should have 2 eyes
if glass[1][0] > glass[0][0]:
DY = ((glass[1][1] + glass[1][3] / 2) - (glass[0][1] + glass[0][3] / 2)) # Height difference between the glass
DX = ((glass[1][0] + glass[1][2] / 2) - glass[0][0] + (glass[0][2] / 2)) # Width difference between the glass
else:
DY = (-(glass[1][1] + glass[1][3] / 2) + (glass[0][1] + glass[0][3] / 2)) # Height difference between the glass
DX = (-(glass[1][0] + glass[1][2] / 2) + glass[0][0] + (glass[0][2] / 2)) # Width difference between the glass
if (DX != 0.0) and (DY != 0.0): # Make sure the the change happens only if there is an angle
Theta = math.degrees(math.atan(round(float(DY) / float(DX), 2))) # Find the Angle
print "Theta " + str(Theta)
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), Theta, 1) # Find the Rotation Matrix
Image = cv2.warpAffine(Image, M, (cols, rows))
# cv2.imshow('ROTATED', Image) # UNCOMMENT IF YOU WANT TO SEE THE
Face2 = face.detectMultiScale(Image, 1.3, 5) # This detects a face in the image
for (FaceX, FaceY, FaceWidth, FaceHeight) in Face2:
CroppedFace = Image[FaceY: FaceY + FaceHeight, FaceX: FaceX + FaceWidth]
return CroppedFace
评论列表
文章目录