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()
相关推荐
CCPC不拿奖不改名1 小时前
两种完整的 Git 分支协作流程
大数据·人工智能·git·python·elasticsearch·搜索引擎·自然语言处理
a努力。1 小时前
字节Java面试被问:TCP的BBR拥塞控制算法原理
java·开发语言·python·tcp/ip·elasticsearch·面试·职场和发展
费弗里1 小时前
一个小技巧轻松提升Dash应用debug效率
python·dash
小小测试开发1 小时前
Python浮点型常用方法全解析:从基础到实战
python
大模型实验室Lab4AI1 小时前
西北工业大学 StereoMV2D 突破 3D 物体检测深度难题,精度与效率兼得
人工智能·计算机视觉·目标跟踪
ValhallaCoder1 小时前
Day53-图论
数据结构·python·算法·图论
爱打代码的小林2 小时前
opencv基础(轮廓检测、绘制与特征)
人工智能·opencv·计算机视觉
lpfasd1232 小时前
PyGithub用法详解
git·python·github
给我来一根2 小时前
用户认证与授权:使用JWT保护你的API
jvm·数据库·python
Lun3866buzha3 小时前
【数学表达式识别】基于计算机视觉技术的数学符号与数字识别系统实现_1
人工智能·计算机视觉