Skip to content

Instantly share code, notes, and snippets.

@datitran
Created December 6, 2016 14:21
Show Gist options
  • Save datitran/12b7171ffc615bda04c5c2e691ad41f4 to your computer and use it in GitHub Desktop.
Save datitran/12b7171ffc615bda04c5c2e691ad41f4 to your computer and use it in GitHub Desktop.
Detect faces function
def detect_faces(image):
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
img = Image.open(StringIO(image))
img_cv2 = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2RGBA)
gray = cv2.cvtColor(img_cv2, cv2.COLOR_RGBA2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
try:
return faces.tolist()
except:
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment