Skip to content

Instantly share code, notes, and snippets.

@PeaceAndHiLight
Last active February 18, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeaceAndHiLight/54b9004f68e49c81e0ed to your computer and use it in GitHub Desktop.
Save PeaceAndHiLight/54b9004f68e49c81e0ed to your computer and use it in GitHub Desktop.
OpenCV Face Detect
#coding: utf-8
import cv2
import numpy as np
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('image.jpg', cv2.IMREAD_COLOR)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face = faceCascade.detectMultiScale(gray, 1.1, 3)
if len(face) > 0:
for rect in face:
cv2.rectangle(img, tuple(rect[0:2]), tuple(rect[0:2]+rect[2:4]), (0, 0,255), thickness=2)
else:
print "no face"
cv2.imwrite('detected.jpg', img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment