#-*- coding: utf-8 -*-
importcsvimportosimportsysimportnumpy as npimportcopyimportshutilimportpandas as pdfrom collections importCounterfrom shutil importcopyfile'''原数据目录如下:
./2019-6-14/
├── 1816740
│ ├── IMG005x020.csv
│ ├── IMG027x033.JPG
│ ├── IMG029x023.csv
│ └── IMG029x023.JPG
... ...
├── 1816765
│ └── ... ...
├── 1816875
│ └── ... ...
├── 1816896
│ └── ... ...
├── 1816900
│ └── ... ...
└── 1816969
└── ... ...
...
分类脚本与/2019-6-14/在同级目录'''data= '2019-6-23'path=os.getcwd()
path_1= path + '/' +data
data_N_root= path + '/' + 'data_N' + '_' +data
data_P_root= path + '/' + 'data_P' + '_' +dataifos.path.exists(data_N_root):
shutil.rmtree(data_N_root+'/')
os.mkdir(data_N_root)ifos.path.exists(data_P_root):
shutil.rmtree(data_P_root+'/')
os.mkdir(data_P_root)print("清理文件夹")print(data_N_root)print(data_P_root)
list_name=os.listdir(path_1)
lossfile=[]
count1=0
count2=0for n inlist_name:
path_2= path_1 + '/' +n
list_name_1=os.listdir(path_2)for m inlist_name_1:
temp= copy.copy(m[-3:])
temp_1= copy.copy(m[:-3])
csv_root= path_1 + '/' + n + '/' +mif temp == 'csv':if not os.path.exists(csv_root[:-3] + 'JPG'):
lossfile.append(csv_root)continuewith open(csv_root,'rwb') as csvfile:
reader=csv.reader(csvfile)
column1= [row[0]for row inreader]
column1= column1[1:]#print("len",len(column1))
column2 = str(np.ones((len(column1),1)))print(column1)print(column2)print(csv_root)
writer=csv.writer(csvfile)for val incolumn2:
writer.writerow('type',val)print("cell type :", column1)
sign=0for k incolumn1:if k == '1' or k == '5':continue
else:
sign= 1
print("sign :", sign)if sign == 1:print("该FOV为阳性")
copyfile(csv_root[:-3] + 'csv',data_P_root + '/' + temp_1 + 'csv')
copyfile(csv_root[:-3] + 'JPG',data_P_root + '/' + temp_1 + 'JPG')
count1= count1 + 1
else:print("该FOV为阴性")
copyfile(csv_root[:-3] + 'csv',data_N_root + '/' + temp_1 + 'csv')
copyfile(csv_root[:-3] + 'JPG',data_N_root + '/' + temp_1 + 'JPG')
count2= count2 + 1
print("===============================")if len(lossfile) ==0:print("data classify OK")else:print("loss file :")for n inlossfile:print(n)print("阳性FOV数量:",count1)print("阴性FOV数量:",count2)