def initialize_images(self, mode=None, current_image_path=None): self._all_images: List = file_walker.walk(mode) self._current_index = 0 self._shuffle_start_index = 0 if current_image_path: self._current_index = self.get_index_from_image_path( current_image_path) self._shuffle_start_index = self._current_index # self.image_shuffle() self._set_image(self._current_index)
def initialize_images(self, mode=None, current_image_path=None, to_sort: bool = False, reverse_sort: bool = False): self._all_images: List = file_walker.walk(mode, is_sorted=to_sort, reverse_sort=reverse_sort) self._current_index = 0 self._shuffle_start_index = 0 if current_image_path: self._current_index = self.get_index_from_image_path( current_image_path) self._shuffle_start_index = self._current_index # self.image_shuffle() self._set_image(self._current_index)
def backup_image(old_image_path, backup_path): command = "mv '{old}' {backup_path}".format(old=old_image_path, backup_path=backup_path) print(command) subprocess.check_output(shlex.split(command)) # def compress_first_10_images(): if __name__ == '__main__': hard_limit = 5491520 # 5491520 bytes = 5240 KB = 5MB image_paths: List = walker.walk(increasing_size_sort=True) new_suffix = "_comp_la" new_ext = '.jpg' for image_path in image_paths: if new_suffix in image_path: continue if os.path.getsize(image_path) < hard_limit: print(image_path) print("image size > hard limit; stopping") break old_image_path: str = image_path new_image_path = old_image_path.replace( os.path.splitext(old_image_path)[1], new_suffix + new_ext) compress_image(old_image_path, new_image_path) backup_image(old_image_path) # break
def __init__(self, data_list=["AFEW"], dir_gt=None, onlyFace=True, image_size=224, transform=None, useIT=False, augment=False, step=1, split=False, nSplit=5, listSplit=[0, 1, 2, 3, 4], wHeatmap=False, isVideo=False, seqLength=None): self.seq_length = seqLength self.isVideo = isVideo self.transform = transform self.onlyFace = onlyFace self.augment = augment self.wHeatmap = wHeatmap self.imageSize = image_size self.imageHeight = image_size self.imageWidth = image_size self.useIT = useIT self.curDir = "/homedtic/gulloa/ValenceArousal/images/" list_gt = [] list_labels_t = [] list_labels_tE = [] counter_image = 0 annotL_name = 'annot' annotE_name = 'annot2' if dir_gt is not None: annot_name = dir_gt for data in data_list: print(("Opening " + data)) for f in file_walker.walk(self.curDir + data + "/"): if f.isDirectory: # Check if object is directory #print((f.name, f.full_path)) # Name is without extension for sub_f in f.walk(): if sub_f.isDirectory: # Check if object is directory #AFEWVA '''for sub_2_f in sub_f.walk(): if sub_2_f.isDirectory: list_dta = [] #print(sub_f.name) if(sub_2_f.name == annotL_name) : #If that's annot, add to labels_t for sub_sub_f in sub_2_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_t.append(sorted(list_dta)) elif(sub_2_f.name == annotE_name) : #If that's annot, add to labels_t for sub_sub_f in sub_2_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_tE.append(sorted(list_dta)) elif(sub_2_f.name == 'img'): #Else it is the image for sub_sub_f in sub_2_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_gt.append(sorted(list_dta)) counter_image+=len(list_dta)''' #SEMAINE list_dta = [] #print(sub_f.name) if (sub_f.name == annotL_name ): #If that's annot, add to labels_t for sub_sub_f in sub_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_t.append(sorted(list_dta)) elif (sub_f.name == annotE_name ): #If that's annot, add to labels_t for sub_sub_f in sub_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_tE.append(sorted(list_dta)) elif (sub_f.name == 'img'): #Else it is the image for sub_sub_f in sub_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_gt.append(sorted(list_dta)) counter_image += len(list_dta) self.length = counter_image print("Now opening keylabels") list_labelsN = [] list_labelsEN = [] list_labels = [] list_labelsE = [] for ix in range(len( list_labels_t)): #lbl,lble in (list_labels_t,list_labels_tE) : lbl_68 = [] #Per folder lbl_2 = [] #Per folder lbl_n68 = [] #Per folder lbl_n2 = [] #Per folder for jx in range(len(list_labels_t[ix])): #lbl_sub in lbl : lbl_sub = list_labels_t[ix][jx] if ('pts' in lbl_sub): x = [] #print(lbl_sub) with open(lbl_sub) as file: data2 = [re.split(r'\t+', l.strip()) for l in file] for i in range(len(data2)): if (i not in [0, 1, 2, len(data2) - 1]): x.append([float(j) for j in data2[i][0].split()]) lbl_68.append(np.array(x).flatten('F')) #1 record lbl_n68.append(lbl_sub) lbl_subE = list_labels_tE[ix][jx] if ('aro' in lbl_subE): x = [] #print(lbl_sub) with open(lbl_subE) as file: data2 = [re.split(r'\t+', l.strip()) for l in file] for i in range(len(data2)): x.append([float(j) for j in data2[i][0].split()]) lbl_2.append(np.array(x).flatten('F')) lbl_n2.append(lbl_sub) list_labelsN.append(lbl_n68) list_labelsEN.append(lbl_n2) list_labels.append(lbl_68) list_labelsE.append(lbl_2) t_l_imgs = [] t_l_gt = [] t_l_gtE = [] t_list_gt_names = [] t_list_gtE_names = [] for i in range(0, len(list_gt)): #For each dataset list_images = [] list_gt_names = [] list_gtE_names = [] indexer = 0 list_ground_truth = np.zeros([len(list_gt[i]), 136]) list_ground_truthE = np.zeros([len(list_gt[i]), 2]) for j in range( 0, len(list_gt[i]), step ): #for number of data #n_skip is usefull for video data list_images.append(list_gt[i][j]) list_gt_names.append(list_labelsN[i][j]) list_gtE_names.append(list_labelsEN[i][j]) list_ground_truth[indexer] = np.array( list_labels[i][j]).flatten('F') list_ground_truthE[indexer] = np.array( list_labelsE[i][j]).flatten('F') indexer += 1 t_l_imgs.append(list_images) t_l_gt.append(list_ground_truth) t_l_gtE.append(list_ground_truthE) t_list_gt_names.append(list_gt_names) t_list_gtE_names.append(list_gtE_names) self.l_imgs = [] self.l_gt = [] self.l_gtE = [] self.list_gt_names = [] self.list_gtE_names = [] if split: indexer = 0 self.l_gt = [] self.l_gtE = [] totalData = len(t_l_imgs) perSplit = int(truediv(totalData, nSplit)) for x in listSplit: print('split : ', x) begin = x * perSplit if x == nSplit - 1: end = begin + (totalData - begin) else: end = begin + perSplit print(begin, end, totalData) for i in range(begin, end): for j in range(len(t_l_imgs[i])): #print('append ',t_l_imgs[i][j]) self.l_imgs.append(t_l_imgs[i][j]) self.l_gt.append(t_l_gt[i][j]) self.l_gtE.append(t_l_gtE[i][j]) self.list_gt_names.append(t_list_gt_names[i][j]) self.list_gtE_names.append(t_list_gtE_names[i][j]) indexer += 1 print(len(self.l_imgs)) self.l_gt = np.asarray(self.l_gt) self.l_gtE = np.asarray(self.l_gtE) else: self.l_gt = np.zeros([counter_image, 136]) self.l_gtE = np.zeros([counter_image, 2]) indexer = 0 for i in range(len(t_l_imgs)): for j in range(len(t_l_imgs[i])): self.l_imgs.append(t_l_imgs[i][j]) print(i, j, '-', len(t_l_imgs[i])) self.l_gt[indexer] = t_l_gt[i][j] self.l_gtE[indexer] = t_l_gtE[i][j] self.list_gt_names.append(t_list_gt_names[i][j]) self.list_gtE_names.append(t_list_gtE_names[i][j]) indexer += 1 print('limgs : ', len(self.l_imgs))
def __init__(self, data_list=["AFEW"], dir_gt=None, onlyFace=True, image_size=224, transform=None, useIT=False, augment=False, step=1, split=False, nSplit=5, listSplit=[0, 1, 2, 3, 4], wHeatmap=False, isVideo=False, seqLength=None): self.seq_length = seqLength self.isVideo = isVideo self.transform = transform self.onlyFace = onlyFace self.augment = augment self.wHeatmap = wHeatmap self.imageSize = image_size self.imageHeight = image_size self.imageWidth = image_size self.useIT = useIT self.curDir = "/homedtic/gulloa/ValenceArousal/images/" list_gt = [] list_labels_t = [] list_labels_tE = [] counter_image = 0 annotL_name = 'annot' annotE_name = 'annot2' if dir_gt is not None: annot_name = dir_gt for data in data_list: print(("Opening " + data)) for f in file_walker.walk(self.curDir + data + "/"): if f.isDirectory: # Check if object is directory #print((f.name, f.full_path)) # Name is without extension for sub_f in f.walk(): if sub_f.isDirectory: # Check if object is directory #AFEWVA for sub_2_f in sub_f.walk(): if sub_2_f.isDirectory: list_dta = [] #print(sub_f.name) if (sub_2_f.name == annotL_name ): #If that's annot, add to labels_t for sub_sub_f in sub_2_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append( sub_sub_f.full_path) list_labels_t.append(sorted(list_dta)) elif (sub_2_f.name == annotE_name ): #If that's annot, add to labels_t for sub_sub_f in sub_2_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append( sub_sub_f.full_path) list_labels_tE.append(sorted(list_dta)) elif (sub_2_f.name == 'img' ): #Else it is the image for sub_sub_f in sub_2_f.walk( ): #this is the data if (".npy" not in sub_sub_f.full_path): list_dta.append( sub_sub_f.full_path) list_gt.append(sorted(list_dta)) counter_image += len(list_dta) #SEMAINE '''list_dta = [] #print(sub_f.name) if(sub_f.name == annotL_name) : #If that's annot, add to labels_t for sub_sub_f in sub_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_t.append(sorted(list_dta)) elif(sub_f.name == annotE_name) : #If that's annot, add to labels_t for sub_sub_f in sub_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_labels_tE.append(sorted(list_dta)) elif(sub_f.name == 'img'): #Else it is the image for sub_sub_f in sub_f.walk(): #this is the data if(".npy" not in sub_sub_f.full_path): list_dta.append(sub_sub_f.full_path) list_gt.append(sorted(list_dta)) counter_image+=len(list_dta)''' self.length = counter_image print("Now opening keylabels") list_labelsN = [] list_labelsEN = [] list_labels = [] list_labelsE = [] for ix in range(len( list_labels_t)): #lbl,lble in (list_labels_t,list_labels_tE) : lbl_68 = [] #Per folder lbl_2 = [] #Per folder lbl_n68 = [] #Per folder lbl_n2 = [] #Per folder for jx in range(len(list_labels_t[ix])): #lbl_sub in lbl : #print(os.path.basename(list_gt[ix][jx])) #print(os.path.basename(list_labels_t[ix][jx])) #print(os.path.basename(list_labels_tE[ix][jx])) lbl_sub = list_labels_t[ix][jx] if ('pts' in lbl_sub): x = [] #print(lbl_sub) with open(lbl_sub) as file: data2 = [re.split(r'\t+', l.strip()) for l in file] for i in range(len(data2)): if (i not in [0, 1, 2, len(data2) - 1]): x.append([float(j) for j in data2[i][0].split()]) lbl_68.append(np.array(x).flatten('F')) #1 record lbl_n68.append(lbl_sub) lbl_subE = list_labels_tE[ix][jx] if ('aro' in lbl_subE): x = [] #print(lbl_sub) with open(lbl_subE) as file: data2 = [re.split(r'\t+', l.strip()) for l in file] for i in range(len(data2)): x.append([float(j) for j in data2[i][0].split()]) lbl_2.append(np.array(x).flatten('F')) lbl_n2.append(lbl_sub) list_labelsN.append(lbl_n68) list_labelsEN.append(lbl_n2) list_labels.append(lbl_68) list_labelsE.append(lbl_2) '''for lbl in list_labels_t : lbl_68 = [] #Per folder for lbl_sub in lbl : #print(lbl_sub) if ('pts' in lbl_sub) : x = [] #print(lbl_sub) with open(lbl_sub) as file: data2 = [re.split(r'\t+',l.strip()) for l in file] for i in range(len(data2)) : if(i not in [0,1,2,len(data2)-1]): x.append([ float(j) for j in data2[i][0].split()] ) lbl_68.append(np.array(x).flatten('F')) #1 record list_labels.append(lbl_68) list_labelsE = [] for lbl in list_labels_tE : lbl_2 = [] #Per folder for lbl_sub in lbl : #print(lbl_sub) if ('aro' in lbl_sub) : x = [] #print(lbl_sub) with open(lbl_sub) as file: data2 = [re.split(r'\t+',l.strip()) for l in file] for i in range(len(data2)) : x.append([ float(j) for j in data2[i][0].split()] ) lbl_2.append(np.array(x).flatten('F')) #1 record list_labelsE.append(lbl_2)''' '''print(len(list_labelsN[0])) print(len(list_labelsEN[0])) print(len(list_labels[0])) print(len(list_labelsE[0]))''' t_l_imgs = [] t_l_gt = [] t_l_gtE = [] t_list_gt_names = [] t_list_gtE_names = [] if not self.isVideo: #Flatten it to one list for i in range(0, len(list_gt)): #For each dataset list_images = [] list_gt_names = [] list_gtE_names = [] indexer = 0 list_ground_truth = np.zeros([len(list_gt[i]), 136]) list_ground_truthE = np.zeros([len(list_gt[i]), 2]) for j in range( 0, len(list_gt[i]), step ): #for number of data #n_skip is usefull for video data list_images.append(list_gt[i][j]) list_gt_names.append(list_labelsN[i][j]) list_gtE_names.append(list_labelsEN[i][j]) list_ground_truth[indexer] = np.array( list_labels[i][j]).flatten('F') list_ground_truthE[indexer] = np.array( list_labelsE[i][j]).flatten('F') indexer += 1 t_l_imgs.append(list_images) t_l_gt.append(list_ground_truth) t_l_gtE.append(list_ground_truthE) t_list_gt_names.append(list_gt_names) t_list_gtE_names.append(list_gtE_names) else: if self.seq_length is None: list_ground_truth = np.zeros([ int(counter_image / (self.seq_length * step)), self.seq_length, 136 ]) indexer = 0 for i in range(0, len(list_gt)): #For each dataset counter = 0 for j in range( 0, int(len(list_gt[i]) / (self.seq_length * step))): #for number of data/batchsize temp = [] temp2 = np.zeros([self.seq_length, 136]) i_temp = 0 for z in range(counter, counter + (self.seq_length * step), step): #1 to seq_size temp.append(list_gt[i][z]) temp2[i_temp] = list_labels[i][z] i_temp += 1 list_images.append(temp) list_ground_truth[indexer] = temp2 indexer += 1 counter += self.seq_length * step #print counter self.l_imgs = list_images self.l_gt = list_ground_truth else: counter_seq = 0 for i in range(0, len(list_gt)): #For each dataset indexer = 0 list_gt_names = [] list_gtE_names = [] list_ground_truth = np.zeros([ int(len(list_gt[i]) / (self.seq_length * step)), self.seq_length, 136 ]) #np.zeros([counter_image,136]) list_ground_truthE = np.zeros([ int(len(list_gt[i]) / (self.seq_length * step)), self.seq_length, 2 ]) #np.zeros([counter_image,2]) counter = 0 list_images = [] for j in range( 0, int(len(list_gt[i]) / (self.seq_length * step))): #for number of data/batchsize tmpn68 = [] tmpn2 = [] temp = [] temp2 = np.zeros([self.seq_length, 136]) temp3 = np.zeros([self.seq_length, 2]) i_temp = 0 for z in range(counter, counter + (self.seq_length * step), step): #1 to seq_size temp.append(list_gt[i][z]) temp2[i_temp] = list_labels[i][z].flatten('F') temp3[i_temp] = list_labelsE[i][z].flatten('F') tmpn68.append(list_labelsN[i][z]) tmpn2.append(list_labelsEN[i][z]) i_temp += 1 counter_seq += 1 list_images.append(temp) list_ground_truth[indexer] = temp2 list_ground_truthE[indexer] = temp3 list_gt_names.append(tmpn68) list_gtE_names.append(tmpn2) indexer += 1 counter += self.seq_length * step #print counter t_l_imgs.append(list_images) t_l_gt.append(list_ground_truth) t_l_gtE.append(list_ground_truthE) t_list_gt_names.append(list_gt_names) t_list_gtE_names.append(list_gtE_names) '''print('length : ',len(t_l_imgs)) #Folder print('lengt2 : ',len(t_l_imgs[0])) #all/seq print('lengt3 : ',len(t_l_imgs[0][0])) #seq print('length4 : ',len(t_l_imgs[0][0][0]))''' #[folder, all/seq,seq] self.l_imgs = [] self.l_gt = [] self.l_gtE = [] self.list_gt_names = [] self.list_gtE_names = [] #print('cimage : ',counter_image) if split: '''print('splitting') self.l_imgs = [] self.l_gt = [] self.l_gtE = [] totalData = len(list_images) perSplit = int(truediv(totalData, nSplit)) for x in listSplit : print('split : ',x) begin = x*perSplit if x == nSplit-1 : end = begin + (totalData - begin) else : end = begin+perSplit print(begin,end,totalData) for x2 in range(begin,end) : #print(x2,totalData) self.l_imgs.append(list_images[x2]) self.l_gt.append(list_ground_truth[x2]) self.l_gtE.append(list_ground_truthE[x2])''' indexer = 0 self.l_gt = [] self.l_gtE = [] '''else : self.l_gt = np.zeros([counter_seq,self.seq_length,136]) self.l_gtE = np.zeros([counter_seq,self.seq_length,2])''' totalData = len(t_l_imgs) perSplit = int(truediv(totalData, nSplit)) for x in listSplit: print('split : ', x) begin = x * perSplit if x == nSplit - 1: end = begin + (totalData - begin) else: end = begin + perSplit print(begin, end, totalData) if not self.isVideo: for i in range(begin, end): for j in range(len(t_l_imgs[i])): #print('append ',t_l_imgs[i][j]) self.l_imgs.append(t_l_imgs[i][j]) self.l_gt.append(t_l_gt[i][j]) self.l_gtE.append(t_l_gtE[i][j]) self.list_gt_names.append(t_list_gt_names[i][j]) self.list_gtE_names.append(t_list_gtE_names[i][j]) indexer += 1 else: for i in range(begin, end): for j in range(len(t_l_imgs[i])): #seq counter t_img = [] t_gt = [] t_gtE = [] t_gt_N = [] t_gt_EN = [] tmp = 0 for k in range(len(t_l_imgs[i][j])): #seq size t_img.append(t_l_imgs[i][j][k]) t_gt.append(t_l_gt[i][j][k]) t_gtE.append(t_l_gtE[i][j][k]) t_gt_N.append(t_list_gt_names[i][j][k]) t_gt_EN.append(t_list_gtE_names[i][j][k]) tmp += 1 #print('append ',t_img) self.l_imgs.append(t_img) self.l_gt.append(t_gt) self.l_gtE.append(t_gtE) self.list_gt_names.append(t_gt_N) self.list_gtE_names.append(t_gt_EN) indexer += 1 print(len(self.l_imgs)) self.l_gt = np.asarray(self.l_gt) self.l_gtE = np.asarray(self.l_gtE) else: if not self.isVideo: self.l_gt = np.zeros([counter_image, 136]) self.l_gtE = np.zeros([counter_image, 2]) indexer = 0 for i in range(len(t_l_imgs)): for j in range(len(t_l_imgs[i])): self.l_imgs.append(t_l_imgs[i][j]) print(i, j, '-', len(t_l_imgs[i])) self.l_gt[indexer] = t_l_gt[i][j] self.l_gtE[indexer] = t_l_gtE[i][j] self.list_gt_names.append(t_list_gt_names[i][j]) self.list_gtE_names.append(t_list_gtE_names[i][j]) indexer += 1 else: self.l_gt = np.zeros([counter_seq, self.seq_length, 136]) self.l_gtE = np.zeros([counter_seq, self.seq_length, 2]) indexer = 0 for i in range(len(t_l_imgs)): #dataset for j in range(len(t_l_imgs[i])): #seq counter t_img = [] t_gt = np.zeros([self.seq_length, 136]) t_gte = np.zeros([self.seq_length, 2]) t_gt_n = [] t_gt_en = [] i_t = 0 for k in range(len(t_l_imgs[i][j])): #seq size t_img.append(t_l_imgs[i][j][k]) t_gt[i_t] = t_l_gt[i][j][k] t_gte[i_t] = t_l_gtE[i][j][k] t_gt_n.append(t_list_gt_names[i][j][k]) t_gt_en.append(t_list_gtE_names[i][j][k]) i_t += 1 self.l_imgs.append(t_img) self.l_gt[indexer] = t_gt self.l_gtE[indexer] = t_gte self.list_gt_names.append(t_gt_n) self.list_gtE_names.append(t_gt_en) indexer += 1 print('limgs : ', len(self.l_imgs))