EasyOCR Documentation

repository·master·Indexed 20 days ago

https://github.com/ushelp/easyocr

A Java-based OCR recognition engine built on Tesseract, designed for localized SDK integration in Android, C/S, and B/S architectures. It features integrated image cleanup, specialized CAPTCHA recognition, and document template matching using ETD files and the EasyTemplateDesigner GUI. The API provides tools for multilingual recognition, character whitelisting, and text region detection via the EasyOCR, ImageClean, and Language classes.

Tokens
3K
Snippets
9
Records
11
Agent score
71%

What's inside EasyOCR

  1. Overview of EasyOCR

    master

    EasyOCR is a Java-based OCR (Optical Character Recognition) engine built upon Tesseract. It is designed primarily for developers and provides localized SDK integration for C/S (Client/Server), B/S (Browser/Server), and Android mobile projects.

    Key features include:

    • Image Processing: Integrated image cleanup capabilities.
    • Specialized Recognition: Support for CAPTCHA recognition and bill/note processing.
    • Extensibility: Supports plugin programming and ETD (Electronic Template Document) templates.
    • GUI Tools: Provides EasyTemplateDesigner GUI for graphical ETD template design.

    Note on Licensing: As of version 4.X, EasyOCR is no longer open source or free. Commercial users can access specific documentation for development, installation (Windows/Non-Windows), API usage, plugin development, and Java docs.

  2. Quickstart: Install and use EasyOCR

    master

    To use EasyOCR in your Java project, follow these three steps:

    1. Install Engine: Set up the underlying OCR engine.
    2. Add the jar package: Include the EasyOCR library in your project dependencies.
    3. Call API: Use the EasyOCR class to perform recognition tasks.
    EasyOCR e = new EasyOCR();
    // Direct recognition of image content
    System.out.println(e.recognize("images/demo_eurotext.png"));
  3. Quickstart: Use EasyOCR for basic text recognition

    master

    To perform simple OCR on an image, instantiate the EasyOCR class and call the recognize method with the image path. This method handles the core OCR process in a single line of code.

    EasyOCR e = new EasyOCR();
    // Directly recognize image content
    System.out.println(e.recognize("images/demo_eurotext.png")); 
  4. Recognize CAPTCHA images with automatic cleaning

    master

    For CAPTCHA images, use recognizeAutoCleanImage to apply cleaning algorithms before recognition. You can specify the ImageType to match the specific CAPTCHA style. For difficult cases, you can adjust the width and height deformation ratios to improve accuracy.

    Available ImageType values for CAPTCHA:

    • CAPTCHA_NORMAL: Common Captcha Image
    • CAPTCHA_INTERFERENCE_LINE: CAPTCHA with interference lines
    • CAPTCHA_SPOT: Dot CAPTCHA
    • CAPTCHA_WHITE_CHAR: White text on a solid background
    • CAPTCHA_HOLLOW_CHAR: Hollow text CAPTCHA
    • CLEAR: Ordinary picture clarity (no specific interference)
    • LINK_BOLD: Adhesion bold font
    EasyOCR e = new EasyOCR();
    
    // Direct recognition with specific interference type
    System.out.println(e.recognizeAutoCleanImage("images/img_INTERFERENCE_LINE.png", ImageType.CAPTCHA_INTERFERENCE_LINE)); 
    
    // Recognition with manual deformation adjustment (widthRatio, heightRatio)
    System.out.println(e.recognizeAutoCleanImage("images/img_NORMAL.jpg", ImageType.CAPTCHA_NORMAL, 1.6, 0.7));
  5. Detect text regions in an image

    master

    Use the static method EasyOCR.findTextRegions to locate specific text areas within a BufferedImage. This method returns a list of TextRegion objects containing coordinates.

    String filePath="./images/idcard.png";
    BufferedImage image = ImageIO.read(new File(filePath));
    
    // Find Text Regions with specific parameter bounds
    List<TextRegion> regions = EasyOCR.findTextRegions(image, 10, 20, 70, 200);
    
    // 'regions' contains TextRegion objects with x, y, x2, y2 coordinates
  6. Core API Classes

    master

    EasyOCR provides several key classes for different OCR tasks:

    • EasyOCR: The primary class for text recognition. It handles the full lifecycle including automatic image cleanup and recognition integration. It supports template-based recognition and cleaning.
    • ImageClean: Used for cleaning images, specifically optimized for CAPTCHAs and notes. It supports various predefined cleanup modes to handle deformation and rotation to improve recognition rates.
    • Language: A list of supported languages. Supports simultaneous mixed-language recognition (e.g., English + Japanese).
    • TextMode: An enumeration for selecting different text recognition modes.
    • CleanType: An enumeration for image filtering types, including NONE, CAPTCHA (verification code cleanup), TEXT (text cleaning), and bill image cleanup algorithms.
  7. Advanced API usage: Multilingual, Templates, and Whitelists

    master

    The EasyOCR class supports advanced configurations for specialized recognition tasks:

    • Multilingual Recognition: Use setLanguage with Language.multiLanguage() to recognize multiple languages in one pass.
    • Template-based Recognition: Use recognizeByTemplate with an .etd template file to improve accuracy for structured documents like bank notes.
    • Character Whitelists: Use setCharList to restrict recognition to a specific set of characters (e.g., digits only).
    • Text Modes: Use setTextMode to specify the layout, such as TextMode.SINGLE_LINE_TEXT or TextMode.UNIFORM_TEXT.
    • Rule Amendments: Use setAmendPath to provide a text file for rule-based result modification.
    EasyOCR ocr = new EasyOCR();
    
    // Multilingual hybrid recognition
    ocr.setLanguage(Language.multiLanguage(Language.ENG, Language.CHI_SIM));
    String res2 = ocr.recognize("images/bank/bill2.tif");
    
    // Recognition Based on EMD template
    ocr.setLanguage(Language.CHI_SIM);
    ocr.setTextMode(TextMode.UNIFORM_TEXT);
    List<String> res3 = ocr.recognizeByTemplate("images/bank/bill3.jpg", "images/bank/bill.etd", ImageType.BILL_NORMAL);
    
    // Cleanup digital content with character whitelist
    ocr.setLanguage(Language.ENG);
    ocr.setCharList("0123456789");
    ocr.setTextMode(TextMode.SINGLE_LINE_TEXT);
    String res4 = ocr.recognizeAutoCleanImage("images/bank/example4.jpg", ImageType.TEXT_BOLD_BLAK);
  8. Configure EasyOCR for advanced recognition tasks

    master

    The EasyOCR class provides several configuration methods to refine recognition results:

    • setLanguage(Language): Sets the recognition language. Supports multi-language recognition via Language.multiLanguage(lang1, lang2, ...).
    • setAmendPath(String): Sets a path to a text file for rule-based result correction (e.g., Chinese recognition correction).
    • setCharList(String): Limits recognition to a specific set of characters (e.g., "0123456789").
    • setTextMode(TextMode): Sets the text recognition mode (e.g., TextMode.UNIFORM_TEXT for uniform size or TextMode.SINGLE_LINE_TEXT for single lines).
    • recognizeByTemplate(path, templatePath, imageType): Performs recognition based on an ETD (EasyTemplateDesigner) template file.
    EasyOCR ocr = new EasyOCR();
    
    // Example: Multi-language recognition
    ocr.setLanguage(Language.multiLanguage(Language.ENG, Language.CHI_SIM));
    String res = ocr.recognize("images/bank/bill2.tif");
    
    // Example: Template-based recognition for forms/bills
    ocr.setLanguage(Language.CHI_SIM);
    ocr.setTextMode(TextMode.UNIFORM_TEXT);
    List<String> res3 = ocr.recognizeByTemplate("images/bank/bill3.jpg", "images/bank/bill.etd", ImageType.BILL_NORMAL);
    
    // Example: Character limiting and single line mode
    ocr.setLanguage(Language.ENG);
    ocr.setCharList("0123456789");
    ocr.setTextMode(TextMode.SINGLE_LINE_TEXT);
    String res4 = ocr.recognizeAutoCleanImage("images/bank/example4.jpg", ImageType.TEXT_BOLD_BLAK);
  9. Find text regions in an image

    master

    You can detect specific text regions within a BufferedImage using the static method EasyOCR.findTextRegions. This returns a list of TextRegion objects containing coordinates.

    String filePath="./images/idcard.png";
    BufferedImage image = ImageIO.read(new File(filePath));
    
    // Find Text Regions with custom bounding parameters
    List<TextRegion> regions = EasyOCR.findTextRegions(image, 10, 20, 70, 200);
    
    for (TextRegion r : regions) {
        // Process each region (e.g., draw rectangles around them)
        System.out.println("Region found at: " + r.x + ", " + r.y);
    }
  10. Reference: ImageType enumeration for CAPTCHA and cleaning

    master

    The ImageType (referred to as CleanType in some contexts) defines the specific cleaning and recognition algorithms used for different image types.

    - CAPTCHA_NORMAL: Normal CAPTCHA images
    - CAPTCHA_INTERFERENCE_LINE: CAPTCHA images with interference lines
    - CAPTCHA_SPOT: Dot-based CAPTCHA images
    - CAPTCHA_WHITE_CHAR: White text on solid background CAPTCHA
    - CAPTCHA_HOLLOW_CHAR: Hollow character CAPTCHA
    - CLEAR: General image clarification (no special interference)
    - LINK_BOLD: Bold/connected fonts
    - BILL_NORMAL: Normal bill/invoice type
    - TEXT_BOLD_BLAK: Bold black text
  11. Reference: Core EasyOCR Classes and Enums

    master

    The following classes form the core API of EasyOCR:

    - EasyOCR: Core class for OCR engine calls. Supports recognize(), recognizeAutoCleanImage(), and recognizeByTemplate().
    - ImageClean: Class for cleaning CAPTCHAs, bills, and regular images.
    - Language: List of supported recognition languages (supports mixed language).
    - TextMode: Enumeration of text recognition modes.
    - CleanType: Enumeration for image filtering/cleaning types (NONE, CAPTCHA, TEXT, etc.).