def __getitem__(self, idx): ops = self.ops af_dict = self.af_dict # randomly choose emotion - wrt neutral probability rndv = th_rand() if rndv < ops.neutral_ratio: # neutral emo = 6 else: # non neutral, choose again from 0~5 emo = th_randint(0, 6) # emo = th_randint(0, 7) # label vec batch_emo = np.zeros(7) batch_emo[emo] = 1 # randomly choose image sample and import rndi = th_randint(len(af_dict[emo])) sel_dict = af_dict[emo][rndi] img = imread_to_rgb(ops.friends_path + sel_dict['img']) batch_img = img # returns batch_img = torch.Tensor(batch_img.transpose(2, 0, 1).copy()) batch_emo = torch.Tensor(batch_emo.copy()) return batch_img, batch_emo
def __getitem__(self, idx): ops = self.ops af_dict = self.fr_dict # randomly choose emotion - wrt neutral probability rndv = th_rand() if rndv < ops.neutral_ratio: # neutral emo = 6 else: # non neutral, choose again from 0~5 emo = th_randint(0, 6) # emo = th_randint(0, 7) # label vec batch_emo = np.zeros(7) batch_emo[emo] = 1 # randomly choose image sample and import rndi = th_randint(len(af_dict[emo])) sel_dict = af_dict[emo][rndi] if self.win_size == 1: img = imread_to_rgb(ops.friends_path + sel_dict['img'][-1]) batch_img = img batch_img = torch.Tensor(batch_img.transpose(2, 0, 1).copy()) batch_emo = torch.Tensor(batch_emo.copy()) else: batch_imgs = [] batch_emos = [] for i in range(self.win_size): img = imread_to_rgb(ops.friends_path + sel_dict['img'][i]) batch_imgs.append(img) batch_emos.append(batch_emo) batch_img = torch.Tensor( np.array(batch_imgs).transpose(0, 3, 1, 2).copy()) batch_emo = torch.Tensor(np.array(batch_emos).copy()) # returns return batch_img, batch_emo
def read_img_resize(self, imf): img_orig = imread_to_rgb(imf) h_orig, w_orig, _ = img_orig.shape MAX_H, MAX_W = self.cfg.im_size s_f = float(MAX_H) / float(h_orig) if float(w_orig) * s_f > MAX_W: s_f = float(MAX_W) / float(w_orig) img_mod = cv2.resize(img_orig, (int(w_orig * s_f), int(h_orig * s_f))) h_mod, w_mod, _ = img_mod.shape img_zero = np.zeros([MAX_H, MAX_W, 3]) img_zero[:h_mod, :w_mod, :] = img_mod return img_zero, s_f
def draw_vid_seq(self, seq_res, seq_name): print '> make video seq...', # preliminaries seq_dict = self.track_dict[seq_name] seq_path = seq_name if not seq_dict.has_key( 'path') else seq_dict['path'] if self.track_dbnm is not 'got10k': seq_path = os.path.join(seq_path, 'img/') seq_path = os.path.join(self.track_path, seq_path) seq_len = len(seq_dict['img']) # draw for all frames im_slist = [] for i, imf in enumerate(seq_dict['img']): # read img im_frame = imread_to_rgb(os.path.join(seq_path, imf)) # draw bb = [xmin, ymin, width, height] bb = seq_res[i].astype(int) im_frame = cv2.rectangle(im_frame, (bb[0], bb[1]), (bb[0] + bb[2], bb[1] + bb[3]), (1, 0, 0), 3) # fnum text fnum_str = str('%04d' % i) im_frame = cv2.putText(im_frame, fnum_str, (0, im_frame.shape[0]), cv2.FONT_HERSHEY_DUPLEX, im_frame.shape[0] / 350., (1, 1, 0)) # save img im_sname = os.path.join('.temp/', seq_name + '_' + fnum_str + '.jpg') im_slist.append(im_sname) plt.imsave(im_sname, im_frame) # encode video vid_clip = mpe.ImageSequenceClip(im_slist, fps=30) vid_clip.write_videofile('test.mp4', logger=None) print 'done' return
def __getitem__(self, idx): ops = self.ops if th_rand() < 0.2: imf, gt, emo = self.get_raf() else: imf, gt, emo = self.get_af() img = imread_to_rgb(imf) xcen = gt[0] + gt[2]*0.5 ycen = gt[1] + gt[3]*0.5 margin = (gt[2]+gt[3])*ops.aug_marg wh_mean = np.sqrt((gt[2]+margin)*(gt[2]+margin)) width = wh_mean height = wh_mean # data augmentation part == aug_prob = ops.aug_prob aug_crop = ops.aug_crop # random crops (always) xmod = th_choice([1,-1])*th_rand_rng(0., width*aug_crop) ymod = th_choice([1,-1])*th_rand_rng(0., height*aug_crop) whmod = th_choice([1,-1])*th_rand_rng(0., aug_crop) wmod = 1.0 + whmod hmod = 1.0 + whmod if img.shape[0]==0 or img.shape[1]==0: print 'b: ',sel_dict['img'], gt if wmod <= 0 or hmod <= 0: print 'b: ',wmod,hmod batch_img = crop_img(img, int(xcen+xmod), int(ycen+ymod), int(width*wmod), int(height*hmod), True, True) batch_img = cv2.resize(batch_img, (ops.img_sz, ops.img_sz)) # flip horizontally if th_rand() < 0.5: batch_img = np.flip(batch_img, axis=1) # add noise / change level if th_rand() < aug_prob: noise_std = th_rand()*0.05 if th_rand() < 0.5: noise_img = np.repeat( (noise_std)*torch.randn(ops.img_sz, ops.img_sz, 1), 3, 2) else: noise_img = (noise_std)*torch.randn(ops.img_sz, ops.img_sz, 3) batch_img += noise_img.contiguous().numpy() if th_rand() < aug_prob: batch_img += (th_rand()-0.5)/50. # gaussian if th_rand() < aug_prob: k_sz = th_randint(1,5,2)*2 +1 batch_img = cv2.GaussianBlur(batch_img, (k_sz[0], k_sz[1]), 0) # end of data augmentation == # reset range [0,1] if batch_img.min() < 0.: batch_img[ batch_img < 0. ] = 0. if batch_img.max() > 1.: batch_img[ batch_img > 1. ] = 1. # returns batch_img = torch.Tensor( batch_img.transpose(2,0,1).copy() ) batch_emo = torch.Tensor( emo.copy() ) #batch_var = torch.Tensor( [sel_dict['val'], sel_dict['aro']] ) return batch_img, batch_emo #, batch_var