Pur*_*e82 2 opencv python-imaging-library
我正在尝试运行一个面部识别训练器,它会查看面部 jpg 图像的文件夹
import os # importing the OS for path
import cv2 # importing the OpenCV library
import numpy as np # importing Numpy library
from PIL import Image # importing Image library
EigenFace = cv2.face.EigenFaceRecognizer_create(15) # creating EIGEN FACE RECOGNISER
FisherFace = cv2.face.FisherFaceRecognizer_create(2) # Create FISHER FACE RECOGNISER
LBPHFace = cv2.face.LBPHFaceRecognizer_create(1, 1, 7,7) # Create LBPH FACE RECOGNISER
path = 'dataSet' # path to the photos
def getImageWithID (path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
FaceList = []
IDs = []
for imagePath in imagePaths:
faceImage = Image.open(imagePath).convert('L') # Open image and convert to gray
faceImage = faceImage.resize((110,110)) # resize the image so the EIGEN recogniser can be trained
faceNP = np.array(faceImage, 'uint8') # convert the image to Numpy array
ID = int(os.path.split(imagePath)[-1].split('.')[1]) # Retrieve the ID of the array
FaceList.append(faceNP) # Append the Numpy Array to the list
IDs.append(ID) # Append the ID to the IDs list
cv2.imshow('Training Set', faceNP) # Show the images in the list
cv2.waitKey(1)
return np.array(IDs), FaceList # The IDs are converted in to a Numpy array
IDs, FaceList = getImageWithID(path)
Run Code Online (Sandbox Code Playgroud)
这又返回错误
Traceback (most recent call last):
File "/Users/jef/PycharmProjects/testProject/Python/Trainer_All.py", line 28, in <module>
IDs, FaceList = getImageWithID(path)
File "/Users/jef/PycharmProjects/testProject/Python/Trainer_All.py", line 19, in getImageWithID
faceImage = Image.open(imagePath).convert('L') # Open image and convert to gray
File "/Users/jef/venv1/lib/python3.6/site-packages/PIL/Image.py", line 2452, in open
% (filename if filename else fp))
OSError: cannot identify image file 'dataSet/.DS_Store'
Run Code Online (Sandbox Code Playgroud)
文件夹 dataSet 存在,我正在我的 mac 上运行代码,以及最新版本的 Pillow、numpy 和 cv2,我已经用 google 搜索了 OSError 但没有太多内容来帮助解决这个特定问题。有任何想法吗?
os.listdir()将为您提供目录中的每个文件,包括隐藏文件,例如.DS_Store. 在 macOS 中,.DS_Store是一个隐藏文件(任何以 a 开头的文件.在 Finder 中都是隐藏的),每当您使用 Finder 查看目录时都会插入目录中,以加快加载文件图标的速度,并将缩略图大小等首选项保存在该文件夹中。您可以在 Wikipedia 上阅读有关该文件的更多信息。
如果您导航到该目录并在终端中使用 .list 文件列出文件,您就可以看到该文件ls -a。
无论如何,您只需要不要尝试将其作为图像文件读取。有无数种方法可以避免这种情况,以下是一些:
for imagePath in imagePaths:
if imagePath == directory + '.DS_Store':
continue
# rest of your program
Run Code Online (Sandbox Code Playgroud)
或者
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
if directory + '.DS_Store' in imagePaths:
imagePaths.remove(directory + '.DS_Store')
Run Code Online (Sandbox Code Playgroud)
或者仅用于glob抓取具有您想要的扩展名的文件:
import glob
imagePaths = [f for f in glob.glob(directory+'*.jpg')] # or .png, .tif, etc
Run Code Online (Sandbox Code Playgroud)
这里的*是一个通配符,意思是“任何字符序列”,因此这将抓住directory/1.jpg和以及以 开头和结尾的directory/asdf.jpg所有其他可能性。directory/.jpg
或者只是将其从终端中的目录中删除
rm .DS_Store
Run Code Online (Sandbox Code Playgroud)
但这只是一个临时解决方案,因为 macOS 会在您下次在 Finder 中查看该文件夹时再次插入该文件。