def video2coco(video_fname, output_folder, image_extraction=False, width=300, height=450): json_output = os.path.join(output_folder, 'coco_annotations.json') os.makedirs(output_folder, exist_ok=True) video = Video.load(video_fname) coco_file = extract_annotations(video, width=width, height=height) save_json(json_output, coco_file) if image_extraction: output_folder_images = os.path.join(output_folder, 'images') os.makedirs(output_folder_images, exist_ok=True) extract_images(output_folder_images, video) return
def extract_annotations(video, width=300, height=450): keypoints, skeleton = get_full_skeleton(video) coco_file = coco_file_init(keypoints, skeleton) Body.width = width Body.height = height image_id = 1 annotation_id = 1 video_name = video.video_name video_name, _ = os.path.splitext(video_name) for frame in tqdm(video): image_data = image_annotation(video_name, frame.id, image_id) empty_frame = True for body in frame: ann = body_annotation(body, keypoints, annotation_id, image_id, width=width, height=height) annotation_id += 1 coco_file['annotations'].append(ann) empty_frame = False if not empty_frame: coco_file['images'].append(image_data) image_id += 1 return coco_file save_json(json_output, coco_file)
def export_tagged(self, output_folder, save_images=True): tag_bodies = self.tagged() _, video_name = os.path.split(self.video_path) video_name, ext = os.path.splitext(video_name) out_path = os.path.join(output_folder, video_name) os.makedirs(out_path, exist_ok=True) json_path = os.path.join(output_folder, "{}.json".format(video_name)) tagged_json= list() for body in tqdm(tag_bodies): tagged_json.append(body.params()) if save_images: fname = "TID{:05}_F{:08}.jpg".format(body.tag_id, body.frameid) fpath = os.path.join(out_path, fname) body.save(fpath) # print("body saved.") save_json(json_path, tagged_json) return
def save(self, path): video_json = self.json() save_json(path, video_json)
def save(self, path): collection_json = self.json() save_json(path, collection_json)