Face regocnition in a photo with opencv3.0 and Python3.4

Opencv provides the cascade which has already learnt the human face. Cool.


image = cv2.imread(r'path/to/image')
image = cv2.resize(image,(0,0), fx=0.1, fy=0.1) // resize image if the image is too big to take long time for the face detection
faceCascae = faceCascade = cv2.CascadeClassifier(r'/home/watanabe/opencv/data/haarcascades/haarcascade_frontalface_default.xml') // set your cascade path
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) // turn image into gray scale
                                                 
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.02,
    minNeighbors=5,
    minSize=(30, 30)
#    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)

print("Found {0} faces!".format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("Faces found" ,image)
cv2.imwrite('/home/watanabe/Pictures/facefound.png', image)
cv2.waitKey(0)








https://realpython.com/blog/python/face-recognition-with-python/

コメント

人気の投稿