How to Crop and Save Detected Faces in OpenCV Python?
OpenCV is a powerful open-source computer vision library that can be used to build advanced, real-time applications. One of its most popular features is face detection, which is used in many applications including security systems, social media platforms, and even mobile games. In this article, we will learn how to use OpenCV to detect faces in an image, crop the detected faces, and save them as individual image files.
Requirements
Before we begin, we need to make sure that OpenCV is installed on our computer. You can install it using pip by running the following command:
pip install opencv-python
Once OpenCV is installed, we can start coding.
Detecting Faces
The first step is to load an image and detect the faces in it. We can do this using the cv2.CascadeClassifier
class and the detectMultiScale
method. Here is an example:
import cv2
# Load image
img = cv2.imread('image.jpg')
# Load classifier
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Detect faces
faces = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5)
In this code, we load an image called image.jpg
and create a CascadeClassifier
object using the haarcascade_frontalface_default.xml
file, which is a pre-trained classifier that can detect faces in an image. We use the detectMultiScale
method to detect faces in the image, which returns a list of rectangles that represent the locations of the detected faces.
Cropping Faces
Once we have detected the faces, we can crop them and save them as individual files. To do this, we need to loop through the list of detected faces and use the cv2.rectangle
and cv2.imwrite
methods to crop and save each face. Here is an example:
# Crop and save faces
for (x, y, w, h) in faces:
# Draw rectangle around face
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Crop face
crop_img = img[y:y+h, x:x+w]
# Save face
cv2.imwrite('face_' + str(x) + '.jpg', crop_img)
In this code, we loop through the list of detected faces and use the cv2.rectangle
method to draw a green rectangle around each face. We then use the cv2.imwrite
method to save each face as an individual file. The file name is created using the x-coordinate of the face, which ensures that each file has a unique name.
Putting It All Together
Here is the complete code that loads an image, detects faces, crops them, and saves them as individual files:
import cv2
# Load image
img = cv2.imread('image.jpg')
# Load classifier
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Detect faces
faces = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5)
# Crop and save faces
for (x, y, w, h) in faces:
# Draw rectangle around face
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Crop face
crop_img = img[y:y+h, x:x+w]
# Save face
cv2.imwrite('face_' + str(x) + '.jpg', crop_img)
Conclusion
In this article, we learned how to use OpenCV to detect faces in an image, crop the detected faces, and save them as individual files. This technique can be used in many applications, such as building a photo tagging system that automatically identifies and categorizes faces in a collection of images. With OpenCV and Python, the possibilities are endless. Happy coding!