face_recognition 实时人脸识别

简介: 目标识别进入摄像头的人是谁face_recognitionface_recognition 是github上一个非常有名气的人脸识别开源工具包,我们可以通过以下指令安装到python环境内$ pip install face_...

目标

  • 识别进入摄像头的人是谁

face_recognition

face_recognition 是github上一个非常有名气的人脸识别开源工具包,我们可以通过以下指令安装到python环境内

$ pip install face_recognition

代码的设计思路

加载认识的人脸图

ray_image = face_recognition.load_image_file("ray.jpg")
ray_face_encoding = face_recognition.face_encodings(ray_image)[0]

将认识的人脸变量加到数组内

known_face_encodings = [ ray_face_encoding ]
known_face_names = [ "Ray" ]

全部代码如下所示:

import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)

ray_image = face_recognition.load_image_file("ray.jpg")
ray_face_encoding = face_recognition.face_encodings(ray_image)[0]

pinky_image = face_recognition.load_image_file("pinky.jpg")
pinky_face_encoding = face_recognition.face_encodings(ray_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    ray_face_encoding,
    pinky_face_encoding
]
known_face_names = [
    "Ray",
    "Pinky"
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # Grab a single frame of video
    ret, frame = video_capture.read()

    # Resize frame of video to 1/4 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Only process every other frame of video to save time
    if process_this_frame:
        # Find all the faces and face encodings in the current frame of video
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # See if the face is a match for the known face(s)
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # If a match was found in known_face_encodings, just use the first one.
            if True in matches:
                first_match_index = matches.index(True)
                name = known_face_names[first_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/4 size
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
相关文章
|
机器人 计算机视觉 Python
智能机器人项目,安装人脸识别face_recognition报错解决
智能机器人项目,安装人脸识别face_recognition报错解决
147 0
|
机器学习/深度学习 计算机视觉 Python
基于face_recognition实现人脸识别
## 简介 我们这次使用基于开源项目face_recognition库来实现人脸识别,首先介绍一下这个项目吧。 使用世界上最简单的人脸识别库从 Python 或命令行识别和操作人脸。 使用dlib使用深度学习构建的最先进的人脸识别技术构建。该模型在 Wild基准的 Labeled Faces 上的准确率为 99.38% 。 这使得我们可以直接调用这个库来进行人脸识别而不用自己编写程序进行深度学习。这是该项目的[github地址](https://github.com/ageitgey/face_recognition)
311 1
|
机器学习/深度学习 人工智能 计算机视觉
Python实现人脸识别功能,face_recognition的使用 | 机器学习
Python实现人脸识别功能,face_recognition的使用 | 机器学习
Python实现人脸识别功能,face_recognition的使用 | 机器学习
|
机器学习/深度学习 人工智能 Linux
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
AI识别照片是谁,人脸识别face_recognition开源项目安装使用 | 机器学习
|
机器学习/深度学习 计算机视觉
|
机器学习/深度学习 计算机视觉 Python
[雪峰磁针石博客]人脸识别工具:face_recognition
简介 face_recognition使用世界上最简单的人脸识别工具,在Python或命令行中识别和操作人脸。 使用dlib最先进的人脸识别技术构建而成,并具有深度学习功能。 该模型在Labeled Faces in the Wild基准中的准确率为99.38%。
|
4月前
|
弹性计算 Java PHP
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
对于初次购买阿里云产品的用户来说,第一步要做的是注册账号并完成实名认证,然后才是购买阿里云服务器或者其他云产品,本文为大家以图文形式展示一下新手用户从注册阿里云账号、实名认证到购买云服务器完整详细教程,以供参考。
新手用户注册阿里云账号、实名认证、购买云服务器图文教程参考
|
3月前
|
文字识别 算法 API
视觉智能开放平台产品使用合集之uniapp框架如何使用阿里云金融级人脸识别
视觉智能开放平台是指提供一系列基于视觉识别技术的API和服务的平台,这些服务通常包括图像识别、人脸识别、物体检测、文字识别、场景理解等。企业或开发者可以通过调用这些API,快速将视觉智能功能集成到自己的应用或服务中,而无需从零开始研发相关算法和技术。以下是一些常见的视觉智能开放平台产品及其应用场景的概览。
|
机器学习/深度学习 搜索推荐 计算机视觉
【阿里云OpenVI-人脸感知理解系列之人脸识别】基于Transformer的人脸识别新框架TransFace ICCV-2023论文深入解读
本文介绍 阿里云开放视觉智能团队 被计算机视觉顶级国际会议ICCV 2023接收的论文 "TransFace: Calibrating Transformer Training for Face Recognition from a Data-Centric Perspective"。TransFace旨在探索ViT在人脸识别任务上表现不佳的原因,并从data-centric的角度去提升ViT在人脸识别任务上的性能。
2037 341