clips_folder = 'test_overlap_clips' if os.path.exists(clips_folder): shutil.rmtree(clips_folder) os.makedirs(clips_folder) # Test the annotator from annotator import Annotator # Initialise the annotator annotator = Annotator([{ 'name': 'test_label_1', 'color': (0, 1, 0) }, { 'name': 'test_label_2', 'color': (0, 0, 1) }, { 'name': 'test_label_3', 'color': (0, 1, 1) }], clips_folder, loop_duration=2, annotation_file='overlap_annotation.json', status_file='overlap_status.json') # Create the overlapping clips annotator.video_to_clips('dummy_digits.mp4', clips_folder, resize=0.5, overlap=0.5, clip_length=6) # Run! annotator.main()
def run_tool(video_file, labels): videoFileName = video_file.split("/")[-1].split(".")[0] out_folder = './output/' if not os.path.exists(out_folder): os.mkdir(out_folder) output_folder = f'{out_folder}{videoFileName}/' if not os.path.exists(output_folder): os.mkdir(output_folder) clips_folder = output_folder + 'clips/' json_file = f'{output_folder}{videoFileName}.json' ref_labels = f'{output_folder}{videoFileName}_ref_labels.json' #Copy the reference json file in to the folder copyfile(labels, ref_labels) # Create the folders if not os.path.exists(clips_folder): os.mkdir(clips_folder) print(json_file) colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (192, 192, 192), (128, 128, 128), (128, 0, 0), (128, 128, 0), (0, 128, 0), (128, 0, 128), (0, 128, 128), (0, 0, 128)] # Initialise the annotator with open(labels) as f: lines = f.readlines() labels = [l.strip() for l in lines] assert len(labels) > 0, "Labels file doesn't have any labels" if len(labels) <= len(colors): colors_need = colors[:len(labels)] else: t = len(labels) // len(colors) r = len(labels) % len(colors) colors_need = t * colors + colors[:r] assert len(labels) == len(colors_need), "Check the code for error" annotation_labels = [] for idx, label in enumerate(labels): annotation_labels.append({'name': label, 'color': colors_need[idx]}) annotator = Annotator(annotation_labels, clips_folder, sort_files_list=True, N_show_approx=16, screen_ratio=16 / 9, image_resize=1, loop_duration=None, annotation_file=json_file) bClippingRequired = True try: filesArr = os.listdir(clips_folder) if len(filesArr) > 0: bClippingRequired = False except: print("no files") if bClippingRequired: # Split the video into clips print('Generating clips from the video...') annotator.video_to_clips(video_file, clips_folder, clip_length=60, overlap=0, resize=0.5) # Run the annotator annotator.main()