python-opencv 人脸识别

python-opencv 人脸识别

代码还使用到了dlib 和face_recognition这两个库,需要安装一下,看一下代码:

import face_recognition
import cv2

# 创建视频捕捉对象
video_capture = cv2.VideoCapture(0)
print(video_capture.isOpened())

# video_capture.set(3, 1280)
# video_capture.set(4, 480)

while video_capture.isOpened():
    # ret 表示读取是否成功
    # frame 具体的图像数据
    ret, frame = video_capture.read()

    # 尺寸缩放为原来的 1/4
    # 作用:为了加速人脸检测过程
    small_frame = cv2.resize(frame, None, fx=0.25, fy=0.25)

    # bgr -> rgb
    rgb_frame = small_frame[:, :, ::-1]

    # 拿到人脸坐标
    face_loc = face_recognition.face_locations(rgb_frame)

    for (top, right, bottlm, left), in zip(face_loc):
        top *= 4
        right *= 4
        bottlm *= 4
        left *= 4

        cv2.rectangle(
            frame,
            (left, top),
            (right, bottlm),
            (255, 0, 0),
            2
        )

    cv2.imshow("face detection", frame)

    # 退出条件
    if cv2.waitKey(1) & 0xFF == 27:
        break

video_capture.release()
cv2.destroyAllWindows()

你可能感兴趣的:(人工智能,python,opencv,python,计算机视觉)