Код: Выделить всё
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.")
Скорость: иногда кажется, что сценарий недостаточно быстр, чтобы уловить очень короткие вспышки текста, хотя я стремлюсь к примерно 0,2 секунды.
Накладные расходы на обработку OCR: pytesseract может работать медленно при работе с полноэкранными изображениями, и мне интересно, если есть способ сделать это быстрее.
Захват всего текста: поскольку я не знаю, где на экране появится текст, мне приходится захватывать весь экран, что добавляет накладные расходы.
Я ищу советы о том, как оптимизировать этот код, чтобы сделать его быстрее и надежнее. В частности:
Есть ли способ ускорить захват экрана, сохраняя при этом обработку всего экрана?
Существуют ли более быстрые альтернативы pytesseract, которые хорошо работают для распознавания текста в реальном времени?Есть ли какие-нибудь общие советы по оптимизации рабочего процесса захвата и последующего распознавания текста для обработки таких коротких вспышек?
Я буду признателен за любые рекомендации или предложения о том, как решить эту проблему. Заранее спасибо!
Подробнее здесь: https://stackoverflow.com/questions/792 ... the-screen