README.md 1.0 KB

dlib

Open in GitHub Codespaces

面部识别

Usage

默认CPU

pip install dlib

import dlib
dlib.DLIB_USE_CUDA   # True 表示可以使用 GPU

模型下载: http://dlib.net/files/mmod_human_face_detector.dat.bz2

import dlib
import cv2
import numpy as np

# 加载 dlib 的人脸检测器
detector = dlib.get_frontal_face_detector()

# 加载输入图像
image = cv2.imread('input_image.jpg')

# 将图像转换为灰度图像
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# 使用人脸检测器检测图像中的人脸
faces = detector(gray)

# 遍历检测到的人脸
for face in faces:
    x, y, w, h = face.left(), face.top(), face.width(), face.height()
    
    # 在图像中标记人脸位置
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

# 显示标记后的图像
cv2.imshow('Detected Faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

License

Reference

davisking/dlib