This is a code to train a dataset of faces captured using OpenCV in python. I get error like this. Help appreciated
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
your
alignedFace
img is invalid / empty, please check the outcome ofimage = cv2.imread(imagePath)
it's probably already from there !unfortunately, we cannot see, what
align.align
doesI did not get it. please help me sir
align(imgDim, rgbImg, bb=None, landmarks=None, landmarkIndices=INNER_EYES_AND_BOTTOM_LIP) Transform and align a face in an image.
duplicate post?
^^ yep, duplicate, i simply deleted the other one
Can u provide me some solution to it. It will be helpful
@VigneshSuresh
align(...)
-- 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