Skip to content

Commit d692bb0

Browse files
committed
updated label utils
1 parent 19d7f0a commit d692bb0

File tree

3 files changed

+55
-34
lines changed

3 files changed

+55
-34
lines changed

cord/configs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from abc import ABCMeta
2020
import cord.exceptions
2121

22-
CORD_ENDPOINT = 'https://api.cord.tech/public'
22+
CORD_ENDPOINT = 'http://api.cord.tech/public'
2323
_CORD_PROJECT_ID = 'CORD_PROJECT_ID'
2424
_CORD_API_KEY = 'CORD_API_KEY'
2525

cord/utils/label_utils.py

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,65 @@
1717
from cord.utils.str_constants import *
1818

1919

20-
def construct_answer_dictionaries(label):
20+
def construct_answer_dictionaries(label_row):
2121
"""
22-
Adds answer object and classification answer dictionaries from a blurb if they do not exist.
22+
Adds answer object and classification answer dictionaries from a label row if they do not exist.
2323
Integrity checks are conducted upon saving of labels.
2424
2525
Args:
26-
label: A label blurb.
26+
label_row: A label row.
2727
2828
Returns:
2929
LabelRow: A label blurb instance with updated answer dictionaries
3030
"""
31-
label = LabelRow(label) # Cast to Label ORM
32-
labels = label.labels
31+
label_row = LabelRow(label_row) # Cast to Label ORM
32+
data_type = label_row.data_type
33+
data_units = label_row.data_units
3334

34-
if OBJECT_ANSWERS in label:
35-
object_answers = label.object_answers
36-
else:
37-
object_answers = {}
35+
object_answers = data_units.get(OBJECT_ANSWERS, {})
36+
classification_answers = data_units.get(CLASSIFICATION_ANSWERS, {})
3837

39-
if CLASSIFICATION_ANSWERS in label:
40-
classification_answers = label.classification_answers
41-
else:
42-
classification_answers = {}
38+
for du in data_units: # Iterate over data units in label row
39+
data_unit = data_units[du]
4340

44-
for frame in labels:
45-
items = labels[frame].get(OBJECTS) + labels[frame].get(CLASSIFICATIONS)
41+
if LABELS in data_unit:
42+
labels = data_unit.get(LABELS)
4643

47-
for item in items:
48-
if OBJECT_HASH in item:
49-
object_hash = item.get(OBJECT_HASH)
50-
if object_hash not in object_answers:
51-
object_answers[object_hash] = {
52-
OBJECT_HASH: object_hash,
53-
CLASSIFICATIONS: [],
54-
}
44+
if data_type == IMG_GROUP: # Go through images
45+
items = labels.get(OBJECTS) + labels.get(CLASSIFICATIONS)
46+
add_answers_to_items(items, classification_answers, object_answers)
5547

56-
if CLASSIFICATION_HASH in item:
57-
classification_hash = item.get(CLASSIFICATION_HASH)
58-
if classification_hash not in classification_answers:
59-
classification_answers[classification_hash] = {
60-
CLASSIFICATION_HASH: classification_hash,
61-
CLASSIFICATIONS: [],
62-
}
48+
elif data_type == VIDEO:
49+
for frame in labels: # Go through frames
50+
items = labels[frame].get(OBJECTS) + labels[frame].get(CLASSIFICATIONS)
51+
add_answers_to_items(items, classification_answers, object_answers)
6352

64-
label[OBJECT_ANSWERS] = object_answers
65-
label[CLASSIFICATION_ANSWERS] = classification_answers
66-
return label
53+
label_row.data_units[OBJECT_ANSWERS] = object_answers
54+
label_row.data_units[CLASSIFICATION_ANSWERS] = classification_answers
55+
return label_row
56+
57+
58+
# -------------------------------------------------
59+
# HELPER FUNCTIONS
60+
# -------------------------------------------------
61+
def add_answers_to_items(items, classification_answers, object_answers):
62+
"""
63+
If object_hash (uid) or classification_hash (uid) are not in answer dictionaries,
64+
add key entry with empty classification list.
65+
"""
66+
for item in items:
67+
if OBJECT_HASH in item:
68+
object_hash = item.get(OBJECT_HASH)
69+
if object_hash not in object_answers:
70+
object_answers[object_hash] = {
71+
OBJECT_HASH: object_hash,
72+
CLASSIFICATIONS: [],
73+
}
74+
75+
if CLASSIFICATION_HASH in item:
76+
classification_hash = item.get(CLASSIFICATION_HASH)
77+
if classification_hash not in classification_answers:
78+
classification_answers[classification_hash] = {
79+
CLASSIFICATION_HASH: classification_hash,
80+
CLASSIFICATIONS: [],
81+
}

cord/utils/str_constants.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
# Label row constants
2+
LABELS = "labels"
13
OBJECTS = "objects"
24
CLASSIFICATIONS = "classifications"
35
OBJECT_HASH = "objectHash"
46
CLASSIFICATION_HASH = "classificationHash"
57
OBJECT_ANSWERS = "object_answers"
68
CLASSIFICATION_ANSWERS = "classification_answers"
9+
10+
# Data types
11+
VIDEO = "video"
12+
IMG_GROUP = "img_group"

0 commit comments

Comments
 (0)