Add proof-of-concept GUI.

This commit is contained in:
Andrey Golovizin 2014-08-10 22:57:08 +02:00
parent 163d02e6c6
commit e9f034f54b
4 changed files with 188 additions and 0 deletions

43
pixelocr/gui/__init__.py Normal file
View file

@ -0,0 +1,43 @@
# Copyright (C) 2014 Andrey Golovizin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import sip
sip.setapi('QString', 2)
from .window import MainWindow
from PyQt4.QtGui import (
QApplication,
)
def main():
app = QApplication(sys.argv)
QApplication.setOrganizationName("PixelOCR");
QApplication.setApplicationName("PixelOCR");
win = MainWindow()
win.show()
from ..page import Page
from ..image import Image
win.pageScene.setPage(Page(Image.fromfile(sys.argv[1]).unframe(10)))
sys.exit(app.exec_())

78
pixelocr/gui/pageview.py Normal file
View file

@ -0,0 +1,78 @@
# Copyright (C) 2014 Andrey Golovizin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtCore import Qt
from PyQt4.QtGui import (
QImage,
QPixmap,
QGraphicsView,
QGraphicsScene,
QGraphicsDropShadowEffect,
QPen,
QBrush,
QColor,
QPalette,
)
import numpy as np
from scipy.ndimage import imread
def ndimage2qimage(image):
return QImage(
np.ascontiguousarray(image.astype(np.uint8).copy()).data,
image.shape[1], image.shape[0],
image.shape[1] * 3,
QImage.Format_RGB888,
)
class PageScene(QGraphicsScene):
page = None
pageItem = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setBackgroundBrush(self.palette().window())
def setPage(self, page):
for item in self.items():
self.remoteItem(item)
self.page = page
self.pageItem = self.addPage(page)
shadow = QGraphicsDropShadowEffect()
shadow.setBlurRadius(20)
shadow.setOffset(2, 2)
self.pageItem.setGraphicsEffect(shadow)
letterPen = QPen(QColor(50, 50, 50, 100))
letterBrush = QBrush(QColor(255, 255, 0, 60))
linePen = QPen(QColor(255, 150, 150, 100))
for line in page:
for letter in line:
if not letter.image.isspace:
self.addRect(letter.x1, letter.y1, letter.width, letter.height, letterPen, letterBrush)
self.addRect(line.x1, line.y1, line.width, line.height, Qt.red)
def addPage(self, page):
qimage = ndimage2qimage(page.image.data)
graphicsitem = self.addPixmap(QPixmap.fromImage(qimage))
return graphicsitem
class PageView(QGraphicsView):
pass

64
pixelocr/gui/window.py Normal file
View file

@ -0,0 +1,64 @@
# Copyright (C) 2014 Andrey Golovizin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from PyQt4.QtCore import (
Qt,
QSettings,
QSize,
)
from PyQt4.QtGui import (
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
from .pageview import PageScene, PageView
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('PixelOCR')
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
self.pageScene = PageScene(self)
self.page = PageView(self.pageScene, centralWidget)
layout = QVBoxLayout(centralWidget)
layout.addWidget(self.page)
self.readSettings()
def sizeHint(self):
return QSize(800, 600)
def closeEvent(self, event):
self.saveSettings()
super().closeEvent(event)
def saveSettings(self):
settings = QSettings()
settings.setValue('windowGeometry', self.saveGeometry())
settings.setValue('windowState', self.saveState())
def readSettings(self):
settings = QSettings()
self.restoreGeometry(settings.value("windowGeometry") or b'')
self.restoreState(settings.value("windowState") or b'')

View file

@ -17,5 +17,8 @@ setup(
'Intended Audience :: End Users/Desktop',
'Operating System :: OS Independent',
],
entry_points={
'console_scripts': ['pixelocr = pixelocr.gui:main']
},
packages=find_packages(),
)