删除python中csv文件中的不匹配行

时间:2018-03-23 08:25:32

标签: python image csv

我正在尝试加载有4000张图片的数据集。我有两个文件,一个test_images和csv文件,其标签和类别名称对应于test_images文件夹中的图像。 test_image文件夹中缺少一些图像。当我面对不在图像文件夹中的图像时,我想删除csv文件的行。我尝试但失败任何人都可以建议我如何获得解决方案

import tensorflow as tf
import os
import pandas as pd
from PIL import Image
import numpy as np
from numpy import array
import matplotlib.pyplot as plt 
def get_data(is_test=False):
    data_path = "train"
    if is_test:
        data_path = "test"
    return pd.read_csv('C:/Users\zaid-pc/AnacondaProjects_old/dataset/new short dataset/listing-'+data_path+'.csv', encoding="ISO-8859-1",
                names=['ASIN', 'F', 'IMAGE_URL', 'TITLE', 'AUTHOR', 'ID', 'CAT'])


def loadImages(fnames,is_test):
    path = os.getcwd() + "train_images/"
    if is_test:
        path = os.getcwd() + "test_images/"
    loadedImages = []
    for image in fnames:
        tmp = Image.open(path + image)
        if tmp            
            img = tmp.copy()
            loadedImages.append(img)
        else


        tmp.close()
    return loadedImages


def get_pixels(fnames,is_test):
    imgs = loadImages(fnames, is_test)
    pixel_list = []
    for img in imgs:
        img = img.resize((227, 227), Image.ANTIALIAS)
        arr = np.array(img, dtype="float32")
        pixel_list.append(list(arr))
    return np.array(pixel_list)


def label_from_category(category_id=None):
    label_list = np.zeros(1)
    label_list[category_id] = 1
    return list(label_list)


def features_from_data(data, is_test=False):
    pixels = get_pixels(data.FILENAME, is_test)
    labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
    return pixels, labels



test_data = get_data(is_test=True)
X_test, Y_test = features_from_data(test_data, is_test=True)

我想从csv文件中删除以下缺失的行。

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-2-e4f621a6886c> in <module>()
     52 
     53 test_data = get_data(is_test=True)
---> 54 X_test, Y_test = features_from_data(test_data, is_test=True)
     55 #print(test_x)
     56 #print(test_labels)

<ipython-input-2-e4f621a6886c> in features_from_data(data, is_test)
     45 
     46 def features_from_data(data, is_test=False):
---> 47         pixels = get_pixels(data.FILENAME, is_test)
     48         labels = np.array(data["CATEGORY_ID"].apply(label_from_category).tolist())
     49         return pixels, labels

<ipython-input-2-e4f621a6886c> in get_pixels(fnames, is_test)
     29 
     30 def get_pixels(fnames,is_test):
---> 31         imgs = loadImages(fnames, is_test)
     32         pixel_list = []
     33         for img in imgs:

<ipython-input-2-e4f621a6886c> in loadImages(fnames, is_test)
     21         loadedImages = []
     22         for image in fnames:
---> 23                 tmp = Image.open(path + image)
     24                 img = tmp.copy()
     25                 loadedImages.append(img)

C:\ProgramData\Anaconda34\lib\site-packages\PIL\Image.py in open(fp, mode)
   2541 
   2542     if filename:
-> 2543         fp = builtins.open(filename, "rb")
   2544         exclusive_fp = True
   2545 

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\zaid-pc\\AnacondaProjects_old\3932084578.jpg'

0 个答案:

没有答案