OSERROR:无法识别图像文件“数据集/ .DS_Store”(OSError: cannot i

2019-10-29 03:27发布

你好我想运行一个面部识别教练,着眼于脸部的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])    # Retreave 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)

这反过来又返回错误

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'

该文件夹中的数据集存在,我在我的Mac上运行的代码,以及最近的枕头,numpy的和CV2的版本,我GOOGLE了OSERROR但没有太多上前帮助这个特殊的问题。 有任何想法吗?

Answer 1:

os.listdir()会给你的每一个文件的目录,包括隐藏文件,如.DS_Store 。 在MacOS上, .DS_Store是一个隐藏的文件(任意文件开始了.从Finder中隐藏)插入目录的,只要你与Finder查看他们加快加载文件的图标,节省您的喜好缩略图大小,这样该文件夹中。 你可以阅读更多有关文件的维基百科 。

你可以看到该文件,如果您导航到该目录,并列出与终端的文件ls -a

在任何情况下,你只需要不尝试和读取为图像文件。 有一个bazillion的方式来避免这种情况,这里有几个:

for imagePath in imagePaths:
    if imagePath == directory + '.DS_Store':
        continue
    # rest of your program

要么

imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
if directory + '.DS_Store' in imagePaths:
    imagePaths.remove(directory + '.DS_Store')

或者只是使用glob抢文件给你想要的扩展:

import glob
imagePaths = [f for f in glob.glob(directory+'*.jpg')]  # or .png, .tif, etc

这里的*是通配符,意思是“任何字符序列”,所以这会抢directory/1.jpgdirectory/asdf.jpg并开始与所有其他可能性directory/和结尾.jpg

或者只是从你的目录与终端删除

rm .DS_Store

但这只是一个临时的解决方案,MacOS将会将您下次在Finder中查看该文件夹的时间再次插入的文件。



文章来源: OSError: cannot identify image file 'dataSet/.DS_Store'