Advertisement
steve-shambles-2109

192-Face Counter

Oct 30th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. """
  2. Python code snippets vol 39:
  3. stevepython.wordpress.com
  4.  
  5. 192-Face Counter
  6.  
  7. requirements: pip3 install opencv-python
  8. Also the file haarcascade_frontalface_default.xml needs to be in the
  9. current directory where you run the source code from.
  10. Do a search on your computer for it
  11.  
  12. source:
  13. https://techtutorialsx.com/2017/05/02/python-opencv-face-detection-and-counting/
  14. """
  15. import cv2
  16. face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  17.  
  18. image = cv2.imread('crowd2.jpg')
  19. grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  20.  
  21. faces = face_cascade.detectMultiScale(grayImage)
  22.  
  23. print(type(faces))
  24.  
  25. if len(faces) == 0:
  26.     print("No faces found")
  27.  
  28. else:
  29.     print(faces)
  30.     print(faces.shape)
  31.     print("Number of faces detected: " + str(faces.shape[0]))
  32.  
  33.     for (x, y, w, h) in faces:
  34.         cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 1)
  35.  
  36.     cv2.rectangle(image, ((0, image.shape[0] -25)),
  37.                   (270, image.shape[0]), (255, 255, 255), -1)
  38.     cv2.putText(image, "Number of faces detected: " + str(faces.shape[0]),
  39.                 (0, image.shape[0] -10),
  40.                 cv2.FONT_HERSHEY_TRIPLEX, 0.5, (0, 0, 0), 1)
  41.  
  42.     cv2.imshow('Image with faces', image)
  43.     cv2.waitKey(0)
  44.     cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement