face_recognition+python-opencv实现摄像头实时人脸识别

参考:

https://www.jianshu.com/p/9d5f0020acd5

完整代码

csharp 复制代码
# -*- coding: utf-8 -*-

import face_recognition

import cv2

import os

camera = cv2.VideoCapture(0)

font = cv2.FONT_HERSHEY_DUPLEX

face_names = []

face_codings = []

person_list = os.listdir("faces/")

for i in range(len(person_list)):

    person_name = os.listdir("faces/" + "person_" + str(i + 1))

    # print(person_name[0])

    face_img = face_recognition.load_image_file("faces/" + "person_" + str(i + 1) + "/" + person_name[0])

    face_codings.append(face_recognition.face_encodings(face_img)[0])

    face_names.append(person_name[0][:person_name[0].index(".")])

while True:

    success,img=camera.read()

    img_new = cv2.resize(img, (0, 0), fx=0.25, fy=0.25)

    process_this_frame = True

    if process_this_frame:

        marks = face_recognition.face_locations(img_new)

        codings = face_recognition.face_encodings(img_new, marks)

        for coding in codings:

            result = face_recognition.compare_faces(face_codings, coding,0.4)

            print(result)

            for i in range(len(result)):

                if result[i]:

                    name = face_names[i]

                    break

                if i == len(result)-1:

                    name = "unknown"

                #break

        process_this_frame = not process_this_frame

        for (top, right, bottom, left)in (marks):

            top *= 4

            right *= 4

            bottom *= 4

            left *= 4

            cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 0), 2)

            cv2.putText(img, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

        cv2.imshow('face', img)

        if cv2.waitKey(1) & 0xFF == ord('q'):

            break

camera.release()

cv2.destroyAllWindows()
相关推荐
xchenhao1 小时前
SciKit-Learn 全面分析分类任务 breast_cancer 数据集
python·机器学习·分类·数据集·scikit-learn·svm
独行soc4 小时前
2025年渗透测试面试题总结-66(题目+回答)
java·网络·python·安全·web安全·adb·渗透测试
多看书少吃饭7 小时前
基于 OpenCV 的眼球识别算法以及青光眼算法识别
人工智能·opencv·计算机视觉
Y学院7 小时前
Python 数据分析:从新手到高手的“摸鱼”指南
python·数据分析
深耕AI7 小时前
【PyTorch训练】准确率计算(代码片段拆解)
人工智能·pytorch·python
eqwaak07 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算
Blossom.1188 小时前
从“能写”到“能干活”:大模型工具调用(Function-Calling)的工程化落地指南
数据库·人工智能·python·深度学习·机器学习·计算机视觉·oracle
蒋星熠8 小时前
破壁者指南:内网穿透技术的深度解构与实战方法
网络·数据库·redis·python·websocket·网络协议·udp
shizidushu8 小时前
使用 Pyinstaller 打包 PPOCRLabel
python·pyinstaller