### Install Ppllocr Library Source: https://context7.com/gitpetyr/ppllocr/llms.txt Installs the Ppllocr library using pip. This is the first step before using any of its functionalities. ```bash pip install ppllocr ``` -------------------------------- ### Initialize Ppllocr OCR Class Source: https://context7.com/gitpetyr/ppllocr/llms.txt Demonstrates how to initialize the OCR class, either with default bundled model and CPU inference, or with a custom model path and optional GPU acceleration. It also shows how to check the installed library version. ```python from ppllocr import OCR # Default initialization (uses bundled model, CPU inference) ocr = OCR() # Custom model path with GPU support ocr_custom = OCR(model_path="/path/to/custom_model.onnx", use_gpu=True) # Check version import ppllocr print(ppllocr.__version__) # Output: "2.2" ``` -------------------------------- ### Authenticate and Solve CAPTCHA with Requests Source: https://context7.com/gitpetyr/ppllocr/llms.txt Demonstrates how to use the requests library to handle authenticated sessions, fetch a CAPTCHA image, solve it using ppllocr's OCR, and submit login credentials. ```python import requests from pplocr import OCR ocr = OCR() # With session for authenticated requests session = requests.Session() session.headers.update({ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36" }) # Login flow with CAPTCHA captcha_img = session.get("https://example.com/captcha").content captcha_text = ocr.classification(captcha_img) login_data = { "username": "user", "password": "pass", "captcha": captcha_text } response = session.post("https://example.com/login", data=login_data) ``` -------------------------------- ### Web Scraping Integration with Ppllocr Source: https://context7.com/gitpetyr/ppllocr/llms.txt Demonstrates how to integrate Ppllocr with web scraping by fetching CAPTCHA images directly from URLs using the `requests` library and processing the image content as bytes. This allows for solving CAPTCHAs in real-time from web sources. ```python import requests from ppllocr import OCR ocr = OCR() # Fetch and recognize CAPTCHA from URL def solve_captcha(url, headers=None): response = requests.get(url, headers=headers) if response.status_code == 200: return ocr.classification(response.content) return None # Example: Luogu CAPTCHA captcha_url = "https://www.luogu.com.cn/lg4/captcha" result = solve_captcha(captcha_url) print(f"CAPTCHA solved: {result}") ``` -------------------------------- ### Detailed Recognition with Bounding Boxes using classification_box() Source: https://context7.com/gitpetyr/ppllocr/llms.txt Returns both the recognized text and detailed information for each detected character, including confidence scores and bounding box coordinates. This is useful for character-level validation or visualization. It also supports custom thresholds. ```python from ppllocr import OCR ocr = OCR() with open("captcha.jpg", "rb") as f: img_bytes = f.read() # Returns tuple: (text, details_list) text, details = ocr.classification_box(img_bytes) print(f"Full text: {text}") # Output: "2a3B" # Details structure: # [ # {'char': '2', 'conf': 0.98, 'box': [10.5, 5.0, 30.2, 45.1]}, # {'char': 'a', 'conf': 0.95, 'box': [35.0, 8.0, 55.0, 48.0]}, # {'char': '3', 'conf': 0.92, 'box': [60.0, 6.0, 80.5, 46.0]}, # {'char': 'B', 'conf': 0.97, 'box': [85.0, 7.0, 105.0, 47.0]} # ] for char_info in details: char = char_info['char'] conf = char_info['conf'] box = char_info['box'] # [x1, y1, x2, y2] print(f"Character: {char}, Confidence: {conf:.2f}, Box: {box}") # Filter low-confidence results high_conf_chars = [d for d in details if d['conf'] > 0.9] print(f"High confidence characters: {''.join([d['char'] for d in high_conf_chars])}") # With custom thresholds text, details = ocr.classification_box(img_bytes, conf=0.5, iou=0.4) ``` -------------------------------- ### Basic Text Recognition with classification() Source: https://context7.com/gitpetyr/ppllocr/llms.txt Performs CAPTCHA recognition and returns the recognized text as a string. This method accepts input from file paths, bytes, or numpy arrays. It also allows for custom confidence and IoU thresholds. ```python from ppllocr import OCR ocr = OCR() # From file path text = ocr.classification("captcha.jpg") print(f"Result: {text}") # Output: "2a3B" # From bytes (common in web scraping) with open("captcha.jpg", "rb") as f: img_bytes = f.read() text = ocr.classification(img_bytes) print(f"Result: {text}") # Output: "X7kP" # From numpy array import cv2 img_array = cv2.imread("captcha.jpg") text = ocr.classification(img_array) print(f"Result: {text}") # Output: "9mNq" # With custom confidence and IoU thresholds # Higher conf = fewer false positives, lower recall # conf: confidence threshold (default 0.25) # iou: NMS overlap threshold (default 0.45) text = ocr.classification(img_bytes, conf=0.6, iou=0.45) print(f"Result: {text}") ``` -------------------------------- ### Legacy Interface predict() Source: https://context7.com/gitpetyr/ppllocr/llms.txt The predict() method is an alias for classification_box(), maintained for backward compatibility. It provides the same functionality of returning text and detailed character information with bounding boxes. ```python from ppllocr import OCR ocr = OCR() with open("captcha.jpg", "rb") as f: img_bytes = f.read() # Equivalent to classification_box() text, details = ocr.predict(img_bytes, conf=0.25, iou=0.45) print(f"Result: {text}") print(f"Details: {details}") ``` -------------------------------- ### Ppllocr Supported Character Set Source: https://context7.com/gitpetyr/ppllocr/llms.txt Defines the full character set supported by ppllocr v2.2, including alphanumeric characters, symbols, and characters for arithmetic expressions. ```python # Full character set supported by ppllocr v2.2 CHARACTERS = [ '#', '%', '*', '+', '-', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '=', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ] # Supports arithmetic expressions (v2.2 enhancement) # Characters: + - ( ) for math CAPTCHAs like "3+5=" or "8-2=" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.