Search code examples
python-3.xnumpyopencvface-detection

How to extract face portion from rotated images?


I want to extract face image/portion from different angled images.

I have images like following:

enter image description here

I have tried with this following code.

import cv2
import sys
import numpy as np
import cv2
import matplotlib.pyplot as plt
%matplotlib inline

# imagePath = sys.argv[1]

image = cv2.imread('_img_1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.3,
    minNeighbors=3,
    minSize=(30, 30)
)

print("[INFO] Found {0} Faces.".format(len(faces)))

for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
    roi_color = image[y:y + h, x:x + w]
    print("[INFO] Object found. Saving locally.")
    cv2.imwrite(str(w) + str(h) + '_faces.jpg', roi_color)
    plt.imshow(roi_color)
    plt.show()

status = cv2.imwrite('faces_detected.jpg', image)
print("[INFO] Image faces_detected.jpg written to filesystem: ", status)

How can extract face image/portion from rotated images?


Solution

  • Consider rotating the image as a first step!

    You could potentially do this by guessing the orientation of the ID, or some deeper detection of the rectangle

    height, width, _ = image.shape
    if height > width:
        image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)