-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapp.py
More file actions
206 lines (158 loc) · 6.25 KB
/
app.py
File metadata and controls
206 lines (158 loc) · 6.25 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
#!/usr/bin/env python3
'''
Usage:
app.py <license.txt>
'''
import sys
from PySide2.QtGui import QPixmap, QImage
from PySide2.QtWidgets import QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog, QTextEdit, QMessageBox, QHBoxLayout
from PySide2.QtCore import QTimer
from barcode_manager import *
import os
import cv2
class UI_Window(QWidget):
def __init__(self, license):
QWidget.__init__(self)
self.FRAME_WIDTH = 640
self.FRAME_HEIGHT = 480
self.WINDOW_WIDTH = 1280
self.WINDOW_HEIGHT = 1000
self._results = None
# Initialize Dynamsoft Barcode Reader
self._barcodeManager = BarcodeManager(license)
# Initialize OpenCV camera
self._cap = cv2.VideoCapture(0)
# cap.set(5, 30) #set FPS
self._cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.FRAME_WIDTH)
self._cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.FRAME_HEIGHT)
# The current path.
self._path = os.path.dirname(os.path.realpath(__file__))
# Create a timer.
self.timer = QTimer()
self.timer.timeout.connect(self.nextFrameUpdate)
# Create a layout.
layout = QVBoxLayout()
# Add a button
self.btn = QPushButton("Load an image")
self.btn.clicked.connect(self.pickFile)
layout.addWidget(self.btn)
# Add a button
button_layout = QHBoxLayout()
btnCamera = QPushButton("Open camera")
btnCamera.clicked.connect(self.openCamera)
button_layout.addWidget(btnCamera)
btnCamera = QPushButton("Stop camera")
btnCamera.clicked.connect(self.stopCamera)
button_layout.addWidget(btnCamera)
layout.addLayout(button_layout)
# Add a label
self.label = QLabel()
self.label.setFixedSize(self.WINDOW_WIDTH - 30, self.WINDOW_HEIGHT - 160)
layout.addWidget(self.label)
# Add a text area
self.results = QTextEdit()
layout.addWidget(self.results)
# Set the layout
self.setLayout(layout)
self.setWindowTitle("Dynamsoft Barcode Reader")
self.setFixedSize(self.WINDOW_WIDTH, self.WINDOW_HEIGHT)
# https://stackoverflow.com/questions/1414781/prompt-on-exit-in-pyqt-application
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 resizeImage(self, pixmap):
lwidth = self.label.maximumWidth()
pwidth = pixmap.width()
lheight = self.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 showMessageBox(self, text):
msgBox = QMessageBox()
msgBox.setText(text)
msgBox.exec_()
def pickFile(self):
self.stopCamera()
# Load an image file.
filename = QFileDialog.getOpenFileName(self, 'Open file',
self._path, "Barcode images (*)")
if filename is None or filename[0] == '':
self.showMessageBox("No file selected")
return
# Read barcodes
frame, results = self._barcodeManager.decode_file(filename[0])
if frame is None:
self.showMessageBox("Cannot decode " + filename[0])
return
self.showResults(frame, results)
def openCamera(self):
if not self._cap.isOpened():
self.showMessageBox("Failed to open camera.")
return
self._barcodeManager.create_barcode_process()
self.timer.start(1000./24)
def stopCamera(self):
self._barcodeManager.destroy_barcode_process()
self.timer.stop()
def showResults(self, frame, results):
out = ''
index = 0
if results is not None and results[0] is not None:
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.label.setPixmap(pixmap)
self.results.setText(out)
def nextFrameUpdate(self):
ret, frame = self._cap.read()
if not ret:
self.showMessageBox('Failed to get camera frame!')
return
self._barcodeManager.append_frame(frame)
self._results = self._barcodeManager.peek_results()
self.showResults(frame, self._results)
def main():
try:
with open(sys.argv[1]) as f:
license = f.read()
except:
license = ""
app = QApplication(sys.argv)
ex = UI_Window(license)
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
print(__doc__)
main()