Go to Post Maybe this is something we should put on the backs of our shirts: "beware - this person will stand up and cheer for other teams during FIRST award ceremonies". - Andy Baker [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 24-01-2017, 22:25
MuskieProgramme MuskieProgramme is offline
Registered User
FRC #6420
Team Role: Programmer
 
Join Date: Dec 2016
Rookie Year: 2014
Location: Muscatine, IA
Posts: 36
MuskieProgramme is an unknown quantity at this point
Switching Camera Views

I would like to know if it is possible to change the current camera display programmatically. What I would like to achieve as the end result is having two cameras, one forward, one backward, that switch based on the direction the robot is currently moving (with a manual override).

I currently am able to manually change the view by selecting the desired view from the drop-down menu on the dashboard, and may display both simultaneously if I wish.
Reply With Quote
  #2   Spotlight this post!  
Unread 24-01-2017, 22:39
dmelcer9 dmelcer9 is offline
Registered User
AKA: Daniel
FRC #0810 (Mechanical Bulls)
Team Role: Leadership
 
Join Date: Dec 2015
Rookie Year: 2012
Location: Smithtown
Posts: 51
dmelcer9 is an unknown quantity at this point
Re: Switching Camera Views

You can do something like this (edited from the screensteps page):

Code:
public void robotInit() {
            new Thread(() -> {
                UsbCamera camera1 = CameraServer.getInstance().startAutomaticCapture(0);
                camera1.setResolution(640, 480);
                UsbCamera camera2 = CameraServer.getInstance().startAutomaticCapture(1);
                camera2.setResolution(640, 480);
                
                CvSink cvSink1 = CameraServer.getInstance().getVideo(camera1);
                CvSink cvSink2 = CameraServer.getInstance().getVideo(camera2);
                CvSource outputStream = CameraServer.getInstance().putVideo("Switcher", 640, 480);
                
                Mat image = new Mat();
                
                while(!Thread.interrupted()) {
                    if(/*switcher button logic*/){
                      cvSink1.grabFrame(image);
                    } else{
                      cvSink2.grabFrame(image);
                    }
                    
                    outputStream.putFrame(image);
                }
            }).start();
    }
I'm tired, so this is all I can think of now. The code may not be entirely right, but the general idea should work. There probably is a much better/simpler/more efficient way that I have no clue about.

Last edited by dmelcer9 : 24-01-2017 at 22:40. Reason: Code
Reply With Quote
  #3   Spotlight this post!  
Unread 28-01-2017, 16:14
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: Switching Camera Views

The above code gets you about 90% of the way their, but still runs into USB bandwidth issues. I modified the code slightly and now have switching cameras working very well, with the occasional error message, but nothing fatal.

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
  #4   Spotlight this post!  
Unread 28-01-2017, 18:17
MuskieProgramme MuskieProgramme is offline
Registered User
FRC #6420
Team Role: Programmer
 
Join Date: Dec 2016
Rookie Year: 2014
Location: Muscatine, IA
Posts: 36
MuskieProgramme is an unknown quantity at this point
Re: Switching Camera Views

When I attempt to add the switcher, I get a large number of errors and warnings. Something like unable to set FPS or unable to set resolution. It's really irritating, the errors go away when I comment out the switching code, but the dashboard still shows >30 FPS for both cameras, and higher resolution (even though my code sets the resolution and FPS lower).

I am using two Lifecam 3000s if it matters.
Reply With Quote
  #5   Spotlight this post!  
Unread 29-01-2017, 12:48
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: Switching Camera Views

On the dashboard are you streaming the Switcher? If you try to stream just the default USB cameras, you will get errors.
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 09:46.

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