0

I am able to find the faces and save them in my local directory using python and open cv as per code below from video

import cv2
import numpy as np
import os

vc = cv2.VideoCapture('new1.avi')
c=1

if vc.isOpened():
    rval , frame = vc.read()
else:
    rval = False

while rval:
 rval, frame = vc.read()
 cv2.imwrite(str(c) + '.jpg',frame)
 image_name=str(c)+'.jpg'
 cascPath = "haarcascade_frontalface_default.xml"
 faceCascade = cv2.CascadeClassifier(cascPath)

 image=cv2.imread(image_name)
 gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


 faces = faceCascade.detectMultiScale(
  gray,
  scaleFactor=1.2,
  minNeighbors=5,
  minSize=(30, 30),
  flags = cv2.cv.CV_HAAR_SCALE_IMAGE
 )

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

 if len(faces)>=1:
  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.waitKey(0)
  
  
 else:
  a="rm "+image_name
  os.popen(a)

 c = c + 1
 cv2.waitKey(1)
        
vc.release()

But now i want to get identification of that person which has face in that video....

How can i define the person's identification?

Like to scan the face and match it into my local face database and if match found give the name and etc etc

Nisarg
  • 463
  • 9
  • 26
  • You need to train a classifier first (like SVM, NNs, etc), and do the prediction job based on it. The performance depends on a lot of factors, like your training data, classifier type & params, and your algorithm for facial feature extraction, etc. Search for it first, there are plenty of examples online. –  Mar 07 '16 at 07:07

2 Answers2

1

To differentiate between people in photos is not a trivial task, but there are some examples out there. As mentioned by Derman in an earlier comment the best way is to use machine learning to teach the program what different persons faces looks like. One way is to manually find and extract features in peoples faces, such as the distance between eyes ratio to distance between eyes and mouth and so on. This would though need attention to the effects of lens distortion and perspective. There is multiple research papers discussing the best techniques, like this paper using eigen vectors from a set of faces to find most probable match Face Recognition Using Eigen Faces

There is a machine learning toolbox for Python which is called scikit-learn which implements support for classification, regression, clustering and so on. You can use it to train neural networks and support vector machines among others. Here is a complete example of how to implement the Eigenface method using SVM with scikit-learn and python: Complete implementation using Python

Daniel Falk
  • 522
  • 4
  • 16
0

You can use Either EigenFaceRecognizer or FisherFaceRecognizer or LBHP

All these three algorithms are inbuilt in python

# Create a recognizer object  
recognizer = cv2.face.createEigenFaceRecognizer()
# But Remember for EigenFaces all the images whether training or testing has to be of same shape
    #==========================================================================
    # get_images_and_labels function will give us list of images and list of labels to train our recognizer that we created in the first line
    # function requires the path of the directory where all the images is stored
    #===========================================================================
    def get_images_and_labels(path):
        # Append all the absolute image paths in a list image_paths
        image_paths = [os.path.join(path, f) for f in os.listdir(path) if not 
        f.endswith('.sad')]
        # images will contains face images
        images = []
        # labels will contains the label that is assigned to the image
        labels = []
        final_images = []
        largest_image_size = 0
        largest_width = 0
        largest_height = 0

        for image_path in image_paths:
           # Read the image and convert to grayscale
           image_pil = Image.open(image_path).convert('L')
           # Convert the image format into numpy array
           image = np.array(image_pil, 'uint8')
           # Get the label of the image
           nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
           # Detect the face in the image
           faces = faceCascade.detectMultiScale(image)
           # If face is detected, append the face to images and the label to labels

        for (x, y, w, h) in faces:
            images.append(image[y: y + h, x: x + w])
            labels.append(nbr)
            cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
            cv2.waitKey(50)
           # return the images list and labels list

        for image in images:
            if image.size > largest_image_size:
                largest_image_size = image.size
        largest_width, largest_height = image.shape

        for image in images:
            image = cv2.resize(image, (largest_width, largest_height), interpolation=cv2.INTER_CUBIC)
            final_images.append(image)

    return final_images, labels, largest_width, largest_height

#===================================================================
# Perform the tranining
# trainer takes two parameters as input
# first parameter is the list of images
# second parameter is a numpy array of their corresponding labels
#===================================================================
recognizer.train(images, np.array(labels)) # training takes as input the   list 



image_paths = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.sad')]
for image_path in image_paths:
    predict_image_pil = Image.open(image_path).convert('L')
    predict_image = np.array(predict_image_pil, 'uint8')
    faces = faceCascade.detectMultiScale(predict_image)
    for (x, y, w, h) in faces:
        result = cv2.face.MinDistancePredictCollector()
        predict_image = predict_image[y: y + h, x: x + w] 
        predict_image = cv2.resize(predict_image, (max_width, max_heigth), interpolation=cv2.INTER_CUBIC)

        # =========================================================  
        # predict method will give us the prediction
        # we will get the label in the next statement
        # predicted_image is the image that you want to recognize 
        # =========================================================  
        recognizer.predict(predict_image, result, 0) # this statement will give the prediction

        # ========================================== 
        # This statement below will give us label 
        # ========================================== 
        nbr_predicted = result.getLabel() 

        # ========================================== 
        # conf will tell us how much confident our recognizer is in it's prediction
        # ========================================== 
        conf = result.getDist()
        nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
        if nbr_actual == nbr_predicted:
           print("{} is Correctly Recognized with confidence {}".format(nbr_actual, conf))
        else:
            print("{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted))
sys.exit()
Jai
  • 3,211
  • 2
  • 17
  • 26