ubuntu下paddlehub口罩识别
1、安装paddlepaddle和paddlehub
百度飞桨下载教程
paddlehub安装教程
本人安装时出现的问题及对应解决方案
2、代码
# -*- coding: utf-8 -*
import sys
import numpy as np
import paddlehub as hub
import os
import cv2
#加载预训练模型
module = hub.Module(name="pyramidbox_lite_mobile_mask")
#获取摄像头
cap = cv2.VideoCapture("/dev/video0")
if __name__=="__main__":
while(1):
ret, frame = cap.read()
results = module.face_detection(images=[frame], use_multi_scale=True, shrink=0.6)
# [{'data': [{'label': 'MASK', 'confidence': 0.9996459484100342, 'top': 197.41336198075373, 'bottom': 431.6184510109457, 'left': 458.3072515341687, 'right': 641.0960207038868}, {'label': 'MASK', 'confidence': 0.9396225214004517, 'top': 343.9779546410195, 'bottom': 561.498121304537, 'left': 949.538959622799, 'right': 1123.6715127943921}, {'label': 'NO MASK', 'confidence': 0.9999265670776367, 'top': 301.32586463021823, 'bottom': 498.0028807622003, 'left': 1165.33205896273, 'right': 1319.6707911061153}], 'path': 'ndarray_time=1617023746789234.0'}]
for i in range(len(results[0]['data'])):
result = results[0]['data'][i]
if result['label'] == 'MASK':
frame = cv2.rectangle(frame, (int(result['left']),int(result['top'])),(int(result['right']),int(result['bottom'])), (0, 255, 0), 4)
print(result)
#展示图片
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
# 存储图片
cv2.imwrite("camera.jpg", frame)
break
cap.release()
cv2.destroyAllWindows()
3、效果