def __init__(self): #initialize Camera self.camera = pylon.InstantCamera(pylon.TlFactory.GetInstance( ).CreateFirstDevice()) #create instance of Camera #Open camera self.camera.Open() print("Using device:", self.camera.GetDeviceInfo().GetModelName()) self.converter = pylon.ImageFormatConverter() self.converter.OutputPixelFormat = pylon.PixelType_BGR8packed self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned #Set Auto-Gain and Auto-Exposure to be OFF self.camera.ExposureAuto.SetValue("Off") self.camera.GainAuto.SetValue("Off") self.camera.Gain.GetValue() self.camera.ExposureTime.GetValue() self.camera.Gamma.GetValue() #Create an image window self.imageWindow = pylon.PylonImageWindow() self.imageWindow.Create(1) #Change save directory os.chdir("C:\\Users\\Wyss User\\Pictures\\Basler Test") print("Current working directory (save location):", os.getcwd()) print("Camera initialized.")
def __init__(self, parameters=None): self.camera = pylon.InstantCamera( pylon.TlFactory.GetInstance().CreateFirstDevice()) self.info = self.camera.GetDeviceInfo() self.camera.MaxNumBuffer = 10 # Default is 10 self.imageWindow = pylon.PylonImageWindow() self.imageWindow.Create(1) if parameters is None: # Use defaults self.parameters = { 'output_file': 'output.mp4', 'width': 160, 'height': 160, 'fps': 500.0, 'exposure_time': 1000.0 } else: self.parameters = parameters # self.parameters['ffmpeg_param_out'] = { # '-vcodec': 'libx264', # '-preset': 'ultrafast', # '-crf': '28', # '-framerate': str(self.parameters['fps']), # #'-r': str(self.parameters['fps']) # #'-g': str(self.parameters['fps']), # #'-keyint_min': str(self.parameters['fps']), # #'-sc_threshold': '0' # } # self.parameters['ffmpeg_param_in'] = { # '-r': str(self.parameters['fps']) #} self.parameters['ffmpeg_param_out'] = { '-vcodec': 'libx264', '-preset': 'ultrafast', '-crf': '8' # '-r': str(self.parameters['fps']) } self.parameters['record'] = True file = Path(self.parameters['output_file']) self.parameters['output_csv'] = file.parent / (file.stem + "_recording.csv") self.data_out = [] self.counter = 0 self.first_timestamp = 0 # First time stamp is used for calculating start point as 0 # Setup converter self.converter = pylon.ImageFormatConverter() # Convert to RGB8 to support skvideo self.converter.OutputPixelFormat = pylon.PixelType_RGB8packed # Convert image BGR8 to support OpenCV # self.converter.OutputPixelFormat = pylon.PixelType_BGR8packed self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
print(spec) text_file.write(spec + '\n') print('resulting frame rate') text_file.write('resulting frame rate' + '\n') cam_info = str(cam.ResultingFrameRate.GetValue()) print(cam_info) text_file.write(cam_info + '\n') # Print the model name of the camera. cam_info = cam.GetDeviceInfo().GetModelName() print("Using device " + cam_info) text_file.write("Using device " + cam_info + '\n') cam.StartGrabbing() imageWindow = pylon.PylonImageWindow() imageWindow.Create(1) img = pylon.PylonImage() try: print() print('object array, E -> W, 1 = food, 0 = no food') print(str(mylist)) text_file.write('\n') text_file.write('object array, E -> W, 1 = food, 0 = no food' + '\n') text_file.write(str(mylist) + '\n') print()
def stream_videos(self, max_frames=None): # Set up display windows if self.live_display: image_windows = [pylon.PylonImageWindow() for i in self.cameras] self.pylon_windows = image_windows for i, window in enumerate(image_windows): window.Create(i) # ? Keep looping to acquire frames # self.grab.GrabSucceeded is false when a camera doesnt get a frame -> exit the loop while True: try: if self.frame_count % 500 == 0: # Print the FPS in the last 100 frames if self.frame_count == 0: start = time.time() else: start = self.print_current_fps(start) # ! Loop over each camera and get frames grab = self.grab_frames() # Read the state of the arduino pins and save to file sensor_states = self.read_arduino_write_to_file(grab.TimeStamp) # Read the state of the door status pins ds = self.read_door_status() # Threshold sensor data and control door of the arena self.live_sensors_control(sensor_states) # If live plotting, add the data and then update plots if self.live_plotting: self.append_sensors_data(sensor_states) try: self.update_sensors_plot() except: raise ValueError( "Could not append live sensor data during live plotting" ) # Update frame count self.frame_count += 1 # Stop if reached max frames if max_frames is not None: if self.frame_count >= max_frames: print("Reached the end of the experiment.") break # stop if enough time has elapsed if self.experiment_duration is not None: if time.time( ) - self.exp_start_time / 1000 > self.experiment_duration: print("Terminating acquisition - reached max time") raise KeyboardInterrupt( "terminating" ) # need to raise an error here to be cached in main except pylon.TimeoutException as e: print("Pylon timeout Exception") raise ValueError("Could not grab frame within timeout") # Close camera for cam in self.cameras: cam.Close()
def GrabFrames(cam_params, camera, writeQueue, dispQueue, stopQueue): n_cam = cam_params["n_cam"] cnt = 0 timeout = 0 # Create dictionary for appending frame number and timestamp information grabdata = {} grabdata['timeStamp'] = [] grabdata['frameNumber'] = [] numImagesToGrab = cam_params['recTimeInSec'] * cam_params['frameRate'] chunkLengthInFrames = int( round(cam_params["chunkLengthInSec"] * cam_params['frameRate'])) if cam_params["displayFrameRate"] <= 0: frameRatio = float('inf') elif cam_params["displayFrameRate"] > 0 and cam_params[ "displayFrameRate"] <= cam_params['frameRate']: frameRatio = int( round(cam_params['frameRate'] / cam_params["displayFrameRate"])) else: frameRatio = cam_params['frameRate'] if sys.platform == 'win32': imageWindow = pylon.PylonImageWindow() imageWindow.Create(n_cam) imageWindow.Show() camera.StartGrabbing(pylon.GrabStrategy_OneByOne) print(cam_params["cameraName"], "ready to trigger.") while (camera.IsGrabbing()): if stopQueue or cnt >= numImagesToGrab: CloseCamera(cam_params, camera, grabdata) writeQueue.append('STOP') break try: # Grab image from camera buffer if available grabResult = camera.RetrieveResult( timeout, pylon.TimeoutHandling_ThrowException) # Append numpy array to writeQueue for writer to append to file writeQueue.append(grabResult.Array) if cnt == 0: timeFirstGrab = grabResult.TimeStamp grabtime = (grabResult.TimeStamp - timeFirstGrab) / 1e9 grabdata['timeStamp'].append(grabtime) cnt += 1 grabdata['frameNumber'].append(cnt) # first frame = 1 if cnt % frameRatio == 0: if sys.platform == 'win32' and cam_params[ 'cameraMake'] == 'basler': try: imageWindow.SetImage(grabResult) imageWindow.Show() except Exception as e: logging.error('Caught exception: {}'.format(e)) else: dispQueue.append( grabResult.Array[::cam_params["displayDownsample"], :: cam_params["displayDownsample"]]) grabResult.Release() if cnt % chunkLengthInFrames == 0: fps_count = int(round(cnt / grabtime)) print('Camera %i collected %i frames at %i fps.' % (n_cam, cnt, fps_count)) # Else wait for next frame available except geni.GenericException: time.sleep(0.0001) except Exception as e: logging.error('Caught exception: {}'.format(e))