分类目录结构转csv结构
【摘要】
分类网络目录结构转pandas csv结构:
import osimport time import pandas as pd if __name__ == '__main__': dir_path=r'F:\project\huajie\abnormal_img\dataset\abnormal_data\val/'...
分类网络目录结构转pandas csv结构:
-
import os
-
import time
-
-
import pandas as pd
-
-
if __name__ == '__main__':
-
-
dir_path=r'F:\project\huajie\abnormal_img\dataset\abnormal_data\val/'
-
-
-
-
if os.path.basename(os.path.dirname(dir_path))=="train":
-
val_type = 1
-
elif os.path.basename(os.path.dirname(dir_path))=="val":
-
val_type = 0
-
else:
-
print("val/train dir is error",os.path.dirname(dir_path))
-
exit(1123)
-
-
imgs = ['%s/%s' % (i[0], j) for i in os.walk(dir_path) for j in i[-1] if j.endswith(('.jpg', '.jpeg'))]
-
-
print("data_len",len(imgs))
-
-
#多分类用/分开
-
columns=["img_path","cls_0/cls_1","val/train"]
-
-
cla_num=10
-
ids=[str(i) for i in range(cla_num)]
-
# ids = ["0", "1","2","3","4","","6","7"]
-
-
datas=[]
-
start=time.time()
-
for img_path in imgs:
-
label_txt = os.path.basename(os.path.dirname(img_path))
-
id = ids.index(label_txt)
-
data_row = pd.Series([img_path.replace(dir_path,"/"), id, val_type], index=columns)
-
datas.append(data_row)
-
-
gdp4 =pd.concat(datas ,axis=1).T
-
-
gdp4.to_csv(dir_path+"/label.csv",index=False)
-
print("time" ,time.time( ) -start,"s")
文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/126801233
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)