Skip to content

Commit 4e3965e

Browse files
authored
Major update to code
1 parent 8523c14 commit 4e3965e

15 files changed

+5694
-258
lines changed

AutocontrollerV3.py

Lines changed: 3771 additions & 0 deletions
Large diffs are not rendered by default.

BaslerCameras.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def connect_camera(self):
5454
self.cam = pylon.InstantCamera(tlf.CreateFirstDevice())
5555
self.cam.Open()
5656
sleep(0.2)
57+
self.cam.AcquisitionFrameRateEnable = False # Default is max fps
58+
try:
59+
# TODO test if this worked, can reach close to 4000 fps with it on a decently large AOI.
60+
self.camera.SensorReadoutMode.SetValue("Fast")
61+
except Exception as ex:
62+
print(f"Sensor readout mode not accepted by camera, {ex}")
5763
return True
5864
except Exception as ex:
5965
self.cam = None
@@ -84,6 +90,14 @@ def set_frame_rate(self, frame_rate):
8490
self.cam.AcquisitionFrameRate.SetValue(float(frame_rate))
8591
except Exception as ex:
8692
print(f"Frame rate not accepted by camera, {ex}")
93+
94+
def set_gain(self,gain):
95+
try:
96+
print(f"Setting gain to {gain}")
97+
self.cam.Gain.Value = int(gain)
98+
except:
99+
100+
pass
87101

88102

89103
def set_AOI(self, AOI):
@@ -117,6 +131,10 @@ def set_AOI(self, AOI):
117131
self.cam.Height = height
118132
self.cam.OffsetX = offset_x
119133
self.cam.OffsetY = offset_y
134+
AOI[0] = offset_x
135+
AOI[1] = offset_x + width
136+
AOI[2] = offset_y
137+
AOI[3] = offset_y + height
120138

121139
except Exception as ex:
122140
print(f"AOI not accepted, AOI: {AOI}, error {ex}")

CameraControlsNew.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import cv2 # Certain versions of this won't work
1010
import tkinter
1111
from tkinter import simpledialog
12-
from time import sleep, strftime, perf_counter
12+
from time import sleep, strftime, perf_counter, time
1313
from threading import Thread
1414
from copy import copy, deepcopy
1515
# from queue import Queue
@@ -105,6 +105,8 @@ def __subclasshook__(cls, subclass):
105105
callable(subclass.set_exposure_time) and
106106
hasattr(subclass, 'set_frame_rate') and
107107
callable(subclass.set_frame_rate) and
108+
hasattr(subclass, 'set_gain') and
109+
callable(subclass.set_gain) and
108110
hasattr(subclass, 'capture_image') and
109111
callable(subclass.capture_image) or
110112
NotImplemented)
@@ -149,9 +151,9 @@ def __init__(self, c_p, camera):
149151

150152
# TODO remove temporary solution
151153

152-
self.camera.cam.AcquisitionFrameRateEnable = False
154+
#self.camera.cam.AcquisitionFrameRateEnable = False
153155
# self.camera.cam.AcquisitionFrameRate = 11
154-
print("Framrate ", self.camera.cam.ResultingFrameRate())
156+
#print("Framrate ", self.camera.cam.ResultingFrameRate())
155157

