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()
相关推荐
Jason-河山1 小时前
利用Java爬虫获取商品评论:技术与实践
java·爬虫·python
胖哥真不错1 小时前
Python实现随机分布式延迟PSO优化算法(RODDPSO)优化CNN回归模型项目实战
python·项目实战·cnn回归模型·roddpso·随机分布式延迟pso优化算法
Easy数模1 小时前
竞赛思享会 | 2024年第十届数维杯国际数学建模挑战赛D题【代码+演示】
python·算法·数学建模
知识的宝藏1 小时前
Django如何配置多个环境的MySQL数据库
python·django
Eric.Lee20212 小时前
数据集-目标检测系列- 人与猫互动 猫 检测数据集 cat in the house >> DataBall
人工智能·yolo·目标检测·计算机视觉·猫咪检测·猫与人互动
往日情怀酿做酒 V17639296382 小时前
Django基础配置
后端·python·django
Mr.鱼2 小时前
opencv undefined reference to `cv::noarray()‘ 。window系统配置opencv,找到opencv库,但连接不了
人工智能·opencv·计算机视觉
凌虚(失业了求个工作)2 小时前
RAG 示例:使用 langchain、Redis、llama.cpp 构建一个 kubernetes 知识库问答
人工智能·redis·python·langchain·llama
0zxm2 小时前
01.Django快速入门
数据库·vscode·python·django·sqlite
数据小爬虫@3 小时前
利用Python爬虫获取淘宝商品评论:实战案例分析
开发语言·爬虫·python