Пример #1
0
  /**
   * Test of ChoiceIterator.
   *
   * @throws Exception
   */
  @Test
  public void testChoiceIterator() throws Exception {
    logger.info("TessResultIteratorGetChoiceIterator");
    String filename = String.format("%s/%s", this.testResourcesDataPath, "eurotext.tif");
    File tiff = new File(filename);
    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);
    TessAPI1.TessBaseAPIInit3(handle, datapath, language);
    TessAPI1.TessBaseAPISetImage(
        handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
    TessAPI1.TessBaseAPISetVariable(handle, "save_blob_choices", "T");
    TessAPI1.TessBaseAPISetRectangle(handle, 37, 228, 548, 31);
    ETEXT_DESC monitor = new ETEXT_DESC();
    ProgressMonitor pmo = new ProgressMonitor(monitor);
    pmo.start();
    TessAPI1.TessBaseAPIRecognize(handle, monitor);
    logger.info("Message: " + pmo.getMessage());
    TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handle);
    int level = TessPageIteratorLevel.RIL_SYMBOL;

    if (ri != null) {
      do {
        Pointer symbol = TessAPI1.TessResultIteratorGetUTF8Text(ri, level);
        float conf = TessAPI1.TessResultIteratorConfidence(ri, level);
        if (symbol != null) {
          logger.info(String.format("symbol %s, conf: %f", symbol.getString(0), conf));
          boolean indent = false;
          TessChoiceIterator ci = TessAPI1.TessResultIteratorGetChoiceIterator(ri);
          do {
            if (indent) {
              System.out.print("\t");
            }
            System.out.print("\t- ");
            String choice = TessAPI1.TessChoiceIteratorGetUTF8Text(ci);
            logger.info(
                String.format("%s conf: %f", choice, TessAPI1.TessChoiceIteratorConfidence(ci)));
            indent = true;
          } while (TessAPI1.TessChoiceIteratorNext(ci) == TessAPI1.TRUE);
          TessAPI1.TessChoiceIteratorDelete(ci);
        }
        logger.info("---------------------------------------------");
        TessAPI1.TessDeleteText(symbol);
      } while (TessAPI1.TessResultIteratorNext(ri, level) == TessAPI1.TRUE);
    }

    assertTrue(true);
  }
Пример #2
0
 /**
  * Test of TessBaseAPIRect method, of class TessDllAPI1.
  *
  * @throws Exception while processing the image
  */
 @Test
 public void testTessBaseAPIRect() throws Exception {
   logger.info("TessBaseAPIRect");
   String expResult = expOCRResult;
   File tiff = new File(this.testResourcesDataPath, "eurotext.tif");
   BufferedImage image = ImageIO.read(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);
   TessAPI1.TessBaseAPIInit3(handle, datapath, language);
   TessAPI1.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO);
   Pointer utf8Text = TessAPI1.TessBaseAPIRect(handle, buf, bytespp, bytespl, 0, 0, 1024, 800);
   String result = utf8Text.getString(0);
   TessAPI1.TessDeleteText(utf8Text);
   logger.info(result);
   assertEquals(expResult, result.substring(0, expResult.length()));
 }
Пример #3
0
 /**
  * Test of TessBaseAPIGetHOCRText method, of class TessAPI1.
  *
  * @throws Exception while getting ocr text from image.
  */
 @Test
 public void testTessBaseAPIGetHOCRText() throws Exception {
   logger.info("TessBaseAPIGetHOCRText");
   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);
   TessAPI1.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO);
   TessAPI1.TessBaseAPIInit3(handle, datapath, language);
   TessAPI1.TessBaseAPISetImage(
       handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
   int page_number = 0;
   Pointer utf8Text = TessAPI1.TessBaseAPIGetHOCRText(handle, page_number);
   String result = utf8Text.getString(0);
   TessAPI1.TessDeleteText(utf8Text);
   assertTrue(result.contains("<div class='ocr_page'"));
 }
Пример #4
0
  /**
   * Test of TessBaseAPIGetUTF8Text method, of class TessAPI1.
   *
   * @throws java.lang.Exception
   */
  @Test
  public void testTessBaseAPIGetUTF8Text_Pix() throws Exception {
    logger.info("TessBaseAPIGetUTF8Text_Pix");
    String expResult = expOCRResult;
    File tiff = new File(this.testResourcesDataPath, "eurotext.tif");
    Leptonica leptInstance = Leptonica.INSTANCE;
    Pix pix = leptInstance.pixRead(tiff.getPath());
    TessAPI1.TessBaseAPIInit3(handle, datapath, language);
    TessAPI1.TessBaseAPISetImage2(handle, pix);
    Pointer utf8Text = TessAPI1.TessBaseAPIGetUTF8Text(handle);
    String result = utf8Text.getString(0);
    TessAPI1.TessDeleteText(utf8Text);
    logger.info(result);

    // release Pix resource
    PointerByReference pRef = new PointerByReference();
    pRef.setValue(pix.getPointer());
    leptInstance.pixDestroy(pRef);

    assertEquals(expResult, result.substring(0, expResult.length()));
  }
