1、安装
python -m pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install -i https://mirror.baidu.com/pypi/simple paddlehub
报错,解决方案:
>pip install -i https://mirror.baidu.com/pypi/simple paddlehub --use-feature=2020-resolver
2、检测口罩
import paddlehub as hub
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
import cv2# 加载口罩检测预训练模型
module = hub.Module(name="pyramidbox_lite_server_mask")
# 获取待检测图片
test_img_path = "mask.jpg"
imgs = [cv2.imread(test_img_path)]
# 口罩检测预测,结果保存
# visualization=True 将预测结果保存图片可视化
# output_dir='detection_result' 预测结果图片保存在当前运行路径下detection_result文件夹下
results = module.face_detection(images=imgs, use_multi_scale=True, shrink=0.6, visualization=True, output_dir='detection_result')
for result in results:print("result: %s" % result)
# 预测结果展示
path = os.path.join('detection_result', os.path.splitext(result['path'])[0] + '.jpg')
print('path: %s' %path)
img = mpimg.imread(path)
# 调用matplotlib, 相当于进入了另一个线程之中
plt.subplot(121)
plt.imshow(imgs[0])
plt.subplot(122)
plt.imshow(img)
plt.axis('off')
plt.show()
运行结果:
参考自:Python 20行代码检测人脸是否佩戴口罩_51CTO博客_python人脸口罩识别检测