树莓派配置摄像头
sudo apt-get install python-opencv
sudo apt-get install fswebcam
配置摄像头
sudo nano /etc/modules
查看树莓派CPU型号
cat /proc/cpuinfo
cpu型号为bcm2711
添加
bcm2711-v4l2
vcgencmd get_camera
配置展示窗口
export DISPLAY=:0.0
实时人脸检测代码
#coding=utf-8#face-detection-camera.pyimport cv2 as cvimport numpy as npprint('开始人脸摄像头实时监测')print('press "q" to exit ')#载入模型文件和权重文件net = cv.dnn.readNet('face-detection-adas-0001.xml','face-detection-adas-0001.bin')#specify target devicenet.setPreferableTarget(cv.dnn.DNN_TARGET_MYRIAD)#从摄像头中读取图像帧cap = cv.VideoCapture(0)while(1):#获取一帧图像ret,frame=cap.read()#prepare input blob and perform an inferenceframe = cv.resize(frame,(480,320),interpolation=cv.INTER_CUBIC)blob = cv.dnn.blobFromImage(frame,size=(672,384),ddepth=cv.CV_8U)net.setInput(blob)out=net.forward()#绘制人脸框for detection in out.reshape(-1,7):confidence = float(detection[2])#获取左上角图片的坐标xmin=int(detection[3]*frame.shape[1])ymin=int(detection[4]*frame.shape[0])#获取右下角图片的坐标xmax=int(detection[5]*frame.shape[1])ymax=int(detection[6]*frame.shape[0])if confidence>0.5:cv.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0))#展示图像cv.imshow("capture",frame)if cv.waitKey(1)&0xFF==ord('q'):#每一毫秒监听一次键盘的动作,按q键结束,并保存图片cv.imwrite('out_cam.png',frame)print("save one image done!")break#关闭摄像头及显示窗口cap.release()cv.destoryAllWindows()