簡體   English   中英

如何使聯合視覺相機對象成為全局變量?

[英]how to make allied vision camera object a global variable?

我使用聯合視覺相機,我需要非常頻繁地獲得單幀,但我發現它需要超過 1 秒才能找到相機並獲得單幀。 請查看此代碼(從pymba編輯)

from pymba import Vimba, VimbaException
from examples.camera._display_frame import display_frame
def capture_single():
    with Vimba() as vimba:
         camera = vimba.camera(0)
         camera.open()
         camera.arm('SingleFrame')
         for i in range(10):
             try:
                 frame = camera.acquire_frame()
                 print(frame)
            
             except VimbaException as e:
                 # rearm camera upon frame timeout
                 if e.error_code == VimbaException.ERR_TIMEOUT:
                    print(e)
                    camera.disarm()
                    camera.arm('SingleFrame')
                 else:
                    raise
    camera.disarm()
    camera.close()
    return frame


   
  if __name__ == '__main__':                     
    for i in range(10):
        frame = capture_single()

            

在主體中,第一個循環非常慢,但函數 capture_single() 中的循環非常快。 那么如何將camera = vimba.camera(0)作為全局變量,所以在我自己的程序中,如果我想在不同的函數中獲取幀,我只需直接快速使用frame = camera.acquire_frame()而不是找到相機並打開相機?

根據您的用例,您可以在方法之外聲明變量,也可以創建一個包裝此行為的類(通常首選后者)。

如果這只是一個一次性腳本,除了單次拍攝之外實際上沒有很多組件,您可以簡單地使用

with Vimba() as vimba:
    # Not sure if you have to call camera.open() or arm() in every frame. If so, move it accordingly.
    camera = vimba.camera(0)
    camera.open()
    camera.arm('SingleFrame')

def capture_single():
     for i in range(10):
         try:
             frame = camera.acquire_frame()
             print(frame)
            
         except VimbaException as e:
             # rearm camera upon frame timeout
             if e.error_code == VimbaException.ERR_TIMEOUT:
                print(e)
                camera.disarm()
                camera.arm('SingleFrame')
             else:
                raise
    camera.disarm()
    camera.close()
    return frame

否則,最好定義一個類來包裝它,即

class CameraInteractor:
    def __init__(self):
        with Vimba() as vimba:
            self.camera = vimba.camera(0)
            self.camera.open()
            self.camera.arm("SingleFrame")
            
    def capture_single(self):
        for i in range(10):
            try:
                frame = self.camera.acquire_frame()
                print(frame)

            except VimbaException as e:
                # rearm camera upon frame timeout
                if e.error_code == VimbaException.ERR_TIMEOUT:
                    print(e)
                    self.camera.disarm()
                    self.camera.arm('SingleFrame')
                else:
                    raise
        # Note: Not sure if you actually want to disarm the camera upon calling capture_single.
        # Perhaps make another method within the class, i.e. def disarm(self) that does this.
        self.camera.disarm()
        self.camera.close()
        return frame

然后,你可以使用

camera = CameraInteractor()
for i in range(10):
    frame = camera.capture_single()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM