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

Using multiple cameras through the C# API

#1
Hello there,

I am trying to use the c# API to connect to multiple cameras but I am facing some issues.
The following line would always return false and ofcourse when removing the second parameter, the function does not know which camera to use so it always returns the image from the first camera.
RDK.Cam2DSnapshot(tempPath, cam_items[i].ItemId)

I was wondering if I am using this correctly or I overlooked something here?

Code:
using OpenCvSharp;
using RoboDk.API;
using RoboDk.API.Model;
using Mat = OpenCvSharp.Mat;

namespace RoboDKExample
{
   class Program
   {
       static void Main()
       {
           var RDK = new RoboDK();
           string[] CAM_NAMES = { "Camera 1", "Camera 2" };
           string CAM_PARAMS = "SIZE=640x480";
           string[] WINDOW_NAMES = { "My Camera Feed 1", "My Camera Feed 2" };

           IItem[] cam_items = { null, null };
           for (int i = 0; i < CAM_NAMES.Length; i++)
           {
               cam_items[i] = RDK.GetItemByName(CAM_NAMES[i], ItemType.Any);
               cam_items[i].SetParam("Open", "1");
           }

           // Create a live feed
           while (true)
           {
               bool isAnyCameraOpen = false;
               for (int i = 0; i < cam_items.Length; i++)
               {
                   if (cam_items[i].SetParam("isOpen") == "1")
                   {
                       isAnyCameraOpen = true;
                       // Method 2: Get the camera image, from disk
                       string tempPath = Path.GetTempPath() + $"/Camera{i}.png";
                       Mat img_png = null;
                       if (RDK.Cam2DSnapshot(tempPath, cam_items[i].ItemId))
                       {
                           img_png = Cv2.ImRead(tempPath);
                       }
                       // Show it to the world!
                       Cv2.ImShow(WINDOW_NAMES[i], img_png);
                   }
               }

               if (!isAnyCameraOpen)
               {
                   break;
               }

               int key = Cv2.WaitKey(1);
               if (key == 27)
               {
                   break;  // User pressed ESC, exit
               }
           }

           // Close the preview and the camera. Ensure you call cam_item.setParam("Open", "1") before reusing a camera!
           for (int i = 0; i < cam_items.Length; i++)
           {
               RDK.Cam2DClose();
           }
           Cv2.DestroyAllWindows();
       }
   }
}
#2
Did you experience this issue with one RDK project specifically or are you observing this issue with any RDK project you use?

Can you share the RoboDK file you are using?
#3
Hey Albert,

Thanks to the quick reply.

I actually tried the same code with python and it works perfectly. The issue I face here is mainly with the C# API.
Attached is also my rdk file.

Thanks alot
Mo.

(07-11-2023, 09:58 AM)Albert Wrote: Did you experience this issue with one RDK project specifically or are you observing this issue with any RDK project you use?

Can you share the RoboDK file you are using?


Attached Files
.rdk   2_cameras.rdk (Size: 2.04 MB / Downloads: 150)
#4
Thanks for these tips. We just updated the official RoboDK API on GitHub to allow taking snapshots using camera items as you are trying to do:
https://github.com/RoboDK/RoboDK-API
#5
Thanks alot for the fix. I will give it a try :)
#6
Works really nice. Would be nice if you would release a new nuget package with the fix though :) Thanks alot
#7
We just updated the latest version of the RoboDK API for C# to the official Nuget website:
https://www.nuget.org/packages/RoboDkApi/

The package is going through the validation process and it should be officially available in less than 1h.
#8
(07-20-2023, 11:30 AM)Albert Wrote: We just updated the latest version of the RoboDK API for C# to the official Nuget website:
https://www.nuget.org/packages/RoboDkApi/

The package is going through the validation process and it should be officially available in less than 1h.

Hey actually the new version still does not include the updated function. It does not match the git repo I think
#9
This update applies only to the official RoboDK API GitHub repository here:
https://github.com/RoboDK/RoboDK-API/tree/master/C%23

And the updated Nuget package I shared in my last message.

What issues do you see?
#10
(07-24-2023, 05:47 PM)Albert Wrote: This update applies only to the official RoboDK API GitHub repository here:
https://github.com/RoboDK/RoboDK-API/tree/master/C%23

And the updated Nuget package I shared in my last message.

What issues do you see?

Its strange, but I do not get the new function with the new release:

 bool Cam2DSnapshot(string fileSaveImg, IItem cam, string cameraParameters = "");
  




Users browsing this thread:
1 Guest(s)