alliedvision / VmbPy

Python API of the Vimba X SDK
BSD 2-Clause "Simplified" License
19 stars 7 forks source link

Compatibility problem with opencv-python ? #2

Open LanXtai opened 1 year ago

LanXtai commented 1 year ago

Hi ! I work on Windows 10, using python 3, and I ensured my version of opencv-python is up to date. I'm trying the minimal example given in VimbaX documentation to get a simple picture :

from vmbpy import *
import cv2
with VmbSystem.get_instance () as vmb:
    cams = vmb.get_all_cameras ()
    with cams [0] as cam:
        frame = cam.get_frame ()
        frame.convert_pixel_format(PixelFormat.Rgb8)
        print(frame)
        cv2.imwrite('frame.jpg', frame.as_opencv_image ())

However, all I get is the print : Frame(id=0, status=FrameStatus.Complete, buffer=0x2a7b34cd040), then an error :

ValueError      Traceback (most recent call last)
Cell In[15], line 8
      6 frame.convert_pixel_format(PixelFormat.Rgb8)
      7 print(frame)
----> 8 cv2.imwrite('frame.jpg', frame.as_opencv_image ())

File ~\AppData\Local\anaconda3\lib\site-packages\vmbpy\frame.py:614, in Frame.as_opencv_image(self)
    611 fmt = self._frame.pixelFormat
    613 if fmt not in OPENCV_PIXEL_FORMATS:
--> 614     raise ValueError('Current Format \'{}\' is not in OPENCV_PIXEL_FORMATS'.format(
    615                      str(PixelFormat(self._frame.pixelFormat))))
    617 return self.as_numpy_ndarray()

ValueError: Current Format 'Rgb8' is not in OPENCV_PIXEL_FORMATS

I tried using version 1.0.2 of vmbpy, but when importing the package I got another error, saying the version did not match the expected 1.0.0.

Using PixelFormat.Bgr8 in frame.convert_pixel didn't change anything to the situation, and I run out of options. Is there a way to fix this ?

Teresa-AlliedVision commented 1 year ago

Hi, the convert_pixel_format() function returns a new frame object. The original frame object remains unchanged, so it still has the same pixel format that the camera is set to.

Change the code like this and it should work:

from vmbpy import *
import cv2
with VmbSystem.get_instance () as vmb:
    cams = vmb.get_all_cameras ()
    with cams [0] as cam:
        frame = cam.get_frame ()
        new_frame = frame.convert_pixel_format(PixelFormat.Bgr8)

        print(frame)
        cv2.imwrite('frame.jpg', new_frame.as_opencv_image ())

The available Opencv pixel formats are the following (from the source code):

OPENCV_PIXEL_FORMATS = (
    PixelFormat.Mono8,
    PixelFormat.Bgr8,
    PixelFormat.Bgra8,
    PixelFormat.Mono16,
    PixelFormat.Bgr16,
    PixelFormat.Bgra16
)
LanXtai commented 1 year ago

Thank you very much, it worked well !