-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp_advanced.py
More file actions
318 lines (247 loc) · 10.8 KB
/
app_advanced.py
File metadata and controls
318 lines (247 loc) · 10.8 KB
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
314
315
316
317
318
#!/usr/bin/env python3
'''
Usage:
app.py <license.txt>
'''
import sys
from PySide2.QtGui import QPixmap, QImage
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtCore import QFile, QTimer
from PySide2.QtWidgets import *
from design import Ui_MainWindow
from barcode_manager import *
import os
import cv2
from dbr import EnumBarcodeFormat, EnumBarcodeFormat_2
class MainWindow(QMainWindow):
def __init__(self, license):
super(MainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Initialization
self._all_data = {}
self._results = None
# Dynamsoft Barcode Reader
self._barcodeManager = BarcodeManager(license)
# Create a timer.
self.timer = None
# Open camera
self._cap = None
# self.openCamera()
# Resolution list
self.ui.comboBox.currentTextChanged.connect(self.onComboBoxChanged)
# The current path.
self._path = os.path.dirname(os.path.realpath(__file__))
# Camera button
self.ui.pushButton_open.clicked.connect(self.openCamera)
self.ui.pushButton_stop.clicked.connect(self.stopCamera)
# Load file
self.ui.actionOpen_File.triggered.connect(self.openFile)
# Load directory
self.ui.actionOpen_Folder.triggered.connect(self.openFolder)
# About
self.ui.actionAbout.triggered.connect(self.about)
## List widget
self.ui.listWidget.currentItemChanged.connect(self.currentItemChanged)
## template button
self.ui.pushButton_template.clicked.connect(self.loadTemplate)
def openCamera(self):
self.stopCamera()
self.set_parameters()
width = 640; height = 480
resolution = self.ui.comboBox.currentText()
if resolution == '640 x 480':
width = 640
height = 480
elif resolution == '320 x 240':
width = 320
height = 240
self._cap = cv2.VideoCapture(0)
self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
width = self._cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = self._cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
if not self._cap.isOpened():
self.showMessageBox('Error', "Failed to open camera.")
return
if not self.ui.checkBox_syncdisplay.isChecked():
self._barcodeManager.create_barcode_process()
self.timer = QTimer()
self.timer.timeout.connect(self.nextFrameUpdate)
self.timer.start(1000./24)
def stopCamera(self):
if self._cap is not None:
self._cap.release()
self._cap = None
if not self.ui.checkBox_syncdisplay.isChecked():
if self.timer is not None:
self.timer.stop()
self.timer = None
self._barcodeManager.destroy_barcode_process()
else:
if self.timer is not None:
self.timer.stop()
self.timer = None
self._results = None
def nextFrameUpdate(self):
ret, frame = self._cap.read()
if not ret:
self.showMessageBox('Error', 'Failed to get camera frame!')
return
if not self.ui.checkBox_syncdisplay.isChecked():
self._barcodeManager.append_frame(frame)
self._results = self._barcodeManager.peek_results()
else:
frame, self._results = self._barcodeManager.decode_frame(frame)
self.showResults(frame, self._results)
def onComboBoxChanged(self):
self.openCamera()
def loadTemplate(self):
filename = QFileDialog.getOpenFileName(self, 'Open template',
self._path, "*.json")
if filename is None or filename[0] == '':
# self.showMessageBox('Open File...', "No file selected")
return
with open(filename[0]) as f:
template = f.read()
self.ui.textEdit_template.setText(template)
def appendFile(self, filename):
if filename not in self._all_data:
item = QListWidgetItem()
item.setText(filename)
self.ui.listWidget.addItem(item)
self._all_data[filename] = None
def currentItemChanged(self, current, previous):
filename = current.text()
self.decodeFile(filename)
def set_parameters(self):
# Get template
template = self.ui.textEdit_template.toPlainText()
self._barcodeManager.set_template(template)
# Get barcode types
types = 0; types2 = 0
if (self.ui.checkBox_code39.isChecked()): types |= EnumBarcodeFormat.BF_CODE_39
if (self.ui.checkBox_code93.isChecked()): types |= EnumBarcodeFormat.BF_CODE_93
if (self.ui.checkBox_code128.isChecked()): types |= EnumBarcodeFormat.BF_CODE_128
if (self.ui.checkBox_codabar.isChecked()): types |= EnumBarcodeFormat.BF_CODABAR
if (self.ui.checkBox_itf.isChecked()): types |= EnumBarcodeFormat.BF_ITF
if (self.ui.checkBox_ean13.isChecked()): types |= EnumBarcodeFormat.BF_EAN_13
if (self.ui.checkBox_ean8.isChecked()): types |= EnumBarcodeFormat.BF_EAN_8
if (self.ui.checkBox_upca.isChecked()): types |= EnumBarcodeFormat.BF_UPC_A
if (self.ui.checkBox_upce.isChecked()): types |= EnumBarcodeFormat.BF_UPC_E
if (self.ui.checkBox_industrial25.isChecked()): types |= EnumBarcodeFormat.BF_INDUSTRIAL_25
if (self.ui.checkBox_qrcode.isChecked()): types |= EnumBarcodeFormat.BF_QR_CODE
if (self.ui.checkBox_pdf417.isChecked()): types |= EnumBarcodeFormat.BF_PDF417
if (self.ui.checkBox_aztec.isChecked()): types |= EnumBarcodeFormat.BF_AZTEC
if (self.ui.checkBox_maxicode.isChecked()): types |= EnumBarcodeFormat.BF_MAXICODE
if (self.ui.checkBox_datamatrix.isChecked()): types |= EnumBarcodeFormat.BF_DATAMATRIX
if (self.ui.checkBox_gs1.isChecked()): types |= EnumBarcodeFormat.BF_GS1_COMPOSITE
if (self.ui.checkBox_patchcode.isChecked()): types |= EnumBarcodeFormat.BF_PATCHCODE
if (self.ui.checkBox_dotcode.isChecked()): types2 |= EnumBarcodeFormat_2.BF2_DOTCODE
if (self.ui.checkBox_postalcode.isChecked()): types2 |= EnumBarcodeFormat_2.BF2_POSTALCODE
self._barcodeManager.set_barcode_types(types)
self._barcodeManager.set_barcode_types_2(types2)
def decodeFile(self, filename):
self.stopCamera()
self.set_parameters()
# Read barcodes
frame, results = self._barcodeManager.decode_file(filename)
if frame is None:
self.showMessageBox('Error', 'Cannot decode ' + filename)
return
self._all_data[filename] = results
self.showResults(frame, results)
def openFile(self):
filename = QFileDialog.getOpenFileName(self, 'Open file',
self._path, "Barcode images (*)")
if filename is None or filename[0] == '':
# self.showMessageBox('Open File...', "No file selected")
return
filename = filename[0]
self.appendFile(filename)
self.decodeFile(filename)
def openFolder(self):
dir = QFileDialog.getExistingDirectory(self, 'Open Folder',
self._path, QFileDialog.ShowDirsOnly)
if dir is '':
# self.showMessageBox('Open Folder...', "No folder selected")
return
files = [os.path.join(dir, f) for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))]
if len(files) == 0:
return
for filename in files:
self.appendFile(filename)
self.decodeFile(files[0])
def resizeImage(self, pixmap):
lwidth = self.ui.label.maximumWidth()
pwidth = pixmap.width()
lheight = self.ui.label.maximumHeight()
pheight = pixmap.height()
wratio = pwidth * 1.0 / lwidth
hratio = pheight * 1.0 / lheight
if pwidth > lwidth or pheight > lheight:
if wratio > hratio:
lheight = pheight / wratio
else:
lwidth = pwidth / hratio
scaled_pixmap = pixmap.scaled(lwidth, lheight)
return scaled_pixmap
else:
return pixmap
def showResults(self, frame, results):
out = ''
index = 0
if results is not None and results[0] is not None:
if self.ui.checkBox_autostop.isChecked():
self.stopCamera()
thickness = 2
color = (0,255,0)
out = 'Elapsed time: ' + "{:.2f}".format(results[1]) + 'ms\n\n'
for result in results[0]:
points = result.localization_result.localization_points
out += "Index: " + str(index) + "\n"
out += "Barcode format: " + result.barcode_format_string + '\n'
out += "Barcode value: " + result.barcode_text + '\n'
out += "Bounding box: " + str(points[0]) + ' ' + str(points[1]) + ' ' + str(points[2]) + ' ' + str(points[3]) + '\n'
out += '-----------------------------------\n'
index += 1
cv2.line(frame, points[0], points[1], color, thickness)
cv2.line(frame, points[1], points[2], color, thickness)
cv2.line(frame, points[2], points[3], color, thickness)
cv2.line(frame, points[3], points[0], color, thickness)
cv2.putText(frame, result.barcode_text, points[0], cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = QImage(frame, frame.shape[1], frame.shape[0], frame.strides[0], QImage.Format_RGB888)
pixmap = QPixmap.fromImage(image)
pixmap = self.resizeImage(pixmap)
self.ui.label.setPixmap(pixmap)
self.ui.textEdit_results.setText(out)
def about(self):
self.showMessageBox("About", "<a href='https://www.dynamsoft.com/Products/Dynamic-Barcode-Reader.aspx'>Dynamsoft Barcode Reader v8.0</a>")
def showMessageBox(self, title, content):
msgBox = QMessageBox()
msgBox.setWindowTitle(title)
msgBox.setText(content)
msgBox.exec_()
def closeEvent(self, event):
msg = "Close the app?"
reply = QMessageBox.question(self, 'Message',
msg, QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
self.stopCamera()
event.accept()
else:
event.ignore()
def main():
try:
with open(sys.argv[1]) as f:
license = f.read()
except:
license = ""
app = QApplication(sys.argv)
window = MainWindow(license)
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
print(__doc__)
main()