def initCamera(self):
        """ 
        Detecta e inicializa la cámara
        """

        print('init Camera...')
        self.cam = Camera()
        self.cam.init()  
        self.configCamera()
Beispiel #2
0
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()
Beispiel #3
0
    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
Beispiel #4
0
 def _instrument_setup(self) -> None:
     # TODO: gain settings? AOI settings? neither are used in labview version
     self.interface = Spinnaker()
     self.interface.init()
     self.interface.PixelFormat = 'RGB8'
     self.interface.ExposureAuto = 'Off'
     self.interface.ExposureMode = 'Timed'
     self.interface.ExposureTime = self.EXPOSURE_TIME_US
Beispiel #5
0
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]
Beispiel #6
0
    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
Beispiel #7
0
class Camera(Instrument):
    # ? https://pypi.org/project/simple-pyspin/

    _config = configuration.from_yml(
        r'W:\Test Data Backup\instruments\config\camera.yml')
    display_name = _config.field(str)
    EXPOSURE_TIME_US = _config.field(float)
    TX_WAIT_S = 0.

    def _instrument_cleanup(self) -> None:
        self.interface.close()

    def _instrument_setup(self) -> None:
        # TODO: gain settings? AOI settings? neither are used in labview version
        self.interface = Spinnaker()
        self.interface.init()
        self.interface.PixelFormat = 'RGB8'
        self.interface.ExposureAuto = 'Off'
        self.interface.ExposureMode = 'Timed'
        self.interface.ExposureTime = self.EXPOSURE_TIME_US

    @proxy.exposed
    def capture(self):
        """
        takes 212ms including start and stop
        this camera model is specced as 7.5FPS
        """
        self.interface.start()
        img = self.interface.get_array()
        self.interface.stop()
        return img

    def _instrument_check(self) -> None:
        _ = self.interface.initialized

    def _instrument_debug(self) -> None:
        from cv2 import cv2
        cv2.imshow('debug image', self.capture())
        cv2.waitKey(0)
Beispiel #8
0
# Select which cameras to show here.
SHOW_CAMERAS = [
    'webcam',
    #    'plume',
    #    'fringe',
    #    'cbs',
]

# Size of region around plume to show
plume_size = 200

print('Initializing cameras...')

if 'plume' in SHOW_CAMERAS:
    plume_camera = Camera(0)
    plume_camera.init()
    plume_camera.start()

if 'cbs' in SHOW_CAMERAS:
    plt.ion()
    fig = plt.figure()

## connect
print('Connecting to publisher...')
fringe_socket = connect_to('camera')
cbs_socket = connect_to('cbs-camera')
webcam_socket = connect_to('webcam')


def from_png(buff, color=False):
from simple_pyspin import Camera, list_cameras
import os
import time
import gc

output_dir = os.path.join('docs', 'cameras')
if not os.path.exists(output_dir):
    os.makedirs(output_dir)

with Camera() as cam:
    print('Resetting camera...')
    cam.DeviceReset()

print('Waiting 5 seconds for it to wake up...')
time.sleep(5)

for n in range(10):
    cl = list_cameras()
    n_cam = cl.GetSize()
    cl.Clear()
    if n_cam:
        break
    time.sleep(1)

print('Reconnecting camera...')
with Camera() as cam:
    cam_name = cam.DeviceVendorName.strip() + ' ' + cam.DeviceModelName.strip()
    ofn = os.path.join(output_dir, cam_name.replace(' ', '_') + '.md')
    print('Generating documentation in file: %s' % ofn)

    with open(ofn, 'wt') as f:
from simple_pyspin import Camera
from PIL import Image
import os

with Camera() as cam:  # Initialize Camera
    # Set the area of interest (AOI) to the middle half
    cam.Width = cam.SensorWidth // 2
    cam.Height = cam.SensorHeight // 2
    cam.OffsetX = cam.SensorWidth // 4
    cam.OffsetY = cam.SensorHeight // 4

    # If this is a color camera, get the image in RGB format.
    if 'Bayer' in cam.PixelFormat:
        cam.PixelFormat = "RGB8"

    # To change the frame rate, we need to enable manual control
    cam.AcquisitionFrameRateAuto = 'Off'
    cam.AcquisitionFrameRateEnabled = True
    cam.AcquisitionFrameRate = 20

    # To control the exposure settings, we need to turn off auto
    cam.GainAuto = 'Off'
    # Set the gain to 20 dB or the maximum of the camera.
    gain = min(20, cam.get_info('Gain')['max'])
    print("Setting gain to %.1f dB" % gain)
    cam.Gain = gain
    cam.ExposureAuto = 'Off'
    cam.ExposureTime = 10000  # microseconds

    # If we want an easily viewable image, turn on gamma correction.
    # NOTE: for scientific image processing, you probably want to
Beispiel #11
0
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
Beispiel #12
0
 def __init__(self):
     self.cam = Camera()
     self.cam.init()
     self.cam.start()
     self.cam.cam.AcquisitionFrameRateAuto = 'Off'
     self.cam.cam.AcquisitionFrameRateEnabled = True
Beispiel #13
0
    cropped = param[:, 160:160 + 960]
    input = cv2.resize(cropped, (300, 300))
    input = numpy.expand_dims(input, axis=0)
    input = imagenet_utils.preprocess_input(input, mode='tf')
    output = model.predict(input)
    # rescale prediction

    return (int(output[0, 0] * 960 + 160), int(output[0, 1] * 960))


## PROCESSING VIDEO STREAM ########
if __name__ == '__main__':

    model = keras.models.load_model('best.hdf5')

    camera = Camera()
    camera.init()
    if 'Bayer' in camera.PixelFormat:
        camera.PixelFormat = "RGB8"
    camera.start()

    while (True):
        frame = camera.get_array()
        frame = cv2.cvtColor(cv2.flip(frame, -1), cv2.COLOR_BGR2RGB)
        predicted = predict(frame, model)
        cv2.circle(frame, predicted, 5, (0, 0, 255), -1)
        cv2.imshow('prediction', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    camera.stop()
class Acquisition():
    """
    Clase para la adquisición de imágenes de la cámara
    """

    def __init__(self):
        pass

    def initCamera(self):
        """ 
        Detecta e inicializa la cámara
        """

        print('init Camera...')
        self.cam = Camera()
        self.cam.init()  
        self.configCamera()

    def configCamera(self):
        """
        Configura algunos parametros de la cámara
        """

        print('config camera...')
        self.cam.Width = self.cam.SensorWidth # 1440
        self.cam.Height = self.cam.SensorHeight # 900
        self.cam.PixelFormat = "RGB8"   
        print(self.cam.PixelFormat)

    def getRgbImage(self):
        """
        Obtiene la imagen rgb de la cámara

        return:

            rgbImage: imagen de cv2
        """

        self.cam.start()
        frame = self.cam.get_array()
        self.cam.stop()
        self.rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGRA2RGB)
        #self.rgbImage = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        return self.rgbImage

    def captureRgbImage(self):
        """
        captura la imagen rgb

        return:

            rgbImageCaptured: imagen de cv2
        """

        self.rgbImageCaptured = self.rgbImage
        return self.rgbImageCaptured

    def saveRgbImage(self, pathImage):         
        """
        Guarda la imagen capturada

        paramter:

                pathImage: ruta en la que se guarda la imagen
        """

        cv2.imwrite(pathImage, self.rgbImageCaptured)
Beispiel #15
0
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.')