首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算OCR精度

计算OCR精度
EN

Stack Overflow用户
提问于 2020-08-22 02:19:51
回答 2查看 1.5K关注 0票数 2

我需要计算OCR字符的准确性。

样地值:

Non sinking ship is friendship

示例ocr值输入

non singing ship is finedship

令人关切的领域是:

  1. 遗漏字符
  2. 额外字符
  3. 错位字符

字符准确性是由实际字符的数量定义的,其位置除以实际字符的总数。

我需要一个python脚本来找到这种准确性。我的初步实施如下:

代码语言:javascript
复制
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

我没能得到我想要的。任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-22 03:13:07

如果您还没有这个精确的定义(或者您正在并想深入研究的细节),那么我将这样解决这个问题:

pip install python-Levenshtein

代码语言:javascript
复制
from Levenshtein import distance

ground_value = "Non sinking ship is friendship"
ocr_value = "non singing ship is finedship"

print(distance(ground_value, ocr_value))

同样的图书馆会以一种相对高性能的方式给出哈明距离、操作码和类似的功能。

如果这是一个家庭作业作业,或者您在这里的目的是学习如何实现字符串算法,那么所有这些都不会有用,但是如果您只需要一个好的度量,这就是我将要使用的。

票数 2
EN

Stack Overflow用户

发布于 2022-01-18 23:16:09

您可以使用SequenceMatcher。它给了你想要的,

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63531985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档