Ejemplo n.º 1
0
  @SuppressWarnings("unused")
  private OcrResult getOcrResult() {
    OcrResult ocrResult;
    String textResult;
    long start = System.currentTimeMillis();

    try {
      baseApi.setImage(ReadFile.readBitmap(bitmap));
      textResult = baseApi.getUTF8Text();
      timeRequired = System.currentTimeMillis() - start;

      // Check for failure to recognize text
      if (textResult == null || textResult.equals("")) {
        return null;
      }
      ocrResult = new OcrResult();
      ocrResult.setWordConfidences(baseApi.wordConfidences());
      ocrResult.setMeanConfidence(baseApi.meanConfidence());
      if (ViewfinderView.DRAW_REGION_BOXES) {
        ocrResult.setRegionBoundingBoxes(baseApi.getRegions().getBoxRects());
      }
      if (ViewfinderView.DRAW_TEXTLINE_BOXES) {
        ocrResult.setTextlineBoundingBoxes(baseApi.getTextlines().getBoxRects());
      }
      if (ViewfinderView.DRAW_STRIP_BOXES) {
        ocrResult.setStripBoundingBoxes(baseApi.getStrips().getBoxRects());
      }

      // Always get the word bounding boxes--we want it for annotating the bitmap after the user
      // presses the shutter button, in addition to maybe wanting to draw boxes/words during the
      // continuous mode recognition.
      ocrResult.setWordBoundingBoxes(baseApi.getWords().getBoxRects());

      //      if (ViewfinderView.DRAW_CHARACTER_BOXES || ViewfinderView.DRAW_CHARACTER_TEXT) {
      //        ocrResult.setCharacterBoundingBoxes(baseApi.getCharacters().getBoxRects());
      //      }
    } catch (RuntimeException e) {
      Log.e(
          "OcrRecognizeAsyncTask",
          "Caught RuntimeException in request to Tesseract. Setting state to CONTINUOUS_STOPPED.");
      e.printStackTrace();
      try {
        baseApi.clear();
        activity.stopHandler();
      } catch (NullPointerException e1) {
        // Continue
      }
      return null;
    }
    timeRequired = System.currentTimeMillis() - start;
    ocrResult.setBitmap(bitmap);
    ocrResult.setText(textResult);
    ocrResult.setRecognitionTimeRequired(timeRequired);
    return ocrResult;
  }
Ejemplo n.º 2
0
  private void testGetUTF8Text(String language, int ocrEngineMode) {
    final String inputText = "hello";
    final Bitmap bmp = getTextImage(inputText, 640, 480);

    // Attempt to initialize the API.
    final TessBaseAPI baseApi = new TessBaseAPI();
    baseApi.init(TESSBASE_PATH, language, ocrEngineMode);
    baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_LINE);
    baseApi.setImage(bmp);

    // Ensure that the result is correct.
    final String outputText = baseApi.getUTF8Text();
    assertEquals("\"" + outputText + "\" != \"" + inputText + "\"", inputText, outputText);

    // Ensure getRegions() works.
    final Pixa regions = baseApi.getRegions();
    assertEquals("Found incorrect number of regions.", regions.size(), 1);
    regions.recycle();

    // Ensure getTextlines() works.
    final Pixa textlines = baseApi.getTextlines();
    assertEquals("Found incorrect number of textlines.", textlines.size(), 1);
    textlines.recycle();

    // Ensure getStrips() works.
    final Pixa strips = baseApi.getStrips();
    assertEquals("Found incorrect number of strips.", strips.size(), 1);
    strips.recycle();

    // Ensure getWords() works.
    final Pixa words = baseApi.getWords();
    assertEquals("Found incorrect number of words.", words.size(), 1);
    words.recycle();

    // Ensure getConnectedComponents() works.
    final Pixa connectedComponents = baseApi.getConnectedComponents();
    assertTrue("Connected components not found.", connectedComponents.size() > 0);
    connectedComponents.recycle();

    // Iterate through the results.
    final ResultIterator iterator = baseApi.getResultIterator();
    String lastUTF8Text;
    float lastConfidence;
    int[] lastBoundingBox;
    Rect lastBoundingRect;
    int count = 0;
    iterator.begin();
    do {
      lastUTF8Text = iterator.getUTF8Text(PageIteratorLevel.RIL_WORD);
      lastConfidence = iterator.confidence(PageIteratorLevel.RIL_WORD);
      lastBoundingBox = iterator.getBoundingBox(PageIteratorLevel.RIL_WORD);
      lastBoundingRect = iterator.getBoundingRect(PageIteratorLevel.RIL_WORD);
      count++;
    } while (iterator.next(PageIteratorLevel.RIL_WORD));
    iterator.delete();

    assertEquals("Found incorrect number of results.", count, 1);
    assertEquals("Found an incorrect result.", lastUTF8Text, outputText);
    assertTrue("Result was not high-confidence.", lastConfidence > 80);
    assertTrue("Result bounding box not found.", lastBoundingBox[2] > 0 && lastBoundingBox[3] > 0);

    boolean validBoundingRect =
        lastBoundingRect.left < lastBoundingRect.right
            && lastBoundingRect.top < lastBoundingRect.bottom;
    assertTrue("Result bounding box Rect is incorrect.", validBoundingRect);

    // Attempt to shut down the API.
    baseApi.end();
    bmp.recycle();
  }