Skip to content

Commit edd2198

Browse files
OCR/working inbuild ui
1 parent 1c05865 commit edd2198

File tree

6 files changed

+253
-18
lines changed

6 files changed

+253
-18
lines changed

PyGame/Introduction/Thumbs.db

0 Bytes
Binary file not shown.

PyQtDesigner/OCR/Ocr.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def __init__(self):
2121
self.buttonGetImage.clicked.connect(self.getImage)
2222
self.buttonExtractText.clicked.connect(self.extractText)
2323
self.buttonExtractText.setEnabled(False)
24+
self.buttonClear.clicked.connect(self.clearText)
25+
self.buttonSave.clicked.connect(self.saveText)
2426
QtCore.pyqtSlot()
2527

2628
def getImage(self):
2729
options = QtWidgets.QFileDialog.Options()
2830
options |= QtWidgets.QFileDialog.DontUseNativeDialog
29-
self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self,"Open an image", "","All Files (*);;Python Files (*.py)", options=options)
31+
self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self,"Open an image", "","All Files (*);;Image Files (*.jpg);;Image Files (*.png)", options=options)
3032
if self.fileName:
3133
print(self.fileName)
3234
pattern = ".(jpg|png|jpeg|bmp|jpe|tiff)$"
@@ -44,13 +46,27 @@ def extractText(self):
4446
# Run tesseract OCR on image
4547
text = pytesseract.image_to_string(img, config=config)
4648
# Print recognized text
47-
self.textBrowser.append(text)
49+
self.textEdit.append(text)
4850
print(text)
4951

52+
def clearText(self):
53+
self.textEdit.clear()
54+
55+
# https://pythonspot.com/pyqt5-file-dialog/
56+
def saveText(self):
57+
options = QFileDialog.Options()
58+
options |= QFileDialog.DontUseNativeDialog
59+
fileName, _ = QFileDialog.getSaveFileName(self,"QFileDialog.getSaveFileName()","","All Files (*);;Text Files (*.txt)", options=options)
60+
if fileName:
61+
print(fileName)
62+
file = open(fileName,'w')
63+
text = self.textEdit.toPlainText()
64+
file.write(text)
65+
file.close()
5066

5167
if __name__ == "__main__":
5268
app = QtWidgets.QApplication(sys.argv)
5369
ocr = Ocr()
5470
ocr.show()
5571

56-
sys.exit(app.exec_())
72+
sys.exit(app.exec_())

