-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmach2.py
211 lines (176 loc) · 7.29 KB
/
mach2.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
import cv2
import numpy as np
import argparse
import sys
import os
from PyQt4 import QtGui, QtCore
import codecs
class detector():
def __init__(self, *args, **kwargs):
self.raw_pic_name = kwargs['raw_pic']
self.detection_number = kwargs['detection']
self.raw_pic = self.read_raw_image()
self.detection_pic = self.get_dtection_image()
self.detection_height = self.detection_pic.shape[0]
self.detection_widths = self.detection_pic.shape[1]
self.result = self.detect()
def get_dtection_image(self):
file_name = 'distinc_digits/{}.png'.format(self.detection_number)
img = cv2.imread(file_name)
return img
def read_raw_image(self):
img = cv2.imread(self.raw_pic_name)
return img
def detect(self):
gray2 = cv2.cvtColor(self.raw_pic, cv2.COLOR_BGR2GRAY)
blur2 = cv2.GaussianBlur(gray2, (5, 5), 0)
ret, thresh2 = cv2.threshold(blur2, 127, 255, 0)
contours, hierarchy = cv2.findContours(thresh2, 2, 1)
for cnt in contours:
[x, y, w, h] = cv2.boundingRect(cnt)
if (h > 27 and h < 35):
tempim = self.raw_pic[y:y + h, x:x + w]
new = cv2.resize(tempim, (self.detection_widths,
self.detection_height))
# new = np.float32(new)
res = cv2.matchTemplate(self.detection_pic, new, 1)
if res < 0.2:
cv2.rectangle(self.raw_pic, (x, y), (x + w, y + h), (0, 0, 255), 2)
return self.raw_pic
def show(self):
cv2.imshow('result', self.result)
cv2.waitKey(0)
def write(self):
cv2.imwrite('result.png', self.result)
class TestListView(QtGui.QDialog):
def __init__(self, type, parent=None):
super(TestListView, self).__init__(parent)
self.setAcceptDrops(True)
self.createFilesTable()
self.label6 = QtGui.QTextEdit(self)
self.label6.setGeometry(70, 260, 100, 30)
self.numLabel = QtGui.QLabel("Number:")
findButton = self.createButton("&Detect", self.find)
buttonsLayout = QtGui.QHBoxLayout()
buttonsLayout.addStretch()
buttonsLayout.addWidget(findButton)
mainLayout = QtGui.QGridLayout()
mainLayout.addWidget(self.filesTable, 3, 0, 1, 3)
browseButton = self.createButton("&Browse...", self.browse)
self.directoryComboBox = self.createComboBox(QtCore.QDir.currentPath())
directoryLabel = QtGui.QLabel("Your Path:")
mainLayout.addWidget(directoryLabel, 2, 0)
mainLayout.addWidget(self.numLabel, 5, 0)
mainLayout.addWidget(self.directoryComboBox, 2, 1)
mainLayout.addWidget(browseButton, 2, 2)
mainLayout.addLayout(buttonsLayout, 5, 0, 1, 3)
self.setLayout(mainLayout)
def browse(self):
fileName = QtGui.QFileDialog.getOpenFileName(self)
if fileName:
inFile = QtCore.QFile(fileName)
if not inFile.open(QtCore.QFile.ReadOnly):
QtGui.QMessageBox.warning(self,
"Codecs",
"Cannot read file %s:\n%s" % (fileName,
inFile.errorString()))
return
self.showFiles(fileName)
def showFiles(self, files):
file = QtCore.QFile(files)
size = QtCore.QFileInfo(file).size()
fileNameItem = QtGui.QTableWidgetItem(files)
fileNameItem.setFlags(fileNameItem.flags() ^ QtCore.Qt.ItemIsEditable)
sizeItem = QtGui.QTableWidgetItem("%d KB" % (int((size + 1023) / 1024)))
sizeItem.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
sizeItem.setFlags(sizeItem.flags() ^ QtCore.Qt.ItemIsEditable)
row = self.filesTable.rowCount()
self.filesTable.insertRow(row)
self.filesTable.setItem(row, 0, fileNameItem)
self.filesTable.setItem(row, 1, sizeItem)
def updateComboBox(self, comboBox):
if comboBox.findText(comboBox.currentText()) == -1:
comboBox.addItem(comboBox.currentText())
def createButton(self, text, member):
button = QtGui.QPushButton(text)
button.clicked.connect(member)
return button
def createComboBox(self, text=""):
comboBox = QtGui.QComboBox()
comboBox.setEditable(True)
comboBox.addItem(text)
comboBox.setSizePolicy(QtGui.QSizePolicy.Expanding,
QtGui.QSizePolicy.Preferred)
return comboBox
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.emit(QtCore.SIGNAL("dropped"), links)
else:
event.ignore()
def createFilesTable(self):
self.filesTable = QtGui.QTableWidget(0, 2)
self.itemm = QtGui.QTableWidgetItem()
self.filesTable.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
self.filesTable.setHorizontalHeaderLabels(("File Name", "Size"))
self.filesTable.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.Stretch)
self.filesTable.verticalHeader().hide()
self.filesTable.setShowGrid(True)
self.filesTable.cellActivated.connect(self.find)
def find(self, fileName):
path = self.directoryComboBox.currentText()
self.updateComboBox(self.directoryComboBox)
item = self.filesTable.item(0, 0)
a = QtGui.QTableWidgetItem(item)
d = detector(raw_pic=str(a.text()),
detection=str(self.label6.toPlainText()))
d.show()
class MainForm(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainForm, self).__init__(parent)
self.view = TestListView(self)
self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
self.setCentralWidget(self.view)
self.resize(700, 300)
def pictureDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QtGui.QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QtGui.QIcon(pixmap)
self.view.showFiles(url)
def main():
app = QtGui.QApplication(sys.argv)
form = MainForm()
form.show()
app.exec_()
if __name__ == '__main__':
main()
"""
if __name__ == "__main__":
description = "Detect digits. Use -h or --help for help menu."
parser = argparse.ArgumentParser(description=description)
parser.add_argument("-P", "-picture_name", help="The name of picture.")
parser.add_argument("-N", "-number", help="The number for detect.")
args = parser.parse_args()
name = args.P
num = args.N
d = detector(raw_pic=name, detection=num)
d.show()
"""