class LeGUI(Frame): def __init__(self, master): self.master = master self.F146B=2892819673481529 self.F201B=2892819673575448 self.guidCam=self.F146B self.guidCam=self.F201B Frame.__init__(self, self.master) self.configure_gui() self.create_widgets() def configure_gui(self): self.erreur=StringVar();self.erreur.set("") # Parametre de la camera self.tint=DoubleVar() #self.tint.trace("w",self.TintModifie) => def TintModifie(self,*args): self.gain=IntVar() self.Stop=False # ------ Initialisation de la camera #self.cam=Camera(guid=2892819673481529) self.cam=Camera(guid=self.guidCam) self.lemodel=self.cam.model.decode()[-5:] # acquisition en cours self.Acquisition=0 # axe x: lambda ou pixel: self.EnMicron=BooleanVar();self.EnMicron.set(False) # fit gaussienne self.fitgauss=BooleanVar();self.fitgauss.set(False) self.w0=IntVar();self.w0.set(80) # borner self.borner=BooleanVar();self.borner.set(False) self.Pix0=IntVar();self.Pix0.set(0) self.Pix1=IntVar();self.Pix1.set(1234) #init graph self.InitGraph=BooleanVar() self.InitGraph.set(True) # focale pixel/micron self.focale=DoubleVar();self.focale.set(6.6) # Nom de base du Fichier de sauvegarde self.Nom=StringVar() # le dossier de travail self.WorkDir=StringVar() self.WorkDir.set(os.getcwd()) #fichier reference self.FichRef=StringVar();self.FichRef.set("!! Ldb non definie !!") def create_widgets(self): self.master.title("CPL Stingray "+self.lemodel) topframe=Frame(self.master) topframe.pack() nbook=ttk.Notebook(topframe) f1=ttk.Frame(nbook) f2=ttk.Frame(nbook) nbook.add(f1,text="Commandes") nbook.add(f2,text="Fichiers") nbook.pack() self.FrCommandes(f1).pack() Fichiers.TkFich(f2,self.Nom,self.FichRef, self.WorkDir) #Les spectres dans une frame Toplevel: FrameTraceSp self.FrameTraceSp=Toplevel() # graphe a part self.FrameTraceSp.title("Image") # de matplotlib.figure: self.Figspectre=Figure(figsize=(8,6),dpi=100) # figsize: H,V self.plot1=self.Figspectre.add_subplot(111) # transfert la figure matplot dans un canvas Tk, inclus ds FrameTraceSp : self.canvas = FigureCanvasTkAgg(self.Figspectre,self.FrameTraceSp) self.canvas.draw() self.canvas.get_tk_widget().pack(side=BOTTOM, fill=BOTH, expand=True) toolbar = NavigationToolbar2Tk(self.canvas,self.FrameTraceSp) toolbar.update() self.canvas._tkcanvas.pack(side=TOP, fill=BOTH, expand=True) #self.canvas.draw #------------------------------------------------------------------ # mise a jour du graphe par # FuncAnimation(my_gui.Figspectre,my_gui.animate,interval=1000) # à la fin #------------------------------------------------------------------ def func(self,x,mean,std,maxi): return maxi*np.exp(-2*(x-mean)**2 / std**2) def updatefig(self,i): if self.Acquisition: #capture une frame et la copy dans CamArray self.cam.start_one_shot() frame =self.cam.dequeue() self.CamArray = frame.copy() frame.enqueue() self.cam.stop_one_shot() # on recupere les info de la camera y1=self.CamArray.sum(axis=1)/self.CamArray.shape[1] # reduction des bornes : if self.borner.get(): y1=y1[self.Pix0.get():self.Pix1.get()] #4.4microns/pixel pour la F201B #4.65 pour la F146B #calibration avec la mire de calibration Leitz pour le "1.3" if self.EnMicron.get(): G=200./self.focale.get() # print("focale:{}, G={}".format(self.focale.get()*1.,G)) x=np.arange(y1.size)*4.4/G larg =int(2* self.w0.get()/4.4*G) # print("largeur en pixel:{}".format(larg)) else : # self.focale.get() x=np.arange(y1.size) larg =int( self.w0.get()) self.data=np.column_stack((x,y1)) maxi= np.max(np.array(y1)) if self.fitgauss.get(): x_data = x[np.argmax(y1)-larg: np.argmax(y1)+larg] y_data = y1[np.argmax(y1)-larg: np.argmax(y1)+larg] init= [x[np.argmax(y1)],larg,maxi] b1= [0.7*x[np.argmax(y1)],0.5*larg,0.7*maxi] b2= [1.3*x[np.argmax(y1)],2*larg,1.3*maxi] try : popt, pcov = curve_fit(self.func, x_data, y_data,\ p0 = init) #bounds=(b1,b2)) except RuntimeError : print("fit pas bon!!!") self.fitgauss.set(False) # print(popt) #--------------------- affichage if self.initaffiche or self.InitGraph.get(): self.plot1.clear() #self.l1,=self.plot1.plot(x,y1,label="ecart type :{}".format(ecart_type)) self.l1,=self.plot1.plot(x,y1,label="cam") if self.fitgauss.get(): self.plot1.plot( x_data,self.func(x_data, *popt),\ label="mean : {:.3f}, 2w(1/e2) : {:.3f}".format(popt[0], 2*popt[1])) self.plot1.set_ylabel("Intensite") self.plot1.legend(loc="upper left") if self.EnMicron.get(): self.plot1.set_xlabel("micron") else : self.plot1.set_xlabel("pixel") self.initaffiche=False else : self.l1.set_data(x,y1) #self.canvas.draw() #------------------------------------------------- #Démarre le spectre: # * desactivation des boutons # la variable self.Acquisition donne l'état de l'acquisition #------------------------------------------------- def goSp(self,spectre): # os.chdir(self.WorkDir.get()) # Nom=self.Nom.get()+str(len(glob.glob(self.Nom.get()+"*"))) # self.FichSauve=Nom self.erreur.set('') self.goSpectre.configure(state=DISABLED) #tps d attente pour interroger le buffer en ms: #self.delay=50 if self.cam.shutter.absolute<0.05 else self.cam.shutter.absolute self.delay=50 time.sleep(0.2) self.iter=0 #print(self.delay) self.cam.start_capture() #self.cam.start_video() self.Acquisition=1 self.initaffiche=True def StopImage(self): if self.Acquisition: self.Acquisition=0 time.sleep(0.1) self.cam.stop_capture() print("Stop") self.goSpectre.configure(state=NORMAL) def Quitte(self): if self.Acquisition: self.Acquisition=0 time.sleep(self.delay*2e-3) self.cam.close() self.master.destroy() #-------------------------------------------------------------- # une frame pour les commandes #-------------------------------------------------------------- def FrCommandes(self,mere): frame=Frame(mere, bd=3,pady=2) # frame.pack() col=0 ligne=0 # ---------------- 4 boutons self.goSpectre=Button(frame, text="Démarre",command=lambda : self.goSp(1)) self.goSpectre.grid(row=ligne,column=col) ttk.Button(frame, text="Stop",command= lambda: self.StopImage())\ .grid(row=ligne,column=col+1) ttk.Button(frame, text="Enr.",command= self.EnregistreSP)\ .grid(row=ligne,column=col+2) Button(frame, text="Close", command= lambda: self.Quitte())\ .grid(row=ligne,column=col+4) ligne+=1;col=0 ttk.Separator(frame,orient='horizontal').grid(row=ligne,columnspan=5,sticky='EW') #------------- options fit gausse et autoscale : ligne+=1;col=0 Checkbutton(frame,variable=self.fitgauss,text="Fit Waist")\ .grid(row=ligne,column=col) Entry(frame,textvariable=self.w0,width=4).grid(row=ligne,column=col+1) Checkbutton(frame,variable=self.InitGraph,text="AutoSc.")\ .grid(row=ligne,column=col+2) # --- pixel/microns ligne+=1;col=0 ttk.Separator(frame,orient='horizontal').grid(row=ligne,columnspan=5,sticky='EW') ligne+=1;col=0 Label(frame,text="f(mm)=").grid(row=ligne,column=col,sticky='E') Entry(frame,textvariable=self.focale,width=4).grid(row=ligne,column=col+1,sticky='W') Checkbutton(frame,variable=self.EnMicron,text="Pix/mic.")\ .grid(row=ligne,column=col+2) #-------------- les bornes ligne+=1;col=0 ttk.Separator(frame,orient='horizontal').grid(row=ligne,columnspan=5,sticky='EW') ligne+=1 lesbornes=LabelFrame(frame,text="Bornes en Pixel",bd=3,pady=4) lesbornes.grid(row=ligne,column=col,columnspan=5,sticky='EW') Checkbutton(lesbornes,text="Borne",variable=self.borner).pack(side=LEFT) Entry(lesbornes,textvariable=self.Pix0,width=4).pack(side=LEFT) Entry(lesbornes,textvariable=self.Pix1,width=4).pack(side=LEFT) #-------------------- erreurs/info ligne+=1;col=0 # Label(frame,textvariable=self.FichRef).grid(row=ligne,column=col,columnspan=5) Label(frame,textvariable=self.erreur).grid(row=ligne+1,column=col,columnspan=4) return frame #-------------------------------------------------------------- def EnregistreSP(self): os.chdir(self.WorkDir.get()) Nom=self.Nom.get()+str(len(glob.glob(self.Nom.get()+"*"))) Nom+=".sp" print("Fichier sauvé sur "+Nom+" dans "+self.WorkDir.get()) if self.EnMicron.get(): entete="Microns Intensite" else : entete="Pixel Intensite" np.savetxt(Nom,self.data,fmt="%g",header=entete) return 1
F201B = 2892819673575448 guidCam = F201B cam = Camera(guid=guidCam) lemodel = cam.model.decode()[-5:] w0 = 80 bs = BS.BabSoleil() cam.start_capture() Npts = 70 step = 0.001 AngleStart = 0.21 data = np.zeros((Npts, 2)) bs.VaLbda(AngleStart) time.sleep(0.2) for i in range(Npts): #capture une frame et la copy dans CamArray bs.VaLbda(AngleStart + i * step) time.sleep(0.2) cam.start_one_shot() frame = cam.dequeue() CamArray = frame.copy() frame.enqueue() cam.stop_one_shot() # on recupere les info de la camera data[i, 0] = AngleStart + i * step data[i, 1] = CamArray.sum() np.savetxt("BS_find_L0_zero", data)
print("Available modes", cam0.modes) print("Available features", cam0.features) #those can be automated, the other are manual try: cam0.brightness.mode = 'auto' cam0.exposure.mode = 'auto' cam0.white_balance.mode = 'auto' except AttributeError: # thrown if the camera misses one of the features pass for feat in cam0.features: print("%s (cam0): %s" % (feat,cam0.__getattribute__(feat).value)) #choose color mode print(cam0.modes) cam0.mode = cam0.modes[0] cam0.start_capture() cam0.start_one_shot() matrix = cam0.dequeue() print("Shape:", matrix.shape) i = Image.fromarray(matrix.copy()) matrix.enqueue() cam0.stop_one_shot() cam0.stop_capture() i.save("t.bmp") i.show()
class CameraPolar: """ Cartographie de la polarisation import ClassAcquisitionStokesCamera;cp=ClassAcquisitionStokesCamera.CameraPolar() 1- pensez aux zero du BS et lame l/4 2- cp.bs.VaLbda(valeur) 3-cp.Mesure(attente=1,fich="lesimages",Npas=37,pas=10) => enregistre les Npas+1 images pour l'angle de la L/4 entre 0, pas, 2*pas ,... Npas*pas """ def __init__(self): F201B = 2892819673575448 # ------ Initialisation de la camera self.cam0 = Camera(guid=F201B) #lemodel=cam.model.decode()[-5:] self.l4 = PR50CC.rotation() print("L4 ok") self.bs = BS.BabSoleil() print("BS ok") self.LHcamera() print("LxH={}x{}".format(self.Larg, self.Haut)) def __repr__(self): """ Affichage cool """ reponse = "-" * 20 + "\n" reponse += "*----- Lambda/4:\n" reponse += "cp.l4.VaPos(-64.15);cp.l4.Zero() \n" reponse += "*---- Babinet soleil\n" reponse += "Pos. en fraction de lbda: cp.bs.VaLbda(0.) \n" reponse += " Pour initialiser 0lbda et 1lbda du BS, voir BS.py \n" reponse += "cp.bs.Litpos()= {0} mm \n".format(self.bs.LitPos()) reponse += "cp.bs.LitLbda()= {} Lbda \n".format(self.bs.LitLbda()) reponse += "0lambda (cp.bs.ZeroLbda): {}\n".format(self.bs.ZeroLbda) reponse += "1lambda (cp.bs.UnLbda): {}\n".format(self.bs.UnLbda) reponse += "LxH= <--cp.LHcamera(): " reponse += "-" * 20 + "\n" reponse += 'Usage: \n cp.Mesure(attente=1,fich="lesimages",Npas=37,pas=10)' reponse += "Traitement des data via: ClassTraitStokesCamera" return reponse def LHcamera(self): """ return hauteur et largeur """ self.cam0.start_capture() self.cam0.start_one_shot() frame = self.cam0.dequeue() (self.Larg, self.Haut) = frame.shape frame.enqueue() self.cam0.stop_one_shot() self.cam0.stop_capture() return (self.Larg, self.Haut) def Mesure(self, attente=1, fich="lesimages", Npas=37, pas=10): self.l4.VaPos(0) self.LHcamera() # on verifie hauteur et largeur #-- initialisation des data: empilement des images #ds un tableau 3D lesdata=np.zeros((Npas+1,self.Larg,self.Haut),\ dtype=np.uint16) Npixel = self.Larg * self.Haut # ------ informations dans le premier tableau #parametres de Stokes de l'entree phase = self.bs.LitLbda() * 2 * np.pi #phase du BS lesdata[0, 0, 0] = 65535 #S0 lesdata[0, 1, 0] = int((1 + np.cos(phase)) * 32767) #S1 lesdata[0, 2, 0] = 0 #S2 lesdata[0, 3, 0] = int((1 + np.sin(phase)) * 32767) #S3 #pas en "degree x 10" lesdata[0, 0, 1] = int(pas * 10) #phase BS en frac de lambda lesdata[0, 0, 2] = int(self.bs.LitLbda() * 65535) # ----- on démarre l'acquisition : self.cam0.start_capture() for i in range(Npas): x = self.l4.VaPos(pas * i) sleep(attente) self.cam0.start_one_shot() frame = self.cam0.dequeue() lesdata[i + 1, :, :] = frame.copy() Nsat = (lesdata[i + 1, :, :] > 50000).sum() print( "Frame number ({}) has {:.2f}% ({}pixels) au dessus de 50 milles " .format(i + 1, Nsat / Npixel * 100, Nsat)) if Nsat > 0.05 * Npixel: print("!!! Attention {} pixels au dessus de 50 milles ".format( Nsat)) self.cam0.stop_one_shot() self.cam0.stop_capture() exit(0) frame.enqueue() self.cam0.stop_one_shot() self.cam0.stop_capture() np.save(fich, lesdata) #comme on a fait un tour, on peut remettre la L/4 à zero : self.l4.VaPos(360) self.l4.Zero() return 1