Search in sources :

Example 1 with TessBaseAPI

use of org.bytedeco.javacpp.tesseract.TessBaseAPI in project snap2 by kevinychen.

the class OpenCvManager method batchFindText.

public Map<BufferedImage, String> batchFindText(Collection<BufferedImage> images, OcrOptions options) {
    try (TessBaseAPI api = new TessBaseAPI()) {
        if (api.Init("./data", "eng") != 0)
            throw new IllegalStateException("Could not initialize tesseract.");
        if (options.allowedCharacters != null)
            api.SetVariable("tessedit_char_whitelist", options.allowedCharacters);
        if (options.singleCharacter) {
            api.SetPageSegMode(10);
            api.SetVariable("load_system_dawg", "0");
            api.SetVariable("load_freq_dawg", "0");
        }
        Map<BufferedImage, String> result = new HashMap<>();
        images.forEach(image -> {
            MatOfByte bytes = new MatOfByte();
            Imgcodecs.imencode(".tif", toMat(image), bytes);
            ByteBuffer buffer = ByteBuffer.wrap(bytes.toArray());
            try (PIX pix = lept.pixReadMem(buffer, buffer.capacity())) {
                api.SetImage(pix);
                api.SetRectangle((int) (image.getWidth() * (1 - options.fullness) / 2), (int) (image.getHeight() * (1 - options.fullness) / 2), (int) (image.getWidth() * options.fullness), (int) (image.getHeight() * options.fullness));
                try (BytePointer textPtr = api.GetUTF8Text();
                    IntPointer confidencePtr = api.AllWordConfidences()) {
                    result.put(image, confidencePtr.get(0) >= options.confidenceThreshold ? textPtr.getString() : "");
                }
            }
        });
        return result;
    }
}
Also used : HashMap(java.util.HashMap) TessBaseAPI(org.bytedeco.javacpp.tesseract.TessBaseAPI) IntPointer(org.bytedeco.javacpp.IntPointer) BytePointer(org.bytedeco.javacpp.BytePointer) MatOfByte(org.opencv.core.MatOfByte) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) PIX(org.bytedeco.javacpp.lept.PIX)

Example 2 with TessBaseAPI

use of org.bytedeco.javacpp.tesseract.TessBaseAPI in project kurento-java by Kurento.

the class BrowserTest method ocr.

public String ocr(BufferedImage imgBuff) {
    String parsedOut = null;
    try {
        // Color image to pure black and white
        for (int x = 0; x < imgBuff.getWidth(); x++) {
            for (int y = 0; y < imgBuff.getHeight(); y++) {
                Color color = new Color(imgBuff.getRGB(x, y));
                int red = color.getRed();
                int green = color.getBlue();
                int blue = color.getGreen();
                if (red + green + blue > OCR_COLOR_THRESHOLD) {
                    // Black
                    red = green = blue = 0;
                } else {
                    // White
                    red = green = blue = 255;
                }
                Color col = new Color(red, green, blue);
                imgBuff.setRGB(x, y, col.getRGB());
            }
        }
        // OCR recognition
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(imgBuff, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        TessBaseAPI api = new TessBaseAPI();
        api.Init(null, "eng");
        ByteBuffer imgBB = ByteBuffer.wrap(imageBytes);
        PIX image = pixReadMem(imgBB, imageBytes.length);
        api.SetImage(image);
        // Get OCR result
        BytePointer outText = api.GetUTF8Text();
        // Destroy used object and release memory
        api.End();
        api.close();
        outText.deallocate();
        pixDestroy(image);
        // OCR corrections
        parsedOut = outText.getString().replaceAll("l", "1").replaceAll("Z", "2").replaceAll("O", "0").replaceAll("B", "8").replaceAll("G", "6").replaceAll("S", "8").replaceAll("'", "").replaceAll("‘", "").replaceAll("\\.", ":").replaceAll("E", "8").replaceAll("o", "0").replaceAll("fl", "0").replaceAll("fi", "6").replaceAll("§", "5").replaceAll("I", "1").replaceAll("T", "7").replaceAll("’", "").replaceAll("U", "0").replaceAll("D", "0");
        if (parsedOut.length() > 7) {
            parsedOut = parsedOut.substring(0, 7) + ":" + parsedOut.substring(8, parsedOut.length());
        }
        parsedOut = parsedOut.replaceAll("::", ":");
        // Remove last part (number of frames)
        int iSpace = parsedOut.lastIndexOf(" ");
        if (iSpace != -1) {
            parsedOut = parsedOut.substring(0, iSpace);
        }
    } catch (IOException e) {
        log.warn("IOException in OCR", e);
    }
    return parsedOut;
}
Also used : TessBaseAPI(org.bytedeco.javacpp.tesseract.TessBaseAPI) Color(java.awt.Color) BytePointer(org.bytedeco.javacpp.BytePointer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) PIX(org.bytedeco.javacpp.lept.PIX)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 BytePointer (org.bytedeco.javacpp.BytePointer)2 PIX (org.bytedeco.javacpp.lept.PIX)2 TessBaseAPI (org.bytedeco.javacpp.tesseract.TessBaseAPI)2 Color (java.awt.Color)1 BufferedImage (java.awt.image.BufferedImage)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 IntPointer (org.bytedeco.javacpp.IntPointer)1 MatOfByte (org.opencv.core.MatOfByte)1