Unable to record Video via cvb.movie2 [CVBPython on Nvidia AGX]

Hey all,

I have a Teledyne DALSA nano camera c1930. I am trying to recorde the video by using the sample code from here.

For this I have installed the cvb suit on Linux and I am able to capture images but unable to record the video

# CVBpy Example Script
 #
 # 1. Starts a stream from device
 # 2. Starts recording.
 # 3. Writes images to movie file.
 # 4. Stops recording.
 # 5. Stops the device's stream.
 #
 
 import os
 import cvb
 import cvb.movie2
 
 with cvb.DeviceFactory.open(os.path.join(cvb.install_path(), "drivers", "CVMock.vin"), port=0) as device:
 
     stream = device.stream
     stream.start()
 
     # Replay cvrv file with Common Vision Blox Viewer
     output_file = os.path.expanduser('~/Movie2ExampleVideo.cvrv')
 
     # Raw video settings
     raw_video_settings = cvb.movie2.RawVideoSettings()
     # Define pixel format
     pixel_format = cvb.movie2.RecorderPixelFormat.Color
     if len(device.device_image.planes) == 1:
         pixel_format = cvb.movie2.RecorderPixelFormat.Mono
     # Create the recorder
     with cvb.movie2.Recorder(output_file, device.device_image.size, pixel_format, raw_video_settings) as recorder:
 
         print("Start recording 100 frames:")
 
         for i in range(100):
             image, status = stream.wait()
             if status == cvb.WaitStatus.Ok:
                 # Write image to file
                 recorder.write(image) 
                 print(".", end="")
 
     stream.abort()
 
     print("\nMovie file written to: " + output_file)

and getting the following error when I try to run the above sample code:

ModuleNotFoundError: No module named 'cvb.movie2'

I have looked everywhere for help to install the movie2 API but there was no success. I don’tt know where I can find it, for this even I visited the stemmer imaging website but no success. Is it that I have to purchase the movie2 software or API ?

Last question, I know I can configure the camera from the GENICAM browser, which is good. However, Is it possible to configure the camera from the PYTHON API or not? I know we can, for instance, given the example code in, I can set the exposure time

/opt/cv/tutorial/ImageManager/cvbPy/DeviceConfiguration/deviceconfiguration.py.

But I would like to know is it possible to set advance configuration for the camera via CVBPython API. For example before I start to capture a video or image, I should be able to set some options or check whether the PTP is locked and synced etc… and such kind of configuration or only record if PTP is in slave mode and is atcive.

Any help would be appreciated.

Thanks,
Aftab

Hi @aftab

unfortunately Movie2 is not available under Linux systems, it is only available for Windows systems.

For your second question: Every node that you can configure via the GenICam Browser can be configured via code. You already posted the example that shows you how. Just replace “ExposureTime” with the name of the node you want to set.

Cheers,
Tim

1 Like

Hey @TStadler,

Thank you so much. Is there any page which contain such information or more like which API are supported by CVBPython etc…

Lastly, I need developer documentation where I can make use of it to make my own API to for example, check or set the PTP mode. I am doing a workaround by debugging and checking which values are there? If you have any idea about the documentation please do share with me.
Thanks again.

Regards,
Aftab

Hi @aftab,

an overview which tools are supported can be found here and also here.

Since each camera has different nodes, it really depends on what hardware you’re using for which nodes are available through code. Like I said before you can use the GenICam browser to check which nodes are available and also which values those nodes have. The documentation for your hardware can also help you.

Cheers,
Tim

1 Like

Thank you for sharing the links for tool.

Hey @TStadler ,

As you have mentioned that movie2 is only supported in windows and not Linux. Then, I thought to make use of OpenCV to make a video. Below is some code snippet for it .

import os
import cvb
import cv2

FPS = 25
# some other code .....

# But I am concerned with below code

