我正在尝试使用OpenCV创建一个人脸识别软件,但是我在库中找到的代码是用Python2编写的,有Python3版本吗?
这里有个链接:https://github.com/thecodacus/Face-Recognition
我已经有了一个数据集和训练器的文件夹。
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))))错误:
Traceback (most recent call last):
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 46, in <module>
faces,ids = getImagesAndLabels(path)
File "/Users/user/Desktop/FacialRecognition/02_face_training.py", line 36, in getImagesAndLabels
id = int(os.path.split(imagePath)[-1].split('.')[1])
ValueError: invalid literal for int() with base 10: 'User'发布于 2019-06-11 13:46:14
在该存储库上有一个dataSet目录,其中包含一个名为以下内容的文件:
In [665]: name='Face-Recognition/dataSet/face-1.1.jpg' 应用于该名称,您的代码示例执行以下操作:
In [668]: os.path.split(name)
Out[668]: ('Face-Recognition/dataSet', 'face-1.1.jpg')
In [669]: os.path.split(name)[-1]
Out[669]: 'face-1.1.jpg'
In [670]: os.path.split(name)[-1].split('.')
Out[670]: ['face-1', '1', 'jpg']
In [671]: os.path.split(name)[-1].split('.')[1]
Out[671]: '1'
In [672]: int(os.path.split(name)[-1].split('.')[1])
Out[672]: 1显然,您的文件具有不同的名称格式,在代码需要一个数字的位置包含“User”。
您需要更正文件名,或更改此解析代码。
发布于 2019-09-27 03:21:57
您在数据集中获得的映像名称为User.*somename*,因此请从所有映像名称中删除User。
发布于 2021-01-02 18:31:37
尝试改变格式图像是'face.1.1.jpg‘,然后你可以用这个代码分割圆点
faceID = int(os.path.split(imagePath)[-1].split(".")[2])https://stackoverflow.com/questions/56536910
复制相似问题