深度学习:不到25行代码实现口罩识别(电脑端可直接运行)
导入依赖
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
代码实现
import cv2
import paddlehub as hub
module = hub.Module(name="pyramidbox_lite_mobile_mask")
video_capture = cv2.VideoCapture(0)
font = cv2.cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, frame = video_capture.read()
results = module.face_detection(images=[frame], confs_threshold=0.5,shrink = 0.1)
try:
print(results)
for i in results[0]['data']:
if 'NO' not in i['label']:
cv2.rectangle(frame, (i['left'], i['top']), (i['right'], i['bottom']), (0, 255, 0), 2)
cv2.putText(frame, i['label']+str(round(i['confidence'],2)), (i['left'], i['top']), font, 1, (0, 255, 0), 1)
else:
cv2.rectangle(frame, (i['left'], i['top']), (i['right'], i['bottom']), (0, 0, 255), 2)
cv2.putText(frame, i['label']+str(round(i['confidence'],2)), (i['left'], i['top']), font, 1, (0, 0, 255), 1)
except:
pass
if cv2.waitKey(25) & 0xFF == ord('q'):
break
cv2.imshow('Video', frame)