本示例利用Ultra-Light-Fast-Generic-Face-Detector-1MB模型完成人脸检测。该模型是针对边缘计算设备或低算力设备(如用ARM推理)设计的实时超轻量级通用人脸检测模型,可以在低算力设备中如用ARM进行实时的通用场景的人脸检测推理。
一、定义待预测数据
以本示例中文件夹下qwer.jpg为待预测图片
安装paddlehhub
pip install paddlehub==1.6.2 -i https://pypi.tuna.tsinghua.edu/simple
# 待预测图片
test_img_path = ["./qwer.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。每一行是待预测图片的存放路径。
展示文本
cat test.txt
用户想要利用Ultra-Light-Fast-Generic-Face-Detector-1MB完成对该文件的人脸检测,只需读入该文件,将文件内容存成list,list中每个元素是待预测图片的存放路径。
with open('test.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_320和ultra_light_fast_generic_face_detector_1mb_640。
ultra_light_fast_generic_face_detector_1mb_320,在预测时会将图片输入缩放为320 * 240,预测速度更快;ultra_light_fast_generic_face_detector_1mb_640,在预测时会将图片输入缩放为640 * 480,预测精度更高。
根据需要,选择具体模型。利用PaddleHub使用该模型时,只需更改指定name,即可实现无缝切换。
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,完成预测功能。
input_dict = {"image": test_img_path}
# 执行预测并打印结果
results = module.face_detection(data=input_dict, visualization=True)
for result in results:
print(result)
# 预测结果展示
img = mpimg.imread("face_detector_640_predict_output/qwer.jpg")
plt.figure(figsize=(10,10))
plt.imshow(img)
plt.axis('off')
plt.show()
输出示例:
{‘data’: [{‘left’: 1055.152099609375, ‘right’: 1260.3958740234375, ‘top’: 788.3377075195312, ‘bottom’: 1046.7877197265625, ‘confidence’: 0.999962568283081}], ‘path’: ‘./test_face_detection.jpg’, ‘save_path’: ‘face_detector_640_predict_output/test_face_detection.jpg’}
{‘data’: [{‘left’: 530.7073364257812, ‘right’: 879.6205444335938, ‘top’: 200.94325256347656, ‘bottom’: 642.6989135742188, ‘confidence’: 0.999997615814209}], ‘path’: ‘./qwer.jpg’, ‘save_path’: ‘face_detector_640_predict_output/qwer.jpg’}
输出参数解释:
left,right,top,bottom:为相对坐标
confidence:准确率
path:路径
save_path:保存路径
更多推荐
利用PaddleHub做人脸检测
发布评论