|
- # 导入必要的库
- import cv2
- import paddlehub as hub
-
- # 加载Paddlehub人脸检测模型
- mask_detector = hub.Module(name="pyramidbox_lite_mobile_mask")
- face_detector = hub.Module(name="pyramidbox_lite_mobile")
-
- cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
- while True:
- # 从摄像头读取图片
- sucess, img = cap.read()
- # os.environ["CUDA_VISIBLE_DEVICES"] = "0,1,2,3"
- result_1 = mask_detector.face_detection(images=[img])
- result_2 = face_detector.face_detection(images=[img])
- print(result_1)
- # 遍历结果并绘制矩形框
- if result_2[0]['data'] != [] and result_1[0]['data'][0]['label'] == 'MASK':
- for face_1 in result_1[0]['data']:
- # 将Dict形式的key-value对转换成变量形式
- locals().update(face_1)
- print('bbox:', [left, top, right, bottom])
- cv2.rectangle(img, tuple([left, top]), tuple([right, bottom]), (0, 255, 127), 2)
- else:
- for face_2 in result_2[0]['data']:
- locals().update(face_2)
- cv2.rectangle(img, tuple([left, top]), tuple([right, bottom]), (0, 0,205), 2)
-
- # 显示图像
- cv2.imshow("img", img)
- # 保持画面的持续。
- k = cv2.waitKey(1)
- if k == 27:
- # 通过esc键退出摄像
- cv2.destroyAllWindows()
- break
-
- # 关闭摄像头
- cap.release()
|