alliedvision / VimbaPython

Old Allied Vision Vimba Python API. The successor to this API is VmbPy
BSD 2-Clause "Simplified" License
93 stars 40 forks source link

need to reconect to the camera evry time #144

Closed shaizae10 closed 1 year ago

shaizae10 commented 1 year ago

hi thare there is example of my code:

import numpy as np from matplotlib import pyplot as plt

from HW.vimba import Vimba

class Sensor:
    def __init__(self, exptier_time: float = 0.3):
        self.frame = None
        vimba = Vimba()
        with vimba.get_instance() as vimba:
            self.camera = vimba.get_all_cameras()
            if len(self.camera) == 1:
                self.camera = self.camera[0]
                with self.camera as cam:
                    self.features = cam.get_all_features()[0]
            elif len(self.camera) == 0:
                raise IOError("camera didn't detected")
            else:
                pass # todo: crate GUI to ask user whitch camera
            print()

    def initialize_communication(self):
        pass

    def take_single_measurement(self) -> np.ndarray:

        self.frame = self.camera.get_frame()

        # frame.
        return self.frame

if __name__ == "__main__":
    exampel = Alvium1800()
    frame = exampel.take_single_measurement()
    plt.imshow(frame)
    plt.show()

evry time it is show me the next error RuntimeError: Called 'Camera.get_frame()' outside of 'with' - statement scope. there is any wey that i can take frame any time whit out the need to reconecr to the camera?

shaizae10 commented 1 year ago

somting get rong whit the copy of the data

Teresa-AlliedVision commented 1 year ago

Hello, please edit the code to show the right format, for Python the formatting shows context, so it is necessary. Try copying the code first, then select the text and then click the <> button. Other than that, please make sure that the camera.get_frame() command is within the with statement of Vimba, like for example so:

def take_single_measurement(self)->np.ndarray:
    with Vimba.get_instance():
        with self.camera:
            frame = self.camera.get_frame()

For reference on how to manage the with statement, you can also look at the code, that Niklas did in this issue: #143 That code is for a different method of acquiring frames, so it is not an example you can use directly, but just to see how to structure the context with the with statement.

  def _stream_frames(self):
      with vimba.Vimba.get_instance():
          with self.camera:
              self._stop_streaming_event.clear()
              _handler = FrameHandler(self.handler)
              self.camera.start_streaming(_handler)
              self._stop_streaming_event.wait()
              self.camera.stop_streaming()
shaizae10 commented 1 year ago

tenx i'll fix the code :) there is no option to connect to the camera and then just read the buffer? i have no need to live streem

Teresa-AlliedVision commented 1 year ago

Yes, of course you don't need to stream, you can keep with synchronous acquisition with get_frame() to get one single image when you need it. Just add the with Vimba.get_instance() .... part to your code. The second example from Niklas is only for the structure of the def, not for the actual code. Because the example from Niklas uses asynchronous acquisition/streaming. I'm not sure what you mean with reading the buffer. Synchronous acquisition functions like this:

For an easier example, please use synchronous_grab.py to see an implementation of getting frames synchronously (as you are doing right now).