OpenCV-Python系列:检测&跟踪人眼

OpenCV-Python系列·检测&跟踪人眼

 

Tip:先确定脸部的位置,然后再确定眼睛的位置。

 
  1. # -*- coding: utf-8 -*-

  2. """

  3. Created on Wed Aug 29 21:48:21 2018

  4.  
  5. @author: Miracle

  6.  
  7. """

  8. import cv2

  9.  
  10. def detectFace():

  11. #加载人脸检测的配置文件

  12. face_cascade = cv2.CascadeClassifier('../data/haarcascades/haarcascade_frontalface_alt.xml')

  13. eye_cascade = cv2.CascadeClassifier('../data/haarcascades/haarcascade_eye.xml')

  14. #判断是否可行

  15. if eye_cascade.empty() and face_cascade.empty():

  16. raise IOError('Cannot load cascade classifier xml files!')

  17. #打开摄像头

  18. cap = cv2.VideoCapture(0)

  19. scaling_factor = 1.15

  20.  
  21. if not cap.isOpened:

  22. raise IOError('Cannot open webcam!')

  23.  
  24. while True:

  25. ret,frame = cap.read()

  26. if not ret:

  27. break

  28. frame = cv2.resize(frame,None,

  29. fx = scaling_factor,

  30. fy = scaling_factor,

  31. interpolation = cv2.INTER_LINEAR)

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

  33.  
  34. #获取脸部位置

  35. face_rects = face_cascade.detectMultiScale(gray)

  36.  
  37. #获取脸部地址

  38. for (x,y,w,h) in face_rects:

  39. roi_gray = gray[y:y+h,x:x+h]

  40. roi_color = frame[y:y+h,x:x+h]

  41. eyes = eye_cascade.detectMultiScale(roi_gray)

  42.  
  43. for (x_eye,y_eye,w_eye,h_eye) in eyes:

  44. center = (int(x_eye+0.5*w_eye),int(y_eye+0.5*h_eye))

  45. radius = int(0.3*(w_eye+h_eye))

  46. color = (0,255,0)

  47. thickness = 3

  48. cv2.circle(roi_color,center,radius,color,thickness)

  49.  
  50. cv2.imshow('detecting eye',frame)

  51.  
  52. if cv2.waitKey(1) == 27:

  53. break

  54. cap.release()

  55. cv2.destroyAllWindows()

  56.  
  57. if __name__ == '__main__':

  58. detectFace()

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值