Skip to content

Commit

Permalink
fix: DEV-1924: Fix two identical OCR fields with no text give 0% agre…
Browse files Browse the repository at this point in the history
…ement (#57)
  • Loading branch information
KonstantinKorotaev authored Mar 31, 2022
1 parent f3b7797 commit b31ad2d
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 5 deletions.
11 changes: 9 additions & 2 deletions evalme/image/object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,16 @@ def compare(self, pred,
pred_results=pred_results,
algorithm=algorithm,
qval=qval)
if text_distance is None:
continue
else:
# in case of different labels or many labels
text_tag_in_result = [item for item in pred_types if
item != 'labels' and item not in OCREvalItem.OCR_SHAPES]
if not text_tag_in_result:
continue
text_distance = 0

# prepare result
if per_label:
item = pred_results_labels[0]['value']['labels']
Expand All @@ -518,7 +525,7 @@ def compare(self, pred,
return results, num_results
else:
values = results.values()
return sum(values) / len(values) if len(values) > 0 else 0
return sum(values) / len(values) if len(values) > 0 else None

def _get_max_iou_rectangles(self, gt, pred):
"""
Expand Down Expand Up @@ -575,7 +582,7 @@ def _compare_text_tags(self,
text_tag_in_result = [item for item in pred_types if item != 'labels' and item not in OCREvalItem.OCR_SHAPES]
# return 0 if there are no text tag in result
if len(text_tag_in_result) == 0:
return 0
return None
# construct list of text results
elif len(text_tag_in_result) == 1:
gt_results_text = gt_results[text_tag_in_result[0]]
Expand Down
143 changes: 140 additions & 3 deletions evalme/tests/test_object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def test_OCR_matching_function_no_rectangle():
obj1 = OCREvalItem(res1)
obj2 = OCREvalItem(res2)

assert obj1.compare(obj2) == 0
assert obj1.compare(obj2) == None


def test_OCR_matching_function_not_matching_text():
Expand Down Expand Up @@ -552,7 +552,7 @@ def test_OCR_matching_with_several_control_types():
assert score == 1.0


def test_ocr_2_gropus_of_regions_with_labels():
def test_ocr_2_groups_of_regions_with_labels():
"""
DEV-1721
Test 2 regions with same labels and 2 regions with different
Expand Down Expand Up @@ -781,4 +781,141 @@ def test_ocr_2_gropus_of_regions_with_labels():
o1 = OCREvalItem(result1)
o2 = OCREvalItem(result2)
score = o1.compare(o2)
assert score == 0.5
assert score == 0.5


def test_ocr_2_groups_of_regions_without_text():
"""
DEV-1721
Test 2 regions with same labels and 2 regions with different
:return:
"""
result1 = {"result": [{
"id": "Tp5yC-6hsd",
"type": "rectangle",
"value": {
"x": 26.666666666666632,
"y": 30.933333333333323,
"width": 12.933333333333314,
"height": 14.933333333333312,
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "bbox",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}, {
"id": "Tp5yC-6hsd",
"type": "labels",
"value": {
"x": 26.666666666666632,
"y": 30.933333333333323,
"width": 12.933333333333314,
"height": 14.933333333333312,
"labels": [
"Text"
],
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "label",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}, {
"id": "Iuq3q9qQXR",
"type": "rectangle",
"value": {
"x": 63.46666666666667,
"y": 53.86666666666666,
"width": 28.933333333333334,
"height": 17.733333333333334,
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "bbox",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}, {
"id": "Iuq3q9qQXR",
"type": "labels",
"value": {
"x": 63.46666666666667,
"y": 53.86666666666666,
"width": 28.933333333333334,
"height": 17.733333333333334,
"labels": [
"Handwriting"
],
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "label",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}
]}
result2 = {"result": [{
"id": "rWSy2ZUZm7",
"type": "rectangle",
"value": {
"x": 26.000000000000036,
"y": 31.6,
"width": 13.066666666666638,
"height": 14.133333333333297,
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "bbox",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}, {
"id": "rWSy2ZUZm7",
"type": "labels",
"value": {
"x": 26.000000000000036,
"y": 31.6,
"width": 13.066666666666638,
"height": 14.133333333333297,
"labels": [
"Text"
],
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "label",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}, {
"id": "ePS5El8SPd",
"type": "rectangle",
"value": {
"x": 62.93333333333333,
"y": 55.2,
"width": 28.799999999999997,
"height": 15.6,
"rotation": 0
},
"origin": "manual",
"to_name": "image",
"from_name": "bbox",
"image_rotation": 0,
"original_width": 1080,
"original_height": 1080
}
]}
o1 = OCREvalItem(result1)
o2 = OCREvalItem(result2)
score = o1.compare(o2)
assert score == None

0 comments on commit b31ad2d

Please sign in to comment.