How do you close a VIN driver?

Hi,
I’m trying to grab an image using the Python API of CVB and I’m relatively new to Python. So my code below grabs 11 images and views the last one. It works so far… only once:

Afterwards the following error appears:

RuntimeError: failed to start acquisition with G2Grab

So what is happening? My guess is that my device is still open after the first run…

import cvb
import os
import matplotlib.pyplot as plt

path = "%CVB%/Drivers/GenICam.vin"
path_exp = os.path.expandvars(path)

device = cvb.DeviceFactory.open(path_exp)
stream = device.stream
stream.start()
for x in range(0,10):
    [img, status] = stream.wait_for(3000)  #we just need the last image
stream.stop()
matImg = cvb.as_array(img)
plt.imshow(matImg.transpose(), cmap="gray")

Hi @guest24,

so it did work the first time? It partly does look like a problem we had in :cvb: < 13.00.005. If you start your script via

python YourScript.py

the process is ended and the vin will be closed anyways.

Actually I’m working with Spyder. To run the script I would click on ‘Run File’ which would run the script

Mmh, maybe Spyder runs the script inside its process.

You could del the device and stream to unload the driver (both must be deleted):

import cvb
import os
import matplotlib.pyplot as plt

path = os.path.expandvars('%CVB%Drivers/GenICam.vin')

device = cvb.DeviceFactory.open(path)
stream = device.stream
stream.start()
for x in range(0,10):
    [img, status] = stream.wait_for(3000)  #we just need the last image
stream.stop()
matImg = cvb.as_array(img)
plt.imshow(matImg.transpose(), cmap="gray")

del stream
del device

Alternatively you can also use the with statement:

import cvb
import os
import matplotlib.pyplot as plt

PATH = os.path.expandvars('%cvb%drivers/genicam.vin')
with cvb.DeviceFactory.open(PATH) as device, device.stream as stream:
    stream.start()
    IMAGES = [stream.wait()[0] for i in range(0, 10)]
    stream.stop()

    LAST_IMAGE = IMAGES[-1]
    MAT_IMAGE = cvb.as_array(LAST_IMAGE).transpose()
    plt.imshow(MAT_IMAGE, cmap='gray')