# Below VideoWriter object will create 
# a frame and output  
# is stored in 'video.avi' file. 
vidName = os.path.join(getDir,'result.avi')
out = cv2.VideoWriter(vidName, cv2.VideoWriter_fourcc(*'DIVX'), FPS, (1920,1200)) 

# some other code .....
while True:
    try:
        with cvb.DeviceFactory.open(cvb.install_path() + "/drivers/GenICam.vin", port=0) as device:
                
            deviceConfiguration(device.node_maps["Device"])
            stream = device.stream

            if PtpModeIsActive(device.node_maps["Device"]):
                while True:
                    image, status = stream.get_snapshot()
                    if(status == cvb.WaitStatus.Ok):
                        np_image = cvb.as_array(image, copy=False)
                        # convert to rbg format
                        rgb_imag = cv2.cvtColor(np_image, cv2.COLOR_BGR2RGB)
                        # Write the frame into the 
                        # file 'result.avi' 
                        out.write(rgb_imag)
                else:
                    print('PTP Mode is not enabled and is not locked. Try Again.')

    except Exception as e:
            print(e)
    finally:
        # No mater what happens, release  
        # the video capture and video  
        out.release()
        stream.abort()

Now I have some questions:

Is it good idea to use OpenCV to save the video?

Lastly,When I run the code, I am getting the video, the size of video is increasing but sometime later the folder freezes. I don’t know why? You can check the screenshot below.

Furthermore, I am unable to playback the saved video. I am getting the following error:

error

Below is the metadata of the video file and size is 2.1 MB.

Any help would be appreciated.

Regards,
Aftab

Hi @aftab

I don’t have any working experience with the OpenCV video features but did spend some time with DirectShow back in the day… What you describe is similar to the situation you get when writing an AVI file with DirectShow which is - for whatever reason - not terminated correctly (hiccup, crash, whatever). AVI files have significant parts of their content description at the end of the file, so if the end is mangled you won’t see anything.

By the way: I do not see from the snipped how do break the two while loops that you wrote. Just to make sure: You do break them, sooner or later, right?

Hey @illusive,

Thank you for your valuable feedback. This was sample code, I made changes to it and only saving 100 frames and break out of both loops then. But I wanted to make 6 videos, which are 10 mints each I mean this code will run 1 hour. Is something then you can guide regarding this to make 10 minutes video

Hi @aftab

first I would recommend to separate the camera device initialization from the rest - you won’t need it interleaved into the video generating code. So open the device first, then perform your PTP check and if that was ok, you won’t need to worry about it any more later on.

For image acquisition I would strongly recommend not to use get_snapshot. get_snapshot has a disadvantage that might not be apparent at first: It initializes the streaming engine, takes one image and then stops and deinitializes the streaming engine. This whole init + teardown is time-consuming and you will notice that you are getting a much lower frame rate than what would be possible with your camera. The preferable approach - at least from a performance point of view - would be to start continuous acquisition:

import os
import cvb

with cvb.DeviceFactory.open(os.path.join(cvb.install_path(), "drivers", "CVMock.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()

(this takes 10 images, then closes the stream and device; your code for adding the frames to a video should go where the print statement is)

Once you have separated the initalization out you can generate your AVI files in a loop and then fill them in a nested loop.

Hey @illusive,

Thank you so much for your valuable feedback. I try to change the code, as I was worried about this get_snapshot function, and replaced my code with yours. Can you point me to some good developer documentations?

The problem for which I was not able to record the video was due to the fact that the horizontal and vertical offset were not set correctly. I adjusted for 1920 x 1080 these horizontal and vertical offset 32 and 92 respectively and than I was able to record video. I also tested it for 640 x 480 image with horizontal and vertical offset of 672 and 392 respectively. I don’t know who was the culprit, but I doubted it might be opencv2. But the problem is solved now.

Thank you so much for your feedback.

Regards,
Aftab

1 Like

define “good”… :slightly_smiling_face:
The only three locations I can actually point you to are the forum, the API documentation and the sample programs (and if I were new the product I would probably address them in reverse order).

Good job! :+1:

1 Like