-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.py
339 lines (243 loc) · 10.7 KB
/
Camera.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
from ximea import xiapi
from CamAdjustFrame import CamAdjustFrame
import PIL.Image
import PIL.ImageDraw
import PIL.ImageFont
import cv2
import numpy as np
import time
import os
from tkinter import *
#this class controls the Ximea camera refer to the ximea API Manual for more detail -> https://www.ximea.com/support/wiki/apis/xiapi_manual
#iamge default size captured by Ximea camera -> width = 4112px , height = 3008px
class Camera():
def __init__(self,system):
self.system = system
self.cam = xiapi.Camera()
self.cam.open_device()
#camera initial settings
self.cam.set_imgdataformat('XI_RGB24')
self.cam.set_aeag_level(71)
self.cam.enable_aeag()
self.cam.set_height(900)
self.cam.set_width(1200)
self.cam.set_sharpness(4)#set to max sharp
self.cam.enable_auto_wb()
#flags and counters
self.vidClosed = True
self.vidPowerCnt = 0
self.set_vals = False
#returns temp in Celcius
def getTemp(self):
return self.cam.get_temp()
#set sharpness (-4 <= val =< 4)
def setSharp(self,val):
self.cam.set_sharpness(val)
def is_auto_wb(self):
x = self.cam.is_auto_wb()
return x
# if('enab' in x):
# return True
# else:
# return False
def is_ag(self):
x = self.cam.is_aeag()
return x
def get_kr(self):
return self.cam.get_wb_kr()
def get_kg(self):
return self.cam.get_wb_kg()
def get_kb(self):
return self.cam.get_wb_kb()
def get_exp(self):
return self.cam.get_exposure()
def get_sharp(self):
return self.cam.get_sharpness() +4
def disable_ag(self):
self.cam.disable_aeag()
def toggle_ag(self):
if(self.cam.is_aeag()):
self.caf.AutoExposureButton.configure(text='''Auto On''')
self.stopAq()
self.cam.disable_aeag()
self.startAq()
self.cam.set_exposure(15000)
print("disabled")
else:
self.caf.AutoExposureButton.configure(text='''Auto Off''')
self.stopAq()
self.cam.set_aeag_level(71)
self.cam.enable_aeag()
self.startAq()
print("enabled")
#used in autofocus to display image to GUI
def displayFocusedImage(self):
self.resizeROI(1200,900)
img = xiapi.Image()
self.cam.get_image(img)
img_analyze = img.get_image_data_numpy()
self.system.currImage_analyze = img_analyze# image data for analysis is different than for display to GUI
img = img.get_image_data_numpy(invert_rgb_order=True)
img = PIL.Image.fromarray(img).convert('RGB')
self.system.currImage = img
self.system.imgChange = True #change image flag set for GUI updateImage function
return img
#used in autofocus to calculate variance
def getImg(self):
img = xiapi.Image()
self.cam.get_image(img)
#create numpy array with data from camera. Dimensions of array are determined
#by imgdataformat
img = img.get_image_data_numpy()
#return acquired image
return img
#changes ROI size in pixels (roi is set to upper left corner, there is a way to
#set ROI position, but was getting errors, refer to API manual)
def resizeROI(self,width,height):
self.stopAq()
self.cam.set_width(width)
self.cam.set_height(height)
self.startAq()
#AqStarted-> returns "XI_ON", AqStopped -> returns "XI_OFF"
def getAqStatus(self):
return str(self.cam.get_acquisition_status())
#if on, turn it off
def startAq(self):
if(self.getAqStatus() == 'XI_OFF'):
self.cam.start_acquisition()
#if off, turn it on
def stopAq(self):
if(self.getAqStatus() == 'XI_ON'):
self.cam.stop_acquisition()
#not currently used
def closeCam(self):
#stop data acquisition
print('Stopping acquisition...')
self.stopAq()
#stop communication
self.cam.close_device()
def set_bg_black(self):
img = np.zeros((700,1200))
img = PIL.Image.fromarray(img,'RGB')
self.system.currImage = img
self.system.imgChange =True
#captures image and displays to GUI
def showIm(self):
self.resizeROI(1200,900)
self.startAq()
img = xiapi.Image()
self.cam.get_image(img,10000000)
self.system.currImage_analyze = img.get_image_data_numpy()#for analysis
#create numpy array with data from camera. Dimensions of array are determined
#by imgdataformat
#NOTE: PIL takes RGB bytes in opposite order, so invert_rgb_order is True
img = img.get_image_data_numpy(invert_rgb_order=True)
img = PIL.Image.fromarray(img).convert('RGB')
img.save('thePic.tif')
self.system.currImage = img
self.system.imgChange =True
#Next 4 lines only for saving image with exposure value
# d = PIL.ImageDraw.Draw(img)
# fnt = PIL.ImageFont.truetype('/usr/share/fonts-droid-fallback/truetype/OpenSans-Bold.ttf',100,encoding="unic")
# d.text((2000,2700),"Exposure: "+str(self.cam.get_exposure()),font = fnt, fill=(255,0,0))
# img.save('xi_example.tiff')
self.stopAq()
return img
#this function start the video Thread and keeps it in an infinite loop
def startVideo(self):
#smaller ROI gets faster frame refresh rate
self.resizeROI(400,300) #change to w= 400, h = 300
self.startAq()
self.img = xiapi.Image()
self.data = None
#infinite loop for video Thread
while True:
time.sleep(0.1) #needed to prevent thread from eating up CPU when video not being displayed
#check parity of video on/off button clicks
if(self.vidPowerCnt %2==1):
#set camera settings here. height and width determine ROI size. Aquisition started in GUI function "startVideo()"
try:
self.stopAq()
# self.cam.set_exposure(70000)#this is a good value for current setup
self.startAq()
self.resizeROI(400,300)#change back to small ROI if not already small
except:
print("can't set camera params")
self.vidClosed = False # set vidClosed Flag to False on odd parity on/off buton click count
while (self.vidPowerCnt %2==1):
self.startAq()
#get data and pass them from camera to img
self.cam.get_image(self.img,100000)#second param is timeout (set really long just in case)
#create numpy array with data from camera. Dimensions of the array are
#determined by imgdataformat
self.data = self.img.get_image_data_numpy()
#calculate variance for image sharpness and display to frame
var = self.system.focus.varianceofLap(self.data) #can use this cv2.variance if wanted instead
# var =np.var(self.data) #using numpy variance currently
var = '{:5.2f}'.format(var)
font = cv2.FONT_HERSHEY_SIMPLEX
#put variance reading on video frame for easier manual focusing
cv2.putText(
self.data, var, (0,100), font, 4, (255, 0, 0), 2 #currently using blue text color
)
if(self.data is None):
print("no data")
cv2.imshow('vid', self.data)
cv2.waitKey(20) #number of milliseconds frame is displayed
#position window and set it to stay on top
cv2.moveWindow("vid", 1400,200)
os.system("wmctrl -r 'vid' -b add,above")
#destroy window and stop camera acquisition, set vidClosed flage to True
if (self.vidClosed == False):
self.vidClosed = True
cv2.destroyWindow('vid')
self.stopAq()
#generates the cam adjust window
def camAdjust(self):
self.r = Tk()
self.r.geometry("450x965+760+10")
self.r.call('wm', 'attributes', '.', '-topmost', '1') #keeps the window on top
self.r.title("Cam Adjust")
self.r.configure(background = "blue")#used for contrasting background
self.r.overrideredirect(True) #remove minimize and exit toolbar from window
#generate camera Adjust window
self.caf = CamAdjustFrame(self.r,self.system)
# self.updateCaf()
def updateCaf(self):
if(not self.set_vals):
self.caf.updateVals()
self.r.after(100,self.updateCaf)
#closes window
def camAdjustExit(self):
self.caf.cafExit()
#enables auto white balance mode
def enableAutoWB(self):
self.cam.enable_auto_wb()
print("enabled awb")
#enables auto white balance mode
def disableAutoWB(self):
self.cam.disable_auto_wb()
self.setManWB(1.0,1.0,1.0) #resets white balance coeffients to all 1.0
print("disabled awb")
#sets manual white balance values (0.0 - 10.0)
def setManWB(self,r,g,b):
self.cam.set_wb_kr(r)
self.cam.set_wb_kg(g)
self.cam.set_wb_kb(b)
#doesn't work, camera hasn't adjustable lens
def setAperture(self,val):
self.cam.enable_lens_mode()
self.cam.set_lens_aperture_value(val)
#not used currently(only works with cv2 images)
def ResizeWithAspectRatio(self,image, width=None, height=None, inter=cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
return cv2.resize(image, dim, interpolation=inter)