一、环境搭建

1.系统环境

04
Python 2.7.14
pycharm 开发工具

2.开发环境,安装各种系统包

  • 人脸检测基于dlib,dlib依赖Boost和cmake

  • 在windows中如果要使用dlib还是比较麻烦的,如果想省时间可以在anaconda中安装

conda install -c conda-forge dlib=19.4

$ sudo apt-get install build-essential cmake
$ sudo apt-get install libgtk-3-dev$ sudo apt-get install libboost-all-dev
  • 其他重要的包
$ pip install numpy$ pip install scipy$ pip install opencv-python$ pip install dlib
  • 安装 face_recognition
# 安装 face_recognition$ pip install face_recognition# 安装face_recognition过程中会自动安装 numpy、scipy 等

这里有我自己整理了一套最新的python系统学习教程,包括从基础的python脚本到web开发、爬虫、数据分析、数据可视化、机器学习等。送给正在学习python的小伙伴!这里是python学习者聚集地,欢迎初学和进阶中的小伙伴!

来微信公众号:“速学Python”,拿Python学习资料

二、使用教程

1、facial_features文件夹

此demo主要展示了识别指定图片中人脸的特征数据,下面就是人脸的八个特征,我们就是要获取特征数据

        'chin',        'left_eyebrow',        'right_eyebrow',        'nose_bridge',        'nose_tip',        'left_eye',        'right_eye',        'top_lip',        'bottom_lip'

运行结果:

自动识别图片中的人脸,并且识别它的特征

原图:

image

image

特征数据,数据就是运行出来的矩阵,也就是一个二维数组

image

代码:

# -*- coding: utf-8 -*-# 自动识别人脸特征# filename : find_facial_features_in_picture.py# 导入pil模块 ,可用命令安装 apt-get install python-Imagingfrom PIL import Image, ImageDraw# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport face_recognition# 将jpg文件加载到numpy 数组中image = face_recognition.load_image_file("chenduling.jpg")#查找图像中所有面部的所有面部特征face_landmarks_list = face_recognition.face_landmarks(image)print("I found {} face(s) in this photograph.".format(len(face_landmarks_list)))for face_landmarks in face_landmarks_list:   #打印此图像中每个面部特征的位置facial_features = [        'chin',        'left_eyebrow',        'right_eyebrow',        'nose_bridge',        'nose_tip',        'left_eye',        'right_eye',        'top_lip',        'bottom_lip']    for facial_feature in facial_features:print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))   #让我们在图像中描绘出每个人脸特征!pil_image = Image.fromarray(image)d = ImageDraw.Draw(pil_image)    for facial_feature in facial_features:d.line(face_landmarks[facial_feature], width=5)pil_image.show()

2、find_face文件夹

不仅能识别出来所有的人脸,而且可以将其截图挨个显示出来,打印在前台窗口

原始的图片

image

识别的图片

image

代码:

# -*- coding: utf-8 -*-#  识别图片中的所有人脸并显示出来# filename : find_faces_in_picture.py# 导入pil模块 ,可用命令安装 apt-get install python-Imagingfrom PIL import Image# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport face_recognition# 将jpg文件加载到numpy 数组中image = face_recognition.load_image_file("yiqi.jpg")# 使用默认的给予HOG模型查找图像中所有人脸# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速# 另请参见: find_faces_in_picture_cnn.pyface_locations = face_recognition.face_locations(image)# 使用CNN模型# 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_locationprint("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))
# 指定人脸的位置信息,然后显示人脸图片face_image = image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show()

3、know_face文件夹

通过设定的人脸图片识别未知图片中的人脸

