0

So, I have a got a folder with 132 subfolders inside it with name of the person as folder name. Each subfolders has 5 face images. I want to loop through all the subfolders and align and crop the image for face recognition and store all the subfolders on a new folder named 'aligned_face'. I have found a code for face cropping and alignment. My question is, how do I use this code to loop through my 132 subfolders and store all the aligned and cropped faces in the folder previously mentioned called 'aligned_face'?

import face_recognition
import cv2
import numpy as np

# load image and find face locations.
image = face_recognition.load_image_file("sample.jpg")
face_locations = face_recognition.face_locations(image, model="hog")

# detect 68-landmarks from image. This includes left eye, right eye, lips, eye brows, nose and chins
face_landmarks = face_recognition.face_landmarks(image)

'''
Let's find and angle of the face. First calculate 
the center of left and right eye by using eye landmarks.
'''
leftEyePts = face_landmarks[0]['left_eye']
rightEyePts = face_landmarks[0]['right_eye']

leftEyeCenter = np.array(leftEyePts).mean(axis=0).astype("int")
rightEyeCenter = np.array(rightEyePts).mean(axis=0).astype("int")

leftEyeCenter = (leftEyeCenter[0],leftEyeCenter[1])
rightEyeCenter = (rightEyeCenter[0],rightEyeCenter[1])

# draw the circle at centers and line connecting to them
cv2.circle(image, leftEyeCenter, 2, (255, 0, 0), 10)
cv2.circle(image, rightEyeCenter, 2, (255, 0, 0), 10)
cv2.line(image, leftEyeCenter, rightEyeCenter, (255,0,0), 10)

# find and angle of line by using slop of the line.
dY = rightEyeCenter[1] - leftEyeCenter[1]
dX = rightEyeCenter[0] - leftEyeCenter[0]
angle = np.degrees(np.arctan2(dY, dX))

# to get the face at the center of the image,
# set desired left eye location. Right eye location 
# will be found out by using left eye location.
# this location is in percentage.
desiredLeftEye=(0.35, 0.35)
#Set the croped image(face) size after rotaion.
desiredFaceWidth = 128
desiredFaceHeight = 128

desiredRightEyeX = 1.0 - desiredLeftEye[0]

# determine the scale of the new resulting image by taking
# the ratio of the distance between eyes in the *current*
# image to the ratio of distance between eyes in the
# *desired* image
dist = np.sqrt((dX ** 2) + (dY ** 2))
desiredDist = (desiredRightEyeX - desiredLeftEye[0])
desiredDist *= desiredFaceWidth
scale = desiredDist / dist

# compute center (x, y)-coordinates (i.e., the median point)
# between the two eyes in the input image
eyesCenter = ((leftEyeCenter[0] + rightEyeCenter[0]) // 2,
    (leftEyeCenter[1] + rightEyeCenter[1]) // 2)

# grab the rotation matrix for rotating and scaling the face
M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)

# update the translation component of the matrix
tX = desiredFaceWidth * 0.5
tY = desiredFaceHeight * desiredLeftEye[1]
M[0, 2] += (tX - eyesCenter[0])
M[1, 2] += (tY - eyesCenter[1])

# apply the affine transformation
(w, h) = (desiredFaceWidth, desiredFaceHeight)
(y2,x2,y1,x1) = face_locations[0] 

output = cv2.warpAffine(image, M, (w, h),
    flags=cv2.INTER_CUBIC)

output = cv2.cvtColor(output, cv2.COLOR_BGR2RGB)

2 Answers2

0

Seems like the question is "How do I loop through files and write back"?

Several questions on how to iterate through files: How do I align and crop the images located in subdirectories for face recognition?, Recursive sub folder search and return files in a list python

To crop images you can use Pillow: https://pillow.readthedocs.io/en/stable/

To save images:

f = open('filepath/filename.png','wb') #wb = write byte. Path from the recursive search
f.write(image) #From opencv+numpy->pillow
f.close()
Punnerud
  • 7,195
  • 2
  • 54
  • 44
0

Firstly, walk all directories and sub directories. The following code block will store the exact paths of images in sub-directories.

employees = []
for r, d, f in os.walk(db_path): # r=root, d=directories, f = files
    for file in f:
        if ('.jpg' in file):
            exact_path = r + "/" + file
            employees.append(exact_path)

employees list stores exact image paths. We need to detect and align faces. Herein, deepface offers detection and alignment in a single function.

#!pip install deepface
from deepface import DeepFace
import cv2

index = 0
for employee in employees:
   aligned_face = DeepFace.detectFace(employee)
   cv2.imwrite('aligned_face/%d.jpg' % (index), aligned_face)
   index = index + 1

This will save detected and aligned faces in aligned_face folder.

Deepface also offers a face recognition module as well but you asked how to detect and align faces.

johncasey
  • 1,250
  • 8
  • 14
  • Besides, you can pass different methods such as opencv, ssd, dlib or mtcnn to detectFace function as an argument. DeepFace.detectFace(employee, detector_backend = 'mtcnn') – johncasey Sep 06 '20 at 05:27