Ошибка:
Трассировка (последний вызов был последним): файл "C: \ Users \ ACER \ Desktop \ ПРОЕКТИТЬ ВСЕ РЕСУРС \ Внедрение \ PYTHON FILES \ training_set.py" , строка 34, в идентификаторах, face = getImagesWithID ('C: / Users / ACER / Desktop / ПРОЕКТИТЬ ВСЕ РЕСУРС / Внедрение / PYTHON FILES / DataSets') # Вызов функции File "C: \ Users \ ACER \ Desktop \ ПРОЕКТИТЬ ВЕСЬ РЕСУРС \ Внедрение \ PYTHON FILES \ training_set.py ", строка 22, в getImagesWithID cv2.imwrite ('C: / Пользователи / ACER / Рабочий стол / ПРОЕКТИТЬ ВЕСЬ РЕСУРС / Внедрение / PYTHON ФАЙЛЫ / Training Images / training.jpg ', alignFace) cv2.error: OpenCV (4.2.0) C: \ projects \ opencv-python \ opencv \ modules \ imgcodecs \ src \loadave. cpp: 715: ошибка : (-215: утверждение не выполнено)! _Img.empty () в функции 'cv :: imwrite'
Код выглядит следующим образом
python
import os #import for taking the imagePaths
import cv2 #openCV
import numpy as np #for numpy arrays
from PIL import Image #pillow
import openface
#Path to dlib's face predictor
dlibFacePredictor = 'shape_predictor_68_face_landmarks.dat'
#Local Binary Patterns Histograms
recognizer = cv2.face.LBPHFaceRecognizer_create()
#Default image dimension
imgDim = 96
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)
#Converting colored and GrayScale images into bilevel images using Floyd-Steinberg dither
faceImg = Image.open('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training Images/training.jpg').convert('L')
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
#Calling the function
Ids, faces = getImagesWithID('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/DataSets')
#Training the faces
recognizer.train(faces, np.array(Ids))
#Saving the yml file
recognizer.save('C:/Users/ACER/Desktop/PROJECT ALL RESOURCE/Implementation/PYTHON FILES/Training/trainingData.yml')
cv2.destroy() #Closing all the opened windows