156158
# Zoom out
157159
self.c_p['AOI'] = [0, self.c_p['camera_width'], 0,
@@ -166,6 +168,8 @@ def update_camera_settings(self):
166168
self.camera.set_exposure_time(self.c_p['exposure_time'])
167169
elif self.c_p['new_settings_camera'][1] == 'frame_rate':
168170
self.camera.set_frame_rate(self.c_p['target_frame_rate'])
171+
elif self.c_p['new_settings_camera'][1] == 'gain':
172+
self.camera.set_gain(self.c_p['image_gain'])
169173

170174
# Resetting the new_settings_camera parameter
171175
self.c_p['new_settings_camera'] = [False, None]
@@ -177,19 +181,19 @@ def run(self):
177181
if self.c_p['new_settings_camera'][0]:
178182
self.update_camera_settings()
179183
count += 1
180-
if count % 20 == 5:
184+
if count % 110 == 5: # % 20 before
181185
p_t = perf_counter()
182186
self.c_p['image'] = self.camera.capture_image()
183187
if self.c_p['image'] is None:
184-
print("None image error!!?!?")
188+
print("None image error!")
185189
if self.c_p['recording']:
186190
img = copy(self.c_p['image'])
187191
name = copy(self.c_p['video_name'])
188192
# TODO add also the time for the frame captured as measured on the controller.
189193
self.c_p['frame_queue'].put([img, name,
190-
self.c_p['video_format']])
191-
if count % 20 == 15:
192-
self.c_p['fps'] = 11 / (perf_counter()-p_t)
194+
self.c_p['video_format'], time()])
195+
if count % 110 == 105: # Unreliable for high framerates, increasing from 10 to 100 measurement rate(from 20 and 15 to 110 and 95)
196+
self.c_p['fps'] = 101 / (perf_counter()-p_t) # Changed from 11 to 111
193197

194198

195199
class VideoFormatError(Exception):
@@ -319,6 +323,8 @@ def __init__(self, threadID, name, c_p):
319323
self.frame_buffer = []
320324
self.frame_buffer_size = 100
321325
self.frame_count = 0
326+
self.frame_time = 0
327+
self.frame_timings = np.zeros(10_000_000) # May want to make this really big and only save it at the end of the video recording.
322328
self.VideoWriter = None
323329
self.np_save_path = None
324330

@@ -357,6 +363,9 @@ def np_save_frames(self):
357363
"""
358364
Saves all the frames in the buffer to a .npy file.
359365
"""
366+
if self.frame_count < 3: # Potential fix for problem with saving the 0--1 as a file.
367+
print("Not enough frames to save, only ", self.frame_count)
368+
return
360369
nbr_frames = self.frame_count % self.frame_buffer_size
361370
if nbr_frames == 0 and self.frame_count != 0:
362371
nbr_frames = self.frame_buffer_size
@@ -366,6 +375,12 @@ def np_save_frames(self):
366375
filename = lower_lim + '-' + upper_lim + '.npy'
367376
with open(self.np_save_path+filename, 'wb') as f:
368377
np.save(f, self.frame_buffer[:nbr_frames])
378+
# TODO if this works change so that it is a single file.
379+
if not self.video_created:
380+
381+
with open(self.np_save_path+'frame_time.npy', 'wb') as f:
382+
nbr_frames = min(self.frame_count, 10_000_000)
383+
np.save(f, self.frame_timings[:nbr_frames])
369384
self.frame_buffer *= 0
370385

371386
def create_NP_writer(self, video_name):
@@ -388,6 +403,8 @@ def write_to_NPY(self):
388403
try:
389404
# TODO fix error here
390405
self.frame_buffer[nbr_frames, :, :] = deepcopy(self.frame)
406+
if self.frame_count<10_000_000:
407+
self.frame_timings[self.frame_count] = self.frame_time
391408
# Do we miss a frame here?
392409
self.frame_count += 1
393410
except Exception as ex:
@@ -450,7 +467,7 @@ def create_video_writer(self, video_name):
450467
self.video_width,
451468
self.video_height, 3]))
452469
self.frame_count = 0
453-
470+
454471
self.create_NP_writer(video_name)
455472
else:
456473
raise VideoFormatError(f"Video format{self.c_p['video_format']}\
@@ -481,7 +498,7 @@ def run(self):
481498

482499
if not self.c_p['frame_queue'].empty():
483500

484-
[self.frame, source_video, self.format] = self.c_p['frame_queue'].get()
501+
[self.frame, source_video, self.format, self.frame_time] = self.c_p['frame_queue'].get()
485502

486503
# Check that name and size are correct, if not create a new
487504
image_shape = np.shape(self.frame)

0 commit comments

Comments
 (0)