-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickOCR.py
More file actions
174 lines (142 loc) · 5.64 KB
/
QuickOCR.py
File metadata and controls
174 lines (142 loc) · 5.64 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env python3
from tkinter import *
import pytesseract
from pynput.mouse import Listener
import os
import random
import string
from PIL import ImageGrab, Image
class LanguageSelection:
"""language selection window"""
def __init__(self, lang):
"""
constructor
:param lang: language
"""
self.runOCR = False
self._lang = lang[0:3] # set language
# create instance
self._lang_selection_window = Tk()
self._lang_selection_window.title("Quick OCR") # add a title
self._lang_selection_window.resizable(False, False) # disable resizing the GUI by passing in False/False
self._lang_selection_window.geometry('200x50') # set window size
# program should be in center of screen
self._lang_selection_window.eval('tk::PlaceWindow . center')
# set icon
current_dir = os.path.dirname(os.path.abspath(__file__))
self._lang_selection_window.iconphoto(False, PhotoImage(file=current_dir + '/logo/quick_ocr_logo.png'))
# create a dropdown menu
self._language_drop_down = StringVar()
self._language_drop_down.set(lang)
_drop_down_menu = OptionMenu(
self._lang_selection_window,
self._language_drop_down,
"chinese",
"english",
"french",
"deutsch",
"italian",
"japanese",
"polish",
"russian",
"spanish",
"ukrainian",
)
_drop_down_menu.grid(column=0, row=0, pady=10)
self._language_drop_down.trace('w', self.change_dropdown) # link function to change dropdown
_ocr_button = Button(self._lang_selection_window, text="OCR", command=self.btn_click, width=10)
_ocr_button.grid(column=1, row=0, pady=10)
def mainloop(self):
"""start tkinter window"""
self._lang_selection_window.mainloop()
def get_lang(self):
"""get language"""
return self._lang
def change_dropdown(self, *args):
"""event listener for dropdown menu on change"""
self._lang = self._language_drop_down.get()[0:3]
def btn_click(self):
"""event listener for button click"""
self.runOCR = True
self._lang_selection_window.destroy()
class AreaSelection:
"""area selection window"""
def __init__(self, lang):
"""
constructor
:param lang: language
"""
self.lang = lang
self._text = ""
self.root = Tk()
self.clicked_x = 0
self.clicked_y = 0
self.mouse_x = 0
self.mouse_y = 0
self.draw_selection_area = False
# Get the current screen width and height
self.screen_width = self.root.winfo_screenwidth()
self.screen_height = self.root.winfo_screenheight()
# Sets geometric string argument as string value, e.g. 800x600
self.root.geometry(str(self.screen_width) + 'x' + str(self.screen_height))
self.root.overrideredirect(True) # Remove window border
self.root.wait_visibility(self.root) # Remove window from taskbar
self.root.wm_attributes("-alpha", 0.3) # Set windows transparent
self.canvas = Canvas(self.root, width=self.screen_width, height=self.screen_height) # Crate canvas
self.canvas.config(cursor="cross") # Change mouse pointer to cross
self.canvas.pack()
def mainloop(self):
"""start tkinter window"""
# collect events until released
with Listener(on_move=self.on_move, on_click=self.on_click) as listener:
self.root.mainloop() # Start tkinter window
listener.join()
def on_click(self, x, y, button, pressed):
"""
event listener for mouse click
:param x: x coordinate
:param y: y coordinate
:param button: mouse button
:param pressed: pressed or released
:return:
"""
if button == button.left:
# Left button pressed then continue
if pressed:
self.draw_selection_area = True
self.clicked_x = x
self.clicked_y = y
else:
self.draw_selection_area = False
self.root.wm_attributes('-alpha', 0)
self.root.quit() # Remove tkinter window
self.ocr_image(x, y)
if not pressed:
return False # Stop listener
# Start and End mouse position
def on_move(self, x, y):
if self.draw_selection_area:
# draw rectangle on canvas as mouse moves and remove alpha from selection area
self.canvas.delete("all") # Remove previous rectangle
self.canvas.create_rectangle(
self.clicked_x, self.clicked_y, x, y, outline='red', width=1, fill='red')
def ocr_image(self, x, y):
"""ocr image"""
# swap coordinates if user drags from bottom right to top left
if self.clicked_x > x:
self.clicked_x, x = x, self.clicked_x
if self.clicked_y > y:
self.clicked_y, y = y, self.clicked_y
img = ImageGrab.grab(bbox=(self.clicked_x, self.clicked_y, x, y)) # Take the screenshot
# random tmp image name
random_string = ''.join(random.choice(string.ascii_lowercase + string.digits) for _ in range(20))
path = f'/tmp/{random_string}.png'
img.save(path)
img = Image.open(path)
text = pytesseract.image_to_string(img, lang=self.lang) # Run tesseract OCR on image
self._text = text
# delete screenshot
os.remove(path)
def get_text(self):
"""get text"""
return self._text