Python实现识别图片中的所有人脸并显示出来

开发 后端 人脸识别
本文介绍一下使用Python3实现识别图片中的所有人脸并显示出来,让我们一起来看一下。

 使用Python3实现识别图片中的所有人脸并显示出来,代码如下: 

  1. # -*- coding: utf-8 -*-  
  2. #  识别图片中的所有人脸并显示出来  
  3. # filename : find_faces_in_picture.py  
  4. from PIL import Image  
  5. import face_recognition  
  6. # 将jpg文件加载到numpy 数组中  
  7. image = face_recognition.load_image_file("linuxidc.com.jpg")  
  8. # 使用默认的给予HOG模型查找图像中所有人脸  
  9. # 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速  
  10. # 另请参见: find_faces_in_picture_cnn.py  
  11. face_locations = face_recognition.face_locations(image)  
  12. # 使用CNN模型  
  13. face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0model="cnn" 
  14. # 打印:我从图片中找到了 多少 张人脸  
  15. print("I found {} face(s) in this photograph.".format(len(face_locations)))  
  16. # 循环找到的所有人脸  
  17. for face_location in face_locations:  
  18.         # 打印每张脸的位置信息  
  19.         top, right, bottom, left = face_location  
  20.         print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))  
  21. # 指定人脸的位置信息,然后显示人脸图片  
  22.         face_image = image[top:bottom, left:right]  
  23.         pil_image = Image.fromarray(face_image)  
  24.         pil_image.show()  
  1. # 或者执行python文件  
  2. $ python3 www.linuxidc.com.py 

从图片中识别出10张人脸,并显示出来。 

  1. I found 10 face(s) in this photograph.  
  2. A face is located at pixel location Top: 445, Left: 1867, Bottom: 534, Right: 1957  
  3. A face is located at pixel location Top: 544, Left: 643, Bottom: 619, Right: 718  
  4. A face is located at pixel location Top: 478, Left: 1647, Bottom: 553, Right: 1722  
  5. A face is located at pixel location Top: 504, Left: 126, Bottom: 594, Right: 215  
  6. A face is located at pixel location Top: 536, Left: 395, Bottom: 611, Right: 469  
  7. A face is located at pixel location Top: 544, Left: 1042, Bottom: 619, Right: 1116  
  8. A face is located at pixel location Top: 553, Left: 818, Bottom: 627, Right: 892  
  9. A face is located at pixel location Top: 511, Left: 1431, Bottom: 586, Right: 1506  
  10. A face is located at pixel location Top: 564, Left: 1227, Bottom: 626, Right: 1289  
  11. A face is located at pixel location Top: 965, Left: 498, Bottom: 1017, Right: 550 

如下图:

 

 

责任编辑:庞桂玉 来源: Linux公社
相关推荐

2019-04-15 13:39:10

容器开发Docker

2013-08-27 13:59:05

微软鲍尔默

2019-12-24 19:36:45

人工智能Windows软件

2021-11-07 14:31:52

PythonWeb页面

2022-05-16 19:53:15

Pythongif动图

2019-10-17 14:07:43

技术云计算Docker

2018-07-16 16:39:00

数据

2013-12-09 10:16:03

Android firAndroid开发移动创业

2024-03-25 09:03:07

Redis开源开发

2011-10-21 15:33:45

Dart

2011-03-03 09:47:00

2022-06-28 10:03:56

CentOSLinux

2021-12-08 18:50:19

机器人人工智能AI

2022-08-11 11:20:49

Python词云图

2020-02-20 16:42:40

云端Kubernetes容器

2013-06-18 09:40:29

BYOD误解

2015-08-19 15:46:29

霍金ACAT开源

2015-04-16 10:24:07

2021-10-19 15:04:05

开源技术 数据
点赞
收藏

51CTO技术栈公众号