前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【深度学习入门案例】二十行代码实现批量人脸检测

【深度学习入门案例】二十行代码实现批量人脸检测

作者头像
川川菜鸟
发布2021-10-19 11:16:46
3670
发布2021-10-19 11:16:46
举报

一.前言

利用Ultra-Light-Fast-Generic-Face-Detector-1MB模型完成人脸检测。该模型是针对边缘计算设备或低算力设备(如用ARM推理)设计的实时超轻量级通用人脸检测模型,可以在低算力设备中如用ARM进行实时的通用场景的人脸检测推理。

二.定义数据

代码语言:javascript
复制
# 待预测图片
test_img_path = ["./test.jpg"]

import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 

img = mpimg.imread(test_img_path[0]) 

# 展示待预测图片
plt.figure(figsize=(10,10))
plt.imshow(img) 
plt.axis('off') 
plt.show()

首先展示如下:

在这里插入图片描述
在这里插入图片描述

若是待预测图片存放在一个文件中,如左侧文件夹所示的test.txt。每一行是待预测图片的存放路径。 例如我创建一个ren.txt

在这里插入图片描述
在这里插入图片描述

读取则代码为:

代码语言:javascript
复制
with open('ren.txt', 'r') as f:
    test_img_path=[]
    for line in f:
        test_img_path.append(line.strip())
print(test_img_path)

返回:

在这里插入图片描述
在这里插入图片描述

三.加载预训练模型

Ultra-Light-Fast-Generic-Face-Detector-1MB提供了 两种预训练模型。**ultra_light_fast_generic_face_detector_1mb_320ultra_light_fast_generic_face_detector_1mb_640。

  1. ultra_light_fast_generic_face_detector_1mb_320,在预测时会将图片输入缩放为320 * 240,预测速度更快。
  2. ultra_light_fast_generic_face_detector_1mb_640,在预测时会将图片输入缩放为640 * 480,预测精度更高。 用户根据需要,选择具体模型。利用PaddleHub使用该模型时,只需更改指定name,即可实现无缝切换。 代码为:
代码语言:javascript
复制
import paddlehub as hub

module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
# module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_320")

四、预测

PaddleHub对于支持一键预测的module,可以调用module的相应预测API,完成预测功能。

代码语言:javascript
复制
input_dict = {"image": test_img_path}

# execute predict and print the result
results = module.face_detection(data=input_dict, visualization=True)
for result in results:
    print(result)

# 预测结果展示
img = mpimg.imread("face_detector_640_predict_output/test_face_detection.jpg")
plt.figure(figsize=(10,10))
plt.imshow(img) 
plt.axis('off') 
plt.show()

返回如下:

在这里插入图片描述
在这里插入图片描述

预测结果保存到了相应的文件夹中,打开一个看看:

在这里插入图片描述
在这里插入图片描述

验证没有问题!yeah!!

五.完整源码

代码语言:javascript
复制
# coding=gbk
"""
作者:川川
@时间  : 2021/8/29 22:28
群:970353786
"""
#待预测图片
# test_img_path = ["./test.jpg"]
#
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
#
# img = mpimg.imread(test_img_path[0])
#
# # 展示待预测图片
# plt.figure(figsize=(10,10))
# plt.imshow(img)
# plt.axis('off')
# plt.show()

with open('ren.txt', 'r') as f:
    test_img_path=[]
    for line in f:
        test_img_path.append(line.strip())
print(test_img_path)

import paddlehub as hub

module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_640")
# module = hub.Module(name="ultra_light_fast_generic_face_detector_1mb_320")
## 预测
input_dict = {"image": test_img_path}
# execute predict and print the result
results = module.face_detection(data=input_dict, visualization=True)
for result in results:
    print(result)

# 预测结果展示
img = mpimg.imread("./test.jpg")
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()

如果你多人工智能也感兴趣,关注我吧,给个三连哦!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-08-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.前言
  • 二.定义数据
  • 三.加载预训练模型
  • 四、预测
  • 五.完整源码
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档