当前位置:   article > 正文

基于python的人脸识别(检测人脸、眼睛、嘴巴、鼻子......)_nose_cascade.detectmultiscale

nose_cascade.detectmultiscale
  1. import numpy as np
  2. import cv2
  3. # 脸
  4. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  5. face_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
  6. # 眼睛
  7. eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
  8. eye_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_eye.xml')
  9. #嘴巴
  10. mouth_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
  11. mouth_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_mcs_mouth.xml')
  12. #鼻子
  13. nose_cascade = cv2.CascadeClassifier('haarcascade_mcs_nose.xml')
  14. nose_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_mcs_nose.xml')
  15. #耳朵
  16. leftear_cascade = cv2.CascadeClassifier('haarcascade_mcs_leftear.xml')
  17. leftear_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_mcs_leftear.xml')
  18. rightear_cascade = cv2.CascadeClassifier('haarcascade_mcs_rightear.xml')
  19. rightear_cascade.load('D:BROWSE/Opencv/opencv/sources/data/haarcascades/haarcascade_mcs_rightear.xml')
  20. #face_cascade = cv2.CascadeClassifier("../../opencv-2.4.9/data/haarcascades/haarcascade_frontalface_default.xml")
  21. #eye_cascade = cv2.CascadeClassifier('../../opencv-2.4.9/data/haarcascades/haarcascade_eye.xml')
  22. img = cv2.imread('22.jpg')
  23. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  24. #脸
  25. faces = face_cascade.detectMultiScale(gray, 1.2, 3)
  26. for (x, y, w, h) in faces:
  27. img = cv2.rectangle(img, (x,y),(x+w, y+h), (255, 0, 0), 2)
  28. roi_gray = gray[y:y+h, x:x+w]
  29. roi_color = img[y:y+h, x:x+w]
  30. #眼睛
  31. eyes = eye_cascade.detectMultiScale(roi_gray, 1.2, 3)
  32. for (ex,ey,ew,eh) in eyes:
  33. cv2.rectangle(roi_color, (ex, ey),(ex+ew, ey+eh), (0, 255, 0), 2)
  34. #嘴巴
  35. mouth = mouth_cascade.detectMultiScale(roi_gray, 1.5,5)
  36. for (mx, my, mw, mh) in mouth:
  37. cv2.rectangle(roi_color, (mx, my), (mx+mw, my+mh), (0, 0, 255), 2)
  38. #鼻子
  39. nose = nose_cascade.detectMultiScale(roi_gray, 1.2, 5)
  40. for (nx, ny, nw, nh) in nose:
  41. cv2.rectangle(roi_color, (nx, ny), (nx+nw, ny+nh), (255, 0, 255), 2)
  42. #耳朵
  43. leftear = leftear_cascade.detectMultiScale(roi_gray,1.01, 2)
  44. for (lx, ly, lw, lh) in leftear:
  45. cv2.rectangle(roi_color, (lx, ly), (lx+lw, ly+lh), (0, 0, 0), 2)
  46. rightear = rightear_cascade.detectMultiScale(roi_gray, 1.01, 2)
  47. for (rx, ry, rw, rh) in rightear:
  48. cv2.rectangle(roi_color, (rx, ry), (rx+rw, ry+rh), (0, 0, 0), 2)
  49. cv2.imshow('img', img)
  50. cv2.waitKey(0)
  51. cv2.destroyAllWindows()

上效果图:



参考博客:

1. 点击打开链接

2.点击打开链接

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/324710
推荐阅读
相关标签
  

闽ICP备14008679号