人工智能之基于face_recognition的人脸检测与识别(代码片段)

步步为营WEB 步步为营WEB     2022-11-13     158

关键词:

不久乘高铁出行,看见高铁火车站已经实现了“刷脸进站”,而且效率很高,很感兴趣,今天抽时间研究一下,其实没那么复杂。

我基本上是基于https://github.com/ageitgey/face_recognition上的资料和源码做一些尝试和试验。

首先,需要配置我们的python环境,我悬着的python27(比较稳定),具体过程不多说了。

然后,需要安装这次的主角face_recognition库,这个的安装花了我不少时间,需要注意一下几点(按照本人的环境):

  1,首先,安装visual studio 2015,因为vs2015默认只安装c#相关组件,所以需要安装c++相关组件。

    ps:vs2015安装c++相关组件的方法:在vs2015中新建c++项目,出现下面场景

    

    选择第二项,确定后就会自动安装。

    为什么需要安装c++,因为安装face_recognition时会先安装dlib,dlib是基于c++的一个库。

  2,安装cmake(一个跨平台编译工具),然后需要将cmake的安装路径加入到系统环境变量path中去。

最后,就可以直接在dos中执行安装命令了(需要切换到python目录下的Script目录下):pip install  face_recognition,命令会自动帮你安装好需要的dlib库。 

到此为止,我们完成了face_recognition安装工作。

 

---------------------------------------------------------------分割线----------------------------------------------------------------------------------

下面给出几个实例来逐步了解“人脸识别”:

1.一行代码实现“人脸识别”

 

在Python目录中新建两个文件夹:分别表示“已知姓名的人”和“未知姓名的人”,图片以额、人名命名,如下:

 

 接下来,我们通过“认识的人”来识别“不认识的人”:

结果表明:1.jpg不认识,3.jpg是obama,unkown.jpg中有两个人,一个是obama,另一个不认识

结果还挺准确的!很给力!!

 

2.识别图片中所有的人脸,并显示出来

