Advertisement
here2share

# cv2_face_detect.py

Nov 22nd, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. # cv2_face_detect.py
  2.  
  3. import cv2
  4. import sys
  5.  
  6. # Get user supplied values
  7. imagePath = sys.argv[1]
  8. cascPath = "haarcascade_frontalface_default.xml"
  9.  
  10. # Create the haar cascade
  11. faceCascade = cv2.CascadeClassifier(cascPath)
  12.  
  13. # Read the image
  14. image = cv2.imread(imagePath)
  15. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  16.  
  17. # Detect faces in the image
  18. faces = faceCascade.detectMultiScale(
  19.     gray,
  20.     scaleFactor=1.1,
  21.     minNeighbors=5,
  22.     minSize=(30, 30),
  23.     flags = cv2.cv.CV_HAAR_SCALE_IMAGE
  24. )
  25.  
  26. print("Found {0} faces!".format(len(faces)))
  27.  
  28. # Draw a rectangle around the faces
  29. for (x, y, w, h) in faces:
  30.     cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
  31.  
  32. cv2.imshow("Faces found", image)
  33. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement