From 6e1e063c041e2e349cd0d7b10818580068a4ea97 Mon Sep 17 00:00:00 2001 From: Ben Hoff Date: Thu, 11 Jul 2019 10:33:06 -0400 Subject: [PATCH] added in command line auto annotation runner (#563) --- CHANGELOG.md | 1 + cvat/apps/auto_annotation/model_manager.py | 25 +++++-- utils/auto_annotation/run_model.py | 76 ++++++++++++++++++++++ 3 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 utils/auto_annotation/run_model.py diff --git a/CHANGELOG.md b/CHANGELOG.md index d403dab1bae5..54089c822c4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Auto annotation using Pixel Link mobilenet v2 - text detection (utils/open_model_zoo) - Ability to create a custom extractors for unsupported media types - Added in PDF extractor +- Added in a command line model manager tester ### Changed - Outside and keyframe buttons in the side panel for all interpolation shapes (they were only for boxes before) diff --git a/cvat/apps/auto_annotation/model_manager.py b/cvat/apps/auto_annotation/model_manager.py index 4f17d4d7d6f6..b3c03d6e68c2 100644 --- a/cvat/apps/auto_annotation/model_manager.py +++ b/cvat/apps/auto_annotation/model_manager.py @@ -44,7 +44,7 @@ def _delete_source_files(): def _run_test(model_file, weights_file, labelmap_file, interpretation_file): test_image = np.ones((1024, 1980, 3), np.uint8) * 255 try: - _run_inference_engine_annotation( + run_inference_engine_annotation( data=[test_image,], model_file=model_file, weights_file=weights_file, @@ -266,6 +266,9 @@ def _create_polyshape(points, label, frame_number, attributes=None): "attributes": attributes or {}, } +class InterpreterError(Exception): + pass + def _process_detections(detections, path_to_conv_script, restricted=True): results = Results() local_vars = { @@ -290,11 +293,23 @@ def _process_detections(detections, path_to_conv_script, restricted=True): imports = import_modules(source_code) global_vars.update(imports) - exec(source_code, global_vars, local_vars) + try: + exec(source_code, global_vars, local_vars) + except SyntaxError as err: + error_class = err.__class__.__name__ + detail = err.args[0] + line_number = err.lineno + except Exception as err: + error_class = err.__class__.__name__ + detail = err.args[0] + cl, exc, tb = sys.exc_info() + line_number = traceback.extract_tb(tb)[-1][1] + else: + return results - return results + raise InterpreterError("%s at line %d: %s" % (error_class, line_number, detail)) -def _run_inference_engine_annotation(data, model_file, weights_file, +def run_inference_engine_annotation(data, model_file, weights_file, labels_mapping, attribute_spec, convertation_file, job=None, update_progress=None, restricted=True): def process_attributes(shape_attributes, label_attr_spec): attributes = [] @@ -377,7 +392,7 @@ def update_progress(job, progress): result = None slogger.glob.info("auto annotation with openvino toolkit for task {}".format(tid)) - result = _run_inference_engine_annotation( + result = run_inference_engine_annotation( data=get_image_data(db_task.get_data_dirname()), model_file=model_file, weights_file=weights_file, diff --git a/utils/auto_annotation/run_model.py b/utils/auto_annotation/run_model.py new file mode 100644 index 000000000000..60499c7178e9 --- /dev/null +++ b/utils/auto_annotation/run_model.py @@ -0,0 +1,76 @@ +import os +import sys +import json +import argparse +import traceback + +os.environ['DJANGO_SETTINGS_MODULE'] = 'cvat.settings.production' + +import django +django.setup() + +import numpy as np +import cv2 + +from cvat.apps.auto_annotation.model_manager import run_inference_engine_annotation + + +def _get_kwargs(): + parser = argparse.ArgumentParser() + parser.add_argument('--py', required=True, help='Path to the python interpt file') + parser.add_argument('--xml', required=True, help='Path to the xml file') + parser.add_argument('--bin', required=True, help='Path to the bin file') + parser.add_argument('--json', required=True, help='Path to the JSON mapping file') + parser.add_argument('--restricted', dest='restricted', action='store_true') + parser.add_argument('--unrestricted', dest='restricted', action='store_false') + parser.add_argument('--image-files', nargs='*', help='Paths to image files you want to test') + + return vars(parser.parse_args()) + + +class InterpreterError(Exception): + pass + + +def main(): + kwargs = _get_kwargs() + + py_file = kwargs['py'] + bin_file = kwargs['bin'] + mapping_file = kwargs['json'] + xml_file = kwargs['xml'] + + if not os.path.isfile(py_file): + print('Py file not found! Check the path') + return + + if not os.path.isfile(bin_file): + print('Bin file is not found! Check path!') + return + + if not os.path.isfile(xml_file): + print('XML File not found! Check path!') + return + + if not os.path.isfile(mapping_file): + print('JSON file is not found! Check path!') + return + + with open(mapping_file) as json_file: + mapping = json.load(json_file) + + restricted = kwargs['restricted'] + image_files = kwargs.get('image_files') + print(image_files, kwargs.keys()) + + if image_files: + image_data = [cv2.imread(f) for f in image_files] + else: + test_image = np.ones((1024, 1980, 3), np.uint8) * 255 + image_data = [test_image,] + attribute_spec = {} + results = run_inference_engine_annotation(image_data, xml_file, bin_file,mapping, attribute_spec, py_file, restricted=restricted) + print('Program Worked!') + +if __name__ == '__main__': + main()