Cropping Faces from Images using OpenCV - Python
Last Updated : 03 Jan, 2023
Opencv is a python library mainly used for image processing and computer vision. In this article first, we detect faces after that we crop the face from the image. Face detection is the branch of image processing that uses to detect faces.
We will use a pre-trained Haar Cascade model to detect faces from the image. A haar cascade is the object detection method used to detect objects from the image. This algorithm was trained by numerous images. You can download the face haar cascade file by click here.
Let's Implementation in stepwise:
Step 1: In this step, we will read the image and converted it into grayscale.
Python3 import cv2 # Read the input image img = cv2.imread('mpw.jpeg') # Convert into grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Explanation: In this code first we import our opencv library using import cv2. cv2.imread() method loads the image from the given path(ex:mpw.jpeg or filepath/mpw.jpeg) After loading the image we convert the image to a grayscale image using COLOR_BGR2GRAY.
Step 2: Use the Haar Cascade model to detect faces from the image.
Python3 face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml') # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4)
Explanation: We use cv2.CascadeClassifier for load haarcascade file in face_cascade. detectMultiScale() function used for detect faces.It takes 3 parameters:
- Gray: input image(gray scale image)
- 1.1: scale factor, it specifies how much the image size is reduced with each scale. It improves detection.
- 4: MinNeighbours, specifies how many neighbors each candidate rectangle should have to retain.
Step 3: Find the face in the image.
Python3 for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2) faces = img[y:y + h, x:x + w] cv2.imshow("face",faces) cv2.imwrite('face.jpg', faces) cv2.imshow('img', img) cv2.waitKey()
Explanation: x,y are pixel location of faces, w,h are width and height of faces. cv2.rectangle() function used for draw rectangle over the detected object, img is input image, (x,y),(x+w, y+h) are locations of rectangle,(0,0,255) is color of a rectangle this argument gets passed as a tuple for BGR,we would use (0,0,255) for red, 2 is thickness of rectangle.
Below is the full implementation:
Image Used -
input image Python3 import cv2 # Read the input image img = cv2.imread('mpw.jpeg') # Convert into grayscale gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Load the cascade face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml') # Detect faces faces = face_cascade.detectMultiScale(gray, 1.1, 4) # Draw rectangle around the faces and crop the faces for (x, y, w, h) in faces: cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2) faces = img[y:y + h, x:x + w] cv2.imshow("face",faces) cv2.imwrite('face.jpg', faces) # Display the output cv2.imwrite('detcted.jpg', img) cv2.imshow('img', img) cv2.waitKey()
Output:
Detected face
cropped image Similar Reads
How to Blur Faces in Images using OpenCV in Python? Prerequisite: OpenCV OpenCV is a huge open-source library for computer vision, machine learning, and image processing. Â It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optim
2 min read
Count number of Faces using Python - OpenCV Prerequisites: Face detection using dlib and openCV In this article, we will use image processing to detect and count the number of faces. We are not supposed to get all the features of the face. Instead, the objective is to obtain the bounding box through some methods i.e. coordinates of the face i
3 min read
Mapping coordinates from 3D to 2D using OpenCV - Python Mapping 3D coordinates to 2D coordinates is a common task in computer vision. This process involves transforming 3D points in a virtual space to their corresponding positions on a 2D image plane. In this article, we'll show you how to perform this task using OpenCV in Python programming. OpenCV is a
6 min read
Creating Hybrid Images Using OpenCV Library | Python Hybrid image, or "multi-scale image", is an exciting concept in computer vision. They are created by blending one image's high-frequency components with another's low-frequency components. The result is an image that appears as one when viewed from a distance but shows a different image when viewed
4 min read
Adding borders to the images using Python - OpenCV Image processing is an interesting field in today's era of Artificial Intelligence and Machine Learning. We can see the applications of image processing in our day-to-day life, like whenever we apply filter over any image (selfie) or when we want to apply some effect like blurring the image, etc. I
1 min read