#-*- coding: utf-8 -*-
importosimportsysimportnumpy as npimportcopyfrom collections importCounter'''标注文件夹名:data ,csv文件深度4
原片文件夹名:image
长度:11
待修改索引:3'''image= 'image'data= 'data'path=os.getcwd()
path_1= path + '/' +data
list_name=os.listdir(path_1)
Allfile_name=[]
Allfile_name_1=[]
Allfile_name_1_root=[]
Allfile_name_jpg=[]
Allfile_name_rename=[]
Allfile_name_rename_root=[]
name_n=[]for n inlist_name:
path_2= path_1 + '/' +n
list_name_1=os.listdir(path_2)for k inlist_name_1:
path_3= path_2 + '/' +k
list_name_2=os.listdir(path_3)for m inlist_name_2:
path_4= path_3 + '/' +m
Allfile_name.append(path_4)
Allfile_name_1.append(m)
Allfile_name_1_root.append(path_2)
Allfile_name_rename.append(k)
Allfile_name_rename_root.append(path_3)
name_n.append(n)#print(Allfile_name)#print(Allfile_name_1)#print(Allfile_name_rename)#print(Allfile_name_rename_root)#raise RuntimeError()
for n,m inzip(Allfile_name_1,Allfile_name):if len(n)!=14:raise RuntimeError("file name is too long")if n[6] == 'X':
rename=list(n)
rename[6] = 'x'rename= ''.join(rename)
path_5= m[:-15] + '/' +rename
os.rename(m,path_5)print("标注文件格式 OK!")print("... ...")print("csv type is OK!")#raise RuntimeError()#根据原片jpg文件路径修改jpg_root_rename
str_start = 'D:\BaiduNetdiskDownload/'str_start= 'D:\BaiduNetdiskDownload\细胞识别项目\原片\阳性 0603/'str_start= 'D:\yfq_data\data0812\data\data2019-8-9/'
for n inAllfile_name:
n= n.replace(' ', '')#print(n)#print("++++++++++++++++++")
binglihao = copy.copy(n[-15-8:-15])if len(binglihao)>7:
binglihao= binglihao[1:8]
m= n[-15:]#print("binglihao: ",binglihao)#print(m)#print(m[:-3] + 'JPG')
jpg_root_rename = copy.copy(m[:-3]+'JPG')#print("lala",jpg_root_rename)
#print("str_start type",type(str_start))
#print("binglihao type",type(binglihao))
#print("aaa",type('\Images'))
#print("jpg_root_rename type",type(jpg_root_rename))
#new_root = str_start + binglihao + '/Images' + jpg_root_rename
new_root = str_start + binglihao +jpg_root_renameprint("==============================")print(new_root)#raise RuntimeError()
if notos.path.exists(new_root):
Allfile_name_jpg.append(jpg_root_rename)if len(Allfile_name_jpg)>0:for n inAllfile_name_jpg:print(n)else:print("所有标注文件均存在对应原片!")print("... ...")print("all cav exits")#print(list_name)#print(name_n)#raise RuntimeError()#将重复的病例号重命名#print("+++++++++++++++++++++++++++++")#print(len(Allfile_name_rename))#print(Allfile_name_1_root[1])#print(Allfile_name_1_root[2])
temp = np.zeros((2,len(Allfile_name_rename)),dtype = 'int')
n=0for m inAllfile_name_rename:
temp[1,n] = m[-7:]
n= n + 1
for n inrange(0,len(Allfile_name_rename)):
temp[0,n]= list(temp[1,:]).count(temp[1,n])
temp_copy=copy.copy(temp)
count=len(Allfile_name_rename)
count1=0for n in temp[1,:]:
count2=0for m in temp[1,:]:
temp[0,count1]= list(temp[1,0:count2]).count(temp[1,count1])if count1 ==count2:continuecount2= count2 + 1count1= count1 + 1str_end=[]
sign_2=0#print("+++++++++++++++++++++")#print(temp_copy)#print(temp)#raise RuntimeError()
for n inrange(0,count):if temp_copy[0,n] != 1:
sign_2= 1
if temp[0,n] ==0:
str_end= 'A'
elif temp[0,n] == 1:
str_end= 'B'
elif temp[0,n] == 2:
str_end= 'C'
elif temp[0,n] == 3:
str_end= 'D'
elif temp[0,n] == 4:
str_end= 'E'
elif temp[0,n] == 5:
str_end= 'F'
else:raise RuntimeError("重复病例号过多,请修改程序!!!")
dirname_1=Allfile_name_rename_root[n]print("=================================================")#print(dirname_1)
rename_1 = dirname_1 +str_end#print(rename_1)#os.rename(dirname_1, rename_1)
if sign_2 ==0:print("没有重复病例")else:print("病例号重命名 OK")print(Allfile_name_rename)