生成all_shuffle_datas.txt,内部包含信息如图:(健康+不健康图像的详细路径,附带标签)
为之后自定义图像数据集作准备
#本段代码为k折交叉验证提供了数据集上的准备
#以读入测试集为例,故只列出了测试集图片所在路径,若读入全部数据需把所有路径加
import glob
import os
import numpy as np
PATHS= ['.\\shallow\health_shallow',".\\shallow\\test_001health_shallow",
'.\\shallow\\hypertension_shallow',".\\shallow\\test_002hypertension_shallow"]
sum=0
img_path=[]
#遍历上面的4个路径,依次把信息追加到img_path列表中
for label,p in enumerate(PATHS):
image_dir=glob.glob(p+"\\"+"*.png")#返回路径下的所有图片详细的路径
sum+=len(image_dir)
print(len(image_dir))
for image in image_dir:
img_path.append((image,str(label//2))) #label//2,健康图像标签都为0,不健康的都为1
#print(img_path[0])
print("%d 个图像信息已经加载到txt文本!!!"%(sum))
np.random.shuffle(img_path) #打乱掉图像的分布
print(img_path[0])
file=open(".\\shallow\\all_shuffle_datas.txt","w",encoding="utf-8")
for img in img_path:
file.write(img[0]+':'+img[1]+'\n') #img[0]是图像的详细路径,img[1]是图像的标签
file.close()