Skip to content

Instantly share code, notes, and snippets.

@PeterJohnson
Created November 17, 2016 06:56
Show Gist options
  • Save PeterJohnson/80b632fb54841536d4c83cfe31882e81 to your computer and use it in GitHub Desktop.
Save PeterJohnson/80b632fb54841536d4c83cfe31882e81 to your computer and use it in GitHub Desktop.
CameraServer API
class CameraServer : public ErrorBase {
public:
static constexpr uint16_t kBasePort = 1181;
static constexpr int kSize640x480 = 0;
static constexpr int kSize320x240 = 1;
static constexpr int kSize160x120 = 2;
public:
static CameraServer* GetInstance();
/**
* Start automatically capturing images to send to the dashboard.
*
* <p>You should call this method to see a camera feed on the dashboard.
* If you also want to perform vision processing on the roboRIO, use
* getVideo() to get access to the camera images.
*
* This overload calls {@link #startAutomaticCapture(int)} with device 0.
*/
cs::USBCamera StartAutomaticCapture();
/**
* Start automatically capturing images to send to the dashboard.
*
* @param dev The device number of the camera interface
*/
cs::USBCamera StartAutomaticCapture(int dev);
/**
* Start automatically capturing images to send to the dashboard.
*
* @param name The name to give the camera
* @param dev The device number of the camera interface
*/
cs::USBCamera StartAutomaticCapture(llvm::StringRef name, int dev);
/**
* Start automatically capturing images to send to the dashboard.
*
* @param name The name to give the camera
* @param path The device path (e.g. "/dev/video0") of the camera
*/
cs::USBCamera StartAutomaticCapture(llvm::StringRef name,
llvm::StringRef path);
/**
* Start automatically capturing images to send to the dashboard from
* an existing camera.
*
* @param camera Camera
*/
void StartAutomaticCapture(const cs::VideoSource& camera);
/**
* Get OpenCV access to the primary camera feed. This allows you to
* get images from the camera for image processing on the roboRIO.
*
* <p>This is only valid to call after a camera feed has been added
* with startAutomaticCapture() or addServer().
*/
cs::CvSink GetVideo();
/**
* Get OpenCV access to the specified camera. This allows you to get
* images from the camera for image processing on the roboRIO.
*
* @param camera Camera (e.g. as returned by startAutomaticCapture).
*/
cs::CvSink GetVideo(const cs::VideoSource& camera);
/**
* Create a MJPEG stream with OpenCV input. This can be called to pass custom
* annotated images to the dashboard.
*
* @param name Name to give the stream
* @param width Width of the image being sent
* @param height Height of the image being sent
*/
cs::CvSource PutVideo(llvm::StringRef name, int width, int height);
/**
* Adds a MJPEG server at the next available port.
*
* @param name Server name
*/
cs::MJPEGServer AddServer(llvm::StringRef name);
/**
* Adds a MJPEG server.
*
* @param name Server name
*/
cs::MJPEGServer AddServer(llvm::StringRef name, int port);
/**
* Adds an already created server.
*
* @param server Server
*/
void AddServer(const cs::VideoSink& server);
/**
* Removes a server by name.
*
* @param name Server name
*/
void RemoveServer(llvm::StringRef name);
/**
* Adds an already created camera.
*
* @param camera Camera
*/
void AddCamera(const cs::VideoSource& camera);
/**
* Removes a camera by name.
*
* @param name Camera name
*/
void RemoveCamera(llvm::StringRef name);
/**
* Sets the size of the image to use. Use the public kSize constants to set
* the correct mode, or set it directly on a camera and call the appropriate
* StartAutomaticCapture method.
*
* @deprecated Use SetResolution on the USBCamera returned by
* StartAutomaticCapture() instead.
* @param size The size to use
*/
void SetSize(int size);
};
public class CameraServer {
public static final int kBasePort = 1181;
public static final int kSize640x480 = 0;
public static final int kSize320x240 = 1;
public static final int kSize160x120 = 2;
/**
* Start automatically capturing images to send to the dashboard.
*
* <p>You should call this method to see a camera feed on the dashboard.
* If you also want to perform vision processing on the roboRIO, use
* getVideo() to get access to the camera images.
*
* This overload calls {@link #startAutomaticCapture(int)} with device 0.
*/
public USBCamera startAutomaticCapture() {
return startAutomaticCapture(0);
}
/**
* Start automatically capturing images to send to the dashboard.
*
* @param dev The device number of the camera interface
*/
public USBCamera startAutomaticCapture(int dev) {
USBCamera camera = new USBCamera("USB Camera " + dev, dev);
startAutomaticCapture(camera);
return camera;
}
/**
* Start automatically capturing images to send to the dashboard.
*
* @param name The name to give the camera
* @param dev The device number of the camera interface
*/
public USBCamera startAutomaticCapture(String name, int dev) {
USBCamera camera = new USBCamera(name, dev);
startAutomaticCapture(camera);
return camera;
}
/**
* Start automatically capturing images to send to the dashboard.
*
* @param name The name to give the camera
* @param path The device path (e.g. "/dev/video0") of the camera
*/
public USBCamera startAutomaticCapture(String name, String path) {
USBCamera camera = new USBCamera(name, path);
startAutomaticCapture(camera);
return camera;
}
/**
* Start automatically capturing images to send to the dashboard from
* an existing camera.
*
* @param camera Camera
*/
public void startAutomaticCapture(VideoSource camera) {
addCamera(camera);
VideoSink server = addServer("serve_" + camera.getName());
server.setSource(camera);
}
/**
* Get OpenCV access to the primary camera feed. This allows you to
* get images from the camera for image processing on the roboRIO.
*
* <p>This is only valid to call after a camera feed has been added
* with startAutomaticCapture() or addServer().
*/
public synchronized CvSink getVideo() {
if (m_cameras.isEmpty()) {
throw VideoException("no camera available");
}
return getVideo(m_cameras[0]);
}
/**
* Get OpenCV access to the specified camera. This allows you to get
* images from the camera for image processing on the roboRIO.
*
* @param camera Camera (e.g. as returned by startAutomaticCapture).
*/
public CvSink getVideo(VideoSource camera) {
CvSink sink = new CvSink("opencv_" + camera.getName());
sink.setSource(camera);
addServer(sink);
return sink;
}
/**
* Create a MJPEG stream with OpenCV input. This can be called to pass custom
* annotated images to the dashboard.
*
* @param name Name to give the stream
* @param width Width of the image being sent
* @param height Height of the image being sent
*/
public CvSource putVideo(String name, int width, int height) {
CvSource source = new CvSource(name, VideoMode.kMJPEG, width, height, 30);
startAutomaticCapture(source);
return source;
}
/**
* Adds a MJPEG server at the next available port.
*
* @param name Server name
*/
public synchronized MJPEGServer addServer(String name) {
return addServer(name, kBasePort + m_servers.size());
}
/**
* Adds a MJPEG server.
*
* @param name Server name
*/
public MJPEGServer addServer(String name, int port) {
MJPEGServer server = new MJPEGServer(name, port);
addServer(server);
return server;
}
/**
* Adds an already created server.
*
* @param server Server
*/
public synchronized void addServer(VideoSink server) {
m_sinks.put(server.getName(), server);
}
/**
* Removes a server by name.
*
* @param name Server name
*/
public synchronized void removeServer(String name) {
m_sinks.remove(name);
}
/**
* Adds an already created camera.
*
* @param camera Camera
*/
public synchronized void addCamera(VideoSource camera) {
m_sources.put(camera.getName(), camera);
}
/**
* Removes a camera by name.
*
* @param name Camera name
*/
public synchronized void removeCamera(String name) {
m_sources.remove(name);
}
/**
* Sets the size of the image to use. Use the public kSize constants to set the correct mode, or
* set it directly on a camera and call the appropriate startAutomaticCapture method.
*
* @deprecated Use setResolution on the USBCamera returned by startAutomaticCapture() instead.
* @param size The size to use
*/
@Deprecated
public synchronized void setSize(int size) {
if (m_cameras.isEmpty()) {
return;
}
switch (size) {
case kSize640x480:
m_cameras[0].setResolution(640, 480);
break;
case kSize320x240:
m_cameras[0].setResolution(320, 240);
break;
case kSize160x120:
m_cameras[0].setResolution(160, 120);
break;
default:
throw new IllegalArgumentException("Unsupported size: " + size);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment