Cell In[4], line 35 32 print("[0] faces trained, Exiting Program".format(len(np.unique(ids)))) 34 if __name__ == '__main__': ---> 35 face_training() Cell In[4], line 22, in face_training() 19 img_gray = Image.open(imagePath).convert('L') 20 img_numpy = np.array(img_gray, 'uint8') ---> 22 user_id = int(os.path.split(imagePath)[-1].split(".")[1]) 23 print(user_id, " ", imagePath) 24 faces = face_detector.detectMultiScale(img_numpy) ValueError: invalid literal for int() with base 10: 'jpg'
时间: 2024-04-26 09:22:38 浏览: 23
这个错误信息显示在执行 `face_training` 函数的时候,出现了一个 `ValueError` 异常。具体来说,这个异常是在第 22 行代码处抛出的,这一行代码试图将图片文件名中的数字部分转换为整数类型,但是却出现了一个无法转换为整数的字符串 'jpg'。
这个问题可能是由于 `imagePath` 变量的值不是一个带有数字后缀的文件名,或者是后缀不是有效的数字格式所导致的。您可以检查一下 `imagePath` 变量的值是否正确,并且确认文件名格式是否符合要求。另外,您还可以在这一行代码之前打印一下 `imagePath` 的值,看看是否存在异常情况。
相关问题
recog.train(faces, np.array(ids))
这段代码是使用 OpenCV 中的人脸识别库进行人脸识别模型的训练。具体解释如下:
- recog 是一个 OpenCV 的人脸识别器对象。
- faces 是一个包含人脸图像数组的列表,每个数组代表一个人脸图像。
- ids 是一个包含相应标签的数组,与 faces 列表中的人脸图像一一对应。
- 使用 recog.train() 函数对 faces 和 ids 进行训练,以构建人脸识别模型。人脸识别器会自动提取每张人脸图像的特征,并使用这些特征来训练模型。
- np.array(ids) 将 ids 列表转换为 numpy 数组,以便能够被传递给 recog.train() 函数。
'''' Training Multiple Faces stored on a DataBase: ==> Each face should have a unique numeric integer ID as 1, 2, 3, etc ==> LBPH computed model will be saved on trainer/ directory. (if it does not exist, pls create one) ==> for using PIL, install pillow library with "pip install pillow" Based on original code by Anirban Kar: https://github.com/thecodacus/Face-Recognition Developed by Marcelo Rovai - MJRoBot.org @ 21Feb18 ''' import cv2 import numpy as np from PIL import Image import os # Path for face image database path = 'dataset' recognizer = cv2.face.LBPHFaceRecognizer_create() detector = cv2.CascadeClassifier("haarcascade_frontalface_default.xml"); # function to get the images and label data def getImagesAndLabels(path): imagePaths = [os.path.join(path,f) for f in os.listdir(path)] faceSamples=[] ids = [] for imagePath in imagePaths: PIL_img = Image.open(imagePath).convert('L') # convert it to grayscale img_numpy = np.array(PIL_img,'uint8') id = int(os.path.split(imagePath)[-1].split(".")[1]) faces = detector.detectMultiScale(img_numpy) for (x,y,w,h) in faces: faceSamples.append(img_numpy[y:y+h,x:x+w]) ids.append(id) return faceSamples,ids print ("\n [INFO] Training faces. It will take a few seconds. Wait ...") faces,ids = getImagesAndLabels(path) recognizer.train(faces, np.array(ids)) # Save the model into trainer/trainer.yml recognizer.write('trainer/trainer.yml') # recognizer.save() worked on Mac, but not on Pi # Print the numer of faces trained and end program print("\n [INFO] {0} faces trained. Exiting Program".format(len(np.unique(ids)))) 翻译各语句
这段代码是用于训练一个人脸数据库的,其中每张人脸都有一个唯一的数字ID。使用LBPH算法来计算模型,模型将被保存在trainer/目录中。如果该目录不存在,则需要创建它。需要安装Pillow库才能使用PIL。代码基于Anirban Kar的原始代码开发,由Marcelo Rovai - MJRoBot.org在2018年2月21日开发。
代码中还定义了一个函数getImagesAndLabels,用于获取图像和标签数据。该函数会遍历指定路径下的所有图片,将其转换为灰度图像,并使用分类器检测人脸。最后,将所有的人脸图像和对应的ID返回。
在代码末尾,使用获取的人脸图像和ID进行训练,并将训练好的模型保存在trainer/trainer.yml文件中。
代码输出训练完成后的人脸数量,并退出程序。
相关推荐
![mat](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)