|
17 | 17 | from cord.utils.str_constants import * |
18 | 18 |
|
19 | 19 |
|
20 | | -def construct_answer_dictionaries(label): |
| 20 | +def construct_answer_dictionaries(label_row): |
21 | 21 | """ |
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. |
23 | 23 | Integrity checks are conducted upon saving of labels. |
24 | 24 |
|
25 | 25 | Args: |
26 | | - label: A label blurb. |
| 26 | + label_row: A label row. |
27 | 27 |
|
28 | 28 | Returns: |
29 | 29 | LabelRow: A label blurb instance with updated answer dictionaries |
30 | 30 | """ |
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 |
33 | 34 |
|
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, {}) |
38 | 37 |
|
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] |
43 | 40 |
|
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) |
46 | 43 |
|
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) |
55 | 47 |
|
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) |
63 | 52 |
|
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 | + } |
0 commit comments