OCR - 使用tesseract 3.0和imagemagick 6.6.5从图像中获取文本

6

我正在尝试构建一个Shell脚本,用于在图像中搜索文本。根据文本,该脚本会尽力从图像中获取文本。但是对于文字字体颜色与其周围较小区域类似的图片,该脚本不能正常工作。您是否能提供帮助和意见?

# !/bin/bash
# 
# imt-ocr.sh is image magick tessearc OCR tool that is used for finding out text in image
#
# Arguments:
# 1     -- image filename (with path)
# 2     -- text to search in image      (default to '')
# 3     -- occurence of text            (default to 1)
# Usage:
# imt-ocr.sh [image_filename] [text_to_search] [occurence]
#

image=$1
txt=$2
occurence=$3    # Default to 1
if [ "$occurence" == "" ]
then
        occurence=1
fi

get_major_color ()
# Returns the major color of an image with its hex value
#       Parameter:      Image filename (with path)
#       Return format:  Returns a string "hex_val_of_color major_color_name"
{
convert $1 -format %c histogram:info: > x.txt
cat x.txt | awk '{print $1}' > x1.txt
h=$(sort -n x1.txt | tail -1);
color_info=$(cat x.txt | grep "$h" | cut -d '#' -f2)
rm -rf x.txt x1.txt
echo "$color_info"
}


invert_color()
# Inverts the color hex value
#       Parameter:      Hex value to be inverted
#       Return format:  Returns in hex
{
input_color_hex=$1                                              # Input color's hex value
white_color_hex=FFFFFF                                          # White color's  hex vlaue
inv_color_hex=`echo $(printf '%06X\n' $((0x$white_color_hex - 0x$input_color_hex)))`
echo $inv_color_hex
}


start_scale=100
end_scale=300
increment_scale=100
tmp_img=dst.tif
attempt=1
for ((scale=$start_scale, attempt=$attempt; scale <= $end_scale ; scale=scale+$increment_scale, attempt++))
        do
                echo "IMT-OCR-LOG: Scaling image to $scale% in attempt #$attempt"
                convert $image -type Grayscale -scale $scale% $tmp_img
                tesseract $tmp_img OUT
                found_oc=$(grep -o "$txt" OUT.txt | wc -l)
                echo "IMT-OCR-LOG: Found $found_oc occurence(s) of text '$txt' in attempt #$attempt"
                if [ $occurence -le $found_oc ] && [ $found_oc -ne 0 ]
                then
                        echo "IMT-OCR-LOG: Printing out the last text found on image"
                        echo "IMT-OCR-LOG: ======================================================"
                        cat OUT.txt
                        echo "IMT-OCR-LOG: ======================================================"
                        rm -rf $tmp_img OUT.txt
                        exit 1
                else
                        echo "IMT-OCR-LOG: Getting major color of image in attempt #$attempt"
                        color_info=`get_major_color $image`
                        true_color=$(echo $color_info | awk '{print $2}')
                        true_val=$(echo $color_info | awk '{print $1}')
                        echo "IMT-OCR-LOG: Major color of image is '$true_color' with hex value of $true_val in attempt #$attempt"

                        # Blur the image
                        echo "IMT-OCR-LOG: Bluring image in attempt #$attempt"
                        convert $tmp_img -blur 1x65535 $tmp_img

                        # Flip the color
                        inverted_val=`invert_color $true_val`
                        echo "IMT-OCR-LOG: Inverting the major color of image from 0x$true_val to 0x$inverted_val in attempt #$attempt"
                        convert $tmp_img -fill \#$inverted_val -opaque \#$true_val $tmp_img

                        # Sharpen the image
                        echo "IMT-OCR-LOG: Sharpening image in attempt #$attempt"
                        convert $tmp_img -sharpen 1x65535 $tmp_img

                        # Find text
                        tesseract $tmp_img OUT
                        found_oc=$(grep -o "$txt" OUT.txt | wc -l)
                        echo "IMT-OCR-LOG: Found $found_oc occurence(s) of text '$txt' in attempt #$attempt"
                        if [ "$found_oc" != "0" ]
                        then
                                if [ $occurence -le $found_oc ]
                                then
                                        echo "IMT-OCR-LOG: Printing out the last text found on image"
                                        echo "IMT-OCR-LOG: ======================================================"
                                        cat OUT.txt
                                        echo "IMT-OCR-LOG: ======================================================"
                                        rm -rf $tmp_img OUT.txt
                                        exit 1
                                fi
                        fi
                fi

                rm -rf OUT.txt

        done