# -*- coding: utf-8 -*-# 识别人脸鉴定是哪个人# 导入face_recogntion模块,可用命令安装 pip install face_recognitionimport face_recognition#将jpg文件加载到numpy数组中chen_image = face_recognition.load_image_file("chenduling.jpg")#要识别的图片unknown_image = face_recognition.load_image_file("sunyizheng.jpg")#获取每个图像文件中每个面部的面部编码#由于每个图像中可能有多个面,所以返回一个编码列表。#但是由于我知道每个图像只有一个脸,我只关心每个图像中的第一个编码,所以我取索引0。chen_face_encoding = face_recognition.face_encodings(chen_image)[0]print("chen_face_encoding:{}".format(chen_face_encoding))
unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0]print("unknown_face_encoding :{}".format(unknown_face_encoding))known_faces = [chen_face_encoding
]#结果是True/false的数组,未知面孔known_faces阵列中的任何人相匹配的结果results = face_recognition.compare_faces(known_faces, unknown_face_encoding)print("result :{}".format(results))print("这个未知面孔是 陈都灵 吗? {}".format(results[0]))print("这个未知面孔是 我们从未见过的新面孔吗? {}".format(not True in results))

4、video文件夹

通过调用电脑摄像头动态获取视频内的人脸,将其和我们指定的图片集进行匹配,可以告知我们视频内的人脸是否是我们设定好的

实现:

image

代码:

# -*- coding: utf-8 -*-
# 摄像头头像识别
import face_recognition
import cv2video_capture = cv2.VideoCapture(0)# 本地图像
chenduling_image = face_recognition.load_image_file("chenduling.jpg")
chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]# 本地图像二
sunyizheng_image = face_recognition.load_image_file("sunyizheng.jpg")
sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]# 本地图片三
zhangzetian_image = face_recognition.load_image_file("zhangzetian.jpg")
zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]# Create arrays of known face encodings and their names# 脸部特征数据的集合
known_face_encodings = [chenduling_face_encoding,sunyizheng_face_encoding,zhangzetian_face_encoding
]# 人物名称的集合
known_face_names = [    "michong",    "sunyizheng",    "chenduling"]face_locations = []
face_encodings = []
face_names = []
process_this_frame = Truewhile True:# 读取摄像头画面ret, frame = video_capture.read()# 改变摄像头图像的大小,图像小,所做的计算就少small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# opencv的图像是BGR格式的,而我们需要是的RGB格式的,因此需要进行一个转换。rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# 根据encoding来判断是不是同一个人,是就输出true,不是为flaseface_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:# 默认为unknownmatches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# if match[0]:#     name = "michong"# 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# 将捕捉到的人脸显示出来    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 sizetop *= 4right *= 4bottom *= 4left *= 4# 矩形框cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)#加上标签cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Displaycv2.imshow('monitor', frame)# 按Q退出    if cv2.waitKey(1) & 0xFF == ord('q'):breakvideo_capture.release()
cv2.destroyAllWindows()

5、boss文件夹

本开源项目,主要是结合摄像头程序+推送,实现识别摄像头中的人脸。并且通过推送平台给移动端发送消息!

