forked from HumanSignal/label-studio-ml-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (51 loc) · 2.49 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
from PIL import Image, ImageOps
from label_studio_tools.core.utils.params import get_env
from label_studio_tools.core.utils.io import get_local_path
DATA_UNDEFINED_NAME = '$undefined$'
logger = logging.getLogger(__name__)
def get_single_tag_keys(parsed_label_config, control_type, object_type):
"""
Gets parsed label config, and returns data keys related to the single control tag and the single object tag schema
(e.g. one "Choices" with one "Text")
:param parsed_label_config: parsed label config returned by "label_studio.misc.parse_config" function
:param control_type: control tag str as it written in label config (e.g. 'Choices')
:param object_type: object tag str as it written in label config (e.g. 'Text')
:return: 3 string keys and 1 array of string labels: (from_name, to_name, value, labels)
"""
assert len(parsed_label_config) == 1
from_name, info = list(parsed_label_config.items())[0]
assert info['type'] == control_type, 'Label config has control tag "<' + info['type'] + '>" but "<' + control_type + '>" is expected for this model.' # noqa
assert len(info['to_name']) == 1
assert len(info['inputs']) == 1
assert info['inputs'][0]['type'] == object_type
to_name = info['to_name'][0]
value = info['inputs'][0]['value']
return from_name, to_name, value, info['labels']
def get_first_tag_keys(parsed_label_config, control_type, object_type):
"""
Reads config and returns the first control tag and the first object tag that match the given types
:param parsed_label_config:
:param control_type:
:param object_type:
:return:
"""
for from_name, info in parsed_label_config.items():
if info['type'] == control_type:
for input in info['inputs']:
if input['type'] == object_type:
return from_name, info
return None, None
def is_skipped(completion):
if len(completion['annotations']) != 1:
return False
completion = completion['annotations'][0]
return completion.get('skipped', False) or completion.get('was_cancelled', False)
def get_choice(completion):
return completion['annotations'][0]['result'][0]['value']['choices'][0]
def get_image_local_path(url, image_cache_dir=None, project_dir=None, image_dir=None):
return get_local_path(url, image_cache_dir, project_dir, get_env('HOSTNAME'), image_dir)
def get_image_size(filepath):
img = Image.open(filepath)
img = ImageOps.exif_transpose(img)
return img.size