@SmallTest public void testWordConfidences() { final String inputText = "one two three"; final Bitmap bmp = getTextImage(inputText, 640, 480); // Attempt to initialize the API. final TessBaseAPI baseApi = new TessBaseAPI(); baseApi.init(TESSBASE_PATH, DEFAULT_LANGUAGE); baseApi.setPageSegMode(TessBaseAPI.PageSegMode.PSM_SINGLE_BLOCK); baseApi.setImage(bmp); String text = baseApi.getUTF8Text(); assertNotNull("Recognized text is null.", text); // Ensure that a mean confidence value is returned. int conf = baseApi.meanConfidence(); boolean validConf = conf > 0 && conf <= 100; assertTrue("Mean confidence value is incorrect.", validConf); // Ensure that word confidence values are returned. int numWords = text.split("\\s+").length; int[] wordConf = baseApi.wordConfidences(); assertEquals("Found the wrong number of word confidence values.", numWords, wordConf.length); for (int i = 0; i < wordConf.length; i++) { boolean valid = 0 <= wordConf[i] && wordConf[i] <= 100; assertTrue("Found an invalid word confidence value.", valid); } // Attempt to shut down the API. baseApi.end(); bmp.recycle(); }
@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; }