From db18595727de8e6631ec6cec07c630daa334400f Mon Sep 17 00:00:00 2001 From: Andrey Zhavoronkov Date: Tue, 12 May 2020 12:47:51 +0300 Subject: [PATCH 1/3] fixed issues --- utils/auto_annotation/run_model.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/auto_annotation/run_model.py b/utils/auto_annotation/run_model.py index 111141fbc9c8..e9902d239290 100644 --- a/utils/auto_annotation/run_model.py +++ b/utils/auto_annotation/run_model.py @@ -36,7 +36,7 @@ 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()) @@ -74,7 +74,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 +82,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)) @@ -148,6 +149,7 @@ def main(): logging.critical('JSON file is not found! Check path!') return + mapping_file = os.path.abspath(mapping_file) with open(mapping_file) as json_file: try: mapping = json.load(json_file) From a87843ae6f57ab3738f1f8478d954f9b32af4671 Mon Sep 17 00:00:00 2001 From: Andrey Zhavoronkov Date: Tue, 12 May 2020 20:04:03 +0300 Subject: [PATCH 2/3] fixed issues and stored credentials cookies inside the session --- utils/auto_annotation/run_model.py | 16 +++++++--------- utils/cli/cli.py | 19 ++++++++----------- utils/cli/core/core.py | 16 ++++++++++++++-- 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/utils/auto_annotation/run_model.py b/utils/auto_annotation/run_model.py index e9902d239290..9df63528ed4d 100644 --- a/utils/auto_annotation/run_model.py +++ b/utils/auto_annotation/run_model.py @@ -39,6 +39,10 @@ def _get_kwargs(): 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 @@ -95,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') @@ -149,7 +150,6 @@ def main(): logging.critical('JSON file is not found! Check path!') return - mapping_file = os.path.abspath(mapping_file) with open(mapping_file) as json_file: try: mapping = json.load(json_file) @@ -235,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 f22bf81520c2..0e2383c87bdc 100755 --- a/utils/cli/cli.py +++ b/utils/cli/cli.py @@ -2,7 +2,6 @@ # # SPDX-License-Identifier: MIT import logging -import requests import sys from http.client import HTTPConnection from core.core import CLI, CVAT_API_V1 @@ -27,16 +26,14 @@ def main(): 'upload': CLI.tasks_upload} args = parser.parse_args() config_log(args.loglevel) - with requests.Session() as session: - session.auth = args.auth - api = CVAT_API_V1(args.server_host, args.server_port) - cli = CLI(session, api) - try: - actions[args.action](cli, **args.__dict__) - except (requests.exceptions.HTTPError, - requests.exceptions.ConnectionError, - requests.exceptions.RequestException) as e: - log.critical(e) + api = CVAT_API_V1(args.server_host, args.server_port) + cli = CLI(args.auth, api) + try: + actions[args.action](cli, **args.__dict__) + except (requests.exceptions.HTTPError, + requests.exceptions.ConnectionError, + requests.exceptions.RequestException) as e: + log.critical(e) if __name__ == '__main__': diff --git a/utils/cli/core/core.py b/utils/cli/core/core.py index 4f8f847e7ba0..8b5778f7ae78 100644 --- a/utils/cli/core/core.py +++ b/utils/cli/core/core.py @@ -14,9 +14,10 @@ class CLI(): - def __init__(self, session, api): + def __init__(self, credentials, api): self.api = api - self.session = session + self.session = requests.Session() + self.login(credentials) def tasks_data(self, task_id, resource_type, resources): """ Add local, remote, or shared files to an existing task. """ @@ -141,6 +142,13 @@ 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 = requests.post(url, auth) + response.raise_for_status() + self.session.cookies = response.cookies + class CVAT_API_V1(): """ Build parameterized API URLs """ @@ -171,3 +179,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={}' \ .format(name, fileformat) + + @property + def login(self): + return self.base + 'auth/login' From 3fd322a1d1d461345a876c55477db1e1c74f1c7a Mon Sep 17 00:00:00 2001 From: Andrey Zhavoronkov Date: Tue, 12 May 2020 21:39:21 +0300 Subject: [PATCH 3/3] fixed tests --- utils/cli/cli.py | 18 ++++++++++-------- utils/cli/core/core.py | 9 +++++---- utils/cli/tests/test_cli.py | 5 ++--- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/utils/cli/cli.py b/utils/cli/cli.py index 0e2383c87bdc..1fc42d6b9be7 100755 --- a/utils/cli/cli.py +++ b/utils/cli/cli.py @@ -2,6 +2,7 @@ # # SPDX-License-Identifier: MIT import logging +import requests import sys from http.client import HTTPConnection from core.core import CLI, CVAT_API_V1 @@ -26,14 +27,15 @@ def main(): 'upload': CLI.tasks_upload} args = parser.parse_args() config_log(args.loglevel) - api = CVAT_API_V1(args.server_host, args.server_port) - cli = CLI(args.auth, api) - try: - actions[args.action](cli, **args.__dict__) - except (requests.exceptions.HTTPError, - requests.exceptions.ConnectionError, - requests.exceptions.RequestException) as e: - log.critical(e) + with requests.Session() as session: + api = CVAT_API_V1(args.server_host, args.server_port) + cli = CLI(session, api, args.auth) + try: + actions[args.action](cli, **args.__dict__) + except (requests.exceptions.HTTPError, + requests.exceptions.ConnectionError, + requests.exceptions.RequestException) as e: + log.critical(e) if __name__ == '__main__': diff --git a/utils/cli/core/core.py b/utils/cli/core/core.py index 8b5778f7ae78..215b8c4ecf36 100644 --- a/utils/cli/core/core.py +++ b/utils/cli/core/core.py @@ -14,9 +14,9 @@ class CLI(): - def __init__(self, credentials, api): + def __init__(self, session, api, credentials): self.api = api - self.session = requests.Session() + self.session = session self.login(credentials) def tasks_data(self, task_id, resource_type, resources): @@ -145,9 +145,10 @@ def tasks_upload(self, task_id, fileformat, filename, **kwargs): def login(self, credentials): url = self.api.login auth = {'username': credentials[0], 'password': credentials[1]} - response = requests.post(url, auth) + response = self.session.post(url, auth) response.raise_for_status() - self.session.cookies = response.cookies + if 'csrftoken' in response.cookies: + self.session.headers['X-CSRFToken'] = response.cookies['csrftoken'] class CVAT_API_V1(): diff --git a/utils/cli/tests/test_cli.py b/utils/cli/tests/test_cli.py index 957093ccf487..8bf5baee4795 100644 --- a/utils/cli/tests/test_cli.py +++ b/utils/cli/tests/test_cli.py @@ -5,7 +5,6 @@ import sys import unittest from django.conf import settings -from requests.auth import HTTPBasicAuth from utils.cli.core import CLI, CVAT_API_V1, ResourceType from rest_framework.test import APITestCase, RequestsClient from cvat.apps.engine.tests.test_rest_api import create_db_users @@ -18,9 +17,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'}],