Ask Your Question

How to fix the error : The matrix is not continuous, thus its number of rows can not be changed in function reshape

asked 2016-02-06 11:08:27 -0500

bilbo gravatar image

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?


# 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()
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 ='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])
            cv2.imshow("Adding faces to traning set...", image[y: y + 130, x: x + 130])

    # 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)
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 ='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)
            print "{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted)
        cv2.imshow("Recognizing Face", predict_image[y: y + 130, x: x + 130])
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2016-09-16 03:28:24 -0500

Alyc gravatar image

Maybe you should check your csv file,for example you can recreate a csv file; I get a error like this :what(): /home/pi/Resource/opencv-2.4.9/modules/core/src/matrix.cpp:802: error: (-13) The matrix is not continuous, thus its number of rows can not be changed in function reshape At the begining I thought my code has error,but after I recreate the csv file today the error has gone. I'm sorry my English is poor.It's my first time to reply ,I'm so excited .haha

edit flag offensive delete link more

answered 2016-02-07 02:25:20 -0500

berak gravatar image

updated 2016-02-07 02:30:31 -0500

EigenFaces cannot predict on a slice of an image, a slice does not have 'continuous data', so you need something like :

    cropped = predict_image[y: y + 130, x: x + 130].copy()

then, due to a current bug, you cannot get confidence and label at the same time, only the label:

    nbr_predicted = recognizer.predict(cropped)

(one way to get the confidence back, would be , to remove the CV_WRAP here and rerun cmake && make && make install)

edit flag offensive delete link more


also, you should rather crop your faces like this:

...x,y,w,h from detection
cropped = cv2.resize(img[y:y+h,x:x+w], (130,130))

your current way is chopping off the mouth, and the right side far too often

berak gravatar imageberak ( 2016-02-07 02:28:02 -0500 )edit

Question Tools

1 follower


Asked: 2016-02-06 10:44:28 -0500

Seen: 3,181 times

Last updated: Feb 07 '16