计算 OCR 准确度
Calculate OCR accuracy
我需要计算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
我无法得到我所希望的。任何帮助将不胜感激。
如果您不喜欢那个精确的定义(或者如果您喜欢并想深入研究 python-Levenshtein 的细节),那么这就是我解决这个问题的方法:
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))
相同的library将以相对high-performance的方式为您提供汉明距离、操作码和类似功能。
None 如果这是一项家庭作业,或者您在这里的目的是学习如何实现字符串算法,那么其中的
None 将很有用,但如果您只需要一个好的指标,这就是我会使用的.
您可以使用 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() 方法详细说明如何将预测转化为真实值。要计算真正的字符数,我们只使用 'equal' 标签。
有关详细信息,请参阅 https://docs.python.org/3/library/difflib.html#sequencematcher-objects。
我需要计算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
我无法得到我所希望的。任何帮助将不胜感激。
如果您不喜欢那个精确的定义(或者如果您喜欢并想深入研究 python-Levenshtein 的细节),那么这就是我解决这个问题的方法:
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))
相同的library将以相对high-performance的方式为您提供汉明距离、操作码和类似功能。
None 如果这是一项家庭作业,或者您在这里的目的是学习如何实现字符串算法,那么其中的
None 将很有用,但如果您只需要一个好的指标,这就是我会使用的.
您可以使用 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() 方法详细说明如何将预测转化为真实值。要计算真正的字符数,我们只使用 'equal' 标签。
有关详细信息,请参阅 https://docs.python.org/3/library/difflib.html#sequencematcher-objects。