forked from lukas-blecher/LaTeX-OCR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
313 lines (266 loc) · 10.3 KB
/
gui.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
from shutil import which
import sys
import os
import tempfile
from PyQt5 import QtCore, QtGui
from PyQt5.QtCore import Qt, pyqtSlot, pyqtSignal, QThread
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtGui import QKeySequence
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox, QVBoxLayout, QWidget, QShortcut,\
QPushButton, QTextEdit, QFormLayout, QHBoxLayout, QDoubleSpinBox
from pix2tex.resources import resources
from pynput.mouse import Controller
from PIL import ImageGrab, Image
import numpy as np
from screeninfo import get_monitors
from pix2tex import cli
from pix2tex.utils import in_model_path
QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
class App(QMainWindow):
isProcessing = False
def __init__(self, args=None):
super().__init__()
self.args = args
self.model = cli.LatexOCR(self.args)
self.initUI()
self.snipWidget = SnipWidget(self)
self.show()
def initUI(self):
self.setWindowTitle("LaTeX OCR")
QApplication.setWindowIcon(QtGui.QIcon(':/icons/icon.svg'))
self.left = 300
self.top = 300
self.width = 500
self.height = 300
self.setGeometry(self.left, self.top, self.width, self.height)
# Create LaTeX display
self.webView = QWebEngineView()
self.webView.setHtml("")
self.webView.setMinimumHeight(80)
# Create textbox
self.textbox = QTextEdit(self)
self.textbox.textChanged.connect(self.displayPrediction)
self.textbox.setMinimumHeight(40)
# Create temperature text input
self.tempField = QDoubleSpinBox(self)
self.tempField.setValue(self.args.temperature)
self.tempField.setRange(0, 1)
self.tempField.setSingleStep(0.1)
# Create snip button
if sys.platform == "darwin":
self.snipButton = QPushButton('Snip [Option+S]', self)
self.snipButton.clicked.connect(self.onClick)
else:
self.snipButton = QPushButton('Snip [Alt+S]', self)
self.snipButton.clicked.connect(self.onClick)
self.shortcut = QShortcut(QKeySequence("Alt+S"), self)
self.shortcut.activated.connect(self.onClick)
# Create retry button
self.retryButton = QPushButton('Retry', self)
self.retryButton.setEnabled(False)
self.retryButton.clicked.connect(self.returnSnip)
# Create layout
centralWidget = QWidget()
centralWidget.setMinimumWidth(200)
self.setCentralWidget(centralWidget)
lay = QVBoxLayout(centralWidget)
lay.addWidget(self.webView, stretch=4)
lay.addWidget(self.textbox, stretch=2)
buttons = QHBoxLayout()
buttons.addWidget(self.snipButton)
buttons.addWidget(self.retryButton)
lay.addLayout(buttons)
settings = QFormLayout()
settings.addRow('Temperature:', self.tempField)
lay.addLayout(settings)
def toggleProcessing(self, value=None):
if value is None:
self.isProcessing = not self.isProcessing
else:
self.isProcessing = value
if self.isProcessing:
text = 'Interrupt'
func = self.interrupt
else:
if sys.platform == "darwin":
text = 'Snip [Option+S]'
else:
text = 'Snip [Alt+S]'
func = self.onClick
self.retryButton.setEnabled(True)
self.shortcut.setEnabled(not self.isProcessing)
self.snipButton.setText(text)
self.snipButton.clicked.disconnect()
self.snipButton.clicked.connect(func)
self.displayPrediction()
@pyqtSlot()
def onClick(self):
self.close()
if which('gnome-screenshot'):
self.snip_using_gnome_screenshot()
else:
self.snipWidget.snip()
@pyqtSlot()
def interrupt(self):
if hasattr(self, 'thread'):
self.thread.terminate()
self.thread.wait()
self.toggleProcessing(False)
def snip_using_gnome_screenshot(self):
try:
with tempfile.NamedTemporaryFile() as tmp:
os.system(f"gnome-screenshot --area --file={tmp.name}")
# Use `tmp.name` instead of `tmp.file` due to compatability issues between Pillow and tempfile
self.returnSnip(Image.open(tmp.name))
except:
print(f"Failed to load saved screenshot! Did you cancel the screenshot?")
print("If you don't have gnome-screenshot installed, please install it.")
self.returnSnip()
def returnSnip(self, img=None):
self.toggleProcessing(True)
self.retryButton.setEnabled(False)
self.show()
try:
self.model.args.temperature = self.tempField.value()
if self.model.args.temperature == 0:
self.model.args.temperature = 1e-8
except:
pass
# Run the model in a separate thread
self.thread = ModelThread(img=img, model=self.model)
self.thread.finished.connect(self.returnPrediction)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.start()
def returnPrediction(self, result):
self.toggleProcessing(False)
success, prediction = result["success"], result["prediction"]
if success:
self.displayPrediction(prediction)
self.retryButton.setEnabled(True)
else:
self.webView.setHtml("")
msg = QMessageBox()
msg.setWindowTitle(" ")
msg.setText("Prediction failed.")
msg.exec_()
def displayPrediction(self, prediction=None):
if self.isProcessing:
pageSource = """<center>
<img src="qrc:/icons/processing-icon-anim.svg" width="50", height="50">
</center>"""
else:
if prediction is not None:
self.textbox.setText("${equation}$".format(equation=prediction))
else:
prediction = self.textbox.toPlainText().strip('$')
pageSource = """
<html>
<head><script id="MathJax-script" src="qrc:MathJax.js"></script>
<script>
MathJax.Hub.Config({messageStyle: 'none',tex2jax: {preview: 'none'}});
MathJax.Hub.Queue(
function () {
document.getElementById("equation").style.visibility = "";
}
);
</script>
</head> """ + """
<body>
<div id="equation" style="font-size:1em; visibility:hidden">$${equation}$$</div>
</body>
</html>
""".format(equation=prediction)
self.webView.setHtml(pageSource)
class ModelThread(QThread):
finished = pyqtSignal(dict)
def __init__(self, img, model):
super().__init__()
self.img = img
self.model = model
def run(self):
try:
prediction = self.model(self.img)
# replace <, > with \lt, \gt so it won't be interpreted as html code
prediction = prediction.replace('<', '\\lt ').replace('>', '\\gt ')
self.finished.emit({"success": True, "prediction": prediction})
except Exception as e:
import traceback
traceback.print_exc()
self.finished.emit({"success": False, "prediction": None})
class SnipWidget(QMainWindow):
isSnipping = False
def __init__(self, parent):
super().__init__()
self.parent = parent
monitos = get_monitors()
bboxes = np.array([[m.x, m.y, m.width, m.height] for m in monitos])
x, y, _, _ = bboxes.min(0)
w, h = bboxes[:, [0, 2]].sum(1).max(), bboxes[:, [1, 3]].sum(1).max()
self.setGeometry(x, y, w-x, h-y)
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
self.mouse = Controller()
def snip(self):
self.isSnipping = True
self.setWindowFlags(Qt.WindowStaysOnTopHint)
QApplication.setOverrideCursor(QtGui.QCursor(QtCore.Qt.CrossCursor))
self.show()
def paintEvent(self, event):
if self.isSnipping:
brushColor = (0, 180, 255, 100)
opacity = 0.3
else:
brushColor = (255, 255, 255, 0)
opacity = 0
self.setWindowOpacity(opacity)
qp = QtGui.QPainter(self)
qp.setPen(QtGui.QPen(QtGui.QColor('black'), 2))
qp.setBrush(QtGui.QColor(*brushColor))
qp.drawRect(QtCore.QRect(self.begin, self.end))
def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
QApplication.restoreOverrideCursor()
self.close()
self.parent.show()
event.accept()
def mousePressEvent(self, event):
self.startPos = self.mouse.position
self.begin = event.pos()
self.end = self.begin
self.update()
def mouseMoveEvent(self, event):
self.end = event.pos()
self.update()
def mouseReleaseEvent(self, event):
self.isSnipping = False
QApplication.restoreOverrideCursor()
startPos = self.startPos
endPos = self.mouse.position
# account for retina display. #TODO how to check if device is actually using retina display
factor = 2 if sys.platform == "darwin" else 1
x1 = int(min(startPos[0], endPos[0]))
y1 = int(min(startPos[1], endPos[1]))
x2 = int(max(startPos[0], endPos[0]))
y2 = int(max(startPos[1], endPos[1]))
self.repaint()
QApplication.processEvents()
try:
img = ImageGrab.grab(bbox=(x1, y1, x2, y2), all_screens=True)
except Exception as e:
if sys.platform == "darwin":
img = ImageGrab.grab(bbox=(x1//factor, y1//factor, x2//factor, y2//factor), all_screens=True)
else:
raise e
QApplication.processEvents()
self.close()
self.begin = QtCore.QPoint()
self.end = QtCore.QPoint()
self.parent.returnSnip(img)
def main(arguments):
with in_model_path():
if os.name != 'nt':
os.environ['QTWEBENGINE_DISABLE_SANDBOX'] = '1'
app = QApplication(sys.argv)
ex = App(arguments)
sys.exit(app.exec())