栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python实现简单的人脸识别——第二次课

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python实现简单的人脸识别——第二次课

文章目录
  • 前言
  • 一、opencv人脸识别
    • 1. 安装opencv
    • 2. 运行代码
    • 3. 结果
  • 二、使用dlib+face_recognition识别人脸
    • 1. 下载
    • 2. dlib的wheel报错
    • 3. 运行代码
    • 4. load_image_file无法识别图像
    • 5. 结果
  • 二、使用dlib进行定点
    • 1. 下载
    • 2. 运行代码
    • 3. 结果
  • 总结


前言

记录下载安装使用opencv、dlib.、face_recognition识别人脸的步骤和遇到的困难。

一、opencv人脸识别 1. 安装opencv

在anaconda python3.9环境下,使用命令“pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python”。

2. 运行代码
import cv2
def detect(filename):
    print("1")
    face_cascade = cv2.CascadeClassifier('C:\software\anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
    print("2")
    img = cv2.imread(filename)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    print("3")
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)    
    print("4")
    for (x, y, w, h) in faces:
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    print("5")
    cv2.imshow('Person Detected!', img)
    print("6")
    cv2.waitKey(0)
    print("7")
    cv2.destroyAllWindows()
detect('test1.jpg')

其中,"test1.jpg"是要识别的图像;cv2.CascadeClassifier()中是分类器的地址在对应python的site-packagescv2data目录下。

3. 结果

二、使用dlib+face_recognition识别人脸 1. 下载
pip install cmake
pip install dlib
pip install face_recognition

# dlib本地安装
pip install dlib-19.19.0-cp38-cp38-win_amd64.whl.whl
2. dlib的wheel报错

解决方案下载whl,本地安装,不报错了。注意不同python版本对应不同版本的whl。

3. 运行代码
import face_recognition
import cv2
image = face_recognition.load_image_file("test1.jpg",mode ='RGB')
face_locations_noCNN=face_recognition.face_locations(image)
face_recognition.face_locations(image,model='cnn')
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)       # The number of faces
org = cv2.imread("test1.jpg")
img = cv2.imread("test1.jpg")
cv2.imshow("test.jpg",img) # 原始图⽚
for i in range(0,face_num2):
   top = face_locations_noCNN[i][0]
   right = face_locations_noCNN[i][1]
   bottom = face_locations_noCNN[i][2]
   left = face_locations_noCNN[i][3]
   start = (left, top)
   end = (right, bottom)
   color = (0,255,255)
   thickness = 2
   cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. load_image_file无法识别图像

发现报错“UnidentifiedImageError: cannot identify image file 'test2.jpg‘”,load_image_file无法识别图像。查看api.py源码,使用的是“import PIL.Image”,因此网上大多数说的将“import Image"改为”from PIL import Image“的方法并不可行,应该存在其他问题。最后发现是因为pollow版本较低的缘故,使用命令”pip install --upgrade pillow“升级pollow,运行代码不再报错。

5. 结果



优于opencv的结果。

二、使用dlib进行定点 1. 下载

前面已经下载安装了dlib库,只需要下载预训练集”shape_predictor_5_face_landmarks.dat“,”shape_predictor_68_face_landmarks.dat“。

2. 运行代码
import cv2
import dlib
path = "test1.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
detector = dlib.get_frontal_face_detector()
dets = detector(gray, 1)
predictor = dlib.shape_predictor("C:\software\anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
for face in dets:
   shape = predictor(img, face)
   for pt in shape.parts():
       pt_pos = (pt.x, pt.y)
       cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
   cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 结果

总结

实验总体顺利,遇到的问题也都在上网查阅资料后一一解决。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883388.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号