python - 无法从 CSV 文件中读取文件名并在另一个文件夹中找到它
问题描述
我需要遍历包含以下结构的 CSV 文件的条目:
图像文件 | 班级
img1.png |
img2.png |
...
第一列包含文件名,另一列Class为空。所有这些文件都保存在路径PATH中的文件夹中。我需要
- 遍历 CSV 文件中的每个文件名
- 从位置 PATH 访问文件
- 将其传递给模型并进行预测(预测类)
- 将预测的类保存到正在处理的图像行中的另一列。
我已经设法对图像进行预测,但我无法对 CSV 文件中提到的给定文件做同样的事情。我应该如何进行?
这是用于重现问题的笔记本的链接:LINK
PS:我的代码可能存在不止 1 个问题。
解决方案
我给了它一些时间并且做对了:
遍历 CSV 文件中的每个文件名
import pandas as pd
dff = pd.read_csv(path, index_col="Column_with_all_filenames")
从位置 PATH 访问文件
for img_name, Class in dff.iterrows():
if(".png" in img_name): #Check is the name is that of a PNG file
if(find(img_name, path) ): #Check if the file was found in the path
im=test_img_dir+'/'+img_name #Modify the path of the
print(im)
im = image.imread(im)
#Make Predictions
将预测的类保存到图像行中的另一列
#Create a list of the [filename--Prediction]
listof_rows.append([file_name, prediction])
#After all the files have been processed...
df=pd.DataFrame(listof_rows, columns=['Image_File', 'Class'])
df.to_csv('Result1.csv', index=False)
推荐阅读
- vba - 在通讯录宏中查找替换
- sql - 基于 SQL 中的 where 条件,不同时间的相同执行时间?我该如何解决?
- oauth - 身份服务器和身份提供者有什么区别?
- php - 如何使用 Doctrine 删除/调试多行?
- c++ - 在 cURL 上使用 Telegram Bot 发送图像
- flutter - 如何在 Flutter 中将 textEditiing 控制器与 Provider 一起使用
- postgresql - 在 postgres SQL 中将行视为列而不使用交叉选项卡,因为交叉选项卡正在提供异常结果
- r - 在 e_visual_map 函数中添加千位逗号分隔符(echarts4r 包)
- reactjs - Is there a way to prevent redirection after form submission and still send the form data to the server?
- android - adb (windows 8.1) 找不到我的设备 (honor 8x)