face_recognition实时人脸识别

具体安装移步:https://www.cnblogs.com/ckAng/p/10981025.html

更多操作移步:https://github.com/ageitgey/face_recognition

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import face_recognition
import cv2
import numpy as np

# This is a demo of running face recognition on live video from your webcam. It‘s a little more complicated than the
# other example, but it includes some basic performance tweaks to make things run a lot faster:
#   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
#   2. Only detect faces in every other frame of video.

# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It‘s only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don‘t require it instead.

# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)

# Load a sample picture and learn how to recognize it.
obama_image = face_recognition.load_image_file("img/kAng.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_image)[0]

# Load a second sample picture and learn how to recognize it.
biden_image = face_recognition.load_image_file("img/test10.jpg")
biden_face_encoding = face_recognition.face_encodings(biden_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    obama_face_encoding,
    biden_face_encoding
]
known_face_names = [
    "kAng",
    "obama"
]

# 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]

            # Or instead, use the known face with the smallest distance to the new face
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_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()

  

原文地址:https://www.cnblogs.com/ckAng/p/10984013.html

时间: 2024-10-09 03:25:09

face_recognition实时人脸识别的相关文章

Asp.net+WebSocket+Emgucv实时人脸识别

上个月在网上看到一个用web实现简单AR效果的文章,然后自己一路折腾,最后折腾出来一个 Asp.net+WebSocket+Emgucv实时人脸识别的东西,网上也有不少相关资料,有用winform的也有asp.net的.其实人脸识别技术早就成熟了,就是没机会接触这方面.百度了一下 找到好多,JqueryFaceDetection,face++,face core,opencv,emgucv等等,这些我都折腾了一遍,并不能很好的满足我的需求,我就是想像手机QQ里边的拍照的时候能识别到人脸并且对图像

c# 利用AForge和百度AI开发实时人脸识别

baiduAIFaceIdentify项目是C#语言,集成百度AI的SDK利用AForge开发的实时人脸识别的小demo,里边包含了人脸检测识别,人脸注册,人脸登录等功能 人脸实时检测识别功能 思路是利用AForge打开摄像头,通过摄像头获取到的图像显示在winform窗体中AForge的控件中,利用AForge控件中的NewFrame事件获取要显示的每一帧的图像,获取图像传输到百度AI平台进行人脸检测,并且将检测结果反馈到界面显示的图像中.在这个过程中有两个问题,获取图像上传到百度AI平台进行

AI人工智能之基于OpenCV+face_recognition实现人脸识别

因近期公司项目需求,需要从监控视频里识别出人脸信息.OpenCV非常庞大,其中官方提供的人脸模型分类器也可以满足基本的人脸识别,当然我们也可以训练自己的人脸模型数据,但是从精确度和专业程度上讲OpenCV所提供的人脸识别要弱于face_recognition,所以我们采取OpenCV处理视频流.face_recognition来识别人脸. 为什么选择Python? 博主本身是Java工程师,在公司主要通过Java语言进行开发,起初我们尝试过通过Java+OpenCV的方式来实现,但是效果并不是很

实时人脸识别

今天研究了几个开源项目,纪录一下. 目标:能够实时获取到  AVCaptureSession  中实时的获取的图像数据,转换为 UIImage : 对获取的 UIImage 进行脸部识别: 最后,达到实时脸部识别的效果. 找到如下例子: 1.一个屏幕中 获取多个 预览界面 效果的 例子. 来源:http://stackoverflow.com/questions/16543075/avcapturesession-with-multiple-previews 代码: https://github

人脸识别相关分享

人脸识别源代码 ※人脸检测(文章+程序)---技术文档及代码非常全『 人脸检测(文章+程序).rar (1.27 MB) 人脸检测(文章+程序).rar (1.27 MB) 下载次数: 12502 2010-12-21 12:26 』 ※完整的Matlab下人脸检测及识别系统源代码『 Face-Recognition-Detection.rar (393.19 KB) Face-Recognition-Detection.rar (393.19 KB) 下载次数: 11604 2010-12-2

TensorFlow人脸识别

TensorFlow框架做实时人脸识别小项目(一)https://blog.csdn.net/Goerge_L/article/details/80208297 TensorFlow框架做实时人脸识别小项目(二)https://blog.csdn.net/Goerge_L/article/details/80229307 TensorFlow框架做实时人脸识别小项目(三)https://blog.csdn.net/Goerge_L/article/details/80547975 TensorF

Github开源人脸识别项目face_recognition

Github开源人脸识别项目face_recognition 原文:https://www.jianshu.com/p/0b37452be63e 译者注: 本项目face_recognition是一个强大.简单.易上手的人脸识别开源项目,并且配备了完整的开发文档和应用案例,特别是兼容树莓派系统. 为了便于中国开发者研究学习人脸识别.贡献代码,我将本项目README文件翻译成中文. 向本项目的所有贡献者致敬. 英译汉:同济大学开源软件协会 子豪兄Tommy Translator's note: f

人工智能之基于face_recognition的人脸检测与识别

不久乘高铁出行,看见高铁火车站已经实现了"刷脸进站",而且效率很高,很感兴趣,今天抽时间研究一下,其实没那么复杂. 我基本上是基于https://github.com/ageitgey/face_recognition上的资料和源码做一些尝试和试验. 首先,需要配置我们的python环境,我悬着的python27(比较稳定),具体过程不多说了. 然后,需要安装这次的主角face_recognition库,这个的安装花了我不少时间,需要注意一下几点(按照本人的环境): 1,首先,安装vi

Python 使用 face_recognition 人脸识别

Python 使用 face_recognition 人脸识别 官方说明:https://face-recognition.readthedocs.io/en/latest/readme.html 人脸识别 face_recognition 是世界上最简单的人脸识别库. 使用 dlib 最先进的人脸识别功能构建建立深度学习,该模型准确率在99.38%. Python模块的使用 Python可以安装导入 face_recognition 模块轻松操作,对于简单的几行代码来讲,再简单不过了. Pyt