forked from Ertugrulmert/Multi_Matrix_Reader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModules.py
More file actions
597 lines (463 loc) · 16.9 KB
/
Modules.py
File metadata and controls
597 lines (463 loc) · 16.9 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
from PyQt5 import QtWidgets,QtCore,QtGui
from PyQt5.QtCore import QRect, Qt
from PyQt5.QtGui import QImage, QPixmap, QPainter, QPen
import cv2, sys
from frameProcessor import frameProcessor
class Camera:
"""
Combines simple camera operations of cv2 operations with an available resolutions
list.
...
Attributes
----------
cam_num : int
index of the camera in the list of available cameras
cap : cv2.VideoCapture
VideoCapture object for camera operations and frame capture
availableResolutions : list of QSize object
List of available resolutions obtained from a QCamera
Each QSize object represents a resolution with its widht and height
attributes.
isPaused : bool
Flag to pause the frame capture
Methods
-------
initialize(self)
Initializes video capture.
isReady(self)
Returns whether the camera is ready to be used.
captureFrame(self)
Returns whether frame was succesfully captured.
get_resolution(self)
Returns index of the current camera resolution in the list of available camera resolutions.
set_resolution(self,resIndex)
Sets camera resolution .
pause_cam(self)
Pauses frame capture.
resume_cam(self)
Resumes frame capture.
open_settings_dialog(self)
Opens the default camera setttings dialog of DirectShow driver.
set_available_resolutions(self,resList)
Sets the list of available resolutions.
get_available_resolutions(self)
Gets the list of available resolutions.
close(self)
Stops frame capture and connection to the camera
"""
def __init__(self, cam_num):
"""
Parameters
----------
cam_num : int
index of the camera in the list of available cameras
"""
self.cam_num = cam_num
self.cap = None
self.availableResolutions = []
self.isPaused = False
def initialize(self):
"""
Initializes video capture
Returns
-------
bool
whether video capture is working
"""
print("initialize camera")
self.cap = cv2.VideoCapture(self.cam_num+ cv2.CAP_DSHOW)
return self.cap.isOpened()
def isReady(self):
"""
Returns
-------
bool
whether the camera is ready to be used.
"""
if self.cap is not None and self.cap.isOpened() and not self.isPaused:
return True
return False
def captureFrame(self):
"""
Returns
-------
bool
whether frame was succesfully captured
np.ndarray
frame captured by camera
"""
if self.cap is not None and self.cap.isOpened() and not self.isPaused:
return self.cap.read()
else: return None,None
#RESOLUTION
def get_resolution(self):
"""
Returns
-------
int
index of the current camera resolution in the list of available camera resolutions
"""
return self.cap.get(cv2.CAP_PROP_FRAME_WIDTH), self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT)
def set_resolution(self,resIndex):
"""
Parameters
----------
resIndex : int
index of the wanted camera resolution in the list of available camera resolutions
Returns
-------
None.
"""
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.availableResolutions[resIndex].width())
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.availableResolutions[resIndex].height())
"""setters for camera pausing"""
def pause_cam(self): self.isPaused = True
def resume_cam(self): self.isPaused = False
def open_settings_dialog(self):
"""
Opens the default camera setttings dialog of DirectShow driver
Returns
-------
None.
"""
self.cap.set(cv2.CAP_PROP_SETTINGS, 0)
def set_available_resolutions(self,resList):
"""
Parameters
----------
resList : list of QSize object
List of available resolutions obtained from a QCamera
Each QSize object represents a resolution with its widht and height
attributes.
Returns
-------
None.
"""
self.availableResolutions =resList
def get_available_resolutions(self):
"""
Returns
-------
list of string
Returns the list of available resolutions formatted for display
"""
return [ str(res.width())+'x'+str(res.height()) for res in self.availableResolutions]
def __str__(self):
return 'Camera'
def close(self):
"""
Stops frame capture and connection to the camera
Returns
-------
None.
"""
self.pause_cam()
if self.cap.isOpened():
self.cap.release()
class Worker(QtCore.QObject):
"""
Contains camera and image processing function calls
Computationally heavy operations are placed here ot be moved to antoher thread
so that the GUI thread will function normally
...
Signals
----------
frameReady : QtCore.pyqtSignal(QtGui.QImage,float)
Sends the frame to the GUI thread to be displayed
updateList : QtCore.pyqtSignal(list)
Sends the new decoded matrix codes to the GUI thread
successUpdate : QtCore.pyqtSignal(bool)
Notifies GUI thread about whether every box has a succesfully decoded matrix
Attributes
----------
processor : frameProcessor
The processor object for image processing, box detection and matrix decoding
camera : Camera
The camera object used to capture frames
processing : bool
Whether the frame will be processed to get boxes and matrix codes
resetProcess : bool
Whether the processor should be reset
img : QImage
Processed frame to be sent to the GUI thread
threadactive : bool
Flag to tell the functions to stop before the thread is stopped
ROI : tuple of 4
ROI = ( x0,y0,x1,y1 ) represents the coordinates of the two opposite
corners of the selected region of interest
(0,0,0,0) by default, ROI not used unless a valid ROI is entered
Methods
-------
setCamera(self,camera)
Sets the camera
setCameraResolution(self,i)
Works as a slot to set camera resolution to value sent from UI thread
getNewFrame(self,processing,resetProcess,ROI)
Obtains and processes a new frame
processFrame(self,frame)
Processes the frame to obtain boxes and datamtrix codes
stop(self)
Stops the functions of the Worker
"""
#SIGNALS
frameReady = QtCore.pyqtSignal(QtGui.QImage,float)
updateList = QtCore.pyqtSignal(list)
successUpdate = QtCore.pyqtSignal(bool)
def __init__(self, parent=None):
super(Worker, self).__init__(parent)
self.processor = frameProcessor()
self.camera = None
self.processing = False
self.resetProcess = False
self.img = None
self.threadactive = True
self.ROI = (0,0,0,0)
def setCamera(self,camera):
"""
Sets the camera
Parameters
----------
camera : Camera
The camera object used to capture frames
Returns
-------
None.
"""
self.camera = camera
@QtCore.pyqtSlot(int)
def setCameraResolution(self,i):
"""
Works as a slot to set camera resolution to value sent from UI thread
Parameters
----------
i : int
index of the resolution in the list of available resolutions
Returns
-------
None.
"""
self.camera.pause_cam()
self.camera.set_resolution(i)
self.camera.resume_cam()
@QtCore.pyqtSlot(bool,bool,tuple)
def getNewFrame(self,processing,resetProcess,ROI):
"""
Obtains and processes a new frame
Parameters
----------
processing : bool
whether the frame will be processed to get boxes and matrix codes
resetProcess : bool
whether the processor should be reset
ROI : tuple
( x0,y0,x1,y1 ) represents the coordinates of the two opposite
corners of the selected region of interest
Returns
-------
None.
"""
try:
self.processing = processing
self.resetProcess = resetProcess
self.ROI = ROI
scaling_factor = 1
#if the thread will not be closed
if self.threadactive:
while self.camera is not None:
#making sure camera is working
if not self.camera.isReady():
continue
#capturing new frame
ret ,frame= self.camera.captureFrame()
if not ret:
continue
#resetting the processor to if needed
if self.resetProcess:
self.processor.reset()
self.resetProcess = False
#processing the frame
if self.processing:
frame = self.processFrame(frame)
#lowering the resolution of the frame if it is above 1920 pixels in width
if frame.shape[1] > 1920 :
frame,scaling_factor = frameProcessor.downSize(frame)
#color format conversion for GUI display
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
self.img = QtGui.QImage(frame, frame.shape[1], frame.shape[0], QtGui.QImage.Format_RGB888)
break
#sending the resulting image and its scaling factor to GUI thread
#sclaing factor is needed for calibrating the relative coordinates of mouse clicks on image
self.frameReady.emit(self.img,scaling_factor)
except Exception as e:
print( e)
def processFrame(self,frame):
"""
Processes the frame to obtain boxes and datamtrix codes
Parameters
----------
frame : np.ndarray
frame to be processed
Returns
-------
frame : np.ndarray
processed frame
"""
#checking if the thread will not be closed and processor is available
if self.threadactive and self.processor is not None:
try:
#processing
frame, matrices = self.processor.process(frame,self.ROI)
#sending the list of decoded datamatrix codes to the GUI thread
self.updateList.emit(matrices)
except:
print("Processor Exception")
print(sys.exc_info()[0], "occurred.")
self.processing = False
return
#if all detected boxes resulted in succesfull matrix decoding, notifyin GUI thread
if self.processor.isAllDetected():
self.successUpdate.emit(True)
else:
self.successUpdate.emit(False)
return frame
def stop(self):
"""
Stops the functions of the Worker
Returns
-------
None.
"""
self.threadactive = False
self.processor = None
self.camera.close()
self.camera = None
class LabelwROI(QtWidgets.QLabel):
"""
Modified QLabel that can draw and return a region of interest (ROI)
...
Attributes
----------
x0,y0,x1,y1 : int
(x0,y0) : the coordinates of the starting point of ROI
(x1,y1) : the coordinates of the ending point of ROI
start : bool
Flag to enable ROI creation
drawROI : bool
Flag that True while a ROI is being drawn
Methods
----------
toggleROI(self)
Enables/Disables ROI creation
getROI(self)
Returns ROI coordinates
mousePressEvent(self,event)
Modified to initiate ROI creation
mouseReleaseEvent(self,event)
Ends ROI creation
mouseMoveEvent(self,event)
Updates the end point of ROI
paintEvent(self, event)
Modified to add the ROI onto any drawn QImage
"""
x0,y0,x1,y1 = 0,0,0,0
start = False
drawROI = False
#
def toggleROI(self):
"""
Enables/Disables ROI creation
"""
self.drawROI = not self.drawROI
x0,y0,x1,y1 = 0,0,0,0
self.start = False
def getROI(self):
"""
Returns ROI coordinates
Returns
-------
tuple of 4 ints
(x0,x1,y0,y1) (x0,y0) : the coordinates of the starting point of ROI
(x1,y1) : the coordinates of the ending point of ROI
"""
if self.drawROI and not self.start:
if self.pixmap():
#to make sure the upper left and lower right corners are returned, min and max are used
#this way, if the ROI is drawn in different directions, the same ROI can be obtained
return (min(self.x0,self.x1),min(self.y0,self.y1),max(self.x0,self.x1),max(self.y0,self.y1))
else: return (0,0,0,0)
def mousePressEvent(self,event):
"""
Modified to initiate ROI creation
"""
if self.drawROI:
self.start = True
self.x0 = event.x()
self.y0 = event.y()
def mouseReleaseEvent(self,event):
"""
Ends ROI creation
"""
self.start = False
def mouseMoveEvent(self,event):
"""
Updates the end point of ROI
"""
if self.drawROI and self.start:
self.x1 = event.x()
self.y1 = event.y()
self.update()
def paintEvent(self, event):
"""
Modified to add the ROI onto any drawn QImage
"""
super().paintEvent(event)
if self.drawROI:
rect =QRect(min(self.x0,self.x1), min(self.y0,self.y1), abs(self.x1-self.x0), abs(self.y1-self.y0))
painter = QPainter(self)
painter.setPen(QPen(Qt.red,2,Qt.SolidLine))
painter.drawRect(rect)
class showLogin(QtWidgets.QDialog):
"""
QDialog that asks for and returns database user credentials
...
Signals
----------
loginSignal : QtCore.pyqtSignal(str,str,str)
Sends the user credentials to the GUI thread to be passed to the database object
Methods
----------
accept_and_send(self)
Emits the login signal if OK button is pressed
"""
loginSignal = QtCore.pyqtSignal(str,str,str)
def __init__(self, parent):
QtWidgets.QDialog.__init__(self, parent)
#delete question mark
self.setWindowFlags(self.windowFlags()
^ QtCore.Qt.WindowContextHelpButtonHint)
#Login ,password and database name fields
self.username = QtWidgets.QLineEdit(self)
self.password = QtWidgets.QLineEdit(self)
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.database_name = QtWidgets.QLineEdit(self)
#Setting up the layout
loginLayout = QtWidgets.QFormLayout()
loginLayout.addRow("Username", self.username)
loginLayout.addRow("Password", self.password)
loginLayout.addRow("Database", self.database_name)
self.buttons = QtWidgets.QDialogButtonBox(QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)
#If OK button is pressed, the custom accept_and_send function will send user credentials to database
self.buttons.accepted.connect(self.accept_and_send)
self.buttons.rejected.connect(self.reject)
self.layout = QtWidgets.QVBoxLayout(self)
self.layout.addLayout(loginLayout)
self.layout.addWidget( self.buttons)
self.setLayout(self.layout)
### set window title
self.setWindowTitle('Database Login')
###lock resize
self.setSizeGripEnabled(False)
self.setFixedSize( self.sizeHint())
def accept_and_send(self):
self.loginSignal.emit(self.password.text(),self.username.text(),self.database_name.text())
self.accept()