首页 > 解决方案 > 有没有办法从图像中只提取所需的文本?

问题描述

我正在做一个项目,我试图通过扫描整个药包来提取药物名称。例如 -

下图是由名为“Health OK”的平板电脑信息组成的图像。

https://i.stack.imgur.com/PuY9k.jpg

我的问题是,通过扫描或使用此图像,是否可以仅提取平板电脑的名称,即“健康正常”?

我试过使用 Pytesseract,但它没有为我提供想要的结果。下面是代码 -

from PIL import Image
import pytesseract, re
f = "ocr.jpg"
t = pytesseract.image_to_string(Image.open(f))
print(t)
m = re.findall(r"[\d—-]+ TABLETS [\d—-]+", t)
if m:
    print(m[0])

有没有使用 ANN 或 CNN 模型的可能解决方案?

标签: pythonandroid-studiomachine-learningocrpython-tesseract

解决方案


我对此的看法是为您要提取的所需文本创建一个变量。在图像上运行 OCR 后,也将其输出存储在列表中。例如:

disired_text = 'Health OK'
OCR_output = ['Multivitamin', 'Multiminerals', 'Amino Acids', 'with Taurine', 'Health OK']

拥有这些列表后,您可以使用任何字符串匹配算法(如模糊匹配)从OCT_output 列表中提取最佳匹配 例如:

score_dict = {} #initializing dictionary to store text and score
for output_text in OCR_output:
    score = fuzzy_match_function(output_text, desired_text)
    score_dict[output_text] = score

您将获得一个包含文本和分数的 score_dict(字典)。您可以提取与所需变量具有最小距离的那个。

希望这对你有用!


推荐阅读