无法使用python中的opencv读取图像

时间:2018-05-01 08:49:32

标签: python opencv

你好我正在尝试训练这个模型,用于检测一个人的眼睛是打开还是关闭。我想在python中使用opencv库从dataset文件夹中读取.jpg图像。 代码是

    def load_images(self,dataframe):
    output = np.zeros((len(dataframe),self.image_shape[0],self.image_shape[1]))
    for index,row in dataframe.iterrows():
        img = cv2.imread(row["file_location"].replace(row["file_location"],"home/samuel/Desktop/eye-closed/dataset_B_FacialImages"))
        if img is None:
            print ("Cv2 error: Unable to read from '"+row["file_location"].replace(row["file_location"],"home/samuel/dataset/eye-closed/dataset_B_Eye_Images/closedLeftEyes/*.jpg")+"'")
            continue
        img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        output[index] = img 
    return output

我正在给我错误

Cv2 error: Unable to read from 'home/samuel/dataset/eye-closed/dataset_B_Eye_Images/closedLeftEyes/*.jpg'

我不知道出了什么问题。任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分:0)

数据集中有一个pickle文件,即包含数据集图像的url的字典。我可以使用此代码使用pandas查看它。

import pickle as pkl
import pandas as pd
if __name__ == '__main__':
    pkl.HIGHEST_PROTOCOL = 2
    df = pd.read_pickle(r"/home/samuel/dataset/eye-closed/dataset_B_FacialImages/train.pkl")
    print(df)

其中提供以下输出

1896  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1897  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       1
1898  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1899  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       1
1900  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1901  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1902  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1903  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1904  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       1
1905  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       1
1906  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1907  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
1908  /home/mtk/dataset/eye-closed/dataset_B_FacialI...       0
[1909 rows x 2 columns]

我想更改这个pickle文件,以便名称/ home / mtk / ...更改为/ home / samuel / ...我该怎么办?