def dump_cvat_task_annotations(db_task, db_user, db_dumper, scheme, host): """Use CVAT's utilities to dump annotations for a task.""" task_id = db_task.id task_image_dir = db_task.get_data_dirname() timestamp = datetime.now().strftime('%Y_%m_%d_%H_%M_%S') output_file_path = os.path.join( db_task.get_task_dirname(), '{}.{}.{}.{}'.format(db_task.id, db_user.username, timestamp, db_dumper.format.lower())) cvat_annotation.dump_task_data(task_id, db_user, output_file_path, db_dumper, scheme, host) return output_file_path
def push(self, user, scheme, host, db_task, last_save): # Update local repository self._pull() os.makedirs(os.path.join(self._cwd, os.path.dirname(self._annotation_file)), exist_ok = True) # Remove old annotation file if it exists if os.path.exists(self._annotation_file): os.remove(self._annotation_file) # Initialize LFS if need if self._lfs: updated = False lfs_settings = ["*.xml\tfilter=lfs diff=lfs merge=lfs -text\n", "*.zip\tfilter=lfs diff=lfs merge=lfs -text\n"] if not os.path.isfile(os.path.join(self._cwd, ".gitattributes")): with open(os.path.join(self._cwd, ".gitattributes"), "w") as gitattributes: gitattributes.writelines(lfs_settings) updated = True else: with open(os.path.join(self._cwd, ".gitattributes"), "r+") as gitattributes: lines = gitattributes.readlines() for setting in lfs_settings: if setting not in lines: updated = True lines.append(setting) gitattributes.seek(0) gitattributes.writelines(lines) gitattributes.truncate() if updated: self._rep.git.add(['.gitattributes']) # Dump an annotation timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S") display_name = "CVAT XML 1.1" display_name += " for images" if self._task_mode == "annotation" else " for videos" cvat_dumper = AnnotationDumper.objects.get(display_name=display_name) dump_name = os.path.join(db_task.get_task_dirname(), "git_annotation_{}.".format(timestamp) + "dump") dump_task_data( pk=self._tid, user=user, filename=dump_name, dumper=cvat_dumper, scheme=scheme, host=host, ) ext = os.path.splitext(self._path)[1] if ext == '.zip': subprocess.call('zip -j -r "{}" "{}"'.format(self._annotation_file, dump_name), shell=True) elif ext == '.xml': shutil.copyfile(dump_name, self._annotation_file) else: raise Exception("Got unknown annotation file type") os.remove(dump_name) self._rep.git.add(self._annotation_file) # Merge diffs summary_diff = { "update": 0, "create": 0, "delete": 0 } old_diffs_dir = os.path.join(os.path.dirname(self._diffs_dir), 'repos_diffs') if (os.path.isdir(old_diffs_dir)): _read_old_diffs(old_diffs_dir, summary_diff) for diff_name in list(map(lambda x: os.path.join(self._diffs_dir, x), os.listdir(self._diffs_dir))): with open(diff_name, 'r') as f: diff = json.loads(f.read()) for key in diff: summary_diff[key] += diff[key] message = "CVAT Annotation updated by {}. \n".format(self._user["name"]) message += 'Task URL: {}://{}/dashboard?id={}\n'.format(scheme, host, db_task.id) if db_task.bug_tracker: message += 'Bug Tracker URL: {}\n'.format(db_task.bug_tracker) message += "Created: {}, updated: {}, deleted: {}\n".format( summary_diff["create"], summary_diff["update"], summary_diff["delete"] ) message += "Annotation time: {} hours\n".format(math.ceil((last_save - self._sync_date).total_seconds() / 3600)) message += "Total annotation time: {} hours".format(math.ceil((last_save - db_task.created_date).total_seconds() / 3600)) self._rep.index.commit(message) self._rep.git.push("origin", self._branch_name, "--force") shutil.rmtree(old_diffs_dir, True) shutil.rmtree(self._diffs_dir, True)