rm -rf $tmp_img

这里是一个带有问题的示例,图片(test.jpeg)http://www.igoipad.com/wp-content/uploads/2012/07/03-Word-Collage-iPad.jpeg

[admin@ba-callgen image-magick-tesseract-processing]$ sh imt-ocr.sh test.jpeg Common
IMT-OCR-LOG: Scaling image to 100% in attempt #1
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Common' in attempt #1
IMT-OCR-LOG: Getting major color of image in attempt #1
IMT-OCR-LOG: Major color of image is 'grey96' with hex value of F5F5F5 in attempt #1
IMT-OCR-LOG: Bluring image in attempt #1
IMT-OCR-LOG: Inverting the major color of image from 0xF5F5F5 to 0x0A0A0A in attempt #1
IMT-OCR-LOG: Sharpening image in attempt #1
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Common' in attempt #1
IMT-OCR-LOG: Scaling image to 200% in attempt #2
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 1 occurence(s) of text 'Common' in attempt #2
IMT-OCR-LOG: Printing out the last text found on image
IMT-OCR-LOG: ======================================================
Settings M...
Text
Common words
Exclude numbers
word case
Theme & Layuul
Color theme
Fnnl
Word layout
Clrien lalion
7301
Lrmclsc ape
\u2018OTC
Ergl sw v.-ords >
li( `
I):Jntc1'\:1r\qa )
Landon Spring >
Hough Trad >
H3'fJ|1d :-Ialf >
H L

IMT-OCR-LOG: ======================================================
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ 
[admin@ba-callgen image-magick-tesseract-processing]$ sh imt-ocr.sh test.jpeg Portrait
IMT-OCR-LOG: Scaling image to 100% in attempt #1
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #1
IMT-OCR-LOG: Getting major color of image in attempt #1
IMT-OCR-LOG: Major color of image is 'grey96' with hex value of F5F5F5 in attempt #1
IMT-OCR-LOG: Bluring image in attempt #1
IMT-OCR-LOG: Inverting the major color of image from 0xF5F5F5 to 0x0A0A0A in attempt #1
IMT-OCR-LOG: Sharpening image in attempt #1
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #1
IMT-OCR-LOG: Scaling image to 200% in attempt #2
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #2
IMT-OCR-LOG: Getting major color of image in attempt #2
IMT-OCR-LOG: Major color of image is 'grey96' with hex value of F5F5F5 in attempt #2
IMT-OCR-LOG: Bluring image in attempt #2
IMT-OCR-LOG: Inverting the major color of image from 0xF5F5F5 to 0x0A0A0A in attempt #2
IMT-OCR-LOG: Sharpening image in attempt #2
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #2
IMT-OCR-LOG: Scaling image to 300% in attempt #3
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #3
IMT-OCR-LOG: Getting major color of image in attempt #3
IMT-OCR-LOG: Major color of image is 'grey96' with hex value of F5F5F5 in attempt #3
IMT-OCR-LOG: Bluring image in attempt #3
IMT-OCR-LOG: Inverting the major color of image from 0xF5F5F5 to 0x0A0A0A in attempt #3
IMT-OCR-LOG: Sharpening image in attempt #3
Tesseract Open Source OCR Engine with Leptonica
IMT-OCR-LOG: Found 0 occurence(s) of text 'Portrait' in attempt #3
[admin@ba-callgen image-magick-tesseract-processing]$ 

您可以看到,我能找到文字“common”,但找不到“Portrait”。原因是“Portrait”字体颜色的问题。有助于改进此脚本的任何帮助...

我正在使用Centos 5。


你所描述的问题并不是唯一的。其他一些单词也没有被正确识别。我建议先尝试将它们正确识别,也许这样可以帮助“Portrait”搜索... - Kurt Pfeifle
友情建议:您应该考虑“接受”和/或“点赞”更多您之前问题所得到的答案。这将增加人们思考您的问题的意愿和努力程度... - Kurt Pfeifle
哦,我看到版本号已经在标题中提到了。没关系... - Kurt Pfeifle
谢谢Kurt。我确实接受并点赞回答/评论...所以,关于文本解析,它完全取决于图像的格式化程度,以便tesseract可以轻松检测到文本。现在,主要问题仅出现在那些字体与主要背景混合的地方。为此,在我的当前实现中,我试图放大、模糊、改变颜色...这是关键逻辑!!因此,我想到了一种只从理论角度起作用的替代解决方案。请阅读下一个评论... - amulllb
你的真正目标是什么?是建立一个通用OCR脚本,可以识别任何我投入其中的内容,甚至是多彩的截图吗?还是建立一个OCR脚本,可以从iPad截图中提取文本,使用典型的iPad颜色呢? - Kurt Pfeifle
显示剩余5条评论
1个回答

7
不要人为地限制自己只评估一两种方法来处理输入图像。目前您似乎只使用了 -blur-scale
您还应该考虑使用以下操作:
  • -contrast
  • -despeckle
  • -edge
  • -negate
  • -normalize
  • -posterize
  • -type grayscale
  • -monochrome
  • -gamma
  • -antialias / +antialias

输入图像: Original screenshot

例如,查看此命令的输出:

convert 03-Word-Collage-iPad.jpeg             \
    -scale 1000%                              \
    -blur 1x65535 -blur 1x65535 -blur 1x65535 \
    -contrast                                 \
    -normalize                                \
    -despeckle -despeckle                     \
    -type grayscale                           \
    -sharpen 1                                \
    -posterize 3                              \
    -negate                                   \
    -gamma 100                                \
    -compress zip                             \
     a.tif

输出图像: 输出图像
(抱歉,在上传TIFF格式的图片到该网站时,它会自动转换为PNG格式。因此,当您下载上面看到的图片时,实际上并不是我使用的TIFF格式,但您仍然可以看到我的真实结果的相似图片。)

注1: 我使用了这个ImageMagick版本进行测试:

convert -version
  Version: ImageMagick 6.7.6-9 2012-05-12 Q16 http://www.imagemagick.org
  Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
  Features:     

注意 2: ImageMagick 的旧版或新版可能会有不同的行为,尤其是在使用 -posterize 参数时!

这是 Tesseract 对于 a.tif 图像进行 OCR 后的结果:

tesseract a.tif OUT  &&  cat OUT.txt

Tesseract Open Source OCR Engine v3.01 with Leptonica
   Page 0
   Text
   Common words Remove English words >
   Exclude numbers
   Word case Don't change 1+
   Theme & Layout
   Color theme London Spring >
   Font Rough Trad >
   Word layout Half and Half >
   Orientation
   Landscape
   Q
   u
   -0
   "H
   I

更新:

我已经验证最新版本的ImageMagick 6.7.9-0(昨天发布)不会产生与我展示的命令和截图相同的结果(使用版本 6.7.6-9)。这里是不同之处:

enter image description here

无论如何,我敢肯定,如果你微调一下我的命令,尝试各种参数,你会让它在你的ImageMagick版本中工作...


“-contrast”和“-normalize”对图像及OCR阅读能力有什么影响? 您能为以下PDF建议ImageMagic预处理吗?http://dl.dropbox.com/u/44235928/sample_rotate-0.pdf我尝试了convert -rotate 90 -geometry 10000 -depth 8 -density 800 sample.pdf img_800_10000.tif - piyush
@piyush:关于-contrast-normalize,你应该提出自己的单独问题。至于你的sample_rotate-0.pdf文件——之前不是已经有一个单独的问题了吗?我记得看到过,但现在找不到了…… - Kurt Pfeifle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接