Posts: 6
Threads: 1
Joined: Oct 2023
Reputation:
0
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?
Posts: 326
Threads: 2
Joined: Jun 2021
Reputation:
25
The speed highly depends on the size of the image you are retrieving.
What is the resolution of the images?
Posts: 6
Threads: 1
Joined: Oct 2023
Reputation:
0
Forgot to mentioned the size of the image is 640*480
Posts: 326
Threads: 2
Joined: Jun 2021
Reputation:
25
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?
Posts: 6
Threads: 1
Joined: Oct 2023
Reputation:
0
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
scene_with_camera.rdk (Size: 278.25 KB / Downloads: 267)
server.py (Size: 1.47 KB / Downloads: 247)
You could run following commands to reproduce the results
> RoboDK scene_with_camera.rdk
> python3 server.py
Posts: 326
Threads: 2
Joined: Jun 2021
Reputation:
25
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
Posts: 6
Threads: 1
Joined: Oct 2023
Reputation:
0
(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?
Posts: 326
Threads: 2
Joined: Jun 2021
Reputation:
25
It seems you have answered your own question.
"static_camera.setParam('Open', 1)" opens the preview window.
Posts: 6
Threads: 1
Joined: Oct 2023
Reputation:
0
(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).
|