Created
January 19, 2023 03:15
-
-
Save SoTosorrow/5246b3c055dc2cd3e84d6743a8f551cf to your computer and use it in GitHub Desktop.
OCR Screenshot Notify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image, ImageGrab | |
from plyer import notification | |
import time | |
import numpy as np | |
import pytesseract | |
import easyocr | |
import keyboard | |
class TesseractOcr: | |
def __init__(self): | |
pytesseract.pytesseract.tesseract_cmd = 'D:/T/tesseract/tesseract.exe' | |
def ocr(self, img, lang="chi_sim"): | |
start = time.time() | |
result = pytesseract.image_to_string(img, lang=lang) | |
end = time.time() | |
print("TEST: OCR_TIME:",end-start) | |
return result | |
class EasyOcr: | |
def __init__(self): | |
self.reader = easyocr.Reader(['ch_sim','en'], | |
model_storage_directory="./model", | |
# user_network_directory="./" | |
download_enabled=False | |
) | |
print("DEBUG: Load Easyocr Model Done") | |
def ocr(self, img): | |
np_img = np.array(img) | |
start = time.time() | |
result = self.reader.readtext(np_img, detail=0, paragraph=True) | |
end = time.time() | |
print("TEST: OCR_TIME:",end-start) | |
return result[0] | |
#!! max length 256 | |
def notify(text): | |
notification.notify( | |
title="1", | |
message=text, | |
timeout=5 | |
) | |
if __name__ == '__main__': | |
use = TesseractOcr() | |
# use = EasyOcr() | |
print('DEBUG: Prepare') | |
while True: | |
keyboard.wait("ctrl+'") | |
print('DEBUG: Start') | |
clip_image = ImageGrab.grabclipboard() | |
if clip_image != None: | |
# print(clip_image) | |
text = use.ocr(clip_image) | |
notify(text) | |
else: | |
print("DEBUG: No Image in ClipBoard") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment