是否有 OCR 库可以输出图像中找到的单词的坐标?

发布于 2024-10-18 04:34:49 字数 1539 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

大海や 2024-10-25 04:34:49

大多数商业 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 也返回角色位置。

Most commercial OCR engines will return word and character coordinate positions but you have to work with their SDK's to extract the information. Even Tesseract OCR will return position information but it has been not easy to get to. Version 3.01 will make easier but a DLL interface is still being worked on.

Unfortunately, most free OCR programs use Tesseract OCR in its basic form and they only report the raw ASCII results.

www.transym.com - Transym OCR - outputs coordinates.
www.rerecognition.com - KADMOS engine returns coordinates.

Also Caere Omnipage, Mitek, Abbyy, Charactell return character positions.

唐婉 2024-10-25 04:34:49

我正在使用 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");

I'm using TessNet (a Tesseract C# wrapper) and I'm getting word coordinates with the following code:

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");
猫性小仙女 2024-10-25 04:34:49

您可以将 hocr “configfile” 与 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 更容易。

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

You can use the hocr "configfile" with tesseract like so:

tesseract syllabus-page1.jpg syllabus-page1 hocr

This will output a mostly HTML5 document with elements like:

<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>

While I'm pretty sure that's not how you're supposed to use XML, I found it easier than digging into the tesseract API.

P.S. I realize that several comments and answers allude to this solution, but none of them actually show how to use the hocr option or describe the output you get from that.

朕就是辣么酷 2024-10-25 04:34:49

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
          }
        ]
      }

Google Vision API does this.
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
          }
        ]
      }
嘦怹 2024-10-25 04:34:49

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

You may also take a look at Gamera framework (http://gamera.informatik.hsnr.de/) it is a set of tools, which allows you to build your own OCR engine. Nevertheless the fastest way is to use Tesseract or OCRopus hOCR (http://en.wikipedia.org/wiki/HOCR) output.

沉默的熊 2024-10-25 04:34:49

对于 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);
}

For Java Developers:

I will recommend for this you to use Tesseract and Tess4j.

You can actually find an example on how to find words on a Image in one of the tests of 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);
}
有木有妳兜一样 2024-10-25 04:34:49

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 团队的一名成员发布。

ABCocr.NET (our component) will allow you to obtain the coordinates of each word found. The values are accessible through the Word.Bounds property, which simply returns a System.Drawing.Rectangle.

The example below shows how you can OCR an image using ABCocr.NET and output the information you need:

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);
            }
        }
    }
}

Disclosure: posted by a member of the WebSupergoo team.

孤独患者 2024-10-25 04:34:49

hocr 是 tesseract OCR 引擎的输出格式之一,它既有单词及其坐标,也有一些附加信息,例如单词识别的置信度。

hocr is a one of the output format of tesseract OCR engine,which has both word and it's coordinates and also has some additional info like confident level of word recognition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文