因为需要做单片机课设,题目是智能风扇,试了很多传感器,都感觉风扇不够智能,所以想直接用一个人脸检测或行人检测的AI模型进行检测人的位置,然后实现风扇对人吹,如果人不在则自动关闭,实现风扇的智能化。
因为AI模型不是项目的重点,所以我不想浪费过多时间在训练模型上,所以选择了paddlehub模型库里的预训练模型完成功能。
安装PaddleHub
建议大家用python3下载使用:
pip install --upgrade paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install --upgrade paddlehub -i https://mirror.baidu.com/pypi/simple
opencv-python安装报错解决方法
如果在用python2安装paddlehub,可能会默认下载4.3版本的opencv,但是python2不支持这么高版本的,需要自行下载低版本opencv即可:
pip install opencv-python==4.1.1.26
然后重新安装paddlehub即可。
去官网选择合适的模型
在本次课设中,考虑到树莓派算力有限,且对识别精度要求不是特别高,所以我选用了pyramidbox_lite_mobile
模型实现人脸检测:
预测代码
import paddlehub as hub
import cv2
import time
def draw_bbox_image(frame, boxes):
cv2.rectangle(frame, (boxes["left"], boxes["top"]), (boxes["right"], boxes["bottom"]), (0,255,0), 2)
#cv2.putText(图像, 文字, (x, y), 字体, 大小, (b, g, r), 宽度)
cv2.putText(frame, str(boxes["confidence"]), (boxes["left"], boxes["top"]-2), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
if __name__ == "__main__":
vedio_capter = cv2.VideoCapture(0)
face_detector = hub.Module(name="pyramidbox_lite_mobile")
while True:
ret, frame = vedio_capter.read()
tim1 = time.time()
result = face_detector.face_detection(images=[frame])
try:
data_dict = result[0]["data"][0]
draw_bbox_image(frame, data_dict)
print("data_dict:", data_dict)
except IndexError:
pass
cv2.imshow("result", frame)
tim2 = time.time()
tim = tim2 - tim1
print("tim", tim)
print("result", result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vedio_capter.realse()
Windows端实现效果
完工!
跑去做单片机啦,嘿嘿嘿。
突然得知,PaddlePaddle不支持arm架构,所以树莓派无法使用paddlehub。
那顺便挂个PYB单片机和树莓派的通讯的文章,有兴趣的小伙伴可以直接做个出来,基本1小时搞定!