从零开始学Python人脸识别技术,人工智能不过如此!相关推荐

  1. python人工智能图像识别_人工智能之Python人脸识别技术,人人都能做识别!

    原标题:人工智能之Python人脸识别技术,人人都能做识别! 作者丨Python小哥哥 https://www.jianshu.com/p/dce1498ef0ee 一.环境搭建 1.系统环境 Ubu ...

  2. 人脸图像识别(python人脸识别技术)

    python人脸识别 人脸识别的崛起 什么是人脸识别 人脸识别技术的应用和发展 python人脸识别 导入库 实现代码 人脸识别的崛起 什么是人脸识别 人脸识别是将采集到的数据信息,根据人脸特征信息进 ...

  3. 图像识别python模块_人工智能之Python人脸识别技术--face_recognition模块

    一.环境搭建1.系统环境Ubuntu 17.04 Python 2.7.14 pycharm 开发工具1 2 32.开发环境,安装各种系统包人脸检测基于dlib,dlib依赖Boost和cmake$ ...

  4. 人工智能之Python人脸识别技术--face_recognition模块

    Github项目地址:https://github.com/MiChongGET/face_collection 一.环境搭建 1.系统环境 Ubuntu 17.04 Python 2.7.14 py ...

  5. python人脸识别opencv_基于python+OpenCV模块的人脸识别定位技术

    什么是OpenCV模块 OpenCV是一款跨平台的视觉库,可以支持的操作系统有Linux.Windows和Mac OS操作系统,并且还提供了多种语言的接口,比如Python,java,MATLAB等常 ...

  6. python学习之基于Python的人脸识别技术学习

    摘要: 面部识别技术的应用越来越广泛,它广泛应用于安全系统.人机交互.社交媒体.医疗保健等领域.本文介绍了基于Python的人脸识别技术,包括人脸检测.人脸特征提取和人脸识别三个部分.我们使用Open ...

  7. 大数据早报:路桥率先利用“人脸识别”技术监管医保 亚马逊与微软联合开发人工智能,打造服务大众的AI(10.19)

    数据早知道,上乐投网看早报! 『数据安全』微软内部Windows漏洞数据库曾被入侵:后果可怕 作为这个星球上覆盖率最高的操作系统,Windows的一举一动都影响着大家的使用体验,对于它的安全微软也是相 ...

  8. 为什么香港计算机科学家多,[转载]为什么香港中文大学汤晓鸥教授团队的人脸识别技术能够击败人类?...

    任何一个人脸自动识别程序,首先要考虑的就是去构建一个合适的数据集来测试算法.那需要一个非常大范围的,各种各样的,带着各种复杂动作.光线和表情的,不同脸的图像,各种人种.年龄和性别都要考虑在内.然后还要 ...

  9. 人脸识别技术发展及实用方案设计

    作者 | 汪彪 责编 | 何永灿 人脸识别技术不但吸引了Google.Facebook.阿里.腾讯.百度等国内外互联网巨头的大量研发投入,也催生了Face++.商汤科技.Linkface.中科云从.依 ...

最新文章

  1. html 使用button调用函数
  2. 【CodeForces - 467C】George and Job(dp,思维)
  3. 计算机网络 --- 数据链路层CSMA/CD协议
  4. html网页设计课程心得,网页设计教学心得体会
  5. Pr:用 Au 协作处理音频
  6. 第一章 基本句型及补语
  7. 组合数性质--二项式系数之和等于2^n的证明
  8. 23种设计模式中英文对照
  9. 程序员如何进行职业规划?
  10. html实现数据的增删查改
  11. Python绘制正弦、余弦函数图像
  12. 获取不带后缀名的Excel文件名Python
  13. 20230105无剩余飞行时间的时间制导律:当导弹位于静止目标右侧时就打不中目标?
  14. 【前端】JS批量生成调查问卷选项
  15. js三级联动之地域的选择
  16. 太空射击第14课: 玩家生命
  17. 飞机机翼机身对接结构数值计算分析(ANSYS)
  18. 身为iOS开发,你是愿意在大公司做凤尾,还是在小公司做鸡头?
  19. 沙雕加速的使用方法和最新官网
  20. 微软、谷歌、百度等公司经典面试100题[第101-160题]

热门文章

  1. MMI of UIQ
  2. python自带的程序编辑器有哪些菜单_python自带的编辑器是
  3. 一节计算机课作文500,难忘的一节课作文500字4篇
  4. python——温度换算(以字母结尾)
  5. FPGA学习前导:FPGA/CPLD简介
  6. 数字电视专业术语--DTV名词扫盲
  7. Brendan Gregg ----Linux Performance Tools NEWS
  8. 小蚁摄像机存储到计算机,小蚁摄像机电脑客户端
  9. 解决:L2TP服务器没有响应。请尝试重新连接。如果仍然有问题,请验证您的设置并与管理员联系。
  10. 给大家推荐一个比较好的VC论坛【VC驿站】