face_detection.py 720 B

1234567891011121314151617181920212223242526272829303132
  1. import cv2
  2. faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
  3. # grab the reference to the webcam
  4. vs = cv2.VideoCapture(0)
  5. # keep looping
  6. while True:
  7. # grab the current frame
  8. ret, frame = vs.read()
  9. # if we are viewing a video and we did not grab a frame,
  10. # then we have reached the end of the video
  11. if frame is None:
  12. break
  13. faces = faceCascade.detectMultiScale(frame)
  14. for (x, y, w, h) in faces:
  15. cv2.rectangle(frame, (x, y), (x+w, y+h), (255,0,0), 2)
  16. # show the frame to our screen
  17. cv2.imshow("Video", frame)
  18. key = cv2.waitKey(1) & 0xFF
  19. # if the 'q' or ESC key is pressed, stop the loop
  20. if key == ord("q") or key == 27:
  21. break
  22. # close all windows
  23. cv2.destroyAllWindows()