Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to fix the error cv2.error: /home/gaurav/opencv/modules/core/src/matrix.cpp:984: error: (-13) The matrix is not continuous, thus its number of rows can not be changed in function reshape

I am new to Opencv and was trying out some face recognition tutorials online.

The problem is the faces detected in the images are of different sizes.So I am passing numpy arrays(of the faces detected) of same size to the EigenFaceRecognizer.But it gives the aforementioned error.Can you guys suggest how to fix it?

#!/usr/bin/python

# Import the required modules
import cv2, os
import numpy as np
from PIL import Image

# For face detection we will use the Haar Cascade provided by OpenCV.
cascadePath = "/home/gaurav/opencv/data/haarcascades/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)

recognizer = cv2.face.createEigenFaceRecognizer()
#sizetuple=(320,243)
def get_images_and_labels(path):

    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 = []
    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,scaleFactor=1.1)
        # 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 + 130, x: x + 130])
            labels.append(nbr)
            cv2.imshow("Adding faces to traning set...", image[y: y + 130, x: x + 130])
            cv2.waitKey(50)

    # return the images list and labels list
    return images, labels

# Path to the Yale Dataset
path = './yalefaces'
# Call the get_images_and_labels function and get the face images and the 
# corresponding labels
images, labels = get_images_and_labels(path)
cv2.destroyAllWindows()
for one in images:
    print "size:",one.size
#Perform the tranining
recognizer.train(images, np.array(labels))

# Append the images with the extension .sad into image_paths
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:
        nbr_predicted, conf = recognizer.predict(predict_image[y: y + 130, x: x + 130])
        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)
        cv2.imshow("Recognizing Face", predict_image[y: y + 130, x: x + 130])
        cv2.waitKey(1300)