Advertisement
Jim421616

face_detection.py

Oct 14th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. import numpy as np
  2. import cv2
  3. faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
  4. cap = cv2.VideoCapture(0)
  5. cap.set(3,640) # set Width
  6. cap.set(4,480) # set Height
  7. print("Press Esc to quit")
  8. while True:
  9. ret, img = cap.read()
  10. img = cv2.flip(img, -1)
  11. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  12. faces = faceCascade.detectMultiScale(
  13. gray,
  14. scaleFactor=1.2,
  15. minNeighbors=5,
  16. minSize=(20, 20)
  17. )
  18. for (x,y,w,h) in faces:
  19. cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
  20. roi_gray = gray[y:y+h, x:x+w]
  21. roi_color = img[y:y+h, x:x+w]
  22. cv2.imshow('video',img)
  23. k = cv2.waitKey(30) & 0xff
  24. if k == 27: # press 'ESC' to quit
  25. break
  26. cap.release()
  27. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement