Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   More Than Two USB Cameras?? (http://www.chiefdelphi.com/forums/showthread.php?t=154894)

5854RoboProgram 07-02-2017 14:39

More Than Two USB Cameras??
 
Hello FRC programming community,
I am the lead programmer on team 5854, this is the team's second year and my first as lead programmer.

I am using usb cameras to get images to my vision system. I have two cameras hooked up to the usb slots on the roborio and I am using a custom class I made using the built in CameraServer library. (Code Example below) I was wondering if there is a way to make a third camera streaming service. Or if there would be a problem using a usb splitter because I'm not sure how the rio will handle it.


Thnak you in advance.
And sorry if the text is confusing or misspelled, I'm dyslexic.

Code on how I'm making the camera server:

Code:

usbCamera = new UsbCamera("USB Camera 0", cameraId);
usbCamera.setResolution(640, 400);
usbCamera.setBrightness(1);

mjpegServer1 = new MjpegServer("serve_USB Camera 0", 1181);

mjpegServer1.setSource(usbCamera);
           
cvSink = new CvSink("opencv_USB Camera 0");

cvSink.setSource(usbCamera);

outputStream = new CvSource("Blur", PixelFormat.kMJPEG, 640, 480, 30);

mjpegServer2 = new MjpegServer("serve_Blur", 1182);

mjpegServer2.setSource(outputStream);


Bkeeneykid 07-02-2017 15:36

Re: More Than Two USB Cameras??
 
Using more than two cameras is certainly possibly. Don't use a "USB Splitter", instead just simply use a USB hub on the robot. It may get a little funky with too many because of current draw. Once plugged in, they should simply show up on the roboRIO dashboard, and you should be able to create another CameraServer class.

Joe Ross 07-02-2017 19:58

Re: More Than Two USB Cameras??
 
We've used 3 HD-3000's. You have to be careful with the resolution and fps, because you can run into USB bandwidth issues.

thecoopster20 07-02-2017 21:33

Re: More Than Two USB Cameras??
 
I highly recommend using something along these lines to make sure that you are only ever streaming the camera you want, instead of wasting bandwidth by streaming multiple cameras at the same time.

Code:

public void robotInit() {
           
            Thread t = new Thread(() -> {
                   
                    boolean allowCam1 = false;
                   
                    UsbCamera camera1 = CameraServer.getInstance().startAutomaticCapture(0);
            camera1.setResolution(320, 240);
            camera1.setFPS(30);
            UsbCamera camera2 = CameraServer.getInstance().startAutomaticCapture(1);
            camera2.setResolution(320, 240);
            camera2.setFPS(30);
           
            CvSink cvSink1 = CameraServer.getInstance().getVideo(camera1);
            CvSink cvSink2 = CameraServer.getInstance().getVideo(camera2);
            CvSource outputStream = CameraServer.getInstance().putVideo("Switcher", 320, 240);
           
            Mat image = new Mat();
           
            while(!Thread.interrupted()) {
                   
                    if(oi.getGamepad().getRawButton(9)) {
                            allowCam1 = !allowCam1;
                    }
                   
                if(allowCam1){
                  cvSink2.setEnabled(false);
                  cvSink1.setEnabled(true);
                  cvSink1.grabFrame(image);
                } else{
                  cvSink1.setEnabled(false);
                  cvSink2.setEnabled(true);
                  cvSink2.grabFrame(image);   
                }
               
                outputStream.putFrame(image);
            }
           
        });
        t.start();


ajsmith 08-02-2017 08:15

Re: More Than Two USB Cameras??
 
Quote:

Originally Posted by thecoopster20 (Post 1641849)
I highly recommend using something along these lines to make sure that you are only ever streaming the camera you want, instead of wasting bandwidth by streaming multiple cameras at the same time.

Code:

public void robotInit() {
           
            Thread t = new Thread(() -> {
                   
                    boolean allowCam1 = false;
                   
                    UsbCamera camera1 = CameraServer.getInstance().startAutomaticCapture(0);
            camera1.setResolution(320, 240);
            camera1.setFPS(30);
            UsbCamera camera2 = CameraServer.getInstance().startAutomaticCapture(1);
            camera2.setResolution(320, 240);
            camera2.setFPS(30);
           
            CvSink cvSink1 = CameraServer.getInstance().getVideo(camera1);
            CvSink cvSink2 = CameraServer.getInstance().getVideo(camera2);
            CvSource outputStream = CameraServer.getInstance().putVideo("Switcher", 320, 240);
           
            Mat image = new Mat();
           
            while(!Thread.interrupted()) {
                   
                    if(oi.getGamepad().getRawButton(9)) {
                            allowCam1 = !allowCam1;
                    }
                   
                if(allowCam1){
                  cvSink2.setEnabled(false);
                  cvSink1.setEnabled(true);
                  cvSink1.grabFrame(image);
                } else{
                  cvSink1.setEnabled(false);
                  cvSink2.setEnabled(true);
                  cvSink2.grabFrame(image);   
                }
               
                outputStream.putFrame(image);
            }
           
        });
        t.start();


I've seen this code in multiple threads here but I have never been able to get it to work. We always end up with the "too many simultaneous client streams" error whenever we try to switch to the second camera (setting allowcam1 to true). Any ideas on why this isn't working for us?

EDIT: We just tried a new robot program with just this code and got it to work. If anyone else has this error, we had to change the source in smartdashboard to "switcher," then RESTART the smartdashboard and the robot, and then it would work. I think the problem was that since the smartdashboard had started off streaming the usb cam 0, it never stopped streaming after switching sources.

Caleb Sykes 09-02-2017 20:44

Re: More Than Two USB Cameras??
 
Quote:

Originally Posted by ajsmith (Post 1641935)
I've seen this code in multiple threads here but I have never been able to get it to work. We always end up with the "too many simultaneous client streams" error whenever we try to switch to the second camera (setting allowcam1 to true). Any ideas on why this isn't working for us?

EDIT: We just tried a new robot program with just this code and got it to work. If anyone else has this error, we had to change the source in smartdashboard to "switcher," then RESTART the smartdashboard and the robot, and then it would work. I think the problem was that since the smartdashboard had started off streaming the usb cam 0, it never stopped streaming after switching sources.

What do you mean when you say you have to change the source to "switcher?"

ajsmith 09-02-2017 21:40

Re: More Than Two USB Cameras??
 
Quote:

Originally Posted by Caleb Sykes (Post 1642767)
What do you mean when you say you have to change the source to "switcher?"

In SmartDashboard, the USB Camera Viewer widget has a source property. In the code above, it says "putVideo("Switcher", 320, 240);" so that is the new video source. The Java code automatically adds a "Camera 0" or "cam0" source for the webcam, but using that produces an error when switching cameras because you are still trying to stream that single camera rather than the special source that will switch between the two.


All times are GMT -5. The time now is 22:35.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi