FocusCamera.py 5.43 KB
Newer Older
1

2 3 4 5 6 7
import Devices.FocusCameraTools.laser_analysis as LA

import pypylon
import matplotlib.pyplot as plt
import numpy as np

Nick Sauerwein's avatar
Nick Sauerwein committed
8

9 10
from Devices.CameraTools.binning import binArray
from Scripts.PlottingServers import Plot2DServer
Nick Sauerwein's avatar
Nick Sauerwein committed
11

12 13 14 15
'''
config file:
ExposureTimeAbs:    exposure time in us
min_intensity:      minimal intensity over which contour of FWHM will be fitted
16
measure_full:       boolean to indicate if image should be analysed (FWHMs, contour, ...)
17 18 19 20 21 22 23 24 25


measurement file:
time:               time (datetime object)
im:                 image of camera
FWHMx:              FWHM of intensity in x
FWHMy:              FWHM of intensity in y
cxmm:               x coordinate of contour [mm]
cymm:               y coordinate of contour [mm]
26
maxit:              maximal intensity [0 - 4096]
27 28
'''

29 30 31 32 33
default_config = {'ExposureTimeAbs': 10000,
                   'camera_serial_number':22005848,
                   'min_intensity': 2000,
                   'measure_full' : True}

34 35 36 37 38 39

class FocusCamera:
    
    def __init__(self, config):
        

40
        self.psize=1.67 #um
Nick Sauerwein's avatar
Nick Sauerwein committed
41 42
        
        self.camera_serial_number = 22005848
43 44 45
        
        self.connect_camera()
        
46
        self.config = config
Nick Sauerwein's avatar
Nick Sauerwein committed
47 48
        
        self.plot_server_im = None
49 50 51 52 53 54 55 56
        
    @property    
    def config(self):
        config = {}
        config['ExposureTimeAbs'] = self.cam.properties['ExposureTimeAbs']
        config['min_intensity'] = self.min_intensity
        config['measure_full'] = self.measure_full
        return config    
57
    
58
    @config.setter
59 60 61 62
    def set_config(self,config):
        self.min_intensity = config['min_intensity']
        
        self.cam.properties['ExposureTimeAbs']=config['ExposureTimeAbs']
63
        self.min_intensity = config['min_intensity']
64 65
        self.measure_full = config['measure_full']
        
66

67 68 69 70 71
    
    def measure(self):
        import datetime
        time = datetime.datetime.now()
        
72 73 74 75
        im = self.cam.grab_image()
        measurement = {'time': time,'im': im}
        
        if self.measure_full:
76
            max_int = np.max(im)
77 78
            if max_int > self.min_intensity:
                maximum_pos, contour = LA.find_beam(im, plot = False)
Nick Sauerwein's avatar
Nick Sauerwein committed
79 80 81
                cxum, cyum = contour
                FWHMx = np.max(cxum) - np.min(cxum)
                FWHMy = np.max(cyum) - np.min(cyum) 
82
                comment = 'beam detected'
83 84
            else:
                comment = 'No beam detected'
Nick Sauerwein's avatar
Nick Sauerwein committed
85 86
                FWHMx = 20
                FWHMy = 20
87 88
                cxmm = None
                cymm = None
89
            
Nick Sauerwein's avatar
Nick Sauerwein committed
90
            measurement['comment'], measurement['FWHMx'], measurement['FWHMy'], measurement['cxum'], measurement['cyum'] = (comment, FWHMx, FWHMy, cxmm, cymm)
91
            measurement['maxit'] = max_int
92 93 94
            
        return measurement
    
Sauerwein Nick Jacob's avatar
Sauerwein Nick Jacob committed
95
    def plot_measure(self, measurement, config = None):
Nick Sauerwein's avatar
Nick Sauerwein committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        
        if self.plot_server_im == None:
            self.plot_server_im = Plot2DServer({'title':'FocusCamera', 'xlabel':'y [um]', 'ylabel': 'x [um]', 'image': True})  

        im = measurement['im'].astype('float32')
        
        
        
        shape = im.shape
        
        nFWHM = 2.
        
        cxum = measurement['cxum']
        cyum = measurement['cyum']
        FWHMx = measurement['FWHMx']
        FWHMy = measurement['FWHMy']
        
        if cxum:
            xminum, xmaxum = np.min(cxum) - nFWHM * FWHMx, np.max(cxum) + nFWHM * FWHMx
            yminum, ymaxum = np.min(cyum) - nFWHM * FWHMy, np.max(cyum) + nFWHM * FWHMy

            im = im[xminum/self.psize:xmaxum/self.psize, yminum/self.psize:ymaxum/self.psize]
            
119
        else:
Nick Sauerwein's avatar
Nick Sauerwein committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
            xminum = 0
            yminum = 0
            xmaxum = shape[0] * self.psize
            ymaxum = shape[1] * self.psize
        
        import math
        #autobinning
        n_binning = math.ceil(((xmaxum - xminum)/self.psize)/500)
        
        im = binArray(im, 0, n_binning, n_binning, np.mean)
        im = binArray(im, 1, n_binning, n_binning, np.mean)
        
        self.plot_server_im.update(np.flipud(im),extent=(yminum,ymaxum, yminum, ymaxum))
        
        
135 136
    
    
137
    def connect_camera(self):
138 139 140 141 142 143 144 145 146 147
        available_cameras = pypylon.factory.find_devices()
        
        if len(available_cameras) == 0:
            assert 'No cameras found'
            
        print ('available cameras:')
        for cam in available_cameras:
            print(cam)
            
        for availca in available_cameras:
Nick Sauerwein's avatar
Nick Sauerwein committed
148 149
            int(str(availca)[-10:-2])
            if int(str(availca)[-10:-2]) == self.camera_serial_number:
150 151
                self.cam_to_connect = availca
                cam = pypylon.factory.create_device(availca)
Nick Sauerwein's avatar
Nick Sauerwein committed
152
                print ('--> camera (',self.camera_serial_number,') connected')
153 154 155 156
           
        try:
            cam.open()
        except:
Nick Sauerwein's avatar
Nick Sauerwein committed
157
            assert('camera not connectable')
158 159
        
        
Nick Sauerwein's avatar
Nick Sauerwein committed
160
        #cam.properties['GevSCPSPacketSize']=9015
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        cam.properties['TriggerMode']='Off'
        cam.properties['PixelFormat'] = 'Mono12'
        cam.properties['AcquisitionFrameRateEnable']=False
        cam.properties['AcquisitionFrameRateAbs']=100
        cam.properties['GainRaw']=150
        
        #These parameters have to be set because of the shot camera
        # --> delete if new camera is used
        cam.properties['Width'] = 2630
        cam.properties['Height'] = 2578
        cam.properties['OffsetX'] = 0
        cam.properties['OffsetY'] = 170
        
        self.cam = cam
    
    def turn_off(self):
        self.cam.close()