如何在 bash 脚本中检查 tesseract 的输出? [英] How do I check for output of tesseract in bash script?

查看:37
本文介绍了如何在 bash 脚本中检查 tesseract 的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 bash 脚本中运行一个循环并将 png 文件传递​​给 tesseract 以读取图像文件的文本.如果 tesseract ocr 的输出显示 Empty page!! 或什么都没有,那么我希望循环继续下一个图像.如果它确实包含文本,那么我想将输出存储在文本文件中.

I am running a loop in bash script and passing png files to tesseract to read the text of image files. If output of the tesseract ocr shows Empty page!! or nothing then I want the loop to proceed to next image. If it does include text then I want to store the output in a text file.

这是我的基本脚本的样子,

This is what my basic script looks like,

for i in {1..100}
do
tesseract file-${i}.png stdout >> result.txt
done 

推荐答案

这大致就是您所需要的.我冒昧地做了一个ls"列出目录中的 png 文件,而不是从 1 到 100 迭代:

This is roughly what you need. I took the liberty to do an "ls" to list png files in a directory, rather than iterating from 1 to 100:

for file in /my/directory/*.png
do
  # Redirect output to a variable. This works even if output is multiline.
  output="$(tesseract "$file" stdout)"
  
  if [ -n "$output" ] && [ "$output" != "Empty page!!" ]
  then
    echo "$output" >> result.txt
  fi
done 

这有点粗糙,你可能想从 tesseract 检查结果代码,以防出现错误,或者你可能想省略标准错误消息,诸如此类.但这应该会给你一个想法.

This is a bit rough, you may want to check result codes from tesseract in case there are errors, or you may want to omit standard error messages, things like that. But this should give you an idea.

这篇关于如何在 bash 脚本中检查 tesseract 的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