Skip to content

Instantly share code, notes, and snippets.

@SoTosorrow
Created January 19, 2023 03:15
Show Gist options
  • Save SoTosorrow/5246b3c055dc2cd3e84d6743a8f551cf to your computer and use it in GitHub Desktop.
Save SoTosorrow/5246b3c055dc2cd3e84d6743a8f551cf to your computer and use it in GitHub Desktop.
OCR Screenshot Notify
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