-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocr.py
121 lines (90 loc) · 3.41 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import requests
import time
import ctypes
import pyautogui
import os
from PIL import Image
from PIL import ImageFilter
import pytesseract
import cv2
class Ocr:
def __init__(self, filePath, url, api=False):
self.filePath = filePath
self.api = api
self.url = url
self.findReference()
self.offsetX = 0
self.offsetY = 0
def action(self):
self.processImage()
data = self.getDataFromImage()
if self.api == True:
if data == "":
self.findReference()
else:
if len(data) == 1:
self.findReference()
print(data)
self.sendToAPI(data)
def findReference(self):
print("SEARCHING FOR HP...")
self.findHp = pyautogui.locateOnScreen('reference.png', confidence=0.5)
print(self.findHp)
if self.findHp is None:
self.findHp = [0,80,100,35]
self.offsetX = 0
self.offsetY = 0
else:
print("FOUND")
#uses external API to parse image to text
def ocrAPI(self, overlay=False, api_key='fc0d1ddf2788957', language='pol'):
payload = {'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
}
with open(self.filePath, 'rb') as f:
r = requests.post('https://api.ocr.space/parse/image',
files={self.filePath: f},
data=payload,
)
text = r.content.decode("utf-8")
parsedText = text[text.find("ParsedText")+13:text.find("ErrorMessage")-3].replace("\\r\\n"," ")
time.sleep(0.5)
return parsedText
#uses internal program 'tesseract' to parse image to text
def ocrTesseract(self):
return pytesseract.image_to_string(Image.open(self.filePath), config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
#makes screenshot and makes it bw
def processImage(self):
#makes screenshot
#myScreenshot = pyautogui.screenshot(region=(0,80,100,35))
""" myScreenshot = pyautogui.screenshot(region=(self.findHp[0] - self.offsetX, self.findHp[1] + self.offsetY,
self.findHp[2] - self.offsetX, self.findHp[3] - self.offsetY)) """
myScreenshot = pyautogui.screenshot(region=(self.findHp[0], self.findHp[1], self.findHp[2], self.findHp[3]))
myScreenshot.save(self.filePath)
#makes screenshot bw
img = Image.open(self.filePath)
img.filter(ImageFilter.SHARPEN)
thresh = 170
fn = lambda x : 255 if x > thresh else 0
r = img.convert('L').point(fn, mode='1')
r.save(self.filePath)
#determines way to get text from image
def getDataFromImage(self):
if(self.api == False):
return self.ocrTesseract()
else:
return self.ocrAPI()
def sendToAPI(self, data):
requests.get(self.url, {'data':data})
def callApi(self):
return requests.get(self.url, {'data':'2137'})
#class end =============================================================
filePath = os.getcwd()+"\\screen.png"
url = 'http://emsonlabels.site/python.php'
i=0
ocr = Ocr(filePath, url)
while i>=0:
#start_time = time.time()
ocr.action()
#print("--- %s seconds ---" % (time.time() - start_time))