Files
PhotonVision/Main/src/main/java/com/chameleonvision/vision/camera/Camera.java

205 lines
5.8 KiB
Java
Raw Normal View History

2019-09-19 14:07:42 -04:00
package com.chameleonvision.vision.camera;
2019-10-04 15:55:45 -04:00
import com.chameleonvision.settings.Platform;
2019-09-19 14:07:42 -04:00
import com.chameleonvision.vision.Pipeline;
2019-09-22 02:49:30 -04:00
import com.chameleonvision.web.ServerHandler;
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
2019-09-22 02:49:30 -04:00
import java.util.Arrays;
2019-09-19 14:07:42 -04:00
import java.util.HashMap;
import java.util.stream.IntStream;
public class Camera {
private static final float DEFAULT_FOV = 60.8f;
2019-09-22 02:49:30 -04:00
private static final int MINIMUM_FPS = 30;
private static final int MINIMUM_WIDTH = 320;
private static final int MINIMUM_HEIGHT = 200;
private static final int MAX_INIT_MS = 1500;
2019-09-20 02:17:22 -04:00
public final String name;
public final String path;
private final UsbCamera UsbCam;
2019-09-20 02:17:22 -04:00
private final VideoMode[] availableVideoModes;
private final CameraServer cs = CameraServer.getInstance();
2019-09-20 02:17:22 -04:00
private final CvSink cvSink;
2019-09-22 02:49:30 -04:00
private final Object cvSourceLock = new Object();
private CvSource cvSource;
private float FOV;
private int streamDivisor;
2019-09-20 02:17:22 -04:00
private CameraValues camVals;
private CamVideoMode camVideoMode;
private int currentPipelineIndex;
private HashMap<Integer, Pipeline> pipelines;
2019-09-20 21:25:54 -04:00
2019-09-20 02:17:22 -04:00
public Camera(String cameraName) {
2019-09-22 02:49:30 -04:00
this(cameraName, DEFAULT_FOV);
2019-09-20 02:17:22 -04:00
}
public Camera(String cameraName, float fov) {
this(cameraName,CameraManager.AllUsbCameraInfosByName.get(cameraName), fov);
2019-09-20 02:17:22 -04:00
}
public Camera(String cameraName, UsbCameraInfo usbCamInfo, float fov) {
this(cameraName ,usbCamInfo, fov, new HashMap<>(), 0);
}
public Camera(String cameraName, float fov, int videoModeIndex) {
this(cameraName, fov, new HashMap<>(), videoModeIndex);
}
public Camera(String cameraName, float fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex) {
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex);
}
public Camera(String cameraName, UsbCameraInfo usbCamInfo, float fov, HashMap<Integer, Pipeline> pipelines, int videoModeIndex) {
2019-09-20 02:17:22 -04:00
FOV = fov;
name = cameraName;
2019-09-20 02:17:22 -04:00
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
this.pipelines = pipelines;
2019-09-22 02:49:30 -04:00
// set up video modes according to minimums
2019-10-04 15:55:45 -04:00
if (Platform.getCurrentPlatform() == Platform.WINDOWS_64 && !UsbCam.isConnected()) {
System.out.print("Waiting on camera... ");
long initTimeout = System.nanoTime();
while(!UsbCam.isConnected())
{
if (((System.nanoTime() - initTimeout) / 1e6 ) >= MAX_INIT_MS) {
break;
}
}
var initTimeMs = (System.nanoTime() - initTimeout) / 1e6;
System.out.printf("Camera initialized in %.2fms\n", initTimeMs);
}
var trueVideoModes = UsbCam.enumerateVideoModes();
availableVideoModes = Arrays.stream(trueVideoModes).filter(v -> v.fps >= MINIMUM_FPS && v.width >= MINIMUM_WIDTH && v.height >= MINIMUM_HEIGHT).toArray(VideoMode[]::new);
if (availableVideoModes.length == 0) {
System.err.println("Camera not supported!");
throw new RuntimeException(new CameraException(CameraException.CameraExceptionType.BAD_CAMERA));
}
if (videoModeIndex <= availableVideoModes.length - 1) {
setCamVideoMode(videoModeIndex, false);
} else {
setCamVideoMode(0, false);
}
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
VideoMode[] getAvailableVideoModes() {
2019-09-22 02:49:30 -04:00
return availableVideoModes;
}
public int getStreamPort() {
var s = (MjpegServer) cs.getServer("serve_" + name);
return s.getPort();
}
public void setCamVideoMode(int videoMode, boolean updateCvSource) {
setCamVideoMode(new CamVideoMode(availableVideoModes[videoMode]), updateCvSource);
2019-09-20 02:17:22 -04:00
}
2019-09-19 14:07:42 -04:00
private void setCamVideoMode(CamVideoMode newVideoMode, boolean updateCvSource) {
var prevVideoMode = this.camVideoMode;
this.camVideoMode = newVideoMode;
UsbCam.setVideoMode(newVideoMode.getActualPixelFormat(), newVideoMode.width, newVideoMode.height, newVideoMode.fps);
// update camera values
2019-09-20 02:17:22 -04:00
camVals = new CameraValues(this);
if (prevVideoMode != null && !prevVideoMode.equals(newVideoMode) && updateCvSource) { // if resolution changed
2019-09-20 21:25:54 -04:00
synchronized (cvSourceLock) {
cvSource = cs.putVideo(name, newVideoMode.width, newVideoMode.height);
2019-09-20 21:25:54 -04:00
}
2019-09-22 02:49:30 -04:00
ServerHandler.sendFullSettings();
}
2019-09-20 02:17:22 -04:00
}
2019-09-19 14:07:42 -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-25 10:32:22 -07:00
public void setCurrentPipelineIndex(int pipelineNumber) {
2019-09-20 02:17:22 -04:00
if (pipelineNumber - 1 > pipelines.size()) return;
currentPipelineIndex = pipelineNumber;
}
public int getStreamDivisor(){
return streamDivisor;
}
public void setStreamDivisor(int divisor){
streamDivisor = divisor;
}
2019-09-22 02:49:30 -04:00
2019-09-20 02:17:22 -04:00
public HashMap<Integer, Pipeline> getPipelines() {
return pipelines;
}
2019-09-19 14:07:42 -04:00
2019-09-20 21:25:54 -04:00
public 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)
2019-09-22 02:49:30 -04:00
.filter(i -> camVideoMode.equals(availableVideoModes[i]))
2019-09-20 02:17:22 -04:00
.findFirst()
.orElse(-1);
}
2019-09-19 14:07:42 -04:00
public float getFOV() {
2019-09-20 02:17:22 -04:00
return FOV;
}
2019-09-19 14:07:42 -04:00
public void setFOV(float fov) {
2019-09-20 02:17:22 -04:00
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) {
2019-09-22 02:49:30 -04:00
return cvSink.grabFrame(image);
}
2019-09-20 02:17:22 -04:00
2019-09-22 02:49:30 -04:00
public CameraValues getCamVals() {
2019-09-20 21:25:54 -04:00
return camVals;
}
2019-09-22 02:49:30 -04:00
public void putFrame(Mat image) {
synchronized (cvSourceLock) {
2019-09-20 21:25:54 -04:00
cvSource.putFrame(image);
}
2019-09-22 02:49:30 -04:00
}
2019-09-19 14:07:42 -04:00
}