OSError: cannot identify image file 'dataSet/.

2019-08-19 06:03发布

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)

which in turn returns the error

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'

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?

1条回答
我想做一个坏孩纸
2楼-- · 2019-08-19 06:49

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.

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

or

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

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

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.

Or just remove it from your directory in terminal with

rm .DS_Store

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

查看更多
登录 后发表回答