-
Notifications
You must be signed in to change notification settings - Fork 94
/
ocr.py
67 lines (52 loc) · 2.4 KB
/
ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Contains all code related to turning a screenshot into a string
"""
from typing import Any
import cv2
import numpy as np
from PIL import ImageGrab
from tesserocr import PyTessBaseAPI
import settings
TESSDATA_PATH = settings.TESSERACT_TESSDATA_PATH
ALPHABET_WHITELIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
ROUND_WHITELIST = "0123456789-"
def image_grayscale(image: ImageGrab.Image) -> Any:
"""Converts an image to grayscale so OCR has an easier time deciphering characters"""
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def image_thresholding(image: ImageGrab.Image) -> Any:
"""Applies thresholding to the image https://docs.opencv.org/4.x/d7/d4d/tutorial_py_thresholding.html"""
return cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
def image_array(image: ImageGrab.Image) -> Any:
"""Turns the image into an array"""
image = np.array(image)
image = image[..., :3]
return image
def image_resize(image: int, scale: int) -> Any:
"""Resizes the image using the scale passed in argument two"""
(width, height) = (image.width * scale, image.height * scale)
return image.resize((width, height))
def get_text(screenxy: tuple, scale: int, psm: int, whitelist: str = "") -> str:
"""Returns text from screen coordinates"""
screenshot = ImageGrab.grab(bbox=screenxy)
resize = image_resize(screenshot, scale)
array = image_array(resize)
grayscale = image_grayscale(array)
thresholding = image_thresholding(grayscale)
with PyTessBaseAPI(path=TESSDATA_PATH) as api:
api.SetVariable("tessedit_char_whitelist", whitelist)
api.SetPageSegMode(psm)
api.SetImageBytes(thresholding.tobytes(),thresholding.shape[1], thresholding.shape[0],1,thresholding.shape[1])
text = api.GetUTF8Text()
return text.strip()
def get_text_from_image(image: ImageGrab.Image, whitelist: str = "") -> str:
"""Takes an image and returns the text"""
resize = image_resize(image, 3)
array = image_array(resize)
grayscale = image_grayscale(array)
thresholding = image_thresholding(grayscale)
with PyTessBaseAPI(path=TESSDATA_PATH) as api:
api.SetVariable("tessedit_char_whitelist", whitelist)
api.SetPageSegMode(7)
api.SetImageBytes(thresholding.tobytes(),thresholding.shape[1], thresholding.shape[0],1,thresholding.shape[1])
text = api.GetUTF8Text()
return text.strip()