一:具体描述

具体程序代码:

import numpy as np
import pandas as pd
import os, sys
from sklearn.decomposition import PCA
from PIL import Image, ImageShow

# 数据集
dataSetsPath = "D:/Download/orl_faces/"
data_dict = {"dirName": [], "dirPath":[], "imgArr":[]}
for ix, file in enumerate(os.walk(dataSetsPath)):
        dir_path, dir_name, content_name = file
        file_name = dir_path.split("/")[-1]
        if 's'in file_name:
            for content in content_name:
                file_dir = dir_path + "/" + content
                tmp_image = Image.open(file_dir)
                img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
                # 存储
                data_dict['dirName'].append(file_name)
                data_dict['dirPath'].append(file_dir.split("/")[-1])
                data_dict['imgArr'].append(img_arr)
                
# dict to DataFrame
data_df = pd.DataFrame(data_dict)
data_df.query("dirName in ['s1']")
# print(data_df.head(5))


错误是这样的

UnidentifiedImageErrorTraceback (most recent call last)
Cell In[30], line 16
     14 for content in content_name:
     15     file_dir = dir_path + "/" + content
---> 16     tmp_image = Image.open(file_dir)
     17     img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
     18     # 存储

File D:\ProgramData\anaconda3\lib\site-packages\PIL\Image.py:3283, in open(fp, mode, formats)
   3281     warnings.warn(message)
   3282 msg = "cannot identify image file %r" % (filename if filename else fp)
-> 3283 raise UnidentifiedImageError(msg)

UnidentifiedImageError: cannot identify image file 'D:/Download/orl_faces/orl_faces/eigen.txt'

需要读取文件如下图所示:

这是一个压缩包,为了实现需求,所以需要先解压

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_ide

解压的截图如下:

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_02


代码中的路径是这样的,

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_数据集_03

以为是对的,寻找了报错处,发现代码一点也没错。问题就是在路径上

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_数据集_04

最后打开解压文件,在解压文件中可以看出

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_ide_05

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_ide_06

最后发现解压的文件夹下还有一层吗,最后将代码改了一下,然后就运行成功了

修改后的代码如下:

import numpy as np
import pandas as pd
import os, sys
from sklearn.decomposition import PCA
from PIL import Image, ImageShow

# 数据集
dataSetsPath = "D:/Download/orl_faces/orl_faces/"
data_dict = {"dirName": [], "dirPath":[], "imgArr":[]}
for ix, file in enumerate(os.walk(dataSetsPath)):
        dir_path, dir_name, content_name = file
        file_name = dir_path.split("/")[-1]
        if 's'in file_name:
            for content in content_name:
                file_dir = dir_path + "/" + content
                tmp_image = Image.open(file_dir)
                img_arr = np.asarray(tmp_image).reshape(1, -1).astype('float').tolist()[0]
                # 存储
                data_dict['dirName'].append(file_name)
                data_dict['dirPath'].append(file_dir.split("/")[-1])
                data_dict['imgArr'].append(img_arr)
                
# dict to DataFrame
data_df = pd.DataFrame(data_dict)
data_df.query("dirName in ['s1']")
# print(data_df.head(5))

运行结果如下图所示:

                   利用jupyter实现程序时报错,显示UnidentifiedImageErrorTraceback (most recent call last) Cell I_Image_07