class Recorder: def __init__(self, resize_width=300, resize_height=300): self.cam = Camera() with Camera() as cam: # Azért kell, hogy a színt be lehessen állítani if 'Bayer' in cam.PixelFormat: cam.PixelFormat = "RGB8" cam.start() self.cam.init() self.cam.start() self.frame = cv2.cvtColor(cv2.flip(self.cam.get_array(), 0), cv2.COLOR_BGR2RGB) self.frame_resized = [] self.started = False self.read_lock = threading.Lock() self.resize_width = resize_width self.resize_height = resize_height def start(self): if self.started: print('[!] Asynchronous video capturing has already been started.') return None self.started = True self.thread = threading.Thread(target=self.update, args=()) self.thread.start() return self def update(self): while self.started: frame = cv2.cvtColor(cv2.flip(self.cam.get_array(), 0), cv2.COLOR_BGR2RGB) frame_cropped = frame[:, 160:160+960] with self.read_lock: self.frame = frame image = cv2.resize(frame_cropped, (self.resize_width, self.resize_height)) image = image / 127.5 image = image - 1. image = numpy.expand_dims(image, axis=0) self.frame_resized = image def read(self): with self.read_lock: #frame = self.frame.copy() frame_resized = self.frame_resized.copy() return frame_resized def stop(self): self.started = False self.thread.join() def __exit__(self, exec_type, exc_value, traceback): self.cam.stop() self.cam.close()
class FrescoCamera: def __init__(self): self.cam = Camera() self.cam.init() self.cam.start() self.cam.cam.AcquisitionFrameRateAuto = 'Off' self.cam.cam.AcquisitionFrameRateEnabled = True def __delete__(self, instance): self.cam.stop() self.cam.close() def get_current_image(self): images = [self.cam.get_array() for n in range(20)] return images[19]
if data is not None: frame = from_png(data['annotated'], color=True) cv2.imshow('Webcam', frame) print(timestamp, 'webcam') if 'plume' in SHOW_CAMERAS: plume_image = plume_camera.get_array() # Extract plume location cx, cy, intensity, _ = fit_image(plume_image) height, width = plume_image.shape cx = round(cx.n * width / 100) cy = round(cy.n * height / 100) plume_image = plume_image[max(cy - plume_size, 0):cy + plume_size, max(cx - plume_size, 0):cx + plume_size] cv2.imshow('Plume', plume_image) print(timestamp, 'plume') if cv2.waitKey(1) & 0xFF == ord('q'): break time.sleep(1e-2) if 'plume' in SHOW_CAMERAS: plume_camera.stop() plume_camera.close() cv2.destroyAllWindows()
class FlirDevice(object): ''' Scopefoundry compatible class to run a FLIR camera with spinnaker software For Pointgrey grasshopper, the bit depth is 16bit or 8bit, specified in the PixelFormat attribute, simple_pyspin is not compatible with 12bit readout. ''' def __init__(self, debug=False, dummy=False): self.debug = debug self.dummy = dummy if not self.dummy: self.cam = Camera() self.cam.init() self.cam.ExposureAuto = 'Off' self.cam.ExposureMode = 'Timed' self.cam.GainAuto = 'Off' self.cam.AcquisitionFrameRateEnabled = True self.cam.AcquisitionFrameRateAuto = 'Off' self.cam.AutoFunctionAOIsControl = 'Off' self.cam.AcquisitionMode = 'Continuous' self.cam.ChunkEnable = False self.cam.EventNotification = 'Off' self.cam.GammaEnabled = False self.cam.PixelFormat = 'Mono16' # specify here the bit depth self.cam.BlackLevel = 1.0 # self.cam.TriggerMode = 'Off' self.cam.OnBoardColorProcessEnabled = False def set_acquisitionmode(self, mode): self.cam.AcquisitionMode = mode def get_acquisitionmode(self): mode = self.cam.AcquisitionMode return (mode) def close(self): self.cam.close() def set_framenum(self, Nframes): if self.cam.AcquisitionMode == 'MultiFrame': self.cam.AcquisitionFrameCount = Nframes def read_temperature(self): resp = self.cam.DeviceTemperature return resp def get_width(self): w = self.cam.Width return w def get_height(self): h = self.cam.Height return h def acq_start(self): self.cam.start() def get_nparray(self): return self.cam.get_array() def acq_stop(self): self.cam.stop() def get_exposure(self): "get the exposure time in ms" return self.cam.ExposureTime / 1000 def set_exposure(self, desired_time): "set the exposure time in ms" maxexp = self.cam.get_info('ExposureTime')['max'] minexp = self.cam.get_info('ExposureTime')['min'] exptime = min(maxexp, max(desired_time * 1000, minexp)) self.cam.ExposureTime = exptime def get_rate(self): return self.cam.AcquisitionFrameRate def set_rate(self, desired_framerate): """ Set the framerate in Hz FLIR Grasshopper runs at: 163.57 fps at 8bit, full frame 87.08 fps at 12 bit, not supported by simple_pyspin 82.47 fps at 16bit is used here """ maxfr = self.cam.get_info('AcquisitionFrameRate')['max'] minfr = self.cam.get_info('AcquisitionFrameRate')['min'] framerate = max(minfr, min(desired_framerate, maxfr)) self.cam.AcquisitionFrameRate = framerate def get_gain(self): return self.cam.Gain def set_gain(self, desired_gain): "set the gain in dB" maxgain = self.cam.get_info('Gain')['max'] self.cam.Gain = min(desired_gain, maxgain) def get_idname(self): cam_name = self.cam.DeviceVendorName.strip( ) + ' ' + self.cam.DeviceModelName.strip() return cam_name
async def run_publisher(): print('Initializing devices...') pressure_gauge = FRG730() thermometers = [(CTC100(31415), ['saph', 'coll', 'bott hs', 'cell'], ['heat saph', 'heat coll']), (CTC100(31416), ['srb4k', 'srb45k', '45k plate', '4k plate'], ['srb45k out', 'srb4k out'])] labjack = Labjack('470022275') mfc = MFC(31417) wm = WM( publish=False ) #wavemeter class used for reading frequencies from high finesse wavemeter pt = PulseTube() camera = Camera(1) camera.init() try: camera.start() except: pass camera.GainAuto = 'Off' camera.Gain = 10 camera.ExposureAuto = 'Off' camera_publisher = create_server('camera') turbo = TurboPump() pt_last_off = time.monotonic() heaters_last_safe = time.monotonic() try: cbs_cam = Ximea(exposure=1e6) cbs_cam.set_roi(500, 500, 700, 700) except: cbs_cam = None print(f'{Fore.RED}ERROR: Ximea camera is unplugged!{Style.RESET_ALL}') cbs_publisher = create_server('cbs-camera') spectrometer_monitor = connect_to('spectrometer') print('Starting publisher') publisher_start = time.monotonic() loop = asyncio.get_running_loop() run_async = lambda f: loop.run_in_executor(None, f) try: with create_server('edm-monitor') as publisher: rough = {} trans = {} for loop_iteration in itertools.count(1): loop_start = time.monotonic() async_getters = [] times = {} ##### Read pressure gauge (Async) ##### chamber_pressure = None def pressure_getter(): nonlocal chamber_pressure, times with Timer('pressure', times): chamber_pressure = pressure_gauge.pressure async_getters.append(run_async(pressure_getter)) ##### Read CTC100 Temperatures + Heaters (Async) ##### temperatures = {} heaters = {} async def CTC_getter(thermometer): """Record data from the given thermometer.""" obj, temp_channels, heater_channels = thermometer with Timer(f'CTC{obj._address[1]}', times): for channel in temp_channels: temperatures[channel] = await obj.async_read( channel) for channel in heater_channels: heaters[channel] = await obj.async_read(channel) async_getters.extend( [CTC_getter(thermometer) for thermometer in thermometers]) ##### Read MFC Flows (Async) ##### flows = {} async def flow_getter(): """Record the flow rates from the MFC.""" with Timer('MFC', times): flows['cell'] = deconstruct( await mfc.async_get_flow_rate_cell()) flows['neon'] = deconstruct( await mfc.async_get_flow_rate_neon_line()) async_getters.append(flow_getter()) ##### Read wavemeter frequencies (Async) ##### frequencies = {} async def frequency_getter(): """Record the frequencies from the wavemeter.""" with Timer('wavemeter', times): frequencies['baf'] = await with_uncertainty( lambda: wm.read_frequency(8)) frequencies['calcium'] = await with_uncertainty( lambda: wm.read_frequency(6)) async_getters.append(frequency_getter()) ##### Read Camera (Async) ##### center = {} refl = {} png = {} def camera_getter(): camera_samples = [] with Timer('camera', times): exposure = camera.ExposureTime image = None while True: capture_start = time.monotonic() sample = camera.get_array() capture_time = time.monotonic() - capture_start camera_samples.append(fit_image(sample)) if image is None: image = sample # Clear buffer (force new acquisition) if capture_time > 20e-3: break # Track fringes fringe_model.update(image, exposure) center_x, center_y, cam_refl, saturation = [ unweighted_mean(arr) for arr in np.array(camera_samples).T ] cam_refl *= 1500 / exposure # Downsample if 16-bit if isinstance(image[0][0], np.uint16): image = (image / 256 + 0.5).astype(np.uint8) # Save images png['raw'] = cv2.imencode('.png', image)[1].tobytes() png['fringe'] = cv2.imencode( '.png', fringe_model.scaled_pattern)[1].tobytes() png['fringe-annotated'] = cv2.imencode( '.png', fringe_model.annotated_pattern)[1].tobytes() # Store data center['x'] = deconstruct(center_x) center['y'] = deconstruct(center_y) center['saturation'] = deconstruct(saturation) center['exposure'] = exposure refl['cam'] = deconstruct(2 * cam_refl) refl['ai'] = deconstruct(fringe_model.reflection) if saturation.n > 99: camera.ExposureTime = exposure // 2 if saturation.n < 30: camera.ExposureTime = exposure * 2 async_getters.append(run_async(camera_getter)) ##### Read turbo status (Async) ##### pt_on = pt.is_on() running = {'pt': pt_on} async def turbo_getter(): """Record the operational status of the turbo pump.""" with Timer('turbo', times): status = await turbo.async_operation_status() running['turbo'] = (status == 'normal') async_getters.append(turbo_getter()) ##### Read labjack (Async) ##### intensities = {} def labjack_getter(): with Timer('labjack', times): intensities['broadband'] = deconstruct( labjack.read('AIN0')) # intensities['hene'] = deconstruct(labjack.read('AIN1')) intensities['LED'] = deconstruct(labjack.read('AIN2')) async_getters.append(run_async(labjack_getter)) # Await all async data getters. # Tasks will run simultaneously. gather_task = asyncio.gather(*async_getters) try: await asyncio.wait_for(gather_task, timeout=15) except: raise ValueError(gather_task.exception()) ##### Read CBS camera (Sync) ##### cbs_png = None cbs_info = {'data': None, 'fit': None} with Timer('CBS Camera', times): if cbs_cam is not None and cbs_cam.capture(): cbs_png = cv2.imencode('.png', cbs_cam.image)[1].tobytes() try: r, I, (peak, width, background), chisq = fit_cbs(cbs_cam.image) cbs_info['data'] = { 'radius': list(r), 'intensity': { 'nom': list(nom(I)), 'std': list(std(I)), } } if max(width.s, peak.s) > 100 or min(width.n, peak.n) < 0: raise ValueError cbs_info['fit'] = { 'peak': deconstruct(peak), 'width': deconstruct(width), 'background': deconstruct(background), 'chisq': chisq, } except: pass # Read spectrometer thread. _, spec_data = spectrometer_monitor.grab_json_data() if spec_data is not None: rough = spec_data['rough'] trans = spec_data['trans'] rough['hdr-chisq'] = spec_data['fit']['chisq'] ### Update models ### saph_temp = temperatures['saph'] growth_model.update(ufloat(*flows['neon']), ufloat(*flows['cell']), saph_temp) fringe_counter.update(refl['ai'][0], grow=(growth_model._growth_rate.n > 0)) if saph_temp > 13: fringe_counter.reset() # Construct final data packet times['loop'] = round(1e3 * (time.monotonic() - loop_start)) uptime = (time.monotonic() - publisher_start) / 3600 data_dict = { 'pressure': deconstruct(chamber_pressure), 'flows': flows, 'temperatures': temperatures, 'heaters': heaters, 'center': center, 'cbs': cbs_info['fit'], 'rough': rough, 'trans': trans, 'refl': refl, 'fringe': { 'count': fringe_counter.fringe_count, 'ampl': fringe_counter.amplitude, }, 'model': { 'height': deconstruct(growth_model.height), }, 'freq': frequencies, 'intensities': intensities, 'running': running, 'debug': { 'times': times, 'uptime': uptime if loop_iteration > 1 else None, 'memory': memory_usage(), 'system-memory': round(psutil.virtual_memory().used / 1024), 'cpu': psutil.cpu_percent(), } } print_tree(data_dict) ### Limit publishing speed ### target_end = PUBLISH_INTERVAL * loop_iteration + publisher_start time.sleep(max(target_end - time.monotonic(), 0)) publisher.send(data_dict) camera_publisher.send(png) if cbs_png is not None: cbs_publisher.send({ 'image': cbs_png, **cbs_info, }) print() print() # Restart if pressure gauge cuts out if data_dict['pressure'] is None: pressure_gauge.close() pressure_gauge = FRG730() finally: print( f'{Fore.RED}{Style.BRIGHT}Crashed, cleaning up...{Style.RESET_ALL}' ) tb = traceback.format_exc() print(tb) print('Stopping fringe camera...') camera.stop() camera.close() camera_publisher.close() print('Stopping CBS camera...') cbs_cam.close() cbs_publisher.close() print('Stopping miscellaneous equipment...') pressure_gauge.close() mfc.close() turbo.close() print('Done.')