99import cv2 # Certain versions of this won't work
1010import tkinter
1111from tkinter import simpledialog
12- from time import sleep , strftime , perf_counter
12+ from time import sleep , strftime , perf_counter , time
1313from threading import Thread
1414from 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
195199class 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