2019-09-19 14:07:42 -04:00
|
|
|
package com.chameleonvision.vision.camera;
|
|
|
|
|
|
|
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-20 02:17:22 -04:00
|
|
|
import edu.wpi.cscore.*;
|
|
|
|
|
import edu.wpi.first.cameraserver.CameraServer;
|
|
|
|
|
import org.opencv.core.Mat;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
public class Camera {
|
|
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
private static double defaultFOV = 60.8;
|
|
|
|
|
|
|
|
|
|
public final String name;
|
|
|
|
|
public final String path;
|
|
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
final UsbCamera UsbCam;
|
2019-09-20 02:17:22 -04:00
|
|
|
private final VideoMode[] availableVideoModes;
|
2019-09-20 02:30:19 -04:00
|
|
|
|
|
|
|
|
private final CameraServer cs = CameraServer.getInstance();
|
2019-09-20 02:17:22 -04:00
|
|
|
private final CvSink cvSink;
|
2019-09-20 02:30:19 -04:00
|
|
|
private CvSource cvSource;
|
2019-09-20 02:17:22 -04:00
|
|
|
|
|
|
|
|
private double FOV;
|
|
|
|
|
|
|
|
|
|
private CameraValues camVals;
|
|
|
|
|
private CamVideoMode camVideoMode;
|
|
|
|
|
|
|
|
|
|
private int currentPipelineIndex;
|
2019-09-20 15:42:13 -04:00
|
|
|
private HashMap<Integer, Pipeline> pipelines;
|
2019-09-20 02:17:22 -04:00
|
|
|
|
|
|
|
|
public Camera(String cameraName) {
|
|
|
|
|
this(cameraName, defaultFOV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(UsbCameraInfo usbCamInfo) {
|
|
|
|
|
this(usbCamInfo, defaultFOV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(String cameraName, double fov) {
|
|
|
|
|
this(CameraManager.AllUsbCameraInfosByName.get(cameraName), fov);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(UsbCameraInfo usbCamInfo, double fov) {
|
2019-09-20 15:42:13 -04:00
|
|
|
this(usbCamInfo, fov, new HashMap<>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(String cameraName, double fov, HashMap<Integer, Pipeline> pipelines) {
|
|
|
|
|
this(CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(UsbCameraInfo usbCamInfo, double fov, HashMap<Integer, Pipeline> pipelines) {
|
2019-09-20 02:17:22 -04:00
|
|
|
FOV = fov;
|
|
|
|
|
name = usbCamInfo.name;
|
|
|
|
|
path = usbCamInfo.path;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
UsbCam = new UsbCamera(name, path);
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
this.pipelines = pipelines;
|
|
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
// set up video mode
|
|
|
|
|
availableVideoModes = UsbCam.enumerateVideoModes();
|
|
|
|
|
setCamVideoMode(new CamVideoMode(availableVideoModes[0]));
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
cvSink = cs.getVideo(UsbCam);
|
|
|
|
|
cvSource = cs.putVideo(name, camVals.ImageWidth, camVals.ImageHeight);
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public void setCamVideoMode(int videoMode) {
|
|
|
|
|
setCamVideoMode(UsbCam.enumerateVideoModes()[videoMode]);
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
private void setCamVideoMode(VideoMode videoMode) {
|
|
|
|
|
setCamVideoMode(new CamVideoMode(videoMode));
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:30:19 -04:00
|
|
|
private void setCamVideoMode(CamVideoMode newVideoMode) {
|
|
|
|
|
var prevVideoMode = this.camVideoMode;
|
|
|
|
|
this.camVideoMode = newVideoMode;
|
|
|
|
|
UsbCam.setPixelFormat(newVideoMode.getActualPixelFormat());
|
|
|
|
|
UsbCam.setFPS(newVideoMode.fps);
|
|
|
|
|
UsbCam.setResolution(newVideoMode.width, newVideoMode.height);
|
|
|
|
|
|
|
|
|
|
// update camera values
|
2019-09-20 02:17:22 -04:00
|
|
|
camVals = new CameraValues(this);
|
2019-09-20 02:30:19 -04:00
|
|
|
if ( prevVideoMode != null && prevVideoMode.width != newVideoMode.width && prevVideoMode.height != newVideoMode.height) { // if resolution changed
|
|
|
|
|
cvSource = cs.putVideo(name, newVideoMode.width, newVideoMode.height);
|
|
|
|
|
}
|
2019-09-20 02:17:22 -04:00
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
void addPipeline() {
|
2019-09-20 02:17:22 -04:00
|
|
|
addPipeline(pipelines.size());
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
private void addPipeline(int pipelineNumber) {
|
|
|
|
|
if (pipelines.containsKey(pipelineNumber)) return;
|
|
|
|
|
pipelines.put(pipelineNumber, new Pipeline());
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public Pipeline getCurrentPipeline() {
|
|
|
|
|
return pipelines.get(currentPipelineIndex);
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public int getCurrentPipelineIndex() {
|
|
|
|
|
return currentPipelineIndex;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
void setCurrentPipelineIndex(int pipelineNumber) {
|
2019-09-20 02:17:22 -04:00
|
|
|
if (pipelineNumber - 1 > pipelines.size()) return;
|
|
|
|
|
currentPipelineIndex = pipelineNumber;
|
|
|
|
|
}
|
|
|
|
|
public HashMap<Integer, Pipeline> getPipelines() {
|
|
|
|
|
return pipelines;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
CamVideoMode getVideoMode() {
|
2019-09-20 02:17:22 -04:00
|
|
|
return camVideoMode;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public int getVideoModeIndex() {
|
|
|
|
|
return IntStream.range(0, availableVideoModes.length)
|
|
|
|
|
.filter(i -> camVideoMode.isEqualToVideoMode(availableVideoModes[i]))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElse(-1);
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public double getFOV() {
|
|
|
|
|
return FOV;
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public void setFOV(double fov) {
|
|
|
|
|
FOV = fov;
|
|
|
|
|
camVals = new CameraValues(this);
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-09-20 02:17:22 -04:00
|
|
|
public int getBrightness() {
|
|
|
|
|
return getCurrentPipeline().brightness;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBrightness(int brightness) {
|
|
|
|
|
getCurrentPipeline().brightness = brightness;
|
|
|
|
|
UsbCam.setBrightness(brightness);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setExposure(int exposure) {
|
|
|
|
|
getCurrentPipeline().exposure = exposure;
|
|
|
|
|
UsbCam.setExposureManual(exposure);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long grabFrame(Mat image) {
|
|
|
|
|
return cvSink.grabFrame(image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long grabFrame(Mat image, double timeout) {
|
|
|
|
|
return cvSink.grabFrame(image, timeout);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void putFrame(Mat image) {
|
|
|
|
|
cvSource.putFrame(image);
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|
|
|
|
|
}
|