Ocr now copies detected text to the clipboard.
This commit is contained in:
parent
65e6dcf980
commit
fdc2fdc41d
@ -23,6 +23,7 @@ import time
|
|||||||
import subprocess
|
import subprocess
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image, ImageOps
|
||||||
import pytesseract
|
import pytesseract
|
||||||
|
import pyperclip
|
||||||
|
|
||||||
def capture_screen(max_retries=3, initial_delay=0.2):
|
def capture_screen(max_retries=3, initial_delay=0.2):
|
||||||
"""
|
"""
|
||||||
@ -102,6 +103,22 @@ def perform_ocr(img, lang='eng'):
|
|||||||
|
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
def copy_to_clipboard(text):
|
||||||
|
"""Copy text to clipboard using pyperclip"""
|
||||||
|
try:
|
||||||
|
# Filter out empty lines and clean up the text
|
||||||
|
lines = [line.strip() for line in text.split('\n') if line.strip()]
|
||||||
|
cleaned_text = '\n'.join(lines) # Preserve line breaks for clipboard
|
||||||
|
|
||||||
|
if cleaned_text:
|
||||||
|
pyperclip.copy(cleaned_text)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error copying to clipboard: {e}")
|
||||||
|
return False
|
||||||
|
|
||||||
def speak_text(text):
|
def speak_text(text):
|
||||||
"""Speak the text using speech-dispatcher"""
|
"""Speak the text using speech-dispatcher"""
|
||||||
# Filter out empty lines and clean up the text
|
# Filter out empty lines and clean up the text
|
||||||
@ -131,9 +148,12 @@ def main():
|
|||||||
# Perform OCR
|
# Perform OCR
|
||||||
text = perform_ocr(processed_img)
|
text = perform_ocr(processed_img)
|
||||||
|
|
||||||
|
# Copy to clipboard
|
||||||
|
clipboard_success = copy_to_clipboard(text)
|
||||||
|
|
||||||
# Speak the results
|
# Speak the results
|
||||||
speak_text(text)
|
speak_text(text)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Let the user know something went wrong
|
# Let the user know something went wrong
|
||||||
error_msg = f"Error during OCR: {str(e)}"
|
error_msg = f"Error during OCR: {str(e)}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user