def make_all_dirs(which): path = os.path.join(tools.main_path, which) cats = os.listdir(path) for c in cats: path2 = os.path.join(tools.main_path_standardized, which) cat_path = os.path.join(path2, c) opt_makedirs(cat_path)
def make_all_dirs_in_split(which): # copy the names from train copy_from = os.path.join(main_path, 'train') classes = os.listdir(copy_from) classes.sort() for i, v in enumerate(classes): cat_path = os.path.join(main_path, which, v) if not os.path.exists(cat_path): # print(i, cat_path) opt_makedirs(cat_path) # make_all_dirs_in_split('test') # def remove_empty_folders(): # folders = os.listdir(main_path) # folders.sort() # # cnt = 0 # for f in folders: # if f not in ['train', 'valid', 'val', 'test']: # cnt = cnt + 1 # the_path = os.path.join(main_path, f) # # print('%d %s' % (cnt, the_path)) # # # ====================================== # # ====================================== # # command = 'rm -rf %s' % the_path # # subprocess.call(command, shell=True) # # ====================================== # # ====================================== # alist = get_all_video_paths('train') # get_downloaded('train') # get_downloaded('valid') # get_downloaded('test') # total train videos: 246534 # successfully downloaded: 59006 # listed as failed: 222611 # total valid videos: 19906 # successfully downloaded: 4472 # listed as failed: 222611 # get_total_per_category_list('train') # download_progress_per_class('train')
def make_dataset(which, num_samples_per_class, num_frames, crop_side, save_data=True): print('which: %s' % which) dot_size_array = np.zeros(shape=(3, num_samples_per_class)) which_seeds = [6, 42, 420] if which == 'train': master_seed = which_seeds[0] elif which == 'val': master_seed = which_seeds[1] else: master_seed = which_seeds[2] np.random.seed(master_seed) avi_which_path = os.path.join(PP.dots_dataset_avi, which) frames_which_path = os.path.join(PP.dots_dataset_frames, which) classes = ['rotate', 'scale', 'translate'] # classes = ['translate'] class_seeds = np.random.randint(0, 10000, 3) for i, c in enumerate(classes): print('class: %s' % c) np.random.seed(class_seeds[i]) video_seeds = np.random.randint(0, 1000000, num_samples_per_class) avi_class_path = os.path.join(avi_which_path, c) frames_class_path = os.path.join(frames_which_path, c) U.opt_makedirs(avi_class_path) U.opt_makedirs(frames_class_path) # for s in tqdm(range(num_samples_per_class)): # ---- # if debugging only # if c == 'translate': # all_frames_dir = os.path.join(PP.dots_dataset_frames, which, 'translate_all_frames') # U.opt_mkdir(all_frames_dir) # ---- for s in range(num_samples_per_class): all_frames_array, annotation, size_dot = generate_sequence( c, num_frames, video_seeds[s], 200, crop_side, save_data=True) # dot_size_array[i, s] = size_dot # all_frames_array, annotation = generate_sequence_dots(c, num_frames, video_seeds[s], side, parameters=params) choose_frame = np.random.randint(0, num_frames) frame_dot_size = Image.fromarray(all_frames_array[choose_frame], mode='L') d_bbox = frame_dot_size.getbbox() frame_dot_size = ((d_bbox[2] - d_bbox[0]) + (d_bbox[3] - d_bbox[1])) // 2 dot_size_array[i, s] = frame_dot_size if save_data: frame = all_frames_array[choose_frame] # save avi if c == 'rotate': num = s elif c == 'scale': num = s + num_samples_per_class else: num = s + 2 * num_samples_per_class vid = '%05d' % (num + 1) avi_save_path = os.path.join(avi_class_path, '%s.avi' % vid) skvid.vwrite(avi_save_path, all_frames_array) print(s, avi_save_path) # ---- # for debugging # if c == 'translate': # sample_path = os.path.join(PP.dots_dataset_frames, which, 'translate_all_frames', vid) # U.opt_mkdir(sample_path) # # for f in range(num_frames): # frem = all_frames_array[f] # frame_save_path = os.path.join(sample_path, 'frame_%d.jpg' % f) # im_frame = Image.fromarray(frem, mode='L') # im_frame.save(frame_save_path) # ---- # save avi annotation avi_annotation = os.path.join(PP.dots_dataset_avi, 'annotations_%s.txt' % which) with open(avi_annotation, 'a') as my_file: # vid,class,seed,direction_hor,direction_ver,speed,frames (if translate) # vid,class,seed,direction,direction,speed,frames (else) line = '%s,%s,%d,%d,%d,%d,%d\n' % ( vid, c, annotation[0], annotation[1], annotation[2], annotation[3], num_frames) my_file.write(line) # save frame frame_save_path = os.path.join(frames_class_path, '%s.jpg' % vid) frame = Image.fromarray(frame, mode='L') frame.save(frame_save_path) # save frame annotation frame_annotation = os.path.join(PP.dots_dataset_frames, 'annotations_%s.txt' % which) with open(frame_annotation, 'a') as my_file: # vid,class,frame line = '%s,%s,%d\n' % (vid, c, choose_frame) my_file.write(line) if not save_data: for i, c in enumerate(classes): data = dot_size_array[i] print('%s mean: %f std: %f' % (c, float(np.mean(data)), float(np.std(data)))) # plt.figure() plt.hist(data, 20) plot_path = os.path.join(PP.dots_samples, '%s_hist_%s_dot_size.jpg' % (which, c)) plt.xlabel('dot_size') plt.ylabel('amount') plt.title('%s %s histogram of dot sizes' % (which, c)) plt.grid(True) plt.savefig(plot_path)
def download_videos(vid_id, which): video_format = 'mp4' if which == 'test': save_folder_path = os.path.join(tools.main_path, which) else: category = tools.get_category(which, vid_id) save_folder_path = os.path.join(tools.main_path, which, category) opt_makedirs(save_folder_path) raw_video_path = os.path.join(save_folder_path, '%s_raw.mp4' % vid_id) slice_path = os.path.join(save_folder_path, '%s.mp4' % vid_id) # check if another process is already downloading it or if it has already been downloaded if os.path.exists(raw_video_path): is_success = False opt_reason = 'raw_exists' return is_success, opt_reason elif os.path.exists(slice_path): is_success = False opt_reason = 'download_complete' return is_success, opt_reason cookies_path = '/fast/gabras/kinetics400_downloader/cookies2.txt' download_command = "youtube-dl https://youtube.com/watch?v=%s --cookies %s --quiet -f bestvideo[ext=%s]+bestaudio/best --output %s --no-continue" \ % (vid_id, cookies_path, video_format, raw_video_path) download_proc = subprocess.Popen(download_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = download_proc.communicate() stderr = stderr.decode('utf-8') stderr = stderr.strip() if 'Syntax error' in stderr: print('here1') elif 'No such file or directory' in stderr: print('here2') if 'mkv' in stderr: raw_video_path = os.path.join(save_folder_path, '%s_raw.mkv' % vid_id) download_success = True elif download_proc.returncode == 0: download_success = True opt_reason = None else: if "YouTube said: Unable" not in stderr: print('here') download_success = False opt_reason = stderr # cut at indicated times if download_success: clip_start, clip_end = tools.get_clip_times(which, vid_id) cut_command = "ffmpeg -loglevel quiet -i %s -strict -2 -ss %f -to %f %s" \ % (raw_video_path, clip_start, clip_end, slice_path) cut_proc = subprocess.Popen(cut_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) stdout, stderr = cut_proc.communicate() if cut_proc.returncode == 0: cut_success = True opt_reason = None else: cut_success = False opt_reason = stderr.decode('utf-8') if cut_success: assert os.path.exists(raw_video_path) and os.path.exists( slice_path) # ========================= os.remove(raw_video_path) # ========================= cut_proc.kill() else: cut_success = False if download_success and cut_success: is_success = True else: is_success = False download_proc.kill() del (stderr) del (stdout) return is_success, opt_reason
def standardize_single_video(video_path, height, width, frames, channels=3): which = video_path.split('/')[5] category = video_path.split('/')[6] video_name = video_path.split('/')[-1].split('.')[0] cat_path = os.path.join(PP.kinetics400_dataset_150_224, which, category) opt_makedirs(cat_path) save_path = os.path.join(cat_path, '%s.avi' % video_name) if not os.path.exists(save_path): video = skvid.vread(video_path) # (frames, height, width, channels) # choose frames num_frames = video.shape[0] if num_frames < frames: missing_frames = frames - num_frames copy_number_times = math.ceil(missing_frames / num_frames) new_num_frames = num_frames frames_to_copy = list(np.arange(0, num_frames)) while new_num_frames < frames: if copy_number_times == 1: extra_frames = list(np.arange(0, missing_frames)) else: extra_frames = list(np.arange(0, num_frames)) frames_to_copy.extend(extra_frames) frames_to_copy.sort() new_num_frames = len(frames_to_copy) missing_frames = frames - new_num_frames if missing_frames > 0: copy_number_times = math.ceil(missing_frames / num_frames) else: try: assert len(frames_to_copy) == frames except AssertionError: print('now we are here') elif num_frames > frames: if num_frames / frames > 2: # copy half of frames, get center frames_to_copy = list(np.arange(0, num_frames, 2)) center = len(frames_to_copy) // 2 to_copy_half = frames // 2 - 1 # copy around the denter p1 = list(frames_to_copy[center - to_copy_half:center]) p2 = list(frames_to_copy[center + 1:center + 1 + to_copy_half]) new_num_frames = len(p1) + len(p2) + 1 if new_num_frames == frames: frames_to_copy = p1 + frames_to_copy[center] + p2 else: try: assert frames > new_num_frames except AssertionError: print('here as well') diff = frames - new_num_frames for i in range(diff): if i % 2 == 1: # copy from left cop = frames_to_copy[center - to_copy_half - i] p1.append(cop) else: # copy from right cop = frames_to_copy[center + 1 + to_copy_half + i] p2.append(cop) frames_to_copy = p1 + [frames_to_copy[center]] + p2 frames_to_copy.sort() # interval = math.floor(num_frames / frames) # frames_to_copy = list(np.arange(0, num_frames, interval)) # # copied = len(frames_to_copy) # # if copied < frames: # # add random frames not previously chosen # diff = frames - copied # bag = list(set(list(np.arange(0, num_frames))) - set(frames_to_copy)) # random_indices = random.sample(bag, k=diff) # frames_to_copy.extend(random_indices) # assert len(frames_to_copy) == frames # # elif len(frames_to_copy) > frames: # # remove random indices # diff = copied - frames # random_indices = random.sample(frames_to_copy, k=diff) # frames_to_copy = list(set(frames_to_copy) - set(random_indices)) # assert len(frames_to_copy) == frames # # else: # assert len(frames_to_copy) == frames else: frames_to_remove = [ n for n in range( 0, num_frames, int(math.ceil(num_frames / (num_frames - frames)))) ] leftover = num_frames - len(frames_to_remove) if leftover < frames: random_indices = random.sample(frames_to_remove, k=(frames - leftover)) for n in random_indices: frames_to_remove.remove(n) assert num_frames - len(frames_to_remove) == frames elif leftover > frames: to_add = leftover - frames if to_add == 1: frames_to_remove.append(frames_to_remove[-1] - 1) else: selection_list = [i for i in range(num_frames)] tmp = [] ind = 0 while len(tmp) != num_frames: tmp.append(selection_list.pop(ind)) if ind == 0: ind = -1 else: ind = 0 for i in range(len(tmp)): if tmp[i] not in frames_to_remove: selection_list.append(tmp[i]) for a_t in range(to_add): frames_to_remove.append(selection_list[a_t]) frames_to_remove.sort() frames_to_copy = list(np.arange(0, num_frames)) for n in frames_to_remove: frames_to_copy.remove(n) assert len(frames_to_copy) == frames else: frames_to_copy = list(np.arange(0, num_frames)) frames_to_copy.sort() # collect the relevant frames video = video[frames_to_copy] new_video = np.zeros(shape=(frames, height, width, channels), dtype=np.uint8) for i in range(frames): new_video[i] = adjust_frame(video[i], height, width, channels) skvid.vwrite(save_path, new_video)
def make_dataset(which, num_samples_per_class, num_frames, side, parameters=None): ''' rad_dot_m_t, num_dots_low_t, num_dots_high_t = parameters rad_dot_m_r, num_dots_low_r, num_dots_high_r = parameters num_dots_low_s, num_dots_high_s, radius_dot_min_s_pos_dir, radius_dot_max_s_pos_dir, radius_dot_min_s_neg_dir, radius_dot_max_s_neg_dir = parameters parameters = [rad_dot_m_t, num_dots_low_t, num_dots_high_t, rad_dot_m_r, num_dots_low_r, num_dots_high_r, num_dots_low_s, num_dots_high_s, radius_dot_min_s_pos_dir, radius_dot_max_s_pos_dir, radius_dot_min_s_neg_dir, radius_dot_max_s_neg_dir] ''' print('which: %s' % which) which_seeds = [6, 42, 420] if which == 'train': master_seed = which_seeds[0] elif which == 'val': master_seed = which_seeds[1] else: master_seed = which_seeds[2] np.random.seed(master_seed) avi_which_path = os.path.join(PP.dots_dataset_avi, which) frames_which_path = os.path.join(PP.dots_dataset_frames, which) classes = ['rotate', 'scale', 'translate'] class_seeds = np.random.randint(0, 10000, 3) for i, c in enumerate(classes): print('class: %s' % c) if parameters is not None: if c == 'translate': params = parameters[0:3] elif c == 'rotate': params = parameters[3:6] elif c == 'scale': params = parameters[6:] else: print('this is a weird class %s' % c) params = None else: params = None np.random.seed(class_seeds[i]) video_seeds = np.random.randint(0, 1000000, num_samples_per_class) avi_class_path = os.path.join(avi_which_path, c) frames_class_path = os.path.join(frames_which_path, c) U.opt_makedirs(avi_class_path) U.opt_makedirs(frames_class_path) # for s in tqdm(range(num_samples_per_class)): for s in range(num_samples_per_class): all_frames_array, annotation = generate_sequence_dots( c, num_frames, video_seeds[s], side, parameters=params) choose_frame = np.random.randint(0, num_frames) frame = all_frames_array[choose_frame] # save avi if c == 'rotate': num = s elif c == 'scale': num = s + num_samples_per_class else: num = s + 2 * num_samples_per_class vid = '%05d' % (num + 1) avi_save_path = os.path.join(avi_class_path, '%s.avi' % vid) skvid.vwrite(avi_save_path, all_frames_array) # save avi annotation avi_annotation = os.path.join(PP.dots_dataset_avi, 'annotations_%s.txt' % which) with open(avi_annotation, 'a') as my_file: # vid,class,seed,direction_hor,direction_ver,speed,frames (if translate) # vid,class,seed,direction,direction,speed,frames (else) line = '%s,%s,%d,%d,%d,%d,%d\n' % ( vid, c, annotation[0], annotation[1], annotation[2], annotation[3], num_frames) my_file.write(line) # save frame frame_save_path = os.path.join(frames_class_path, '%s.jpg' % vid) frame = Image.fromarray(frame, mode='L') frame.save(frame_save_path) # save frame annotation frame_annotation = os.path.join(PP.dots_dataset_frames, 'annotations_%s.txt' % which) with open(frame_annotation, 'a') as my_file: # vid,class,frame line = '%s,%s,%d\n' % (vid, c, choose_frame) my_file.write(line)