-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
character_recognition.py
74 lines (55 loc) · 2.52 KB
/
character_recognition.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
69
70
71
72
73
74
"""
Copyright (c) 2018-2024 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from ..representation import CharacterRecognitionAnnotation, CharacterRecognitionPrediction
from .metric import PerImageEvaluationMetric
from .average_meter import AverageMeter
from .average_editdistance_meter import AverageEditdistanceMeter
from .distance import editdistance_eval
from ..config import BoolField
class CharacterRecognitionAccuracy(PerImageEvaluationMetric):
__provider__ = 'character_recognition_accuracy'
annotation_types = (CharacterRecognitionAnnotation, )
prediction_types = (CharacterRecognitionPrediction, )
@classmethod
def parameters(cls):
params = super().parameters()
params.update({
'remove_spaces': BoolField(optional=True, default=False)
})
return params
def configure(self):
self.accuracy = AverageMeter(lambda annotation, prediction: int(annotation == prediction))
self.remove_spaces = self.get_value_from_config('remove_spaces')
def update(self, annotation, prediction):
gt_label = annotation.label
pred_label = prediction.label
if self.remove_spaces:
gt_label = gt_label.replace(' ', '')
pred_label = pred_label.replace(' ', '')
return self.accuracy.update(gt_label, pred_label)
def evaluate(self, annotations, predictions):
return self.accuracy.evaluate()
def reset(self):
self.accuracy.reset()
class LabelLevelRecognitionAccuracy(PerImageEvaluationMetric):
__provider__ = 'label_level_recognition_accuracy'
annotation_types = (CharacterRecognitionAnnotation, )
prediction_types = (CharacterRecognitionPrediction, )
def configure(self):
self.accuracy = AverageEditdistanceMeter(editdistance_eval)
def update(self, annotation, prediction):
return self.accuracy.update(annotation.label, prediction.label)
def evaluate(self, annotations, predictions):
return self.accuracy.evaluate()
def reset(self):
self.accuracy.reset()