Go to Post If they want my business, then they should act accordingly. If they don't, I am quite happy to take my business (and that of my $16.5 billion organization) elsewhere. - dlavery [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 07-02-2017, 21:33
thecoopster20 thecoopster20 is offline
4th Year Programmer - Java
FRC #3602 (Robomos)
Team Role: Programmer
 
Join Date: Mar 2016
Rookie Year: 2014
Location: Escanaba, MI
Posts: 27
thecoopster20 is an unknown quantity at this point
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();
Reply With Quote
  #2   Spotlight this post!  
Unread 08-02-2017, 08:15
ajsmith's Avatar
ajsmith ajsmith is offline
Captain, Programming, Build
AKA: Andy Smith
FRC #5546 (A.R.T.)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2014
Location: Petersburg, VA
Posts: 10
ajsmith is a jewel in the roughajsmith is a jewel in the roughajsmith is a jewel in the rough
Re: More Than Two USB Cameras??

Quote:
Originally Posted by thecoopster20 View Post
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.

Last edited by ajsmith : 08-02-2017 at 08:48. Reason: Fixed problem.
Reply With Quote
  #3   Spotlight this post!  
Unread Yesterday, 20:44
Caleb Sykes's Avatar
Caleb Sykes Caleb Sykes is offline
Registered User
FRC #4536 (MinuteBots)
Team Role: Mentor
 
Join Date: Feb 2011
Rookie Year: 2009
Location: St. Paul, Minnesota
Posts: 1,076
Caleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond reputeCaleb Sykes has a reputation beyond repute
Re: More Than Two USB Cameras??

Quote:
Originally Posted by ajsmith View Post
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?"
Reply With Quote
  #4   Spotlight this post!  
Unread Yesterday, 21:40
ajsmith's Avatar
ajsmith ajsmith is offline
Captain, Programming, Build
AKA: Andy Smith
FRC #5546 (A.R.T.)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2014
Location: Petersburg, VA
Posts: 10
ajsmith is a jewel in the roughajsmith is a jewel in the roughajsmith is a jewel in the rough
Re: More Than Two USB Cameras??

Quote:
Originally Posted by Caleb Sykes View Post
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.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 01:40.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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