2019-09-19 14:07:42 -04:00
|
|
|
package com.chameleonvision.vision.camera;
|
|
|
|
|
|
2019-10-24 15:20:15 -04:00
|
|
|
import com.chameleonvision.Main;
|
2019-10-04 15:55:45 -04:00
|
|
|
import com.chameleonvision.settings.Platform;
|
2019-10-24 15:20:15 -04:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-11-01 11:38:58 +02:00
|
|
|
import com.chameleonvision.web.Server;
|
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;
|
2019-10-29 15:10:43 +02:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
2019-09-20 02:17:22 -04:00
|
|
|
import org.opencv.core.Mat;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
2019-10-25 15:02:17 +03:00
|
|
|
import java.nio.channels.Pipe;
|
2019-11-01 11:38:58 +02:00
|
|
|
import java.util.*;
|
2019-10-24 15:20:15 -04:00
|
|
|
import java.util.stream.Collectors;
|
2019-09-19 14:07:42 -04:00
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
public class Camera {
|
|
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
private static final double DEFAULT_FOV = 60.8;
|
|
|
|
|
private static final StreamDivisor DEFAULT_STREAMDIVISOR = StreamDivisor.none;
|
2019-11-01 11:38:58 +02:00
|
|
|
private static final int DEFAULT_EXPOSURE = 50;
|
|
|
|
|
private static final int DEFAULT_BRIGHTNESS = 50;
|
2019-10-20 10:13:07 +03: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-10-24 15:20:15 -04:00
|
|
|
private static final List<VideoMode.PixelFormat> ALLOWED_PIXEL_FORMATS = Arrays.asList(VideoMode.PixelFormat.kYUYV, VideoMode.PixelFormat.kMJPEG);
|
2019-10-20 10:13:07 +03:00
|
|
|
|
2019-11-01 11:38:58 +02:00
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
public final String name;
|
|
|
|
|
public final String path;
|
|
|
|
|
|
2019-10-24 15:20:15 -04:00
|
|
|
private String nickname;
|
|
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
private final UsbCamera UsbCam;
|
|
|
|
|
private final VideoMode[] availableVideoModes;
|
|
|
|
|
|
|
|
|
|
private final CameraServer cs = CameraServer.getInstance();
|
|
|
|
|
private final CvSink cvSink;
|
|
|
|
|
private final Object cvSourceLock = new Object();
|
|
|
|
|
private CvSource cvSource;
|
|
|
|
|
private Double FOV;
|
|
|
|
|
private StreamDivisor streamDivisor;
|
|
|
|
|
private CameraValues camVals;
|
|
|
|
|
private CamVideoMode camVideoMode;
|
|
|
|
|
private int currentPipelineIndex;
|
2019-10-25 15:02:17 +03:00
|
|
|
private List<Pipeline> pipelines;
|
2019-10-20 10:13:07 +03:00
|
|
|
|
2019-11-01 11:38:58 +02:00
|
|
|
//Driver mode camera settings
|
|
|
|
|
public int driverExposure;
|
|
|
|
|
public int driverBrightness;
|
2019-11-01 18:02:48 +02:00
|
|
|
public boolean isDriver;
|
2019-11-01 11:38:58 +02:00
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
public Camera(String cameraName) {
|
|
|
|
|
this(cameraName, DEFAULT_FOV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(String cameraName, double fov) {
|
|
|
|
|
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(String cameraName, UsbCameraInfo usbCameraInfo, double fov) {
|
|
|
|
|
this(cameraName, usbCameraInfo, fov, DEFAULT_STREAMDIVISOR);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov, StreamDivisor divisor) {
|
2019-11-01 17:01:10 +02:00
|
|
|
this(cameraName, usbCamInfo, fov, new ArrayList<>(), 0, divisor, DEFAULT_EXPOSURE, DEFAULT_BRIGHTNESS,false);
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-01 17:01:10 +02:00
|
|
|
public Camera(String cameraName, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, int driverExposure, int driverBrightness,boolean isDriver) {
|
|
|
|
|
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex, divisor, driverExposure, driverBrightness, isDriver);
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
2019-11-01 17:01:10 +02:00
|
|
|
public Camera(String cameraName, double fov, int videoModeIndex, StreamDivisor divisor, int driverExposure, int driverBrightness,boolean isDriver) {
|
|
|
|
|
this(cameraName, fov, new ArrayList<>(), videoModeIndex, divisor, driverExposure, driverBrightness,isDriver);
|
2019-10-25 15:02:17 +03:00
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
2019-11-01 17:01:10 +02:00
|
|
|
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, int driverExposure, int driverBrightness,boolean isDriver) {
|
2019-10-20 10:13:07 +03:00
|
|
|
FOV = fov;
|
|
|
|
|
name = cameraName;
|
2019-10-24 15:20:15 -04:00
|
|
|
|
|
|
|
|
if (Platform.getCurrentPlatform().isWindows()) {
|
|
|
|
|
path = usbCamInfo.path;
|
|
|
|
|
} else {
|
|
|
|
|
var truePath = Arrays.stream(usbCamInfo.otherPaths).filter(x -> x.contains("/dev/v4l/by-path")).findFirst();
|
|
|
|
|
path = truePath.isPresent() ? truePath.toString() : null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
streamDivisor = divisor;
|
|
|
|
|
UsbCam = new UsbCamera(name, path);
|
|
|
|
|
|
|
|
|
|
this.pipelines = pipelines;
|
|
|
|
|
|
|
|
|
|
// set up video modes according to minimums
|
|
|
|
|
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();
|
2019-10-24 15:20:15 -04:00
|
|
|
availableVideoModes = Arrays.stream(trueVideoModes).filter(v ->
|
|
|
|
|
v.fps >= MINIMUM_FPS && v.width >= MINIMUM_WIDTH && v.height >= MINIMUM_HEIGHT && ALLOWED_PIXEL_FORMATS.contains(v.pixelFormat)).toArray(VideoMode[]::new);
|
2019-10-20 10:13:07 +03:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cvSink = cs.getVideo(UsbCam);
|
|
|
|
|
cvSource = cs.putVideo(name, camVals.ImageWidth, camVals.ImageHeight);
|
2019-11-01 17:01:10 +02:00
|
|
|
|
|
|
|
|
//Driver mode settings
|
|
|
|
|
this.isDriver = isDriver;
|
2019-11-01 11:38:58 +02:00
|
|
|
this.driverBrightness=driverBrightness;
|
|
|
|
|
this.driverExposure=driverExposure;
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
VideoMode[] getAvailableVideoModes() {
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
camVals = new CameraValues(this);
|
|
|
|
|
if (prevVideoMode != null && !prevVideoMode.equals(newVideoMode) && updateCvSource) { // if resolution changed
|
|
|
|
|
synchronized (cvSourceLock) {
|
|
|
|
|
cvSource = cs.putVideo(name, newVideoMode.width, newVideoMode.height);
|
|
|
|
|
}
|
|
|
|
|
ServerHandler.sendFullSettings();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
public void addPipeline() {
|
2019-10-25 15:02:17 +03:00
|
|
|
Pipeline p = new Pipeline();
|
|
|
|
|
p.nickname = "New pipeline " + pipelines.size();
|
|
|
|
|
addPipeline(p);
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
|
|
|
|
public void addPipeline(Pipeline p) {
|
2019-10-25 15:02:17 +03:00
|
|
|
this.pipelines.add(p);
|
2019-10-24 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
2019-10-25 15:02:17 +03:00
|
|
|
public void deletePipeline(int index) {
|
|
|
|
|
pipelines.remove(index);
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
2019-10-25 15:02:17 +03:00
|
|
|
public void deletePipeline() {
|
|
|
|
|
deletePipeline(getCurrentPipelineIndex());
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Pipeline getCurrentPipeline() {
|
2019-10-24 15:20:15 -04:00
|
|
|
return getPipelineByIndex(currentPipelineIndex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Pipeline getPipelineByIndex(int pipelineIndex) {
|
|
|
|
|
return pipelines.get(pipelineIndex);
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getCurrentPipelineIndex() {
|
|
|
|
|
return currentPipelineIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCurrentPipelineIndex(int pipelineNumber) {
|
|
|
|
|
if (pipelineNumber - 1 > pipelines.size()) return;
|
|
|
|
|
currentPipelineIndex = pipelineNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public StreamDivisor getStreamDivisor() {
|
|
|
|
|
return streamDivisor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setStreamDivisor(int divisor) {
|
|
|
|
|
streamDivisor = StreamDivisor.values()[divisor];
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-25 15:02:17 +03:00
|
|
|
public List<Pipeline> getPipelines() {
|
2019-10-20 10:13:07 +03:00
|
|
|
return pipelines;
|
|
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
|
|
|
|
public List<String> getPipelinesNickname() {
|
2019-10-25 14:02:42 +03:00
|
|
|
var pipelines = getPipelines();
|
2019-10-25 15:02:17 +03:00
|
|
|
return pipelines.stream().map(pipeline -> pipeline.nickname).collect(Collectors.toList());
|
2019-10-25 14:02:42 +03:00
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
|
|
|
|
|
public CamVideoMode getVideoMode() {
|
|
|
|
|
return camVideoMode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getVideoModeIndex() {
|
|
|
|
|
return IntStream.range(0, availableVideoModes.length)
|
|
|
|
|
.filter(i -> camVideoMode.equals(availableVideoModes[i]))
|
|
|
|
|
.findFirst()
|
|
|
|
|
.orElse(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double getFOV() {
|
|
|
|
|
return FOV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setFOV(Number fov) {
|
|
|
|
|
FOV = fov.doubleValue();
|
|
|
|
|
camVals = new CameraValues(this);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 11:38:58 +02:00
|
|
|
public void setDriverMode(boolean state)
|
|
|
|
|
{
|
|
|
|
|
isDriver=state;
|
|
|
|
|
if(isDriver){
|
2019-11-01 17:01:10 +02:00
|
|
|
UsbCam.setBrightness(driverBrightness);//We call setBrightness because it updates after 2 calls
|
|
|
|
|
UsbCam.setBrightness(driverBrightness);//Check it after we update to 2020 libraries
|
2019-11-01 11:38:58 +02:00
|
|
|
try{UsbCam.setExposureManual(driverExposure);}
|
|
|
|
|
catch (VideoException e)
|
|
|
|
|
{
|
2019-11-01 17:01:10 +02:00
|
|
|
System.out.println("Exposure change isn't supported");
|
2019-11-01 11:38:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
UsbCam.setBrightness(getCurrentPipeline().brightness);
|
|
|
|
|
UsbCam.setBrightness(getCurrentPipeline().brightness);
|
|
|
|
|
try{UsbCam.setExposureManual(getCurrentPipeline().exposure);}
|
|
|
|
|
catch (VideoException e)
|
|
|
|
|
{
|
2019-11-01 17:01:10 +02:00
|
|
|
System.out.println("Exposure change isn't supported");
|
2019-11-01 11:38:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-01 17:01:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getDriverMode()
|
|
|
|
|
{
|
|
|
|
|
return isDriver;
|
2019-11-01 11:38:58 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
public int getBrightness() {
|
2019-11-01 11:38:58 +02:00
|
|
|
return UsbCam.getBrightness();
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-01 17:01:10 +02:00
|
|
|
|
|
|
|
|
|
2019-10-20 10:13:07 +03:00
|
|
|
public void setBrightness(int brightness) {
|
2019-11-01 11:38:58 +02:00
|
|
|
if (isDriver)
|
|
|
|
|
driverBrightness=brightness;
|
|
|
|
|
else
|
|
|
|
|
getCurrentPipeline().brightness = brightness;
|
2019-10-20 10:13:07 +03:00
|
|
|
UsbCam.setBrightness(brightness);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setExposure(int exposure) {
|
2019-11-01 11:38:58 +02:00
|
|
|
if (isDriver)
|
|
|
|
|
driverExposure=exposure;
|
|
|
|
|
else
|
|
|
|
|
getCurrentPipeline().exposure = exposure;
|
2019-10-29 15:26:10 +02:00
|
|
|
try {
|
|
|
|
|
UsbCam.setExposureManual(exposure);
|
2019-11-01 11:38:58 +02:00
|
|
|
} catch (VideoException e) {
|
2019-10-29 15:26:10 +02:00
|
|
|
System.err.println("Camera Does not support exposure change");
|
|
|
|
|
}
|
2019-10-20 10:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long grabFrame(Mat image) {
|
|
|
|
|
return cvSink.grabFrame(image);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CameraValues getCamVals() {
|
|
|
|
|
return camVals;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void putFrame(Mat image) {
|
|
|
|
|
synchronized (cvSourceLock) {
|
|
|
|
|
cvSource.putFrame(image);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-24 15:20:15 -04:00
|
|
|
|
|
|
|
|
public List<String> getResolutionList() {
|
|
|
|
|
return Arrays.stream(availableVideoModes)
|
|
|
|
|
.map(res -> String.format("%sx%s@%sFPS, %s", res.width, res.height, res.fps, res.pixelFormat.toString()))
|
|
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNickname(String newNickname) {
|
2019-10-29 15:26:10 +02:00
|
|
|
//Deletes old camera nt table
|
2019-11-01 11:38:58 +02:00
|
|
|
NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname).getInstance().deleteAllEntries();
|
2019-10-24 15:20:15 -04:00
|
|
|
nickname = newNickname;
|
2019-10-29 15:10:43 +02:00
|
|
|
if (CameraManager.AllVisionProcessesByName.containsKey(this.name)) {
|
|
|
|
|
NetworkTable newNT = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname);
|
|
|
|
|
CameraManager.AllVisionProcessesByName.get(this.name).resetNT(newNT);
|
|
|
|
|
}
|
2019-10-24 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getNickname() {
|
|
|
|
|
return nickname == null ? name : nickname;
|
|
|
|
|
}
|
2019-10-29 15:10:43 +02:00
|
|
|
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|