Skip to content

Commit

Permalink
fixed linter issues and store credentials cookie in the session object (
Browse files Browse the repository at this point in the history
cvat-ai#1526)

* fixed issues

* fixed issues and stored credentials cookies inside the session

* fixed tests
  • Loading branch information
azhavoro authored and Fernando Martínez González committed Aug 3, 2020
1 parent c11a88d commit 89462be
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
20 changes: 10 additions & 10 deletions utils/auto_annotation/run_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -63,25 +67,23 @@ 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

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
xml_file = model.model_file.name
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))
Expand All @@ -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')
Expand Down Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions utils/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 14 additions & 1 deletion utils/cli/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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. """
Expand Down Expand Up @@ -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 """
Expand Down Expand Up @@ -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'
5 changes: 2 additions & 3 deletions utils/cli/tests/_test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'}],
Expand Down

0 comments on commit 89462be

Please sign in to comment.