我需要计算OCR字符的准确性。
样地值:
Non sinking ship is friendship
示例ocr值输入
non singing ship is finedship
令人关切的领域是:
字符准确性是由实际字符的数量定义的,其位置除以实际字符的总数。
我需要一个python脚本来找到这种准确性。我的初步实施如下:
ground_value = "Non sinking ship is friendship"
ocr_value = "non singing ship is finedship"
ground_value_characters = (re.sub('\s+', '',
ground_value)).strip() # remove all spaces from the gr value string
ocr_value_characters = (re.sub('\s+', '',
ocr_value)).strip() # remove all the spaces from the ocr string
total_characters = float(len(
ground_value_characters))
def find_matching_characters(ground, ocr):
total = 0
for char in ground:
if char in ocr:
total = total + 1
ocr = ocr.replace(char, '', 1)
return total
found_characters = find_matching_characters(ground_value_characters,
ocr_value_characters)
accuracy = found_characters/total_characters我没能得到我想要的。任何帮助都将不胜感激。
发布于 2020-08-22 03:13:07
如果您还没有这个精确的定义(或者您正在并想深入研究的细节),那么我将这样解决这个问题:
pip install python-Levenshtein
from Levenshtein import distance
ground_value = "Non sinking ship is friendship"
ocr_value = "non singing ship is finedship"
print(distance(ground_value, ocr_value))同样的图书馆会以一种相对高性能的方式给出哈明距离、操作码和类似的功能。
如果这是一个家庭作业作业,或者您在这里的目的是学习如何实现字符串算法,那么所有这些都不会有用,但是如果您只需要一个好的度量,这就是我将要使用的。
发布于 2022-01-18 23:16:09
您可以使用SequenceMatcher。它给了你想要的,
from difflib import SequenceMatcher
ground_value = "Non sinking ship is friendship"
ocr_value = "non singing ship is finedship"
sm = SequenceMatcher(None, ocr_value, ground_value)
true_positive_char_num = 0
for tag, i1, i2, j1, j2 in sm.get_opcodes():
if tag== 'equal':
true_positive_char_num += (j2 - j1)
else:
pass
print(f'accuracy = {true_positive_char_num/len(ground_value)}')accuracy = 0.8666666666666667
在这里,我们首先创建SequenceMatcher对象并使用get_opcodes()方法,该方法给出了如何将预测转化为实际真值的详细信息。要计数真正的字符,我们只使用“相等”标记。
有关更多详细信息,请参阅https://docs.python.org/3/library/difflib.html#sequencematcher-objects。
https://stackoverflow.com/questions/63531985
复制相似问题