PyQtDesigner/OCR/Ocr_inbuild.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file 'ocrUI.ui'
4+
#
5+
# Created by: PyQt5 UI code generator 5.10.1
6+
#
7+
# WARNING! All changes made in this file will be lost!
8+
9+
import os , re , cv2 , pytesseract
10+
from PyQt5 import QtCore, QtGui, QtWidgets
11+
from PyQt5.QtGui import QIcon ,QPixmap
12+
13+
class Ui_MainWindow(object):
14+
15+
def __init__(self):
16+
pytesseract.pytesseract.tesseract_cmd = 'H:\Program Files (x86)\Tesseract-OCR\\tesseract.exe'
17+
18+
def setupUi(self, MainWindow):
19+
# window = QtWidgets.QMainWindow()
20+
# window.setWindowTitle('PictureWorkshop')
21+
22+
MainWindow.setObjectName("MainWindow")
23+
MainWindow.resize(1158, 719)
24+
MainWindow.setStyleSheet("QMainWindow#MainWindow\n"
25+
"{\n"
26+
" background-color:rgb(72,164,136);\n"
27+
" /*background-image: url(\'H:/Github/PythonScripts/PyQtDesigner/WebScrapper/bg.png\');*/\n"
28+
" background-position: center; /* Center the image */\n"
29+
" background-repeat: no-repeat;\n"
30+
"}\n"
31+
"\n"
32+
"\n"
33+
"\n"
34+
"QLabel\n"
35+
"{ \n"
36+
" background-color:rgb(255,255,255);\n"
37+
"}\n"
38+
"\n"
39+
"QPushButton\n"
40+
"{\n"
41+
" border-color: black;\n"
42+
" border-width: 1px 1px 1px 1px;\n"
43+
"\n"
44+
"}\n"
45+
"\n"
46+
"\n"
47+
"")
48+
self.centralwidget = QtWidgets.QWidget(MainWindow)
49+
self.centralwidget.setObjectName("centralwidget")
50+
self.buttonGetImage = QtWidgets.QPushButton(self.centralwidget)
51+
self.buttonGetImage.setGeometry(QtCore.QRect(30, 620, 130, 40))
52+
font = QtGui.QFont()
53+
font.setPointSize(10)
54+
self.buttonGetImage.setFont(font)
55+
self.buttonGetImage.setObjectName("buttonGetImage")
56+
self.labelImage = QtWidgets.QLabel(self.centralwidget)
57+
self.labelImage.setGeometry(QtCore.QRect(30, 50, 711, 521))
58+
self.labelImage.setFrameShape(QtWidgets.QFrame.WinPanel)
59+
self.labelImage.setText("")
60+
self.labelImage.setObjectName("labelImage")
61+
self.buttonExtractText = QtWidgets.QPushButton(self.centralwidget)
62+
self.buttonExtractText.setGeometry(QtCore.QRect(190, 620, 130, 40))
63+
font = QtGui.QFont()
64+
font.setPointSize(10)
65+
self.buttonExtractText.setFont(font)
66+
self.buttonExtractText.setObjectName("buttonExtractText")
67+
self.buttonClear = QtWidgets.QPushButton(self.centralwidget)
68+
self.buttonClear.setGeometry(QtCore.QRect(860, 620, 130, 40))
69+
font = QtGui.QFont()
70+
font.setPointSize(10)
71+
self.buttonClear.setFont(font)
72+
self.buttonClear.setObjectName("buttonClear")
73+
self.buttonSave = QtWidgets.QPushButton(self.centralwidget)
74+
self.buttonSave.setGeometry(QtCore.QRect(1030, 620, 130, 40))
75+
font = QtGui.QFont()
76+
font.setPointSize(10)
77+
self.buttonSave.setFont(font)
78+
self.buttonSave.setObjectName("buttonSave")
79+
self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
80+
self.textEdit.setGeometry(QtCore.QRect(860, 50, 451, 521))
81+
font = QtGui.QFont()
82+
font.setPointSize(10)
83+
self.textEdit.setFont(font)
84+
self.textEdit.setFrameShape(QtWidgets.QFrame.WinPanel)
85+
self.textEdit.setFrameShadow(QtWidgets.QFrame.Plain)
86+
self.textEdit.setObjectName("textEdit")
87+
MainWindow.setCentralWidget(self.centralwidget)
88+
self.menubar = QtWidgets.QMenuBar(MainWindow)
89+
self.menubar.setGeometry(QtCore.QRect(0, 0, 1158, 21))
90+
self.menubar.setObjectName("menubar")
91+
MainWindow.setMenuBar(self.menubar)
92+
self.statusbar = QtWidgets.QStatusBar(MainWindow)
93+
self.statusbar.setObjectName("statusbar")
94+
MainWindow.setStatusBar(self.statusbar)
95+
96+
self.buttonGetImage.clicked.connect(self.getImage)
97+
self.buttonExtractText.clicked.connect(self.extractText)
98+
self.buttonExtractText.setEnabled(False)
99+
self.buttonClear.clicked.connect(self.clearText)
100+
self.buttonSave.clicked.connect(self.saveText)
101+
102+
self.retranslateUi(MainWindow)
103+
QtCore.QMetaObject.connectSlotsByName(MainWindow)
104+
105+
def retranslateUi(self, MainWindow):
106+
_translate = QtCore.QCoreApplication.translate
107+
MainWindow.setWindowTitle(_translate("MainWindow", "Optical Character Recognition"))
108+
scriptDir = os.path.dirname(os.path.realpath(__file__))
109+
MainWindow.setWindowIcon(QtGui.QIcon(scriptDir + os.path.sep + 'ocrLogo.png'))
110+
self.buttonGetImage.setText(_translate("MainWindow", "Load image"))
111+
self.buttonExtractText.setText(_translate("MainWindow", "Extract text"))
112+
self.buttonClear.setText(_translate("MainWindow", "Clear"))
113+
self.buttonSave.setText(_translate("MainWindow", "Save"))
114+
115+
116+
def getImage(self):
117+
options = QtWidgets.QFileDialog.Options()
118+
options |= QtWidgets.QFileDialog.DontUseNativeDialog
119+
self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(options=options)
120+
# self.fileName, _ = QtWidgets.QFileDialog.getOpenFileName(self,"Open a image", "","All Files (*);;Image Files (*.jpg);;Image Files (*.png)", options=options)
121+
if self.fileName:
122+
print(self.fileName)
123+
pattern = ".(jpg|png|jpeg|bmp|jpe|tiff)$"
124+
if re.search(pattern,self.fileName):
125+
self.setImage(self.fileName)
126+
127+
def setImage(self,fileName):
128+
self.labelImage.setPixmap(QPixmap(fileName))
129+
self.buttonExtractText.setEnabled(True)
130+
131+
def extractText(self):
132+
config = ('-l eng --oem 1 --psm 3')
133+
img = cv2.imread(self.fileName, cv2.IMREAD_COLOR)
134+
# Run tesseract OCR on image
135+
text = pytesseract.image_to_string(img, config=config)
136+
# Print recognized text
137+
self.textEdit.append(text)
138+
print(text)
139+
140+
def clearText(self):
141+
self.textEdit.clear()
142+
143+
def saveText(self):
144+
options = QtWidgets.QFileDialog.Options()
145+
options |= QtWidgets.QFileDialog.DontUseNativeDialog
146+
# fileName, _ = QtWidgets.QFileDialog.getSaveFileName(self,"Save text","All Files (*);;Text Files (*.txt)", options=options)
147+
fileName, _ = QtWidgets.QFileDialog.getSaveFileName(options=options)
148+
if fileName:
149+
print(fileName)
150+
file = open(fileName,'w')
151+
text = self.textEdit.toPlainText()
152+
file.write(text)
153+
file.close()
154+
155+
156+
if __name__ == "__main__":
157+
import sys
158+
app = QtWidgets.QApplication(sys.argv)
159+
MainWindow = QtWidgets.QMainWindow()
160+
ui = Ui_MainWindow()
161+
ui.setupUi(MainWindow)
162+
MainWindow.show()
163+
sys.exit(app.exec_())
164+

