-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocrtranslator.py
223 lines (191 loc) · 7.98 KB
/
ocrtranslator.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import atexit
import os
import signal
import sys
import warnings
from glob import glob
from http.server import HTTPServer, SimpleHTTPRequestHandler
from random import randint
from threading import Thread
from time import strftime
from urllib.parse import urlencode
from PyQt5.QtCore import QFileInfo, QPoint, QRect, QSize, Qt, QTimer, QUrl
from PyQt5.QtGui import (QBrush, QColor, QFont, QIcon, QPainter, QPen, QPixmap,
QStaticText)
from PyQt5.QtWidgets import (QApplication, QDialog, QLabel, QRubberBand,
QVBoxLayout, QWidget, qApp)
try:
from PyQt5.QtWebKitWidgets import QWebView as QWebEngineView
except ImportError:
from PyQt5.QtWebEngineWidgets import QWebEngineView
warnings.filterwarnings("ignore")
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL)
class OCRHTTPHandler(SimpleHTTPRequestHandler):
def __init__(self, *arg, **kwargs):
super(OCRHTTPHandler, self).__init__(*arg, **kwargs)
self.protocol_version = "HTTP/1.0"
def log_message(self, format, *args):
return
class Selector(QRubberBand):
def __init__(self, *arg, **kwargs):
super(Selector, self).__init__(*arg, **kwargs)
def paintEvent(self, ev):
pen = QPen()
pen.setStyle(Qt.DotLine)
pen.setWidth(2)
pen.setColor(QColor(Qt.white))
brush = QBrush()
brush.setStyle(Qt.SolidPattern)
brush.setColor(QColor(0, 0, 0))
painter = QPainter(self)
painter.setPen(pen)
painter.setBrush(brush)
painter.drawRect(ev.rect())
class InfoPanel(QDialog):
def __init__(self, parent, f=Qt.WA_TranslucentBackground | Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.X11BypassWindowManagerHint):
super(InfoPanel, self).__init__(parent, f)
self.setAutoFillBackground(True)
self.setModal(True)
logo = QPixmap(os.path.join(OCRTranslator.getFilePath(), "www", "img", "infopanel-logo.png"), "PNG")
content = QLabel()
content.setTextFormat(Qt.RichText)
with open(os.path.join(OCRTranslator.getFilePath(), "resources", "info.html"), "r") as file:
content.setText(file.read())
content.setStyleSheet('''table { background: #FFF; color: #000; border-radius:6px; border:1px solid #000; }
td > b { font-size: 11pt; } td { font-size: 10pt; }''')
layout = QVBoxLayout()
layout.setContentsMargins(0, 15, 15, 0)
layout.addWidget(QLabel(pixmap=logo), 0, Qt.AlignRight)
layout.addWidget(content, 0, Qt.AlignRight)
self.setLayout(layout)
def keyPressEvent(self, ev):
if ev.key() == Qt.Key_Escape:
self.close()
class OCRTranslator(QDialog):
def __init__(self, parent=None, f=Qt.Tool | Qt.FramelessWindowHint | Qt.X11BypassWindowManagerHint):
super(OCRTranslator, self).__init__(parent, f)
self.desktopGeometry = self.getDesktopGeometry()
self.setGeometry(self.desktopGeometry)
self.setStyleSheet("background-color: #FFF;")
self.setModal(True)
self.setWindowOpacity(0.4)
self.setCursor(Qt.CrossCursor)
self.rubberBand = Selector(QRubberBand.Rectangle, self)
self.rubberBand.setWindowOpacity(0.7)
self.start, self.end = QPoint(), QPoint()
self.hasSelected = False
self.screenshot = QPixmap()
self.shotfilename = None
self.info = InfoPanel(self)
self.info.setFixedSize(QSize(450, 100))
self.info.setGeometry(OCRTranslator.getDesktopGeometry().width() - 460, 0, 450, 100)
self.info.show()
def mousePressEvent(self, ev):
if ev.button() == Qt.LeftButton:
self.info.close()
self.setCursor(Qt.CrossCursor)
self.start = QPoint(ev.pos())
self.rubberBand.setGeometry(QRect(self.start, QSize()))
self.rubberBand.show()
def mouseMoveEvent(self, ev):
if not self.start.isNull():
self.rubberBand.setGeometry(QRect(self.start, ev.pos()).normalized())
self.hasSelected = True
def mouseReleaseEvent(self, ev):
if ev.button() == Qt.LeftButton and self.hasSelected:
self.end = QPoint(ev.pos())
self.setCursor(Qt.ArrowCursor)
self.hasSelected = False
def keyPressEvent(self, ev):
if ev.key() == Qt.Key_Escape:
self.close()
elif ev.key() in [Qt.Key_Enter, Qt.Key_Return] and not self.start.isNull() and not self.end.isNull():
self.hide()
QTimer().singleShot(500, self.takeScreenshot)
def takeScreenshot(self):
x = self.desktopGeometry.x() + self.start.x()
y = self.desktopGeometry.y() + self.start.y()
w = self.end.x() - self.start.x()
h = self.end.y() - self.start.y()
self.screenshot = qApp.screens()[0].grabWindow(qApp.desktop().winId(), x, y, w, h)
self.shotfilename = os.path.join(self.getFilePath(), "www", "temp", strftime('%Y%m%d-%H%M%S')) + ".png"
self.screenshot.save(self.shotfilename, "PNG", 100)
if not self.shotfilename is None and type(self.screenshot) is QPixmap:
self.openTranslator()
@staticmethod
def getDesktopGeometry():
totalWidth = 0
maxHeight = 0
minX = 0
screens = (qApp.screens()[i] for i in range(qApp.desktop().screenCount()))
rects = [screen.geometry() for screen in screens]
for rect in rects:
totalWidth += rect.width()
if rect.x() < minX:
minX = rect.x()
if rect.height() > maxHeight:
maxHeight = rect.height()
return QRect(minX, 0, totalWidth, maxHeight)
@staticmethod
def getFilePath():
if getattr(sys, 'frozen', False):
return sys._MEIPASS
return os.path.dirname(os.path.realpath(sys.argv[0]))
def getAppFrameSize(self):
viewWidth = self.screenshot.width() + 85
if viewWidth < 600:
viewWidth = 600
viewHeight = self.screenshot.height() + 185
if viewHeight < 550:
viewHeight = 550
return QSize(viewWidth, viewHeight)
def webServer(self):
if getattr(sys, 'frozen', False):
os.chdir(sys._MEIPASS)
for r, d, f in os.walk(sys._MEIPASS):
os.chmod(r, 0o755)
else:
os.chdir(os.path.join(QFileInfo(__file__).absolutePath(), 'www'))
httpd = HTTPServer((self.address, self.port), OCRHTTPHandler)
httpd.serve_forever()
def openTranslator(self):
self.address = "127.0.0.1"
self.port = randint(2000, 5000)
self.t = Thread(target=self.webServer)
self.t.daemon = True
self.t.start()
self.view = QWebEngineView()
self.view.setWindowTitle("OCR Translator")
self.view.setWindowIcon(QIcon(os.path.join(self.getFilePath(), "www", "img", "app-icon.png")))
self.view.setContextMenuPolicy(Qt.NoContextMenu)
qryparam = urlencode({'img': self.shotfilename.split('\\').pop().split('/').pop()})
self.url = QUrl("http://" + self.address + ":" + str(self.port) + "/index.html#?" + qryparam)
self.view.load(self.url)
self.view.setMinimumSize(self.getAppFrameSize())
self.view.closeEvent = self.closeEvent
self.view.show()
def closeEvent(self, ev):
qApp.quit()
def Cleanup():
temppath = os.path.join(OCRTranslator.getFilePath(), "www", "temp", "**")
try:
filelist = glob(temppath)
for file in filelist:
os.remove(file)
except:
print("Error deleting file " + temppath + ":", sys.exc_info()[0])
pass
def main():
atexit.register(Cleanup)
QApplication.setStyle('Fusion')
app = QApplication(sys.argv)
app.setApplicationName("OCR Translator")
app.setQuitOnLastWindowClosed(True)
ocrtranslator = OCRTranslator()
ocrtranslator.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()