python - 有没有办法从图像中只提取所需的文本?
问题描述
我正在做一个项目,我试图通过扫描整个药包来提取药物名称。例如 -
下图是由名为“Health OK”的平板电脑信息组成的图像。
我的问题是,通过扫描或使用此图像,是否可以仅提取平板电脑的名称,即“健康正常”?
我试过使用 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 模型的可能解决方案?
解决方案
我对此的看法是为您要提取的所需文本创建一个变量。在图像上运行 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(字典)。您可以提取与所需变量具有最小距离的那个。
希望这对你有用!
推荐阅读
- php - 获取数据库配置设置到 codeigniter 中的控制器/视图
- python - 在 Python 中检测范围
- python-3.x - 解析用户输入
- angular - Angular中主题订阅的未知类型错误
- compiler-errors - STD_LOGIC_VECTOR 与整数文字不匹配
- celery - Airflow dags 无限期地陷入“运行”状态
- google-cloud-dataflow - Cloud Dataflow 资源共享池
- typescript - VSCode Mocha typescript 通过转译文件而不是源 .ts 文件测试调试步骤
- java - Spinner 和 Button 无法转到另一个视图
- docker - Windows 10,端口 80 的 Docker 问题,端口不可用:listen tcp 0.0.0.0:80: