python摄像头人脸识别小程序 开门,OpenCV3-Python人脸识别方法—基于摄像头

1. Viola-Jones分类器

Viola-Jones分类器在级联的每个节点中使用AdaBoost来学习一个高检测率低拒绝率的多层树分类器,其有以下几点创新:

(1)使用类Haar输入特征,对矩形图像区域的“和”或者“差”进行阈值化。

(2)积分图像技术加速矩形图像区的45°旋转值的计算,被用来加速类Haar输入特征的计算。

(3)使用统计boosting创建两类问题(人脸、非人脸)的分类器节点(高通过率、低拒绝率)。

(4)把弱分类节点组成筛选式级联。换句话说:第一组分类器是最优,能通过包含物体的图像区域,同时允许一些不包含物体的图像通过;第二组分类器次优分类器,也是有较低的拒绝率;以此类推,在测试模式下,只要图像区域通过了整个级联,则认为里面有物体。

2. 摄像头实时人脸识别

加载人脸检测器文件haarcascade_frontalface_default.xml和人眼检测器文件 haarcascade_eye.xml;调用 cv2.VideoCapture(0)打开USB摄像头,获取实时图像,程序会根据获取的图像,检测并绘制出人脸和眼睛。

若佩戴着眼镜,请加载检测器文件 haarcascade_eye_tree_eyeglasses.xml 。import cv2

def detect():

face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')

eye_cascade = cv2.CascadeClassifier('./cascades/haarcascade_eye.xml')

#eye_cascade = cv2.CascadeClassifier('./cascades/haarcascade_eye_tree_eyeglasses.xml')

camera = cv2.VideoCapture(0)

while (True):

ret, frame = camera.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)

#img = cv2.imread(frame)

for (x,y,w,h) in faces:

img = cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)

roi_gray = gray[y:y+h, x:x+w]

'''

cv2.CascadeClassifier.detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) → objects

Parameters:

image – Matrix of the type CV_8U containing an image where objects are detected.

scaleFactor – Parameter specifying how much the image size is reduced at each image scale.

minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

minSize – Minimum possible object size. Objects smaller than that are ignored.

maxSize – Maximum possible object size. Objects larger than that are ignored.

objects – Vector of rectangles where each rectangle contains the detected object, the rectangles may be partially outside the original image.

'''

eyes = eye_cascade.detectMultiScale(roi_gray, 1.03, 5, 0, (80,80))

for (ex,ey,ew,eh) in eyes:

cv2.rectangle(img,(x+ex,y+ey),(x+ex+ew,y+ey+eh),(0,255,0),2)

cv2.imshow("camera", frame)

if cv2.waitKey(100) & 0xff == ord("q"):

break

camera.release()

cv2.destroyAllWindows()

if __name__ == "__main__":

detect()

3. 识别结果

ed8d827c135362ed559ff01574e80710.png

4. 注意事项

实际中进行眼睛检测时,其结果可能会和鼻子混淆,此时可通过添加minSize来限制最小区域:minSize – Minimum possible object size. Objects smaller than that are ignored.

注意:本站所有文章除特别说明外,均为原创,转载请务必以超链接方式并注明作者出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值