[파이썬] 얼굴 인식 및 감지

Python provides various libraries and frameworks for face recognition and detection. In this blog post, we will explore some popular options and demonstrate how to use them in Python.

OpenCV

OpenCV (Open Source Computer Vision) is a widely-used library for computer vision tasks. It provides pre-trained models and functions for face recognition and detection. Let’s see an example of face detection using OpenCV:

import cv2

# Load the pre-trained face detection model
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")

# Load the image for face detection
image = cv2.imread("image.jpg")

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Perform face detection
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))

# Draw rectangles around the detected faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Display the image with detected faces
cv2.imshow("Faces", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In the above example, we load the pre-trained face detection model from OpenCV, and then perform face detection on an image. Detected faces are marked with rectangles, and the modified image is displayed.

Dlib

Dlib is a popular C++ library with Python bindings for machine learning and computer vision tasks. It provides a facial recognition model called “dlib_face_recognition_resnet_model_v1”. Here’s an example of face recognition using Dlib:

import dlib
import cv2

# Load the pre-trained face recognition model
cnn_face_detector = dlib.cnn_face_detection_model_v1("mmod_human_face_detector.dat")
shape_predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
face_recognition_model = dlib.face_recognition_model_v1("dlib_face_recognition_resnet_model_v1.dat")

# Load the image for face recognition
image = cv2.imread("image.jpg")

# Convert the image to RGB
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Detect faces in the image
faces = cnn_face_detector(rgb, 1)

# Iterate over the detected faces
for face in faces:
    shape = shape_predictor(image, face.rect)
    face_descriptor = face_recognition_model.compute_face_descriptor(image, shape)
    # Perform face recognition tasks here

# Display the image with detected faces
cv2.imshow("Faces", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

In this example, we load the pre-trained face detection and recognition models from Dlib. We then use the face detection model to detect faces in an image and obtain their facial landmarks. Finally, we can perform face recognition tasks using the obtained facial descriptors.

Conclusion

In this blog post, we explored how to perform face recognition and detection in Python using the OpenCV and Dlib libraries. These libraries offer powerful tools for face-related tasks and can be used in various applications such as facial authentication, emotion detection, and more. Feel free to experiment with different parameters and functionalities to enhance your face recognition and detection capabilities in Python.