Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Possible ways to speed up Cam2D_Snapshot

#1
Hi there,

I was trying to implement a camera live stream viewer, which reads the image with 'Cam2D_Snapshot' function and show it with 'cv::imshow'. My code is like
Code:
cam_item = RDK.Item(CAM_NAME)
bytes_img = RDK.Cam2D_Snapshot("", cam_item, "")
nparr = np.frombuffer(bytes_img, np.uint8)
img_socket = cv.imdecode(nparr, cv.IMREAD_COLOR)


But it turned out that 'Cam2D_Snapshot' function took around 200~400 ms, is there anything I can do to speed up the snapshot in order to increase the FPS?
#2
The speed highly depends on the size of the image you are retrieving.
What is the resolution of the images?
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#3
Forgot to mentioned the size of the image is 640*480
#4
You should be able to have a 60 FPS feed with this resolution. Are you doing any other processing to the image in your Python code?
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#5
Actually, it is the function "Cam2D_Snapshot" which is a bit slow, the processing of the images is not relevant to this issue.

I realized that having the preview window opened will help to reduce the execution time of "Cam2D_Snapshot" to 70~120 ms. But can it be even faster?

I have attached the .rdk file and the python script

.rdk   scene_with_camera.rdk (Size: 278.25 KB / Downloads: 101)

.py   server.py (Size: 1.47 KB / Downloads: 81)

You could run following commands to reproduce the results

> RoboDK scene_with_camera.rdk
> python3 server.py
#6
For optimal results, undock the window, used fixed window size, and minimize the preview window.
You can do so in the API using:

Code:
    # Connect to the running instance of RoboDK (make sure it's already running)
    logging.info("Connecting...")
    RDK = Robolink()
    RDK.Connect()
    logging.info("Connected...")

    CAM_NAME = 'StaticCamera'
    static_camera = RDK.Item(CAM_NAME)

    # Optimal settings: undocked, minimized, fixed sensor size
    RDK.Cam2D_SetParams("SIZE=640x480 WINDOWFIXED MINIMIZED", static_camera)
    static_camera.setParam('Open', 1)

    for i in range(1,20):
        start_time = time.time()
        logging.info("Taking snapshot...")
        bytes_img = RDK.Cam2D_Snapshot("", static_camera)
        end_time = time.time()
        logging.info("Snapshot took %.6f seconds", (end_time - start_time))

I have around 12 to 25 ms.

More examples in the docs:
https://robodk.com/doc/en/PythonAPI/exam...#camera-2d
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#7
(10-16-2023, 07:56 PM)Sam Wrote: For optimal results, undock the window, used fixed window size, and minimize the preview window.
You can do so in the API using:

Code:
    # Connect to the running instance of RoboDK (make sure it's already running)
    logging.info("Connecting...")
    RDK = Robolink()
    RDK.Connect()
    logging.info("Connected...")

    CAM_NAME = 'StaticCamera'
    static_camera = RDK.Item(CAM_NAME)

    # Optimal settings: undocked, minimized, fixed sensor size
    RDK.Cam2D_SetParams("SIZE=640x480 WINDOWFIXED MINIMIZED", static_camera)
    static_camera.setParam('Open', 1)

    for i in range(1,20):
        start_time = time.time()
        logging.info("Taking snapshot...")
        bytes_img = RDK.Cam2D_Snapshot("", static_camera)
        end_time = time.time()
        logging.info("Snapshot took %.6f seconds", (end_time - start_time))

I have around 12 to 25 ms.

More examples in the docs:
https://robodk.com/doc/en/PythonAPI/exam...#camera-2d

I have tried your approach and had FPS around 30Hz.

But I have got 2 questions still:

- By undocking the window, we would probably need to manually open the preview window, right? Is there a method in the API or parameters which we can set, to open the preview window automatically?

- And what exactly is "static_camera.setParam('Open', 1)" doing? Is it really necessary?
#8
It seems you have answered your own question.

"static_camera.setParam('Open', 1)" opens the preview window.
Please read the Forum Guidelines before posting!
Find useful information about RoboDK by visiting our Online Documentation.
#9
(10-17-2023, 11:52 AM)Sam Wrote: It seems you have answered your own question.

"static_camera.setParam('Open', 1)" opens the preview window.

Okkk, now it makes much more sense to me. So actually I should use "SetParam('Open', 1)" to open the preview window first and then call "RDK.Cam2D_Snapshot()" to set the parameters like image resolution, right?

I was following the python code snippet in the previous message (which called Cam2D_Snapshot first and then SetParam('Open', 1) ), and ended up only getting small image (100*75).
  




Users browsing this thread:
1 Guest(s)