OSError:无法识别图像文件"dataSet/.DS_Store" [英] OSError: cannot identify image file 'dataSet/.DS_Store'

查看:519
本文介绍了OSError:无法识别图像文件"dataSet/.DS_Store"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试运行一个面部识别训练器,该训练器会查看人脸jpg图像的文件夹

Hello I am trying to run a facial recognition trainer that looks at a folder of jpg images of faces

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'

文件夹dataSet存在,并且我正在Mac上运行代码,以及Pillow,numpy和cv2的最新版本,我已经在OSError上进行了谷歌搜索,但并没有太多帮助解决此特定问题的方法.有什么想法吗?

The folder dataSet exists and i'm running the code on my mac, and the most recent versions of Pillow, numpy and cv2, I've googled the OSError but not much came up to help this particular problem. Any ideas?

推荐答案

os.listdir()将为您提供目录中的每个文件,包括.DS_Store之类的隐藏文件.在macOS中,.DS_Store是一个隐藏文件(从Finder中隐藏以.开头的任何文件),只要您使用Finder查看目录,它们就会插入目录中,以加快文件图标的加载速度以及保存缩略图大小的首选项,例如该文件夹.您可以在Wikipedia上阅读有关文件 的更多信息.

os.listdir() will give you every single file in the directory, including hidden files like .DS_Store. In macOS, .DS_Store is a hidden file (any file starting with a . is hidden from Finder) inserted in directories whenever you view them with Finder to speed up loading the file icons and saving your preferences for thumbnail sizes and such in that folder. You can read more about the file on Wikipedia.

如果您导航到目录并使用ls -a在终端中列出文件,则可以看到该文件.

You can see the file if you navigate to the directory and list the files in terminal with ls -a.

无论如何,您只需要尝试将其读取为图像文件即可.有无数种方法可以避免这种情况,以下是几种方法:

In any case, you just need to not try and read that as an image file. There are a bazillion ways to avoid this, here's a few:

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来仅捕获具有所需扩展名的文件:

or just use glob to grab files only with the extensions you want:

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

此处*是通配符,表示任何字符序列",因此它将抓取directory/1.jpgdirectory/asdf.jpg以及所有其他以directory/开始并以.jpg结尾的可能性.

Here the * is a wildcard meaning "any sequence of characters" so this will grab directory/1.jpg and directory/asdf.jpg and all other possibilities starting with directory/ and ending with .jpg.

或者只需使用

rm .DS_Store

但这只是一个临时解决方案,因为macOS会在您下次在Finder中查看文件夹时再次插入文件.

but this is only a temporary solution as macOS will insert the file again next time you view the folder in Finder.

这篇关于OSError:无法识别图像文件"dataSet/.DS_Store"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