【问题标题】:Is there an OCR library that outputs coordinates of words found within an image? [closed]是否有一个 OCR 库可以输出在图像中找到的单词的坐标? [关闭]
【发布时间】:2011-06-29 18:55:02
【问题描述】:

根据我的经验,OCR 库倾向于仅输出在图像中找到的文本,而不是在哪里找到文本。是否有一个 OCR 库可以输出在图像中找到的单词以及找到这些单词的坐标 (x, y, width, height)?

【问题讨论】:

    标签: ocr


    【解决方案1】:

    大多数商业 OCR 引擎会返回单词和字符坐标位置,但您必须使用他们的 SDK 来提取信息。即使是 Tesseract OCR 也会返回位置信息,但要获得它并不容易。版本 3.01 将变得更容易,但 DLL 接口仍在开发中。

    不幸的是,大多数免费 OCR 程序都以基本形式使用 Tesseract OCR,并且它们只报告原始 ASCII 结果。

    www.transym.com - Transym OCR - 输出坐标。 www.rerecognition.com - KADMOS 引擎返回坐标。

    还有 Caere Omnipage、Mitek、Abbyy、Charactell 返回角色位置。

    【讨论】:

    【解决方案2】:

    我正在使用 TessNet(一个 Tesseract C# 包装器)并且我正在使用以下代码获取单词坐标:

    TextWriter tw = new StreamWriter(@"U:\user files\bwalker\ocrTesting.txt");
    Bitmap image = new Bitmap(@"u:\user files\bwalker\2849257.tif");
    tessnet2.Tesseract ocr = new tessnet2.Tesseract();
    // If digit only
    ocr.SetVariable("tessedit_char_whitelist", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,$-/#&=()\"':?");
    // To use correct tessdata
    ocr.Init(@"C:\Users\bwalker\Documents\Visual Studio 2010\Projects\tessnetWinForms\tessnetWinForms\bin\Release\", "eng", false); 
    List<tessnet2.Word> result = ocr.DoOCR(image, System.Drawing.Rectangle.Empty);
    string Results = "";
    foreach (tessnet2.Word word in result)
    {
        Results += word.Confidence + ", " + word.Text + ", " +word.Top+", "+word.Bottom+", "+word.Left+", "+word.Right+"\n";
    }
    using (StreamWriter writer = new StreamWriter(@"U:\user files\bwalker\ocrTesting2.txt", true))
    {
        writer.WriteLine(Results);//+", "+word.Top+", "+word.Bottom+", "+word.Left+", "+word.Right);
        writer.Close();
    }
    MessageBox.Show("Completed");
    

    【讨论】:

      【解决方案3】:

      您可以像这样使用hocr“配置文件”和tesseract

      tesseract syllabus-page1.jpg syllabus-page1 hocr
      

      这将输出一个主要是 HTML5 的文档,其中包含以下元素:

      <div class='ocr_page' id='page_1' title='image "syllabus-page1.jpg"; bbox 0 0 2531 3272; ppageno 0'>
        <div class="ocr_carea" id="block_1_4" title="bbox 265 1183 2147 1778">
          <p class="ocr_par" dir="ltr" id="par_1_8" title="bbox 274 1305 655 1342">
            <span class="ocr_line" id="line_1_14" title="bbox 274 1305 655 1342; baseline -0.005 0; x_size 46.378059; x_descenders 10.378059; x_ascenders 12">
              <span class="ocrx_word" id="word_1_78" title="bbox 274 1307 386 1342; x_wconf 90" lang="eng" dir="ltr">needs</span>
              <span class="ocrx_word" id="word_1_79" title="bbox 402 1318 459 1342; x_wconf 90" lang="eng" dir="ltr">are</span>
              <span class="ocrx_word" id="word_1_80" title="bbox 474 1305 655 1341; x_wconf 86" lang="eng" dir="ltr">different:</span>
            </span>
          </p>
          ...
        </div>  
        ...
      </div>
      

      虽然我很确定这不是您应该使用 XML 的方式,但我发现它比深入研究 tesseract API 更容易。

      附:我意识到有几个 cmets 和答案都提到了这个解决方案,但它们都没有真正展示如何使用 hocr 选项或描述你从中获得的输出。

      【讨论】:

      • 这真的很容易使用,不需要太多设置。我认为这是最好的解决方案。
      【解决方案4】:

      Google Vision API 可以做到这一点。 https://cloud.google.com/vision/docs/detecting-text

      "description": "Wake up human!\n",
            "boundingPoly": {
              "vertices": [
                {
                  "x": 29,
                  "y": 394
                },
                {
                  "x": 570,
                  "y": 394
                },
                {
                  "x": 570,
                  "y": 466
                },
                {
                  "x": 29,
                  "y": 466
                }
              ]
            }
      

      【讨论】:

        【解决方案5】:

        您还可以查看 Gamera 框架 (http://gamera.informatik.hsnr.de/),它是一组工具,可让您构建自己的 OCR 引擎。不过最快的方法是使用 Tesseract 或 OCRopus hOCR (http://en.wikipedia.org/wiki/HOCR) 输出。

        【讨论】:

          【解决方案6】:

          对于 Java 开发人员:

          为此,我建议您使用 Tesseract 和 Tess4j

          您实际上可以在 Tess4j 的一项测试中找到有关如何在图像上查找单词的示例。

          https://github.com/nguyenq/tess4j/blob/master/src/test/java/net/sourceforge/tess4j/TessAPITest.java#L449-L517

          public void testResultIterator() throws Exception {
              logger.info("TessBaseAPIGetIterator");
              File tiff = new File(this.testResourcesDataPath, "eurotext.tif");
              BufferedImage image = ImageIO.read(new FileInputStream(tiff)); // require jai-imageio lib to read TIFF
              ByteBuffer buf = ImageIOHelper.convertImageData(image);
              int bpp = image.getColorModel().getPixelSize();
              int bytespp = bpp / 8;
              int bytespl = (int) Math.ceil(image.getWidth() * bpp / 8.0);
              api.TessBaseAPIInit3(handle, datapath, language);
              api.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO);
              api.TessBaseAPISetImage(handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
              ETEXT_DESC monitor = new ETEXT_DESC();
              TimeVal timeout = new TimeVal();
              timeout.tv_sec = new NativeLong(0L); // time > 0 causes blank ouput
              monitor.end_time = timeout;
              ProgressMonitor pmo = new ProgressMonitor(monitor);
              pmo.start();
              api.TessBaseAPIRecognize(handle, monitor);
              logger.info("Message: " + pmo.getMessage());
              TessResultIterator ri = api.TessBaseAPIGetIterator(handle);
              TessPageIterator pi = api.TessResultIteratorGetPageIterator(ri);
              api.TessPageIteratorBegin(pi);
              logger.info("Bounding boxes:\nchar(s) left top right bottom confidence font-attributes");
              int level = TessPageIteratorLevel.RIL_WORD;
          
              // int height = image.getHeight();
              do {
                  Pointer ptr = api.TessResultIteratorGetUTF8Text(ri, level);
                  String word = ptr.getString(0);
                  api.TessDeleteText(ptr);
                  float confidence = api.TessResultIteratorConfidence(ri, level);
                  IntBuffer leftB = IntBuffer.allocate(1);
                  IntBuffer topB = IntBuffer.allocate(1);
                  IntBuffer rightB = IntBuffer.allocate(1);
                  IntBuffer bottomB = IntBuffer.allocate(1);
                  api.TessPageIteratorBoundingBox(pi, level, leftB, topB, rightB, bottomB);
                  int left = leftB.get();
                  int top = topB.get();
                  int right = rightB.get();
                  int bottom = bottomB.get();
                  /******************************************/
                  /* COORDINATES AND WORDS ARE PRINTED HERE */
                  /******************************************/
                  System.out.print(String.format("%s %d %d %d %d %f", word, left, top, right, bottom, confidence));
                  // logger.info(String.format("%s %d %d %d %d", str, left, height - bottom, right, height - top)); //
                  // training box coordinates
          
                  IntBuffer boldB = IntBuffer.allocate(1);
                  IntBuffer italicB = IntBuffer.allocate(1);
                  IntBuffer underlinedB = IntBuffer.allocate(1);
                  IntBuffer monospaceB = IntBuffer.allocate(1);
                  IntBuffer serifB = IntBuffer.allocate(1);
                  IntBuffer smallcapsB = IntBuffer.allocate(1);
                  IntBuffer pointSizeB = IntBuffer.allocate(1);
                  IntBuffer fontIdB = IntBuffer.allocate(1);
                  String fontName = api.TessResultIteratorWordFontAttributes(ri, boldB, italicB, underlinedB, monospaceB,
                          serifB, smallcapsB, pointSizeB, fontIdB);
                  boolean bold = boldB.get() == TRUE;
                  boolean italic = italicB.get() == TRUE;
                  boolean underlined = underlinedB.get() == TRUE;
                  boolean monospace = monospaceB.get() == TRUE;
                  boolean serif = serifB.get() == TRUE;
                  boolean smallcaps = smallcapsB.get() == TRUE;
                  int pointSize = pointSizeB.get();
                  int fontId = fontIdB.get();
                  logger.info(String.format("  font: %s, size: %d, font id: %d, bold: %b,"
                          + " italic: %b, underlined: %b, monospace: %b, serif: %b, smallcap: %b", fontName, pointSize,
                          fontId, bold, italic, underlined, monospace, serif, smallcaps));
              } while (api.TessPageIteratorNext(pi, level) == TRUE);
          
              assertTrue(true);
          }
          

          【讨论】:

            【解决方案7】:

            ABCocr.NET(我们的组件)将允许您获取找到的每个单词的坐标。可通过 Word.Bounds 属性访问这些值,该属性仅返回 System.Drawing.Rectangle。

            以下示例展示了如何使用ABCocr.NET 对图像进行 OCR 并输出您需要的信息:

            using System;
            using System.Drawing;
            using WebSupergoo.ABCocr3;
            
            namespace abcocr {
                class Program {
                    static void Main(string[] args) {
            
                        Bitmap bitmap = (Bitmap)Bitmap.FromFile("example.png");
                        Ocr ocr = new Ocr();
                        ocr.SetBitmap(bitmap);
            
                        foreach (Word word in ocr.Page.Words) {
                            Console.WriteLine("{0}, X: {1}, Y: {2}, Width: {3}, Height: {4}",
                                word.Text,
                                word.Bounds.X,
                                word.Bounds.Y,
                                word.Bounds.Width,
                                word.Bounds.Height);
                        }
                    }
                }
            }
            

            披露:由 WebSupergoo 团队的成员发布。

            【讨论】:

              【解决方案8】:

              hocr是tesseract OCR引擎的一种输出格式,它既有单词也有它的坐标,还有一些额外的信息,比如单词识别的置信度。

              【讨论】:

                猜你喜欢
                • 2013-01-07
                • 1970-01-01
                • 2015-04-29
                • 1970-01-01
                • 2018-05-12
                • 1970-01-01
                • 2014-10-16
                • 2018-09-02
                • 2011-09-16
                相关资源
                最近更新 更多