diff --git a/utils/auto_annotation/run_model.py b/utils/auto_annotation/run_model.py index 111141fbc9c8..9df63528ed4d 100644 --- a/utils/auto_annotation/run_model.py +++ b/utils/auto_annotation/run_model.py @@ -36,9 +36,13 @@ def _get_kwargs(): parser.add_argument('--show-image-delay', default=0, type=int, help='Displays the images for a set duration in milliseconds, default is until a key is pressed') parser.add_argument('--serialize', default=False, action='store_true', help='Try to serialize the result') parser.add_argument('--show-labels', action='store_true', help='Show the labels on the window') - + return vars(parser.parse_args()) +def _init_django(settings): + import django + os.environ['DJANGO_SETTINGS_MODULE'] = settings + django.setup() def random_color(): rgbl=[255,0,0] @@ -63,10 +67,7 @@ def find_min_y(array): return array[index] def _get_docker_files(model_name: str, task_id: int): - os.environ['DJANGO_SETTINGS_MODULE'] = 'cvat.settings.development' - - import django - django.setup() + _init_django('cvat.settings.development') from cvat.apps.auto_annotation.models import AnnotationModel from cvat.apps.engine.models import Task as TaskModel @@ -74,7 +75,7 @@ def _get_docker_files(model_name: str, task_id: int): task = TaskModel(pk=task_id) model = AnnotationModel.objects.get(name=model_name) - images_dir = task.get_data_dirname() + images_dir = task.data.get_data_dirname() py_file = model.interpretation_file.name mapping_file = model.labelmap_file.name @@ -82,6 +83,7 @@ def _get_docker_files(model_name: str, task_id: int): bin_file = model.weights_file.name image_files = [] + images_dir = os.path.abspath(images_dir) for root, _, filenames in os.walk(images_dir): for filename in fnmatch.filter(filenames, '*.jpg'): image_files.append(os.path.join(root, filename)) @@ -94,7 +96,7 @@ def main(): py_file = kwargs.get('py') bin_file = kwargs.get('bin') - mapping_file = kwargs.get('json') + mapping_file = os.path.abspath(kwargs.get('json')) xml_file = kwargs.get('xml') model_name = kwargs.get('model_name') @@ -233,9 +235,7 @@ def main(): cv2.destroyWindow(str(index)) if kwargs['serialize']: - os.environ['DJANGO_SETTINGS_MODULE'] = 'cvat.settings.production' - import django - django.setup() + _init_django('cvat.settings.production') from cvat.apps.engine.serializers import LabeledDataSerializer diff --git a/utils/cli/cli.py b/utils/cli/cli.py index ed749d7e3312..9ac826b4bff4 100755 --- a/utils/cli/cli.py +++ b/utils/cli/cli.py @@ -28,9 +28,8 @@ def main(): args = parser.parse_args() config_log(args.loglevel) with requests.Session() as session: - session.auth = args.auth api = CVAT_API_V1('%s:%s' % (args.server_host, args.server_port)) - cli = CLI(session, api) + cli = CLI(session, api, args.auth) try: actions[args.action](cli, **args.__dict__) except (requests.exceptions.HTTPError, diff --git a/utils/cli/core/core.py b/utils/cli/core/core.py index b9ce5c0ff90c..f684608ea914 100644 --- a/utils/cli/core/core.py +++ b/utils/cli/core/core.py @@ -17,9 +17,10 @@ class CLI(): - def __init__(self, session, api): + def __init__(self, session, api, credentials): self.api = api self.session = session + self.login(credentials) def tasks_data(self, task_id, resource_type, resources): """ Add local, remote, or shared files to an existing task. """ @@ -144,6 +145,14 @@ def tasks_upload(self, task_id, fileformat, filename, **kwargs): "with annotation file {} finished".format(filename) log.info(logger_string) + def login(self, credentials): + url = self.api.login + auth = {'username': credentials[0], 'password': credentials[1]} + response = self.session.post(url, auth) + response.raise_for_status() + if 'csrftoken' in response.cookies: + self.session.headers['X-CSRFToken'] = response.cookies['csrftoken'] + class CVAT_API_V1(): """ Build parameterized API URLs """ @@ -174,3 +183,7 @@ def tasks_id_annotations_format(self, task_id, fileformat): def tasks_id_annotations_filename(self, task_id, name, fileformat): return self.tasks_id(task_id) + '/annotations?format={}&filename={}' \ .format(fileformat, name) + + @property + def login(self): + return self.base + 'auth/login' diff --git a/utils/cli/tests/_test_cli.py b/utils/cli/tests/_test_cli.py index 510b1ccc8c17..3977159cc657 100644 --- a/utils/cli/tests/_test_cli.py +++ b/utils/cli/tests/_test_cli.py @@ -10,7 +10,6 @@ from django.conf import settings from PIL import Image -from requests.auth import HTTPBasicAuth from rest_framework.test import APITestCase, RequestsClient from cvat.apps.engine.tests._test_rest_api import (create_db_users, @@ -22,9 +21,9 @@ class TestCLI(APITestCase): @unittest.mock.patch('sys.stdout', new_callable=io.StringIO) def setUp(self, mock_stdout): self.client = RequestsClient() - self.client.auth = HTTPBasicAuth('admin', 'admin') + self.credentials = ('admin', 'admin') self.api = CVAT_API_V1('testserver') - self.cli = CLI(self.client, self.api) + self.cli = CLI(self.client, self.api, self.credentials) self.taskname = 'test_task' self.cli.tasks_create(self.taskname, [{'name' : 'car'}, {'name': 'person'}],