import Image
import face_recognition
image = face_recognition.load_image_file(\'F:/Python27/Scripts/all.jpg\')
face_locations = face_recognition.face_locations(image)

#face_locations =face_recognition.

#face_locations(image,number_of_times_to_upsample=0,model=\'cnn\')
print(\'i found  face(s) in this photograph.\'.format(len(face_locations)))
for face_location in face_locations:
    top,right,bottom,left = face_location
    print(\'A face is located at pixel location Top:,Left:,Bottom:,Right:\'.format(top,right,bottom,left))
    face_image = image[top:bottom,left:right]
    pil_image=Image.fromarray(face_image)
    pil_image.show()
View Code

避坑指南:import Image需要先安装PIL库,在pycharm中安装的时候会报错(因为pil没有64位的版本),这时我们安装Pillow-PIL就好了。

我们的all.jpg如下:

 

 执行以下,看看结果:

没有错,总共12个人脸都被识别出来了!!!

 

3.给照片“美颜”

face_recognition可以识别人像的下巴,眼睛,鼻子,嘴唇,眼球等区域,包含以下这些个特征:

  facial_features = [ \'chin\', \'left_eyebrow\', \'right_eyebrow\', \'nose_bridge\', \'nose_tip\', \'left_eye\', \'right_eye\', \'top_lip\', \'bottom_lip\' ]

       利用这些特征属性,可以轻松的给人像“美颜”

from PIL import Image, ImageDraw
face_recognition
import face_recognition


image = face_recognition.load_image_file("F:/Python27/Scripts/known_people/obama.jpg")

#查找图像中所有面部的所有面部特征
face_landmarks_list = face_recognition.face_landmarks(image)

for face_landmarks in face_landmarks_list:
    pil_image = Image.fromarray(image)
    d = ImageDraw.Draw(pil_image, \'RGBA\')

    #让眉毛变成了一场噩梦
    d.polygon(face_landmarks[\'left_eyebrow\'], fill=(68, 54, 39, 128))
    d.polygon(face_landmarks[\'right_eyebrow\'], fill=(68, 54, 39, 128))
    d.line(face_landmarks[\'left_eyebrow\'], fill=(68, 54, 39, 150), width=5)
    d.line(face_landmarks[\'right_eyebrow\'], fill=(68, 54, 39, 150), width=5)

    #光泽的嘴唇
    d.polygon(face_landmarks[\'top_lip\'], fill=(150, 0, 0, 128))
    d.polygon(face_landmarks[\'bottom_lip\'], fill=(150, 0, 0, 128))
    d.line(face_landmarks[\'top_lip\'], fill=(150, 0, 0, 64), width=8)
    d.line(face_landmarks[\'bottom_lip\'], fill=(150, 0, 0, 64), width=8)

    #闪耀眼睛
    d.polygon(face_landmarks[\'left_eye\'], fill=(255, 255, 255, 30))
    d.polygon(face_landmarks[\'right_eye\'], fill=(255, 255, 255, 30))

    #涂一些眼线
    d.line(face_landmarks[\'left_eye\'] + [face_landmarks[\'left_eye\'][0]], fill=(0, 0, 0, 110), width=6)
    d.line(face_landmarks[\'right_eye\'] + [face_landmarks[\'right_eye\'][0]], fill=(0, 0, 0, 110), width=6)

    pil_image.show()
View Code

执行下看看结果:

有点辣眼睛!!!!

 

4.利用笔记本摄像头识别人像

回到前面说的高铁站的“刷脸”,其实就是基于摄像头的“人像识别”。

这里要调用电脑的摄像头,而且涉及一些计算机视觉系统的计算,所以我们要先安装opencv库,

安装方法:

pip install --upgrade setuptools
pip install numpy Matplotlib
pip install opencv-python

 ps:如果报错:EnvironmentError: [Errno 13] Permission denied: 在install后加上--user即可

         小技巧:可以在python命令行中用 import site; site.getsitepackages()来确定当前的python环境的site-packages目录的位置

目的:这里我们需要用摄像头识别自己,那么首先需要有一张自己的照片,我将我的照片命名为mike.jpg,然后使用摄像头来识别我自己。

 看看代码:

import face_recognition
import cv2

# 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("F:/Python27/Scripts/known_people/obama.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("F:/Python27/Scripts/known_people/mike.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 = [
    "Barack Obama",
    "mike"
]

# 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()
View Code

只想看看结果:

 

看来,我被识别成功了。看起来有点小激动呢。

 

 

 

通过上面四个小例子基本了解face_recognition的用法,这只是小试牛刀,具体在现实中的应用要复杂很多,

我们需要大量的人脸数据,会涉及到机器学习和数学算法等等,而且根据应用场景的不同也会出现很多不同的要求。

这里只是一起学习分享,期待后续关于"人工智能"的内容。

 

山东大学项目实训四——face_recognition使用opencv和dlib实现基于视频的人脸识别(代码片段)

Face_Recognition使用Opencv和Dlib实现基于视频的人脸识别文件夹介绍1、Resources\\pictures此文件夹下存放人脸保存结果2、Resources\\video此文件夹下存放带标注视频保存结果3、Resources\\faceS此文件夹下存放各个人物的图片,用于人脸库... 查看详情

人工智能之问

人工智能的出现,给生活带来了很多的变化,机器人也替代了很多的工作岗位。人工智能的发展,给我们带了很多的疑问。人工智能会影响哪些行业?人工智能是否会取代人类?人工智能会给各行各业带来什么新的变革?人工智... 查看详情

毕业设计之--基于单片机的智能水箱

...丹成学长,今天向大家介绍一个学长做的单片机项目基于单片机的智能水箱大家可用于课程设计或毕业设计2概述2.1系统描述楼顶水箱 查看详情

python基于opencv+face_recognition实现人脸捕捉与人脸识别(照片对比)(代码片段)

1.安装包依赖与上篇通过摄像头动态识别人脸一样,先下载好opencv-python、face-recognition,这里因为使用的是照片对比的方式,特意使用tkinter画了一个简单的GUI方便操作。在python3以上版本tkinter是环境自带的,所以这... 查看详情

ai&blockchain:“知名博主独家讲授”人工智能创新应用竞赛精选实战作品之《基于计算机视觉自然语言处理区块链和爬虫技术的智能会议系统》软件系统案例的界面简介功能介绍分享之总篇

AI&BlockChain:“知名博主独家讲授”人工智能创新应用竞赛【精选实战作品】之《基于计算机视觉、自然语言处理、区块链和爬虫技术的智能会议系统》软件系统案例的界面简介、功能介绍分享之总篇前言:博主目前已... 查看详情

三十五.智能驾驶之基于polarnet的点云语义分割及ros系统实践

...分割在自动驾驶领域的感知模块占据重要地位,从多年前基于传统的点云聚类和分割,到近些年基于深度学习的点云语义分割方法,技术逐渐成熟已经进入实时端到端的阶段.前有基于球面投影映射出二维深度图的SqueezeSeg,后又百度的... 查看详情

转:催熟智能音箱,争夺一个人工智能之梦

原文:http://36kr.com/p/5079975.html深圳的硬件方案商吴桥今年很“牛气”:接到的合作电话络绎不绝,他拒绝掉的人也数不胜数。感谢智能音箱。吴桥估摸说,自己平均每个月会接到20至30家公司的电话,来自互联网公司或硬件厂商... 查看详情

基于ls1046a的边缘计算之人脸识别方案

...样。我们创建安全、高度可编程和灵活的计算系统来增强人工智能(AI)和机器学习(ML),有助于开创本地AI的时代 查看详情

dl之perceptron:感知器(多层感知机/人工神经元)的原理之基于numpy定义2层感知机底层逻辑代码(与门and/与非门nand/或门or是)解决xor异或问题之详细攻略(代码片段)

...之Perceptron:感知器(多层感知机/人工神经元)的原理之基于numpy定义2层感知机底层逻辑代码(与门AND/与非门NAND/或门OR是)解决XOR异或问题之详细攻略目录感知器(多层感知机/人工神经元)的原理之基于numpy定义2层感知机底层逻辑... 查看详情

行业智能化之三大主要智能工厂建设模式

...在动力在于产品品质可控,侧重从生产数字化建设起步,基于品控需求从产品末端控制向全流程控制转变。因此其智能工厂建设模式为:    一是推进生产过程数字化,在生产制造、过程管理等单个环节信息化系统建设的基... 查看详情

人脸识别----face_recognition安装与应用(附代码)(代码片段)

  face_recognition号称是世界上最简单的基于python的人脸识别库,是在大名鼎鼎的深度学习框架dlib上做的整合,dlib模型在LFW(LabeledFacesintheWild)能有99.38的准确率。另外face_recognition提供了相应的命令行工具,可以通过命令... 查看详情

毕业设计之-基于stm32的智能药箱系统设计与实现(代码片段)

文章目录1简介2绪论2.1课题背景2.2实现功能3系统设计3.1系统架构3.2硬件设计3.2.1wifi电路设计3.2.2其他电路设计3.3软件设计3.3.1软件工作流程3.3.2闹铃提醒程序设计3.3.3液晶显示程序设计3.3.4信息存储程序设计3.3.5报警电路程序设计3.4... 查看详情

人工智能之knn算法(代码片段)

转载自:https://www.cnblogs.com/magic-girl/p/python-kNN.html基于python实现的KNN算法邻近算法(k-NearestNeighbor)是机器学习中的一种分类(classification)算法,也是机器学习中最简单的算法之一了。虽然很简单,但在解决特定问题时却能发挥很... 查看详情

基于imageai的图像识别

博主简介博主是一名大二学生,主攻人工智能研究。感谢让我们在CSDN相遇,博主致力于在这里分享关于人工智能,c++,Python,爬虫等方面知识的分享。如果有需要的小伙伴可以关注博主,博主会继续更新的,如果有错误之处,... 查看详情

3星|埃森哲《机器与人》:基于人工智能新闻甚至宣传稿的畅想与分析

机器与人:埃森哲论新人工智能主题是未来人工智能与人的合作关系的讨论。作者认为人工智能与人各有优劣势,中间有一大片交叉地带是需要人工智能与人一起合作的,这些交叉地带目前还有很多空缺岗位找不到合适的人。书... 查看详情

基于无线控制的智能家居技术探讨

...着科技的不断发展,无线技术也进行着日新月异的改进。基于其灵活性、无需布线等优点,在市场上也得到相当广泛应用。智能家居产品的出现,更是为无线技术开辟了另一方新的应用空间。相比较传统智能家居系统采用的有线... 查看详情

基于python的智能视频分析之人数统计的多种实现(代码片段)

目录摘要11理论基础21.1传统的人群计数算法[1]31.1.1基于检测的方法31.1.2基于轨迹聚类的方法31.1.3基于回归的方法31.2基于深度学习的人群计数算法41.2.1YOLOv3目标检测算法[2]41.2.2MCNN人群计数算法[3]51.2.3LSC-CNN人群计数算法[4]51.2.4视觉... 查看详情

人工智能之目标检测系列综述

文章目录前言正文1.传统目标检测2.目标检测-神经网络2-1.R-CNN2-2.FastR-CNN2-3.FasterR-CNN2-4.MaskR-CNN2-5.Yolo2-6.SSD前言参考https://blog.csdn.net/jiaoyangwm/article/details/89111539时间线慢慢补充正文1.传统目标检测在深度学习出现之前,传统的目... 查看详情