PyQtDesigner/OCR/ocrLogo.png

8.91 KB
Loading

PyQtDesigner/OCR/ocrUI.ui

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,40 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>1289</width>
9+
<width>1158</width>
1010
<height>719</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
1414
<string>MainWindow</string>
1515
</property>
16+
<property name="styleSheet">
17+
<string notr="true">QMainWindow#MainWindow
18+
{
19+
background-color:rgb(72,164,136);
20+
/*background-image: url('H:/Github/PythonScripts/PyQtDesigner/WebScrapper/bg.png');*/
21+
background-position: center; /* Center the image */
22+
background-repeat: no-repeat;
23+
background-size: cover;
24+
}
25+
26+
27+
28+
QLabel
29+
{
30+
background-color:rgb(255,255,255);
31+
}
32+
33+
QPushButton
34+
{
35+
border-color: black;
36+
border-width: 1px 1px 1px 1px;
37+
38+
}
39+
40+
41+
</string>
42+
</property>
1643
<widget class="QWidget" name="centralwidget">
1744
<widget class="QPushButton" name="buttonGetImage">
1845
<property name="geometry">
@@ -48,16 +75,6 @@
4875
<string/>
4976
</property>
5077
</widget>
51-
<widget class="QTextBrowser" name="textBrowser">
52-
<property name="geometry">
53-
<rect>
54-
<x>860</x>
55-
<y>50</y>
56-
<width>411</width>
57-
<height>521</height>
58-
</rect>
59-
</property>
60-
</widget>
6178
<widget class="QPushButton" name="buttonExtractText">
6279
<property name="geometry">
6380
<rect>
@@ -76,11 +93,11 @@
7693
<string>Extract text</string>
7794
</property>
7895
</widget>
79-
<widget class="QPushButton" name="butttonClear">
96+
<widget class="QPushButton" name="buttonClear">
8097
<property name="geometry">
8198
<rect>
8299
<x>860</x>
83-
<y>610</y>
100+
<y>620</y>
84101
<width>130</width>
85102
<height>40</height>
86103
</rect>
@@ -98,22 +115,48 @@
98115
<property name="geometry">
99116
<rect>
100117
<x>1030</x>
101-
<y>610</y>
118+
<y>620</y>
102119
<width>130</width>
103120
<height>40</height>
104121
</rect>
105122
</property>
123+
<property name="font">
124+
<font>
125+
<pointsize>10</pointsize>
126+
</font>
127+
</property>
106128
<property name="text">
107129
<string>Save</string>
108130
</property>
109131
</widget>
132+
<widget class="QTextEdit" name="textEdit">
133+
<property name="geometry">
134+
<rect>
135+
<x>860</x>
136+
<y>50</y>
137+
<width>451</width>
138+
<height>521</height>
139+
</rect>
140+
</property>
141+
<property name="font">
142+
<font>
143+
<pointsize>10</pointsize>
144+
</font>
145+
</property>
146+
<property name="frameShape">
147+
<enum>QFrame::WinPanel</enum>
148+
</property>
149+
<property name="frameShadow">
150+
<enum>QFrame::Plain</enum>
151+
</property>
152+
</widget>
110153
</widget>
111154
<widget class="QMenuBar" name="menubar">
112155
<property name="geometry">
113156
<rect>
114157
<x>0</x>
115158
<y>0</y>
116-
<width>1289</width>
159+
<width>1158</width>
117160
<height>21</height>
118161
</rect>
119162
</property>

PyQtDesigner/OCR/para.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1.1 Whatis computer vision?
2+
3+
As humans, we perceive the three-dimensional structure of the world around us with apparent
4+
ease. Think of how vivid the three-dimensional percept is when you look at a vase of flowers
5+
sitting on the table next to you. You can tell the shape and translucency of each petal through
6+
the subtle patterns of light and shading that play across its surface and effortlessly segment
7+
each flower from the background of the scene (Figure 1.1). Looking at a framed group por-
8+
trait, you can easily count (and name) all of the people in the Picture and even guess at their
9+
emotions from their facial appearance. Perceptual psychologists have spent decades trying to
10+
understand how the visual system works and, even though they can devise optical illusions!
11+
to tease apart some of its principles (Figure 1.3), a complete solution to this puzzle remains
12+
elusive (Marr 1982; Palmer 1999; Livingstone 2008).

0 commit comments

Comments
 (0)