Capturing a frame from a stream

Hi I have just got the CVBpy running and have run the discover_devices.py example
and I can see the timestamps as they are printed.
I would like to capture a frame from the stream and save it as a bmp/jpg etc.
I cannot find an example that does this I have looked through the forum and all the examples and cannot find this.
I see some people have used time delay getsnapshot() but there are no code examples to work from.

Basically I would like to be able to view the stream and then capture an image using some type of python code so I can then process the image with opencv.
Any help or links to examples I may have missed would be appreciated.
Thank you
Mark

Hi @Mark

if I understand that correctly you want to discover a device, get an image from it and forward it to OpenCV.

Discover:

import cvb 

interface_flags = cvb.DiscoverFlags.UpToLevelInterface | cvb.DiscoverFlags.IgnoreGevSD 
all_interfaces = cvb.DeviceFactory.discover_from_root(interface_flags)  

broadcast_flags = cvb.DiscoverFlags.IgnoreVins | cvb.DiscoverFlags.IncludeInaccessible | cvb.DiscoverFlags.IgnoreGevSD 

all_devices = [] 

for interface in all_interfaces: 
    cvb.DiscoveryInformation.set_genapi_feature(interface, "TLInterface", "DisableSubnetMatch", "1") 
    cvb.DiscoveryInformation.set_genapi_feature(interface, "TLInterface", "AllowBroadcastDiscoveryResponse", "1") 

    found_devices = cvb.DeviceFactory.discover_from_level(interface.access_token, broadcast_flags) 

    for dev in found_devices: 
        all_devices.append(dev)  

for dev in all_devices: 
    # Check devices access status etc.: 
    # dev.read_property(cvb.DiscoveryProperties.DeviceAccessStatus)  

with cvb.DeviceFactory.open(all_devices[0].access_token) as device: # open first camera found (check accessibility before!) 
    # work with camera... 

To get a CVB image into a numpy array you can use the following code:

import os  
import cvb    

image = cvb.Image(os.path.join(cvb.install_path(), "tutorial", "Clara.bmp"))    

m = cvb.as_array(image, copy=False)    

for y in range(len(m)):  
    for x in range(len(m[0])):  
        new_value = m[y, x] + 20  
        if new_value > 255:  
            new_value = 255  
        m[y, x] = new_value    

image.save(os.path.join(cvb.install_path(), "tutorial", "ClaraTest.bmp")) 

I hope this was what you needed.

Cheers
Chris

Hi Chris ,
thank you for the help. I can use the following code :
import os
import cvb

with cvb.DeviceFactory.open(
os.path.join(cvb.install_path(), “drivers”, “CVMock.vin”),
cvb.AcquisitionStack.Vin) as device:

stream = device.stream()
stream.start()

for i in range(10):
    image, status = stream.wait()
    if status == cvb.WaitStatus.Ok:
        print("Acquired image: " + str(i) + " | Timestamp: " + str(image.raw_timestamp))

stream.abort()

I need to take a single frame from this stream.
Then view it in like cv2.imshow()
and then save it I had seen the image.save() but how do I display and save the image that the image below?
print("Acquired image: " + str(i) + " | Timestamp: " + str(image.raw_timestamp))

Thank you
Mark

Hi @Mark

wait() takes a single frame from the stream.
As_array converts this image into a numpy array that you can then pass into OpenCV and do whatever processing you like (display, save, change).

Opening a saved image works as in my second code example.
You could also display this image with Matplotlib, which is ok for single images but not for streams.

Cheers
Chris

Thank you Chris , I believe that will get me started.
Mark

Hi Mark,

we also have some qml tutorials in the ImageManager subfolder, as far as I am aware, there are ways to get an application running with QML and Python as well allowing you to use our CVBDisplay.

This however only makes sense, once you want to display a live stream of the camera and not just sinlge images.

For now I think you should be good with OpenCV for displaying the images (arrays) or simply matplotlib.

Thanks Chris,
I will need to display the live stream as well so I will take a look at this .
Many Thanks
Mark

Hi Mark,

%cvb%Tutorial\Image Manager\CVBpy\QmlStreamDisplay

should be exactly what you need.

Hi Chris ,
I have managed to do everything needed to get my project started.
I did not realize that CVBpy is a full featured machine vision toolbox , so I may be able to do everything with CVBpy rather than opencv.
Again many thanks as I was stalled for the foreseeable future.
Mark

Hi @Mark ,

glad to hear that, if there is something missing with CVBPy you can still push your imagedata into OpenCV.
The great advantage is, that you are not bound to one manufacturers SDK for image acquisition with CVB as long as the camera is GenApi compliant you can access it.

Cheers
Chris