### Install LanyOCR Package Source: https://github.com/jc1da/lanyocr/blob/main/README.md Installs the LanyOCR library using pip. This is the first step to using the OCR tool. ```bash pip install lanyocr ``` -------------------------------- ### Initialize LanyOcr with Various Recognizer Models Source: https://context7.com/jc1da/lanyocr/llms.txt Provides an example of initializing LanyOcr with different recognition models, highlighting options for English, multi-language support (French, Latin), and alternative models like MMOCR. It demonstrates setting up OCR for high-accuracy English text recognition. ```python from lanyocr import LanyOcr # Available recognizer options: recognizers = { # English recognizers "paddleocr_en_ppocr_v3": "Latest PaddleOCR v3 (recommended)", "paddleocr_en_ppocr_v3_fp16": "FP16 version for GPU acceleration", "paddleocr_en_server": "High accuracy server model", "paddleocr_en_mobile": "Lightweight mobile model", # Multi-language recognizers "paddleocr_french_mobile": "French language support", "paddleocr_latin_mobile": "Latin script languages", # Alternative models "mmocr_satrn": "MMOCR SATRN model", "mmocr_satrn_sm": "MMOCR SATRN small model" } # Example: High-accuracy English OCR ocr = LanyOcr( recognizer_name="paddleocr_en_server", merge_rotated_boxes=True ) results = ocr.infer_from_file("images/document.jpg") ``` -------------------------------- ### Command Line Interface Usage for LanyOCR Source: https://context7.com/jc1da/lanyocr/llms.txt Provides examples for using the detect.py script via the command line. Covers basic inference, enabling advanced features like rotated box merging, language selection, GPU acceleration, and debug mode. ```bash PYTHONPATH=. python detect.py --image_path images/example1.jpg PYTHONPATH=. python detect.py \ --merge_rotated_boxes true \ --merge_vertical_boxes true \ --image_path images/example1.jpg PYTHONPATH=. python detect.py \ --merge_boxes_inference true \ --image_path images/example1.jpg PYTHONPATH=. python detect.py \ --recognizer_name paddleocr_french_mobile \ --image_path images/french_example1.jpg PYTHONPATH=. python detect.py \ --use_gpu true \ --output_path outputs/custom_output.jpg \ --image_path images/document.jpg PYTHONPATH=. python detect.py \ --debug true \ --image_path images/example1.jpg ``` -------------------------------- ### Run LanyOCR Detection Example Source: https://github.com/jc1da/lanyocr/blob/main/README.md Executes the main detection script with various configurations for merging rotated and vertical text boxes. It takes an image path as input and outputs the result to outputs/output.jpg. ```python PYTHONPATH=. python detect.py --merge_rotated_boxes true --merge_vertical true --image_path images/example1.jpg ``` ```python PYTHONPATH=. python detect.py --merge_rotated_boxes true --merge_vertical true --merge_boxes_inference true --image_path images/example1.jpg ``` ```python PYTHONPATH=. python detect.py --merge_rotated_boxes true --merge_vertical true --recognizer_name paddleocr_en_mobile --image_path images/example1.jpg ``` ```python PYTHONPATH=. python detect.py --merge_rotated_boxes true --merge_vertical true --recognizer_name paddleocr_french_mobile --image_path images/french_example1.jpg ``` -------------------------------- ### Multi-Language OCR with LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Shows how to perform OCR in different languages by specifying the appropriate recognizer model during LanyOcr initialization. This example demonstrates French text recognition. ```python from lanyocr import LanyOcr # French text recognition french_ocr = LanyOcr( recognizer_name="paddleocr_french_mobile", merge_rotated_boxes=True ) french_results = french_ocr.infer_from_file("images/french_document.jpg") for result in french_results: print(f"French: {result.text}") ``` -------------------------------- ### Configure Text Box Merging Strategies in LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Illustrates how to configure LanyOcr for different text box merging strategies. It shows examples for no merging, and craft-based merging which intelligently handles rotated and vertical text lines, including processing and printing directional information. ```python from lanyocr import LanyOcr # No merging - each text box processed independently ocr_no_merge = LanyOcr( merger_name="lanyocr_nomerger", merge_rotated_boxes=False, merge_vertical_boxes=False ) # Craft-based merging - intelligent line merging ocr_craft_merge = LanyOcr( merger_name="lanyocr_craftbased", merge_rotated_boxes=True, # Merge upward/downward slanted text merge_vertical_boxes=True # Merge vertical text columns ) # Process rotated text document results = ocr_craft_merge.infer_from_file("images/rotated_text.jpg") for result in results: print(f"Merged line: {result.text}") print(f"Direction: {result.line.direction}") ``` -------------------------------- ### Initialize LanyOCR and Run Benchmarks Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates how to instantiate the LanyOcr engine with specific detector and recognizer models, then utilize the LanyBenchmarkerICDAR2015 to evaluate performance metrics including detector, recognizer, and end-to-end accuracy. ```python from lanyocr import LanyOcr, LanyBenchmarkerICDAR2015 ocr = LanyOcr( detector_name="easyocr_craft", recognizer_name="paddleocr_en_server" ) benchmarker = LanyBenchmarkerICDAR2015( ocr=ocr, dataset_path="./datasets/ICDAR/2015" ) detector_accuracy = benchmarker.compute_detector_accuracy() print(f"Detector Accuracy: {detector_accuracy}") recognizer_accuracy = benchmarker.compute_recognizer_accuracy() print(f"Recognizer Accuracy: {recognizer_accuracy}") e2e_accuracy = benchmarker.compute_e2e_accuracy() print(f"End-to-End Accuracy: {e2e_accuracy}") ``` -------------------------------- ### Initialize LanyOcr with Different Text Detectors Source: https://context7.com/jc1da/lanyocr/llms.txt Shows how to initialize LanyOcr using various text detection models, including EasyOCR CRAFT, PaddleOCR v3, and a faster FP16 version for GPU. This allows users to choose based on their accuracy and speed requirements. ```python from lanyocr import LanyOcr # EasyOCR CRAFT detector - good general-purpose detection ocr_craft = LanyOcr( detector_name="easyocr_craft", recognizer_name="paddleocr_en_server" ) # PaddleOCR v3 detector - recommended default ocr_ppocr = LanyOcr( detector_name="paddleocr_en_ppocr_v3", recognizer_name="paddleocr_en_ppocr_v3" ) # PaddleOCR v3 FP16 detector - faster with GPU ocr_fp16 = LanyOcr( detector_name="paddleocr_en_ppocr_v3_fp16", recognizer_name="paddleocr_en_ppocr_v3_fp16", use_gpu=True ) ``` -------------------------------- ### Initialize LanyOcr OCR Pipeline Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates how to initialize the LanyOcr main class with default settings or custom configurations for detector, recognizer, angle classifier, and merger components. It also shows options for GPU usage and merging strategies. ```python from lanyocr import LanyOcr # Initialize with default settings (English) ocr = LanyOcr() # Initialize with custom configuration ocr = LanyOcr( detector_name="paddleocr_en_ppocr_v3", # Text detection model recognizer_name="paddleocr_en_ppocr_v3", # Text recognition model angle_classifier_name="paddleocr_mobile", # Angle classification model merger_name="lanyocr_craftbased", # Text box merger strategy merge_boxes_inference=False, # Merge boxes before inference (faster but less accurate) merge_rotated_boxes=True, # Enable merging upward/downward text merge_vertical_boxes=False, # Enable merging vertical text use_gpu=False, # Use GPU for inference disable_angle_classifier=False, # Disable 180-degree flip detection use_threadpool=False, # Use thread pool for parallel processing debug=False # Enable debug output ) ``` -------------------------------- ### Available Detectors Source: https://context7.com/jc1da/lanyocr/llms.txt Lists and demonstrates the initialization of different text detection models available in LanyOCR. ```APIDOC ## Available Detectors LanyOCR provides multiple text detection models through the factory pattern. Choose based on accuracy vs speed requirements. ### EasyOCR CRAFT detector Good general-purpose detection. ```python from lanyocr import LanyOcr ocr_craft = LanyOcr( detector_name="easyocr_craft", recognizer_name="paddleocr_en_server" ) ``` ### PaddleOCR v3 detector Recommended default detector. ```python from lanyocr import LanyOcr ocr_ppocr = LanyOcr( detector_name="paddleocr_en_ppocr_v3", recognizer_name="paddleocr_en_ppocr_v3" ) ``` ### PaddleOCR v3 FP16 detector Faster detection with GPU acceleration. ```python from lanyocr import LanyOcr ocr_fp16 = LanyOcr( detector_name="paddleocr_en_ppocr_v3_fp16", recognizer_name="paddleocr_en_ppocr_v3_fp16", use_gpu=True ) ``` ``` -------------------------------- ### Initialize LanyOcr for Latin and English OCR Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates initializing LanyOcr with different recognizer models for Latin and English text. It shows how to load an image from a file and iterate through the recognition results, printing the extracted text. ```python from lanyocr import LanyOcr latin_ocr = LanyOcr( recognizer_name="paddleocr_latin_mobile", merge_rotated_boxes=True ) latin_results = latin_ocr.infer_from_file("images/latin_document.jpg") for result in latin_results: print(f"Latin: {result.text}") # English with high-accuracy server model english_ocr = LanyOcr( recognizer_name="paddleocr_en_server", merge_rotated_boxes=True ) english_results = english_ocr.infer_from_file("images/english_document.jpg") for result in english_results: print(f"English: {result.text}") ``` -------------------------------- ### GPU Acceleration Source: https://context7.com/jc1da/lanyocr/llms.txt Instructions on how to enable GPU acceleration for faster OCR processing. ```APIDOC ## GPU Acceleration Enable GPU inference for faster processing on CUDA-compatible systems. ### Initialize LanyOcr with GPU Support ```python from lanyocr import LanyOcr # Enable GPU for all components ocr = LanyOcr( detector_name="paddleocr_en_ppocr_v3_fp16", recognizer_name="paddleocr_en_ppocr_v3_fp16", use_gpu=True, use_threadpool=True # Parallel processing ) # Process high-resolution image results = ocr.infer_from_file("images/high_res_document.jpg") print(f"Processed {len(results)} text lines") ``` ``` -------------------------------- ### Basic OCR Usage Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates how to perform OCR on Latin and English documents using different LanyOcr recognizers. ```APIDOC ## Basic OCR Usage This section shows how to initialize `LanyOcr` and perform text recognition on image files. ### Initialize LanyOcr for Latin Text ```python from lanyocr import LanyOcr latin_ocr = LanyOcr( recognizer_name="paddleocr_latin_mobile", merge_rotated_boxes=True ) latin_results = latin_ocr.infer_from_file("images/latin_document.jpg") for result in latin_results: print(f"Latin: {result.text}") ``` ### Initialize LanyOcr for High-Accuracy English Text ```python from lanyocr import LanyOcr english_ocr = LanyOcr( recognizer_name="paddleocr_en_server", merge_rotated_boxes=True ) english_results = english_ocr.infer_from_file("images/english_document.jpg") for result in english_results: print(f"English: {result.text}") ``` ``` -------------------------------- ### Generate Output Visualization with LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Illustrates how to generate a visual representation of OCR results, overlaying detected text boxes and recognized text onto the original image. The output can be saved to a file. ```python from lanyocr import LanyOcr ocr = LanyOcr( merge_rotated_boxes=True, merge_vertical_boxes=True ) # Process image results = ocr.infer_from_file("images/example1.jpg") # Generate visualization with detected text boxes ocr.visualize( image="images/example1.jpg", # Image path or numpy array results=results, # OCR results visualize_sub_boxes=False, # Show individual text boxes within lines output_path="outputs/result.jpg", # Output file path font_scale=0.5 # Text font scale ) # Output image saved to outputs/result.jpg ``` -------------------------------- ### Process Image File with LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Shows how to use the infer_from_file method to perform OCR on an image located at a specified file path. It returns detected text, confidence scores, and bounding box information. ```python from lanyocr import LanyOcr ocr = LanyOcr( merge_rotated_boxes=True, merge_vertical_boxes=True ) # Process image file results = ocr.infer_from_file("images/example1.jpg") # Access OCR results for result in results: print(f"Text: {result.text}") print(f"Confidence: {result.prob:.2f}") # Access bounding box coordinates line_rrect = result.line.get_rrect() center_x, center_y = line_rrect[0] width, height = line_rrect[1] angle = line_rrect[2] print(f"Position: ({center_x:.1f}, {center_y:.1f}), Size: {width:.1f}x{height:.1f}, Angle: {angle:.1f}") print("---") ``` -------------------------------- ### Download ICDAR 2015 Dataset Source: https://github.com/jc1da/lanyocr/blob/main/README.md Downloads the ICDAR 2015 dataset using a bash script, which is likely used for benchmarking and accuracy validation. ```bash bash datasets/download_icdar2015.sh ``` -------------------------------- ### Enable GPU Acceleration in LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Explains how to enable GPU inference for faster OCR processing on systems with CUDA support. It shows initializing LanyOcr with GPU enabled for both detector and recognizer, along with parallel processing using a thread pool. ```python from lanyocr import LanyOcr # Enable GPU for all components ocr = LanyOcr( detector_name="paddleocr_en_ppocr_v3_fp16", recognizer_name="paddleocr_en_ppocr_v3_fp16", use_gpu=True, use_threadpool=True # Parallel processing ) # Process high-resolution image results = ocr.infer_from_file("images/high_res_document.jpg") print(f"Processed {len(results)} text lines") ``` -------------------------------- ### Benchmarking LanyOcr with ICDAR 2015 Dataset Source: https://context7.com/jc1da/lanyocr/llms.txt Provides information on evaluating LanyOCR's accuracy using the ICDAR 2015 dataset benchmark. This snippet imports the necessary benchmarker class for evaluation purposes. ```python from lanyocr import LanyOcr from lanyocr.benchmarker.benchmarker_icdar2015 import LanyBenchmarkerICDAR2015 ``` -------------------------------- ### Available Recognizers Source: https://context7.com/jc1da/lanyocr/llms.txt Details the various text recognition models available, categorized by language and performance. ```APIDOC ## Available Recognizers Multiple recognition models are available for different accuracy and speed trade-offs across languages. ### Recognizer Options ```python from lanyocr import LanyOcr # Available recognizer options: recognizers = { # English recognizers "paddleocr_en_ppocr_v3": "Latest PaddleOCR v3 (recommended)", "paddleocr_en_ppocr_v3_fp16": "FP16 version for GPU acceleration", "paddleocr_en_server": "High accuracy server model", "paddleocr_en_mobile": "Lightweight mobile model", # Multi-language recognizers "paddleocr_french_mobile": "French language support", "paddleocr_latin_mobile": "Latin script languages", # Alternative models "mmocr_satrn": "MMOCR SATRN model", "mmocr_satrn_sm": "MMOCR SATRN small model" } ``` ### Example: High-accuracy English OCR ```python from lanyocr import LanyOcr ocr = LanyOcr( recognizer_name="paddleocr_en_server", merge_rotated_boxes=True ) results = ocr.infer_from_file("images/document.jpg") ``` ``` -------------------------------- ### Text Box Merging Strategies Source: https://context7.com/jc1da/lanyocr/llms.txt Explains the different strategies for merging detected text boxes into coherent lines, including handling rotated and vertical text. ```APIDOC ## Text Box Merging Strategies LanyOCR can merge detected text boxes into lines using different strategies. The craft-based merger handles rotated and vertical text. ### No Merging Each text box is processed independently. ```python from lanyocr import LanyOcr ocr_no_merge = LanyOcr( merger_name="lanyocr_nomerger", merge_rotated_boxes=False, merge_vertical_boxes=False ) ``` ### Craft-based Merging Intelligent line merging that handles rotated and vertical text. ```python from lanyocr import LanyOcr ocr_craft_merge = LanyOcr( merger_name="lanyocr_craftbased", merge_rotated_boxes=True, # Merge upward/downward slanted text merge_vertical_boxes=True # Merge vertical text columns ) # Process rotated text document results = ocr_craft_merge.infer_from_file("images/rotated_text.jpg") for result in results: print(f"Merged line: {result.text}") print(f"Direction: {result.line.direction}") ``` ``` -------------------------------- ### Benchmarking with ICDAR 2015 Source: https://context7.com/jc1da/lanyocr/llms.txt Information on evaluating OCR accuracy using the ICDAR 2015 dataset benchmark. ```APIDOC ## Benchmarking with ICDAR 2015 Evaluate OCR accuracy using the ICDAR 2015 dataset benchmark. ```python from lanyocr import LanyOcr from lanyocr.benchmarker.benchmarker_icdar2015 import LanyBenchmarkerICDAR2015 # Further benchmarking code would go here... ``` ``` -------------------------------- ### Accessing LanyOcrResult Object Properties Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates how to access and utilize the information within the LanyOcrResult object. This includes retrieving recognized text, confidence scores, line geometry (rotated rectangles, bounding boxes), and details of individual sub-rectangles within a line. ```python from lanyocr import LanyOcr import cv2 import numpy as np ocr = LanyOcr(merge_rotated_boxes=True) results = ocr.infer_from_file("images/example1.jpg") for result in results: # Access text and confidence text = result.text # Recognized text string confidence = result.prob # Confidence score (0.0 - 1.0) # Access text line information line = result.line # Get minimum area rotated rectangle rrect = line.get_rrect() center = rrect[0] # (x, y) center point size = rrect[1] # (width, height) angle = rrect[2] # Rotation angle # Get bounding box points box_points = cv2.boxPoints(rrect) box = np.int0(box_points) # Access sub-rectangles (individual text boxes) for sub_rrect in line.sub_rrects: sub_text = sub_rrect.text sub_center = sub_rrect.getCenter() sub_bbox = sub_rrect.getBoundingBox() print(f"Sub-box: {sub_text} at ({sub_bbox.left}, {sub_bbox.top})") ``` -------------------------------- ### Process Image Array with LanyOcr Source: https://context7.com/jc1da/lanyocr/llms.txt Demonstrates OCR processing on an image represented as a BGR numpy array, commonly obtained from libraries like OpenCV. This method is suitable for real-time video streams or in-memory image manipulation. ```python import cv2 from lanyocr import LanyOcr ocr = LanyOcr( recognizer_name="paddleocr_en_ppocr_v3", merge_rotated_boxes=True ) # Load image with OpenCV image = cv2.imread("document.jpg") # Perform OCR on image array results = ocr.infer(image) # Extract all detected text detected_texts = [result.text for result in results if result.text] print("Detected text lines:") for idx, text in enumerate(detected_texts): print(f" {idx + 1}. {text}") # Calculate average confidence if results: avg_confidence = sum(r.prob for r in results) / len(results) print(f"Average confidence: {avg_confidence:.2%}") ``` -------------------------------- ### Validate LanyOCR Accuracy Source: https://github.com/jc1da/lanyocr/blob/main/README.md Runs the benchmark script to validate the accuracy of LanyOCR, presumably using the downloaded ICDAR 2015 dataset. ```python python benchmark.py ``` -------------------------------- ### LanyOcrResult Object Source: https://context7.com/jc1da/lanyocr/llms.txt Describes the structure and attributes of the `LanyOcrResult` object, which contains OCR output details. ```APIDOC ## LanyOcrResult - OCR Result Object The result object containing recognized text, confidence score, and text line geometry information. ```python from lanyocr import LanyOcr import cv2 import numpy as np ocr = LanyOcr(merge_rotated_boxes=True) results = ocr.infer_from_file("images/example1.jpg") for result in results: # Access text and confidence text = result.text # Recognized text string confidence = result.prob # Confidence score (0.0 - 1.0) # Access text line information line = result.line # Get minimum area rotated rectangle rrect = line.get_rrect() center = rrect[0] # (x, y) center point size = rrect[1] # (width, height) angle = rrect[2] # Rotation angle # Get bounding box points box_points = cv2.boxPoints(rrect) box = np.int0(box_points) # Access sub-rectangles (individual text boxes) for sub_rrect in line.sub_rrects: sub_text = sub_rrect.text sub_center = sub_rrect.getCenter() sub_bbox = sub_rrect.getBoundingBox() print(f"Sub-box: {sub_text} at ({sub_bbox.left}, {sub_bbox.top})") ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.