Skip to content

Instantly share code, notes, and snippets.

@butaji
Created January 7, 2017 10:50
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 butaji/00d2dc3e06eef2125593f6e9cdf8619a to your computer and use it in GitHub Desktop.
Save butaji/00d2dc3e06eef2125593f6e9cdf8619a to your computer and use it in GitHub Desktop.
import logging
import cv2
import numpy
import glob, os
faceClassifier = cv2.CascadeClassifier()
faceClassifier.load('haarcascade_frontalface_default.xml')
imageProbes = []
typeProbes = []
trainShape = (100,110)
def fillFaces(facesList,typesList, directory, typeOfFace):
for img in glob.glob(directory):
print "Loading ", img
try:
i = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
faces = faceClassifier.detectMultiScale(i,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
for face in faces:
faceImage = i[face[1]:face[1]+face[3], face[0]:face[0]+face[2]]
faceImage = cv2.resize(faceImage, trainShape)
imageProbes.append(numpy.asarray(faceImage, dtype=numpy.uint8))
typeProbes.append(typeOfFace)
except Exception as er:
print er
fillFaces(imageProbes, typeProbes, "y/*", 1)
fillFaces(imageProbes, typeProbes, "n/*", 0)
recognizer = cv2.createFisherFaceRecognizer()
recognizer.train(
numpy.asarray(imageProbes),
numpy.asarray(typeProbes))
for img in glob.glob("t/*"):
print "Testing ", img
i = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
faces = faceClassifier.detectMultiScale(i,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
for face in faces:
faceImage = i[face[1]:face[1]+face[3], face[0]:face[0]+face[2]]
faceImage = cv2.resize(faceImage, trainShape)
label, confidence = recognizer.predict(faceImage)
print label, confidence
cv2.imshow('image',faceImage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment