-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.py
228 lines (198 loc) · 7.63 KB
/
scan.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
import cv2
import time
import arrow
import os
import data
import Util
from PyQt4 import QtCore, QtGui
from scanwindow import Ui_MainWindow
# TODO: Add camera selection to combobox
# TODO: Query UPC DB online for unique bar codes
# TODO: Add tests!
class CameraThread(QtCore.QThread):
imgSaved = QtCore.pyqtSignal(str)
camReady = QtCore.pyqtSignal()
def __init__(self, camera_port=0):
QtCore.QThread.__init__(self)
self.camera_port = camera_port
self.camera = None
self.openCamera(camera_port)
self.camera.set(3, 1600) # width
self.camera.set(4, 1200) # height
self.height = self.camera.get(4)
self.width = self.camera.get(3)
self.fps = self.get_fps()
self.last_image = None
self.active = True
def __del__(self):
self.active = False
self.wait()
if self.isRunning():
self.closeCamera()
def run(self):
while(self.active):
retval, self.last_image = self.camera.read()
self.camReady.emit()
def get_fps(self):
"""Returns the approximate FPS of the opened camera"""
num_frames = 15
start = time.time()
for i in range(0, num_frames):
self.camera.read()
end = time.time()
seconds = end - start
fps = num_frames / seconds
return fps
def save_image(self, image_file):
"""Save image to disk"""
cv2.imwrite(image_file, self.last_image)
self.imgSaved.emit(image_file)
def isRunning(self):
"""Returns true if the camera object is active"""
return self.camera.isOpened()
def openCamera(self, port=0):
"""Opens a camera by port"""
self.camera = cv2.VideoCapture(port)
#Following may be required for some cameras
if not self.isRunning():
self.camera.open()
def closeCamera(self):
"""Deactivates the camera"""
self.camera.release()
def read(self):
"""Gets a frame from the camera"""
return self.camera.read() # Returnvalue, PILImage
class StartScan(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.db = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btnTakePhoto.clicked.connect(self._snapshot)
self.ui.btnRefreshCameras.clicked.connect(self.refreshCameras)
self.ui.cmbCamera.currentIndexChanged.connect(self.changeCamera)
self.ui.txtInput.returnPressed.connect(self.getinput)
self.camera_port = 0
self.livecam = None
self.ui.dockSnapshotPreview.resizeEvent = self.resizePreview
self.last_text = None
self.last_image = None
self.image_folder = "./img/"
os.makedirs(self.image_folder, exist_ok=True)
self.saved_image_format = ".jpg"
self.live_image_format = ".jpg"
self.record_count = 0
self.ui.txtInput.setFocus()
def start_livecam(self):
if self.livecam is not None:
del self.livecam
self.livecam = CameraThread(self.camera_port)
self.livecam.camReady.connect(self.updateLiveView)
self.livecam.imgSaved.connect(self.updatePreview)
self.livecam.start()
def create_database(self, data_file: str='') -> bool:
"""Creates a new data file"""
return data.LocalData(data_file).create_database()
def open_database(self, data_file: str='') -> bool:
"""Opens the database and sets the DB object"""
if data_file == '':
self.db = data.LocalData()
else:
self.db = data.LocalData(data_file)
return self.db.open_database()
def close_database(self):
if self.data_file() != '': self.db = None
def data_file(self) -> str:
"""Returns the currently connected database"""
if self.db is not None:
return self.db.data_file
return ''
def closeEvent(self, QCloseEvent):
"""Gracefully shutdown the camera"""
self.camera.active = False
# Save any pending data
self.writeout()
# Delay 1 frame to allow camera to finish any in-process frame grabs
time.sleep(1 / self.camera.fps)
self.camera.closeCamera()
def changeCamera(self, port=0):
"""Selects and opens a new camera for input"""
pass
def _snapshot(self):
"""Creates snapshot from button press"""
self.snapshot()
def snapshot(self, prod_id=None):
"""Gets a frame from the camera and returns its path"""
if prod_id is None:
if self.last_text is None:
prod_id = '0'
else:
prod_id = self.last_text
self.last_image = self.image_folder + \
str(prod_id) + \
'-' + \
str(arrow.now('local').format('YYYYMMDD.HHmmss.SS')) + \
self.saved_image_format
self.camera.save_image(self.last_image)
self.ui.txtInput.setFocus()
return self.last_image
def updateLiveView(self):
"""Updates the live camera view"""
retval, img = cv2.imencode(self.live_image_format, self.camera.last_image)
im = QtGui.QImage.fromData(img)
pix = QtGui.QPixmap(im)
pix = pix.scaled(self.ui.livePreviewContainer.size(), QtCore.Qt.KeepAspectRatio)
self.ui.lblLiveView.setPixmap(pix)
def updatePreview(self, image_file):
"""Updates the preview window of the last image written"""
pix = QtGui.QPixmap(image_file)
pix = pix.scaled(self.ui.dockSnapshotPreview.size(), QtCore.Qt.KeepAspectRatio)
self.ui.lblPreview.setPixmap(pix)
def resizePreview(self, event):
"""Refreshes the preview window when the dock is resized"""
if self.last_image is not None and os.path.exists(self.last_image):
self.updatePreview(self.last_image)
def refreshCameras(self):
"""Gets the available cameras and populates the combo box"""
current_camera = 0
if self.camera.isRunning():
current_camera = self.camera.camera_port
self.camera.exit()
for i in range(10, 0, -1):
self.camera.camera = cv2.VideoCapture(i)
if self.camera.isRunning():
self.ui.cmbCamera.clear()
for y in range(i, -1, -1):
self.ui.cmbCamera.addItem(str(y), y)
if self.camera.camera.isRunning() == False:
self.camera.openCamera(current_camera)
def writeout(self):
"""Writes a record to the data file"""
if self.last_image is None:
self.last_image = ""
if self.last_text is not None:
self.db.put_row(self.last_text, self.last_image)
self.db.do_commit()
self.last_text = None
self.record_count += 1
self.ui.txtInput.setFocus()
def getinput(self):
"""Takes the input from the input box"""
self.writeout()
if self.ui.txtInput.text() != "":
self.last_text = self.ui.txtInput.text()
self.snapshot(self.last_text)
self.ui.txtHistory.setPlainText(str(self.record_count) +
": " +
self.last_text +
"\n" +
self.ui.txtHistory.toPlainText())
self.ui.txtInput.setText("")
else:
self.close()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartScan()
myapp.show()
sys.exit(app.exec_())