Skip to content

Commit

Permalink
added in command line auto annotation runner (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
benhoff authored and nmanovic committed Jul 11, 2019
1 parent 018cd14 commit 6e1e063
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 20 additions & 5 deletions cvat/apps/auto_annotation/model_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = []
Expand Down Expand Up @@ -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,
Expand Down
76 changes: 76 additions & 0 deletions utils/auto_annotation/run_model.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 6e1e063

Please sign in to comment.