171026 data preparation for cooling-bin

来源:互联网 发布:数据融合主要技术 编辑:程序博客网 时间:2024/06/06 06:35
# -*- coding: utf-8 -*-"""Created on Thu Oct 26 21:39:45 2017@author: brucelau"""#%%# data preparationimport matplotlib.pyplot as plt import numpy as npimport osfrom PIL import Image#%%# make folders for data preparationif not os.path.exists('data_preparation'):    for i in range(4):        os.makedirs('data_preparation\\Light'+str(i+1)+'\\Good')        os.makedirs('data_preparation\\Light'+str(i+1)+'\\Bad')    print('data_preparation and TRTE folders created.')else:    print('data_preparation or TRTE folders have been created.')#%%# make folders for training and testing if not os.path.exists('TRTE'):    os.makedirs('TRTE\\TRGood')    os.makedirs('TRTE\\TRBad')    os.makedirs('TRTE\\TEGood')    os.makedirs('TRTE\\TEBad')    print('TRTE folders created.')else:    print('TRTE folders have been created.')#%%# get pictures pathgood_files = os.listdir('Good\\')good_files = ['Good\\' + i for i in good_files]bad_files = os.listdir('Bad\\')bad_files = ['Bad\\' + i for i in bad_files]#%%# crop and save picturesdef crop_save_pic(original_files=good_files,good_or_bad='Good\\'):    index = 0 # crop name index    if os.listdir('data_preparation\\Light1\\Good\\')==[]:        for idx, item in enumerate(original_files):            im = Image.open(original_files[idx])            box = (700,160,1750,860)            region = im.crop(box)            order = idx%4            region.save('data_preparation\\Light'+str(order+1)+'\\'+good_or_bad # save path                        +str(int((index-index%4)/4)+1)+'.png')                                     # save name    #        if idx+1==8:    #            break            index +=1             print(index)    else:        print('all pictures has been croped.')crop_save_pic(original_files=good_files,good_or_bad='Good\\')crop_save_pic(original_files=bad_files,good_or_bad='Bad\\')#%%# randomly select pictures as training or testing data with a fixed probability def sel_prob(x):    val = ['True','False']    p_true = 0.88    return np.random.choice(val,p=[p_true,1-p_true])def sel_index(arr):    all_index  = np.arange(len(arr))    pro_value = np.array(np.array([sel_prob(i) for i in arr]))      sel_index_1  = np.where(pro_value=='True')    sel_index_2  = np.where(pro_value=='False')    return all_index[sel_index_1], all_index[sel_index_2]good = np.arange(279)bad =  np.arange(83)TRGood_index,TEGood_index = sel_index(good)TRBad_index, TEBad_index  = sel_index(bad)#%%# save cropped pitures as training or testing datadef trans_cropped(cropped_path, trans_path):    for i in cropped_path:        name = i.split('\\')[-1]        im = Image.open(i)        im.save(trans_path+'\\'+name)TRTE_path = ['TRTE\\TRGood\\','TRTE\\TRBad\\','TRTE\\TEGood\\','TRTE\\TEBad\\']path_TRGood = ['data_preparation\\Light1\\Good\\'+str(i+1)+'.png' for i in TRGood_index]path_TRBad = ['data_preparation\\Light1\\Bad\\'+str(i+1)+'.png' for i in TRBad_index]path_TEGood = ['data_preparation\\Light1\\Good\\'+str(i+1)+'.png' for i in TEGood_index]path_TEBad = ['data_preparation\\Light1\\Bad\\'+str(i+1)+'.png' for i in TEBad_index]if os.listdir('TRTE\\TRGood\\')==[]:    trans_cropped(path_TRGood,TRTE_path[0])    trans_cropped(path_TRBad,TRTE_path[1])    trans_cropped(path_TEGood,TRTE_path[2])    trans_cropped(path_TEBad,TRTE_path[3])else:    print('all cropped pictures have been transfered!')
原创粉丝点击