forked from Fu-physics/Ximea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UI_ximea_two_camrea.py
448 lines (325 loc) · 17.1 KB
/
UI_ximea_two_camrea.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
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
from __future__ import unicode_literals
import sys
import os
import random
import time
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QInputDialog, QPushButton, QMainWindow, QApplication, QSpinBox, QLabel
from PyQt5.QtWidgets import QWidget, QAction, QTabWidget,QVBoxLayout, QHBoxLayout
from PyQt5.QtWidgets import QGroupBox, QDialog,QGridLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot
import numpy as np
from numpy import arange, sin, pi
import matplotlib
# Make sure that we are using QT5BB
matplotlib.use('Qt5Agg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import Ximea_cmos
from UI_figure_anim_imshow import UI_figure
progname = os.path.basename(sys.argv[0])
progversion = "0.1"
class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt5 tabs - pythonspot.com'
self.left = 0
self.top = 0
self.width = 1000
self.height = 800
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.table_widget = MyTableWidget(self) # creat a Widgat.
self.setCentralWidget(self.table_widget) # set the Widget to be CentralWidget of QMainWindow.
self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super(QWidget, self).__init__(parent)
self.amp = 1.0
self.t=0
self.cnt = 0
self.layout = QVBoxLayout(self) # create a Layout, which will be setted for self
# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
#self.tabs.resize(800,600)
self.tabs.addTab(self.tab1,"Tab 1") # Add tabs
self.tabs.addTab(self.tab2,"Tab 2")
self.layout.addWidget(self.tabs) # Add tabs to widget
self.setLayout(self.layout)
self.initalUI_tab_1()
self.initalUI_tab_2()
def initalUI_tab_1(self):
# Create first tab
self.tab1_layout = QHBoxLayout(self) # create a Layout, which will be setted for tab1
self.tab1_layout_R = QVBoxLayout(self)
self.button_scope_pre = QPushButton("Connecte to Scope")
#self.button_scope_pre.clicked.connect(self.connect_Scope)
self.button_scope = QPushButton("Get Scope Figure")
#self.button_scope.clicked.connect(self.get_Scope_fig)
self.button_plot_tab1 = QPushButton('Plot') #button connected to `plot` method
#self.button_plot_tab1.clicked.connect(self.plot_tab1)
# add buttons onto tabl1.layout
self.tab1_layout_R.addWidget(self.button_scope_pre)
self.tab1_layout_R.addWidget(self.button_scope)
self.tab1_layout_R.addWidget(self.button_plot_tab1)
self.tab1_layout.addStretch() # put the plot_layout right side
self.tab1_layout.addLayout(self.tab1_layout_R) # here is "addLayout" not "addWedget"
self.tab1.setLayout(self.tab1_layout) # set tab1.layout to be the layout of tabl1
def initalUI_tab_2(self):
#################### Create Plot cavas widget ###################################################
# Create first tab
self.tab2_layout = QHBoxLayout(self) # create a Layout, which will be setted for tab_2
######### camera 1 UI, which is on the left side of table 2
self.camera_1 = QVBoxLayout(self)
self.cmaera_1_up = QVBoxLayout(self)
self.camera1_para("Camera 1 Para Setting Panel")
self.cmaera_1_up.addWidget(self.camera1_GroupBox)
self.cmaera_1_down = QHBoxLayout(self)
self.figure_1 = plt.figure() # a figure instance to plot on
#if put "plt.ion" on the head, which will make two more figures idependently.
self.button_plot_1 = QPushButton('Make Plot') # Just some button connected to `plot` method
self.button_plot_1.clicked.connect(self.plot_1)
self.button_preparing_plot_1 = QPushButton('Prepaering Plot') # Just some button connected to `plot` method
self.button_preparing_plot_1.clicked.connect(self.preparing_plot_1)
self.button_connect_1 = QPushButton('Connect Ximea Camera') # Just some button connected to `plot` method
self.button_connect_1.clicked.connect(self.connect_Ximea_cmos_1)
self.cmaera_1_down.addWidget(self.button_connect_1)
self.cmaera_1_down.addWidget(self.button_preparing_plot_1)
self.cmaera_1_down.addWidget(self.button_plot_1)
self.camera_1.addLayout(self.cmaera_1_up)
#self.tab2_layout.addStretch() # put the plot_layout right side
self.camera_1.addLayout(self.cmaera_1_down)
######### camera 2 UI, which is on the right side of table 2
self.camera_2 = QVBoxLayout(self)
self.cmaera_2_up = QVBoxLayout(self)
self.camera2_para("Camera 2 Para Setting Panel")
self.cmaera_2_up.addWidget(self.camera2_GroupBox)
self.cmaera_2_down = QHBoxLayout(self)
self.figure_2 = plt.figure() # a figure instance to plot on
#if put "plt.ion" on the head, which will make two more figures idependently.
self.button_plot_2 = QPushButton('Make Plot') # Just some button connected to `plot` method
self.button_plot_2.clicked.connect(self.plot_2)
self.button_preparing_plot_2 = QPushButton('Prepaering Plot') # Just some button connected to `plot` method
self.button_preparing_plot_2.clicked.connect(self.preparing_plot_2)
self.button_connect_2 = QPushButton('Connect Ximea Camera') # Just some button connected to `plot` method
self.button_connect_2.clicked.connect(self.connect_Ximea_cmos_2)
self.cmaera_2_down.addWidget(self.button_connect_2)
self.cmaera_2_down.addWidget(self.button_preparing_plot_2)
self.cmaera_2_down.addWidget(self.button_plot_2)
self.camera_2.addLayout(self.cmaera_2_up)
#self.tab2_layout.addStretch() # put the plot_layout right side
self.camera_2.addLayout(self.cmaera_2_down)
#################################### put camera 1 on left and 2 on right ##########################
self.tab2_layout.addLayout(self.camera_1)
self.tab2_layout.addStretch()
self.tab2_layout.addLayout(self.camera_2)
self.tab2.setLayout(self.tab2_layout) # set tab2.layout to be the layout of tab_2
def camera1_para(self, layout_name):
self.camera1_GroupBox = QGroupBox(layout_name)
layout = QGridLayout()
#layout.setColumnStretch(1, 4)
#layout.setColumnStretch(2, 4)
self.cmos_para_bt_1 = QPushButton('Cmos para set', self)
self.cmos_para_bt_1.clicked.connect(self.Cmose_para_set_1)
self.expose_spinbox_1 = QSpinBox()
self.expose_spinbox_1.setRange(1,10000)
self.expose_spinbox_1.setValue(8000)
self.xpixs_spinbox_1 = QSpinBox()
self.xpixs_spinbox_1.setRange(1,2048)
self.xpixs_spinbox_1.setValue(2048)
self.xoffset_spinbox_1 = QSpinBox()
self.xoffset_spinbox_1.setRange(0,2048)
self.xoffset_spinbox_1.setValue(0)
self.ypixs_spinbox_1 = QSpinBox()
self.ypixs_spinbox_1.setRange(1,1088)
self.ypixs_spinbox_1.setValue(1088)
self.yoffset_spinbox_1 = QSpinBox()
self.yoffset_spinbox_1.setRange(0,1088)
self.yoffset_spinbox_1.setValue(0)
layout.addWidget(QLabel('Expose time'),0,0)
layout.addWidget(self.expose_spinbox_1,0,1)
layout.addWidget(QLabel('X pixs'),1,0)
layout.addWidget(self.xpixs_spinbox_1,1,1)
layout.addWidget(QLabel('X offset'),1,2)
layout.addWidget(self.xoffset_spinbox_1,1,3)
layout.addWidget(QLabel('Y pixs'),2,0)
layout.addWidget(self.ypixs_spinbox_1,2,1)
layout.addWidget(QLabel('Y offset'),2,2)
layout.addWidget(self.yoffset_spinbox_1,2,3)
layout.addWidget(QLabel('Cmos para set'),5,0)
layout.addWidget(self.cmos_para_bt_1,5,0)
self.camera1_GroupBox.setLayout(layout)
def camera2_para(self, layout_name):
self.camera2_GroupBox = QGroupBox(layout_name)
layout = QGridLayout()
#layout.setColumnStretch(1, 4)
#layout.setColumnStretch(2, 4)
self.cmos_para_bt_2 = QPushButton('Cmos para set', self)
self.cmos_para_bt_2.clicked.connect(self.Cmose_para_set_2)
self.expose_spinbox_2 = QSpinBox()
self.expose_spinbox_2.setRange(1,10000)
self.expose_spinbox_2.setValue(8000)
self.xpixs_spinbox_2 = QSpinBox()
self.xpixs_spinbox_2.setRange(1,2048)
self.xpixs_spinbox_2.setValue(2048)
self.xoffset_spinbox_2 = QSpinBox()
self.xoffset_spinbox_2.setRange(0,2048)
self.xoffset_spinbox_2.setValue(0)
self.ypixs_spinbox_2 = QSpinBox()
self.ypixs_spinbox_2.setRange(1,1088)
self.ypixs_spinbox_2.setValue(1088)
self.yoffset_spinbox_2 = QSpinBox()
self.yoffset_spinbox_2.setRange(0,1088)
self.yoffset_spinbox_2.setValue(0)
layout.addWidget(QLabel('Expose time'),0,0)
layout.addWidget(self.expose_spinbox_2,0,1)
layout.addWidget(QLabel('X pixs'),1,0)
layout.addWidget(self.xpixs_spinbox_2,1,1)
layout.addWidget(QLabel('X offset'),1,2)
layout.addWidget(self.xoffset_spinbox_2,1,3)
layout.addWidget(QLabel('Y pixs'),2,0)
layout.addWidget(self.ypixs_spinbox_2,2,1)
layout.addWidget(QLabel('Y offset'),2,2)
layout.addWidget(self.yoffset_spinbox_2,2,3)
layout.addWidget(QLabel('Cmos para set'),5,0)
layout.addWidget(self.cmos_para_bt_2,5,0)
self.camera2_GroupBox.setLayout(layout)
def connect_Ximea_cmos_1(self):
try:
self.ximea_cam_1 = Ximea_cmos.Ximea_app(ID_str ='16770651') ## plase use the ID, which is a string not integer number
print("The CMOS Camera connected ! ")
except: # if the cmos open, it will go to here
if self.ximea_cam_1.cam.CAM_OPEN:
print("The CMOS Camera already connected ! ")
else:
print("Fall to connect Cmos, Please do it agian ! ")
#after the test, we need this line. Ortherwise, there is no plot at the first time you click "Preparing Plot" and "Make plot".
#You need to click "Preparing Plot" and "Make plot" agine, then you could got the plot.
#if you add "self.plot_L()", click "Preparing Plot" and "Make plot" one time, you could get the plot.
self.plot_1()
def connect_Ximea_cmos_2(self):
try:
self.ximea_cam_2 = Ximea_cmos.Ximea_app(ID_str ='16770651') ## plase use the ID, which is a string not integer number
print("The CMOS Camera connected ! ")
except: # if the cmos open, it will go to here
if self.ximea_cam_2.cam.CAM_OPEN:
print("The CMOS Camera already connected ! ")
else:
print("Fall to connect Cmos, Please do it agian ! ")
#after the test, we need this line. Ortherwise, there is no plot at the first time you click "Preparing Plot" and "Make plot".
#You need to click "Preparing Plot" and "Make plot" agine, then you could got the plot.
#if you add "self.plot_L()", click "Preparing Plot" and "Make plot" one time, you could get the plot.
self.plot_2()
def Cmose_para_set_1(self):
print("Cmos_1 para setting")
self.ximea_cam_1.cam.set_width(self.xpixs_spinbox_1.value())
print("xpixs_spinbox_1.value is:", self.xpixs_spinbox_1.value())
self.ximea_cam_1.cam.set_height(self.ypixs_spinbox_1.value())
print("ypixs_spinbox_1.value is:", self.ypixs_spinbox_1.value())
print("Cmos para set done !")
def Cmose_para_set_2(self):
print("Cmos_1 para setting")
self.ximea_cam_2.cam.set_width(self.xpixs_spinbox_2.value())
print("xpixs_spinbox_1.value is:", self.xpixs_spinbox_2.value())
self.ximea_cam_2.cam.set_height(self.ypixs_spinbox_2.value())
print("ypixs_spinbox_1.value is:", self.ypixs_spinbox_2.value())
print("Cmos para set done !")
def image_size_1(self):
cnt = 1
#print("imag_size is:", imag_size)
while cnt:
imag_size = [self.expose_spinbox_1.value(),
self.xpixs_spinbox_1.value(),
self.xoffset_spinbox_1.value(),
self.xpixs_spinbox_1.value(),
self.yoffset_spinbox_1.value() ]
cam_data = self.ximea_cam_1.get_data(imag_size)
yield cam_data
def image_size_2(self):
cnt = 1
#print("imag_size is:", imag_size)
while cnt:
imag_size = [self.expose_spinbox_2.value(),
self.xpixs_spinbox_2.value(),
self.xoffset_spinbox_2.value(),
self.xpixs_spinbox_2.value(),
self.yoffset_spinbox_2.value() ]
cam_data = self.ximea_cam_2.get_data(imag_size)
yield cam_data
def plot_1(self):
try : #check whether there is "self.figure_1", if it exists, then print its location
print("The figure_1 is:", self.figure_1)
except AttributeError: #if it does not exist, then we create it
print("\n")
print("No figure_1")
self.figure_1 = plt.figure() # a figure instance to plot on
try :
print("rebuilt the figure_1:", self.figure_1) #if created, print the location
except AttributeError:
print("rebuilt fall") #if not created, raise the information
cam_data_1 = self.image_size_1()
self.cmos_app_1 = UI_figure(self.figure_1, cam_data_1)
self.cmos_app_1.UI_Animation_plot()
def plot_2(self):
try : #check whether there is "self.figure_1", if it exists, then print its location
print("The figure_2 is:", self.figure_2)
except AttributeError: #if it does not exist, then we create it
print("\n")
print("No figure_2")
self.figure_2 = plt.figure() # a figure instance to plot on
try :
print("rebuilt the figure_2:", self.figure_2) #if created, print the location
except AttributeError:
print("rebuilt fall") #if not created, raise the information
cam_data_2 = self.image_size_2()
self.cmos_app_2 = UI_figure(self.figure_2, cam_data_2)
self.cmos_app_2.UI_Animation_plot()
def preparing_plot_1(self):
try:
print("\n")
print(" --- Begin to delete the figure ! ")
del self.cmos_app_1
#self.cmos_app.close()
print("Unsuccessfully delete self.cmos_app_1:", self.cmos_app_1)
except AttributeError:
print("Successfully delete self.cmos_app_1 ! ")
try:
del self.figure_1 #, self.canvas_L, self.toolbar_L
print("Unsuccessfully delete self.figure_1:", self.figure_1)
except AttributeError:
print("Successfully delete self.figure_1 ! ")
print("It's ready to make a plot!")
def preparing_plot_2(self):
try:
print("\n")
print(" --- Begin to delete the figure ! ")
del self.cmos_app_2
#self.cmos_app.close()
print("Unsuccessfully delete self.cmos_app_2:", self.cmos_app_2)
except AttributeError:
print("Successfully delete self.cmos_app_2 ! ")
try:
del self.figure_2 #, self.canvas_L, self.toolbar_L
print("Unsuccessfully delete self.figure_2:", self.figure_2)
except AttributeError:
print("Successfully delete self.figure_2 ! ")
print("It's ready to make a plot!")
def array_data_gen(self, t = 0):
cnt = 0
sample_num = 300
while cnt< 100*sample_num:
t = (cnt*sample_num + np.linspace(0,sample_num-1,sample_num))*0.01
data = np.sin(t)
cnt +=1
yield t, data
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
print("the last step!")
sys.exit(app.exec_())