2 Copyright (c) 2019 Intel Corporation
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 from .metric import PerImageEvaluationMetric, BaseMetricConfig
19 from ..config import BoolField, NumberField
20 from ..representation import TextDetectionPrediction, TextDetectionAnnotation
21 from ..utils import polygon_from_points
24 def get_union(detection_polygon, annotation_polygon):
25 area_prediction = detection_polygon.area
26 area_annotation = annotation_polygon.area
27 return area_prediction + area_annotation - get_intersection_area(detection_polygon, annotation_polygon)
30 def get_intersection_over_union(detection_polygon, annotation_polygon):
31 union = get_union(detection_polygon, annotation_polygon)
32 intersection = get_intersection_area(detection_polygon, annotation_polygon)
33 return intersection / union if union != 0 else 0.0
36 def get_intersection_area(detection_polygon, annotation_polygon):
37 return detection_polygon.intersection(annotation_polygon).area
40 class TextDetectionMetricConfig(BaseMetricConfig):
41 iou_constrain = NumberField(min_value=0, max_value=1, optional=True)
42 ignore_difficult = BoolField(optional=True)
43 area_precision_constrain = NumberField(min_value=0, max_value=1, optional=True)
46 class TextDetectionMetric(PerImageEvaluationMetric):
47 __provider__ = 'text_detection'
49 annotation_types = (TextDetectionAnnotation, )
50 prediction_types = (TextDetectionPrediction, )
52 def validate_config(self):
53 text_detection_metric_config = TextDetectionMetricConfig(
54 'TextDetectionMetric_config', TextDetectionMetricConfig.ERROR_ON_EXTRA_ARGUMENT
56 text_detection_metric_config.validate(self.config)
59 self.iou_constrain = self.config.get('iou_constrain', 0.5)
60 self.area_precision_constrain = self.config.get('area_precision_constrain', 0.5)
61 self.ignore_difficult = self.config.get('ignore_difficult', False)
62 self.number_matched_detections = 0
63 self.number_valid_annotations = 0
64 self.number_valid_detections = 0
66 def update(self, annotation, prediction):
67 gt_polygons = list(map(polygon_from_points, annotation.points))
68 prediction_polygons = list(map(polygon_from_points, prediction.points))
69 num_gt = len(gt_polygons)
70 num_det = len(prediction_polygons)
71 gt_difficult_mask = np.full(num_gt, False)
72 prediction_difficult_mask = np.full(num_det, False)
74 if self.ignore_difficult:
75 gt_difficult_inds = annotation.metadata.get('difficult_boxes', [])
76 prediction_difficult_inds = prediction.metadata.get('difficult_boxes', [])
77 gt_difficult_mask[gt_difficult_inds] = True
78 prediction_difficult_mask[prediction_difficult_inds] = True
79 for det_id, detection_polygon in enumerate(prediction_polygons):
80 for gt_difficult_id in gt_difficult_inds:
81 gt_difficult_polygon = gt_polygons[gt_difficult_id]
82 intersected_area = get_intersection_area(gt_difficult_polygon, detection_polygon)
83 pd_dimensions = detection_polygon.area
84 precision = 0 if pd_dimensions == 0 else intersected_area / pd_dimensions
86 if precision >= self.area_precision_constrain:
87 prediction_difficult_mask[det_id] = True
89 if num_gt > 0 and num_det > 0:
90 iou_matrix = np.empty((num_gt, num_det))
91 gt_matched = np.zeros(num_gt, np.int8)
92 det_matched = np.zeros(num_det, np.int8)
94 for gt_id, gt_polygon in enumerate(gt_polygons):
95 for pred_id, pred_polygon in enumerate(prediction_polygons):
96 iou_matrix[gt_id, pred_id] = get_intersection_over_union(pred_polygon, gt_polygon)
97 not_matched_before = gt_matched[gt_id] == 0 and det_matched[pred_id] == 0
98 not_difficult = not gt_difficult_mask[gt_id] and not prediction_difficult_mask[pred_id]
99 if not_matched_before and not_difficult:
100 if iou_matrix[gt_id, pred_id] >= self.iou_constrain:
101 gt_matched[gt_id] = 1
102 det_matched[pred_id] = 1
105 num_ignored_gt = np.sum(gt_difficult_mask)
106 num_ignored_pred = np.sum(prediction_difficult_mask)
107 num_valid_gt = num_gt - num_ignored_gt
108 num_valid_pred = num_det - num_ignored_pred
110 self.number_matched_detections += num_det_matched
111 self.number_valid_annotations += num_valid_gt
112 self.number_valid_detections += num_valid_pred
114 def evaluate(self, annotations, predictions):
116 0 if self.number_valid_annotations == 0
117 else float(self.number_matched_detections) / self.number_valid_annotations
120 0 if self.number_valid_detections == 0
121 else float(self.number_matched_detections) / self.number_valid_detections
124 return 0 if recall + precision == 0 else 2 * recall * precision / (recall + precision)