3495| 1
|
[讨论] 【树莓派】人脸追踪(3)树莓派+OpenCV |
1、代码 [mw_shl_code=python,true]import numpy as np import cv2 from time import sleep import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) pan = 27 tilt = 17 GPIO.setup(tilt, GPIO.OUT) # white => TILT GPIO.setup(pan, GPIO.OUT) # gray ==> PAN def setServoAngle(servo, angle): assert angle >=5 and angle <= 175 pwm = GPIO.PWM(servo, 50) pwm.start(8) dutyCycle = angle / 18. + 3. pwm.ChangeDutyCycle(dutyCycle) sleep(0.1) pwm.stop() faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml') cap = cv2.VideoCapture(0) cap.set(3,160) # set Width cap.set(4,120) # set Height AngleX=90 AngleY=90 setServoAngle(tilt, AngleX) setServoAngle(pan, AngleY) while True: ret, img = cap.read() #img = cv2.flip(img, -1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=3, minSize=(5, 5) ) faceX=0 faceY=0 faceW=0 faceH=0 for (x,y,w,h) in faces: if(w*h>faceW*faceH): faceX=x faceY=y faceW=w faceH=h if(faceW*faceH>0): cv2.rectangle(img,(faceX,faceY),(faceX+faceW,faceY+faceH),(255,0,0),2) roi_gray = gray[y:faceY+faceH, x:faceX+faceW] roi_color = img[y:faceY+faceH, x:faceX+faceW] faceX=faceX+faceW/2 faceY=faceY+faceH/2 if(faceX>90): AngleX-=1 if(faceX<70): AngleX+=1 if(AngleX>140): AngleX=140 if(AngleX<30): AngleX=30 if(faceY>70): AngleY-=1 if(faceY<50): AngleY+=1 if(AngleY>140): AngleY=140 if(AngleY<30): AngleY=30 setServoAngle(tilt, AngleX) setServoAngle(pan, AngleY) cv2.imshow('video',img) print(faceX) print(faceY) k = cv2.waitKey(30) & 0xff if k == 27: # press 'ESC' to quit break cap.release() cv2.destroyAllWindows() [/mw_shl_code] ———————————————— 2、CascadeClassifier检测void CascadeClassifier::detectMultiScale( const Mat& image, vector<Rect>& objects, double scaleFactor, int minNeighbors, int flags, Size minObjectSize, Size maxObjectSize) { vector<int> fakeLevels; vector<double> fakeWeights; detectMultiScale( image, objects, fakeLevels, fakeWeights, scaleFactor, minNeighbors, flags, minObjectSize, maxObjectSize, false ); } 参数意思: 1. const Mat& image:输入图像 2. vector& objects:输出的矩形向量组 3. double scaleFactor=1.1:这个是每次缩小图像的比例,默认是1.1 4. minNeighbors=3:匹配成功所需要的周围矩形框的数目,每一个特征匹配到的区域都是一个矩形框,只有多个矩形框同时存在的时候,才认为是匹配成功,比如人脸,这个默认值是3。 5. flags=0:可以取如下这些值: CASCADE_DO_CANNY_PRUNING=1, 利用canny边缘检测来排除一些边缘很少或者很多的图像区域 CASCADE_SCALE_IMAGE=2, 正常比例检测 CASCADE_FIND_BIGGEST_OBJECT=4, 只检测最大的物体 CASCADE_DO_ROUGH_SEARCH=8 初略的检测 6. minObjectSize maxObjectSize:匹配物体的大小范围 ———————————————— |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed