[Python] How can I capture and process text flashing for 0.2 seconds on the screen?

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    I'm trying to build a Python script that can detect text that flashes on the screen for a very short period (around 0.2 seconds). I'm using mss for screen capturing and pytesseract for OCR. Below is the code I'm working with:

    python
    Copy code
    import cv2
    import pytesseract
    import numpy as np
    from mss import mss
    import time
    import threading

    # Configure pytesseract path
    pytesseract.pytesseract_cmd = "/opt/homebrew/bin/tesseract"

    # Define the full-screen capture region
    sct = mss()
    monitor = sct.monitors[1] # Full screen (adjust for multiple monitors)

    # Initialize variables
    last_detected_text = ""
    detected_text_lock = threading.Lock()

    # Function to capture the screen and process OCR
    def capture_and_process_text():
    global last_detected_text
    while True:
    start_time = time.time()
    # Capture the screen
    screenshot = np.array(sct.grab(monitor))
    # Skip grayscale or thresholding for speed
    text = pytesseract.image_to_string(screenshot, lang="eng").strip()
    # Normalize the text to reduce noise
    normalized_text = " ".join(text.split())

    # Only display new and non-empty text
    with detected_text_lock:
    if normalized_text and normalized_text != last_detected_text:
    print(f"Detected Text: {normalized_text}")
    last_detected_text = normalized_text

    # Dynamically adjust loop timing
    end_time = time.time()
    print(f"Frame processed in {end_time - start_time:.5f} seconds")

    # Run the optimized text capture loop
    print("Starting full-screen text capture...")
    capture_thread = threading.Thread(target=capture_and_process_text)
    capture_thread.start()

    try:
    while True:
    time.sleep(1) # Keep the main thread alive
    except KeyboardInterrupt:
    print("Text capture stopped.")


    This works reasonably well for capturing and processing text on the screen, but I'm facing a few challenges:

    Speed: Sometimes it seems like the script isn't fast enough to catch very brief flashes of text, while I'm aiming around 0.2 seconds. OCR Processing Overhead: pytesseract can be slow when dealing with full-screen images, and I’m wondering if there’s a way to make it faster. Capturing All Text: Since I don’t know where on the screen the text will appear, I have to capture the entire screen, which adds overhead. I’m looking for advice on how to optimize this code to make it faster and more reliable. Specifically:

    Is there a way to make screen capturing faster while still processing the entire screen? Are there any faster alternatives to pytesseract that work well for real-time OCR? Any general tips for optimizing the capture-then-OCR workflow to handle such short flashes? I’d appreciate any guidance or suggestions on how to approach this problem. Thanks in advance!

    Continue reading...

Compartilhe esta Página