Ask Your Question
0

This is a code to train a dataset of faces captured using OpenCV in python. I get error like this. Help appreciated

asked 2020-02-16 04:53:37 -0600

VigneshSuresh gravatar image

updated 2020-02-16 05:38:14 -0600

berak gravatar image

The error is:-

Traceback (most recent call last):
  File "C:\Users\ACER\Desktop\PROJECT ALL RESOURCE\Implementation\PYTHON FILES\training_set.py", line 34, in <module>
    Ids, faces = getImagesWithID('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/DataSets')                                              # Calling the function
  File "C:\Users\ACER\Desktop\PROJECT ALL RESOURCE\Implementation\PYTHON FILES\training_set.py", line 22, in getImagesWithID
    cv2.imwrite('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training Images/training.jpg', alignedFace)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'cv::imwrite'

and the code:

import os                                                                       # import for taking the imagePaths
import cv2                                                                      # openCV
import numpy as np                                                              # for numpy arrays
from PIL import Image                                                           # pillow
import openface
dlibFacePredictor = 'shape_predictor_68_face_landmarks.dat'                     # Path to dlib's face predictor
recognizer = cv2.face.LBPHFaceRecognizer_create()                                     # Local Binary Patterns Histograms
imgDim = 96                                                                     # Default image dimension
align = openface.AlignDlib(dlibFacePredictor)

def getImagesWithID(path):
    imageFolders = [os.path.join(path, f) for f in os.listdir(path)]    # Joining './dataset' and '<image names>'
    faces = []                                                                  # Empty array for faces
    Ids = []
    for imageFolder in imageFolders:
        imagePaths = [os.path.join(imageFolder, f) for f in os.listdir(imageFolder)]
        for imagePath in imagePaths:
            image = cv2.imread(imagePath)
            rgbImg = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            bb = align.getLargestFaceBoundingBox(rgbImg)
            alignedFace = align.align(imgDim, rgbImg, bb=None, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
            cv2.imwrite('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training Images/training.jpg', alignedFace)
            faceImg = Image.open('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training Images/training.jpg').convert('L')        # Converting colored and GrayScale images into bilevel images using Floyd-Steinberg dither
            faceNp = np.array(faceImg, 'uint8')                                     # Converting face array into numpy array
            ID = int(os.path.split(imagePath)[-1].split('.')[1])                    # Check this again
            faces.append(faceNp)                                                    # adding the dilevel face into faces array
            Ids.append(ID)                                                          # index of ID and faceNp is same in both arrays
            cv2.imshow("Training", faceNp)                                          # Showing the faces which are getting trained
            cv2.waitKey(10)
                                                                        # Empty array for Person Ids
                                                             # Waiting time id 10 milisecond
    return Ids, faces

Ids, faces = getImagesWithID('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/DataSets')                                              # Calling the function
recognizer.train(faces, np.array(Ids))                                          # Training the faces
recognizer.save('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training/trainingData.yml')                                # Saving the yml file
cv2.destroy()                                                         # Closing all the opened windows
edit retag flag offensive close merge delete

Comments

your alignedFace img is invalid / empty, please check the outcome of image = cv2.imread(imagePath) it's probably already from there !

unfortunately, we cannot see, what align.align does

berak gravatar imageberak ( 2020-02-16 05:41:21 -0600 )edit

I did not get it. please help me sir

VigneshSuresh gravatar imageVigneshSuresh ( 2020-02-16 05:54:24 -0600 )edit

align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP) Transform and align a face in an image.

VigneshSuresh gravatar imageVigneshSuresh ( 2020-02-16 05:54:55 -0600 )edit
1
LBerger gravatar imageLBerger ( 2020-02-16 06:32:38 -0600 )edit
1

^^ yep, duplicate, i simply deleted the other one

berak gravatar imageberak ( 2020-02-16 07:30:44 -0600 )edit

Can u provide me some solution to it. It will be helpful

VigneshSuresh gravatar imageVigneshSuresh ( 2020-02-16 10:27:44 -0600 )edit

@VigneshSureshalign(...) -- how would we know, what it does ?

somehow, your input to imwrite() is invalid (and it might have happened a long way up your prog logic, most vulnerable place is reading in an image), so please check

berak gravatar imageberak ( 2020-02-16 12:15:40 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-03-03 17:40:23 -0600

lnx gravatar image

alignedface is invalid or pointing to null make sure from where you are getting alignedface is correctly returning the image or image array

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-02-16 04:53:37 -0600

Seen: 1,123 times

Last updated: Mar 03 '20