Пример #5
0
  /**
   * Test of TessBaseAPIGetComponentImages method, of class TessAPI1.
   *
   * @throws java.lang.Exception
   */
  @Test
  public void testTessBaseAPIGetComponentImages() throws Exception {
    logger.info("TessBaseAPIGetComponentImages");
    File image = new File(this.testResourcesDataPath, "eurotext.png");
    int expResult = 12; // number of lines in the test image
    Pix pix = Leptonica1.pixRead(image.getPath());
    TessAPI1.TessBaseAPIInit3(handle, datapath, language);
    TessAPI1.TessBaseAPISetImage2(handle, pix);
    PointerByReference pixa = null;
    PointerByReference blockids = null;
    Boxa boxes =
        TessAPI1.TessBaseAPIGetComponentImages(
            handle, TessPageIteratorLevel.RIL_TEXTLINE, TRUE, pixa, blockids);
    //        boxes = TessAPI1.TessBaseAPIGetRegions(handle, pixa); // equivalent to
    // TessPageIteratorLevel.RIL_BLOCK
    int boxCount = Leptonica1.boxaGetCount(boxes);
    for (int i = 0; i < boxCount; i++) {
      Box box = Leptonica1.boxaGetBox(boxes, i, L_CLONE);
      if (box == null) {
        continue;
      }
      TessAPI1.TessBaseAPISetRectangle(handle, box.x, box.y, box.w, box.h);
      Pointer utf8Text = TessAPI1.TessBaseAPIGetUTF8Text(handle);
      String ocrResult = utf8Text.getString(0);
      TessAPI1.TessDeleteText(utf8Text);
      int conf = TessAPI1.TessBaseAPIMeanTextConf(handle);
      System.out.print(
          String.format(
              "Box[%d]: x=%d, y=%d, w=%d, h=%d, confidence: %d, text: %s",
              i, box.x, box.y, box.w, box.h, conf, ocrResult));
    }

    // release Pix resource
    PointerByReference pRef = new PointerByReference();
    pRef.setValue(pix.getPointer());
    Leptonica1.pixDestroy(pRef);

    assertEquals(expResult, boxCount);
  }
Пример #6
0
  /**
   * Test of ResultIterator and PageIterator.
   *
   * @throws Exception
   */
  @Test
  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);
    TessAPI1.TessBaseAPIInit3(handle, datapath, language);
    TessAPI1.TessBaseAPISetPageSegMode(handle, TessPageSegMode.PSM_AUTO);
    TessAPI1.TessBaseAPISetImage(
        handle, buf, image.getWidth(), image.getHeight(), bytespp, bytespl);
    ETEXT_DESC monitor = new ETEXT_DESC();
    ITessAPI.TimeVal timeout = new ITessAPI.TimeVal();
    timeout.tv_sec = new NativeLong(0L); // time > 0 causes blank ouput
    monitor.end_time = timeout;
    ProgressMonitor pmo = new ProgressMonitor(monitor);
    pmo.start();
    TessAPI1.TessBaseAPIRecognize(handle, monitor);
    logger.error("Message: " + pmo.getMessage());
    TessResultIterator ri = TessAPI1.TessBaseAPIGetIterator(handle);
    TessPageIterator pi = TessAPI1.TessResultIteratorGetPageIterator(ri);
    TessAPI1.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 = TessAPI1.TessResultIteratorGetUTF8Text(ri, level);
      String word = ptr.getString(0);
      TessAPI1.TessDeleteText(ptr);
      float confidence = TessAPI1.TessResultIteratorConfidence(ri, level);
      IntBuffer leftB = IntBuffer.allocate(1);
      IntBuffer topB = IntBuffer.allocate(1);
      IntBuffer rightB = IntBuffer.allocate(1);
      IntBuffer bottomB = IntBuffer.allocate(1);
      TessAPI1.TessPageIteratorBoundingBox(pi, level, leftB, topB, rightB, bottomB);
      int left = leftB.get();
      int top = topB.get();
      int right = rightB.get();
      int bottom = bottomB.get();
      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 =
          TessAPI1.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 (TessAPI1.TessPageIteratorNext(pi, level) == TRUE);

    assertTrue(true);
  }