mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Replace classes with new classabstraction classes
This commit is contained in:
@@ -1,13 +1,10 @@
|
||||
package com.chameleonvision;
|
||||
|
||||
import com.chameleonvision.classabstraction.VisionManager;
|
||||
import com.chameleonvision.classabstraction.config.ConfigManager;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.network.NetworkManager;
|
||||
import com.chameleonvision.settings.GeneralSettings;
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.util.Platform;
|
||||
import com.chameleonvision.util.Utilities;
|
||||
import com.chameleonvision.vision.camera.CameraManager;
|
||||
import com.chameleonvision.vision.VisionManager;
|
||||
import com.chameleonvision.web.Server;
|
||||
import edu.wpi.cscore.CameraServerCvJNI;
|
||||
import edu.wpi.cscore.CameraServerJNI;
|
||||
@@ -17,7 +14,7 @@ import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import java.io.IOException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static com.chameleonvision.settings.Platform.CurrentPlatform;
|
||||
import static com.chameleonvision.util.Platform.CurrentPlatform;
|
||||
|
||||
public class Main {
|
||||
|
||||
@@ -134,40 +131,8 @@ public class Main {
|
||||
throw new RuntimeException("Failed to load JNI Libraries!");
|
||||
}
|
||||
|
||||
if (testMode) {
|
||||
ConfigManager.initializeSettings();
|
||||
NetworkManager.initialize(manageNetwork);
|
||||
NetworkTableInstance.getDefault().startServer();
|
||||
|
||||
boolean visionSourcesOk = VisionManager.initializeSources();
|
||||
if (!visionSourcesOk) {
|
||||
System.out.println("No cameras connected!");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean visionProcessesOk = VisionManager.initializeProcesses();
|
||||
if (!visionProcessesOk) {
|
||||
System.err.println("shit");
|
||||
return;
|
||||
}
|
||||
|
||||
VisionManager.startProcesses();
|
||||
|
||||
// runServer(true);
|
||||
} else {
|
||||
if (CameraManager.initializeCameras()) {
|
||||
SettingsManager.initialize();
|
||||
NetworkManager.initialize(manageNetwork);
|
||||
CameraManager.initializeThreads();
|
||||
runServer(false);
|
||||
} else {
|
||||
System.err.println("No cameras connected!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void runServer(boolean test) {
|
||||
GeneralSettings settings = test ? ConfigManager.settings : SettingsManager.generalSettings;
|
||||
ConfigManager.initializeSettings();
|
||||
NetworkManager.initialize(manageNetwork);
|
||||
|
||||
if (ntServerMode) {
|
||||
System.out.println("Starting NT Server");
|
||||
@@ -177,10 +142,24 @@ public class Main {
|
||||
if (ntClientModeServer != null) {
|
||||
NetworkTableInstance.getDefault().startClient(ntClientModeServer);
|
||||
} else {
|
||||
NetworkTableInstance.getDefault().startClientTeam(settings.teamNumber);
|
||||
NetworkTableInstance.getDefault().startClientTeam(ConfigManager.settings.teamNumber);
|
||||
}
|
||||
}
|
||||
|
||||
boolean visionSourcesOk = VisionManager.initializeSources();
|
||||
if (!visionSourcesOk) {
|
||||
System.out.println("No cameras connected!");
|
||||
return;
|
||||
}
|
||||
|
||||
boolean visionProcessesOk = VisionManager.initializeProcesses();
|
||||
if (!visionProcessesOk) {
|
||||
System.err.println("shit");
|
||||
return;
|
||||
}
|
||||
|
||||
VisionManager.startProcesses();
|
||||
|
||||
System.out.printf("Starting Webserver at port %d\n", DEFAULT_PORT);
|
||||
Server.main(DEFAULT_PORT);
|
||||
}
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
|
||||
public enum PipelineType {
|
||||
PIPELINE_2D,
|
||||
PIPELINE_3D,
|
||||
PIPELINE_DRIVER;
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.chameleonvision.classabstraction.config;
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.USBCameraProperties;
|
||||
import com.chameleonvision.vision.camera.USBCameraProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.classabstraction.config;
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.USBCameraProperties;
|
||||
import com.chameleonvision.classabstraction.camera.USBCameraProcess;
|
||||
import com.chameleonvision.vision.camera.USBCameraProperties;
|
||||
import com.chameleonvision.vision.camera.USBCameraProcess;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.SerializerProvider;
|
||||
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.chameleonvision.classabstraction.config;
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.classabstraction.util.ProgramDirectoryUtilities;
|
||||
import com.chameleonvision.settings.GeneralSettings;
|
||||
import com.chameleonvision.util.ProgramDirectoryUtilities;
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.settings;
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.network.NetworkIPMode;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.network;
|
||||
|
||||
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.util.Platform;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
|
||||
import java.net.SocketException;
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
package com.chameleonvision.settings;
|
||||
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
import com.chameleonvision.vision.camera.CameraManager;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class SettingsManager {
|
||||
public static final Path SettingsPath = Paths.get(System.getProperty("user.dir"), "settings");
|
||||
public static GeneralSettings generalSettings;
|
||||
|
||||
private SettingsManager() {}
|
||||
|
||||
public static void initialize() {
|
||||
initGeneralSettings();
|
||||
var allCameras = CameraManager.getAllCamerasByName();
|
||||
if (!allCameras.containsKey(generalSettings.currentCamera) && allCameras.size() > 0) {
|
||||
var cam = allCameras.entrySet().stream().findFirst().get().getValue();
|
||||
generalSettings.currentCamera = cam.name;
|
||||
generalSettings.currentPipeline = cam.getCurrentPipelineIndex();
|
||||
}
|
||||
}
|
||||
|
||||
private static void initGeneralSettings() {
|
||||
FileHelper.CheckPath(SettingsPath);
|
||||
try {
|
||||
generalSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(), "settings.json").toString()), com.chameleonvision.settings.GeneralSettings.class);
|
||||
} catch (FileNotFoundException e) {
|
||||
generalSettings = new GeneralSettings();
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateCameraSetting(String cameraName, int pipelineNumber) {
|
||||
generalSettings.currentCamera = cameraName;
|
||||
generalSettings.currentPipeline = pipelineNumber;
|
||||
}
|
||||
|
||||
public static void updatePipelineSetting(int pipelineNumber) {
|
||||
generalSettings.currentPipeline = pipelineNumber;
|
||||
}
|
||||
|
||||
public static void saveSettings() {
|
||||
CameraManager.saveCameras();
|
||||
saveGeneralSettings();
|
||||
}
|
||||
|
||||
private static void saveGeneralSettings() {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
FileWriter writer = new FileWriter(Paths.get(SettingsPath.toString(), "settings.json").toString());
|
||||
gson.toJson(generalSettings, writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.util;
|
||||
package com.chameleonvision.util;
|
||||
|
||||
import org.opencv.core.Scalar;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.util;
|
||||
package com.chameleonvision.util;
|
||||
|
||||
/**
|
||||
* A thread that tries to run at a specified loop time
|
||||
@@ -1,6 +1,4 @@
|
||||
package com.chameleonvision.settings;
|
||||
|
||||
import com.chameleonvision.util.ShellExec;
|
||||
package com.chameleonvision.util;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.util;
|
||||
package com.chameleonvision.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URISyntaxException;
|
||||
@@ -1,28 +0,0 @@
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Pipeline {
|
||||
public int exposure = 50;
|
||||
public int brightness = 50;
|
||||
public Orientation orientation = Orientation.Normal;
|
||||
public List<Number> hue = Arrays.asList(50, 180);
|
||||
public List<Number> saturation = Arrays.asList(50, 255);
|
||||
public List<Number> value = Arrays.asList(50, 255);
|
||||
public boolean erode = false;
|
||||
public boolean dilate = false;
|
||||
public List<Number> area = Arrays.asList(0.0, 100.0);
|
||||
public List<Number> ratio = Arrays.asList(0.0, 20.0);
|
||||
public List<Number> extent = Arrays.asList(0, 100);
|
||||
public Number speckle = 5;
|
||||
public boolean isBinary = false;
|
||||
public SortMode sortMode = SortMode.Largest;
|
||||
public TargetGroup targetGroup = TargetGroup.Single;
|
||||
public TargetIntersection targetIntersection = TargetIntersection.Up;
|
||||
public double m = 1;
|
||||
public double b = 0;
|
||||
public List<Number> point = Arrays.asList(0,0);
|
||||
public CalibrationMode calibrationMode = CalibrationMode.None;
|
||||
public String nickname;
|
||||
}
|
||||
@@ -1,12 +1,11 @@
|
||||
package com.chameleonvision.classabstraction;
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
||||
import com.chameleonvision.classabstraction.camera.USBCameraProcess;
|
||||
import com.chameleonvision.classabstraction.config.CameraConfig;
|
||||
import com.chameleonvision.classabstraction.config.ConfigManager;
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipelineSettings;
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.vision.camera.USBCamera;
|
||||
import com.chameleonvision.config.CameraConfig;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.util.Platform;
|
||||
import com.chameleonvision.vision.camera.CameraProcess;
|
||||
import com.chameleonvision.vision.camera.USBCameraProcess;
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.UsbCameraInfo;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
@@ -1,11 +1,10 @@
|
||||
package com.chameleonvision.classabstraction;
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
||||
import com.chameleonvision.classabstraction.camera.CameraStreamer;
|
||||
import com.chameleonvision.classabstraction.config.ConfigManager;
|
||||
import com.chameleonvision.classabstraction.pipeline.*;
|
||||
import com.chameleonvision.classabstraction.util.LoopingRunnable;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.util.LoopingRunnable;
|
||||
import com.chameleonvision.vision.camera.CameraProcess;
|
||||
import com.chameleonvision.vision.camera.CameraStreamer;
|
||||
import com.chameleonvision.vision.pipeline.*;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CamVideoMode {
|
||||
public final int fps;
|
||||
public final int width;
|
||||
public final int height;
|
||||
public final String pixel_format;
|
||||
|
||||
public CamVideoMode(VideoMode videoMode) {
|
||||
fps = videoMode.fps;
|
||||
width = videoMode.width;
|
||||
height = videoMode.height;
|
||||
pixel_format = videoMode.pixelFormat.name();
|
||||
}
|
||||
|
||||
public VideoMode.PixelFormat getActualPixelFormat() {
|
||||
return VideoMode.PixelFormat.valueOf(pixel_format);
|
||||
}
|
||||
|
||||
public boolean isEqualToVideoMode(VideoMode videoMode) {
|
||||
return videoMode.fps == fps && videoMode.width == width && videoMode.height == height && videoMode.pixelFormat == getActualPixelFormat();
|
||||
}
|
||||
|
||||
public boolean equals(VideoMode vm) {
|
||||
return vm.fps == fps &&
|
||||
vm.width == width &&
|
||||
vm.height == height &&
|
||||
vm.pixelFormat == getActualPixelFormat();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (obj instanceof CamVideoMode) {
|
||||
var cvm = (CamVideoMode) obj;
|
||||
return cvm.fps == fps &&
|
||||
cvm.width == width &&
|
||||
cvm.height == height &&
|
||||
cvm.pixel_format.equals(pixel_format);
|
||||
} else if (obj instanceof VideoMode) {
|
||||
var vm = (VideoMode) obj;
|
||||
return equals(vm);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JavaType;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CameraDeserializer implements JsonDeserializer<USBCamera> {
|
||||
@Override
|
||||
public USBCamera deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
try {
|
||||
var jsonObj = jsonElement.getAsJsonObject();
|
||||
var camFOV = jsonObj.get("FOV").getAsDouble();
|
||||
var camName = jsonObj.get("name").getAsString();
|
||||
var camNickname = jsonObj.get("nickname").getAsString();
|
||||
var videoModeIndex = jsonObj.get("resolution").getAsInt();
|
||||
|
||||
// new for 2.0
|
||||
var isDriverObj = jsonObj.get("isDriver");
|
||||
var driverExposureObj = jsonObj.get("driverExposure");
|
||||
var driverBrightnessObj = jsonObj.get("driverBrightness");
|
||||
var divisorObj = jsonObj.get("streamDivisor");
|
||||
|
||||
// always null-check new features
|
||||
boolean isDriver = isDriverObj != null && isDriverObj.getAsBoolean();
|
||||
int driverExposure = driverExposureObj == null ? USBCamera.DEFAULT_EXPOSURE : driverExposureObj.getAsInt();
|
||||
int driverBrightness = driverBrightnessObj == null ? USBCamera.DEFAULT_BRIGHTNESS : driverBrightnessObj.getAsInt();
|
||||
StreamDivisor divisor = divisorObj == null ? StreamDivisor.NONE : StreamDivisor.values()[divisorObj.getAsInt()];
|
||||
|
||||
var pipelines = jsonObj.get("pipelines");
|
||||
List<Pipeline> actualPipelines = new ArrayList<>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
TypeFactory typeFactory = mapper.getTypeFactory();
|
||||
JavaType arrayType = typeFactory.constructCollectionType(List.class, Pipeline.class);
|
||||
try {
|
||||
actualPipelines = mapper.readValue(pipelines.toString(), arrayType);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
var newCamera = actualPipelines != null ? new USBCamera(camName, camFOV, actualPipelines, videoModeIndex, divisor, isDriver) : new USBCamera(camName, camFOV, videoModeIndex, divisor, isDriver);
|
||||
newCamera.setNickname(camNickname != null ? camNickname : "");
|
||||
newCamera.setDriverExposure(driverExposure);
|
||||
newCamera.setDriverBrightness(driverBrightness);
|
||||
return newCamera;
|
||||
}
|
||||
catch (NullPointerException e)
|
||||
{
|
||||
System.err.println("Error while reading json, value doesn't exist!");
|
||||
System.err.println("Try to delete the camera settings in settings/cameras/YOURCAMERA.json");
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
public class CameraException extends Exception {
|
||||
public enum CameraExceptionType {
|
||||
NO_CAMERA,
|
||||
BAD_CAMERA,
|
||||
BAD_PIPELINE,
|
||||
BAD_SETTING;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this) {
|
||||
case NO_CAMERA: return "No camera connected!";
|
||||
case BAD_CAMERA: return "Invalid camera!";
|
||||
case BAD_PIPELINE: return "Invalid pipeline!";
|
||||
case BAD_SETTING: return "Invalid camera/pipeline setting!";
|
||||
default: return "Unknown camera exception!";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CameraException(CameraExceptionType camExceptionType) {
|
||||
super(camExceptionType.toString());
|
||||
}
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.util.FileHelper;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.vision.process.USBCameraProcess;
|
||||
import com.chameleonvision.vision.process.VisionProcess;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.UsbCameraInfo;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class CameraManager {
|
||||
|
||||
private static final Path CamConfigPath = Paths.get(SettingsManager.SettingsPath.toString(), "cameras");
|
||||
|
||||
private static LinkedHashMap<String, USBCamera> allCamerasByName = new LinkedHashMap<>();
|
||||
public static HashMap<String, VisionProcess> allVisionProcessesByName = new HashMap<>();
|
||||
|
||||
static HashMap<String, UsbCameraInfo> allUsbCameraInfosByName = new HashMap<>() {{
|
||||
var suffix = 0;
|
||||
for (var info : UsbCamera.enumerateUsbCameras()) {
|
||||
var cap = new VideoCapture(info.dev);
|
||||
if (cap.isOpened()) {
|
||||
cap.release();
|
||||
var name = info.name;
|
||||
while (this.containsKey(name)) {
|
||||
suffix++;
|
||||
name = String.format("%s(%s)", info.name, suffix);
|
||||
}
|
||||
put(name, info);
|
||||
}
|
||||
}
|
||||
}};
|
||||
|
||||
public static HashMap<String, USBCamera> getAllCamerasByName() {
|
||||
return allCamerasByName;
|
||||
}
|
||||
public static List<String> getAllCameraByNickname() {
|
||||
var cameras = getAllCamerasByName();
|
||||
return cameras.values().stream().map(USBCamera::getNickname).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static boolean initializeCameras() {
|
||||
if (allUsbCameraInfosByName.size() == 0) return false;
|
||||
FileHelper.CheckPath(CamConfigPath);
|
||||
allUsbCameraInfosByName.forEach((key, value) -> {
|
||||
var camPath = Paths.get(CamConfigPath.toString(), String.format("%s.json", key));
|
||||
File camJsonFile = new File(camPath.toString());
|
||||
if (camJsonFile.exists() && camJsonFile.length() != 0) {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(USBCamera.class, new CameraDeserializer()).create();
|
||||
var camJsonFileReader = new FileReader(camPath.toString());
|
||||
var gsonRead = gson.fromJson(camJsonFileReader, USBCamera.class);
|
||||
allCamerasByName.put(key, gsonRead);
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
if (!addCamera(new USBCamera(key), key)) {
|
||||
System.err.println("Failed to add camera! Already exists!");
|
||||
}
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void initializeThreads(){
|
||||
allCamerasByName.forEach((name, camera) -> {
|
||||
VisionProcess visionProcess = new VisionProcess(new USBCameraProcess(camera));
|
||||
allVisionProcessesByName.put(name, visionProcess);
|
||||
new Thread(visionProcess).start();
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean addCamera(USBCamera USBCamera, String cameraName) {
|
||||
if (allCamerasByName.containsKey(cameraName)) return false;
|
||||
USBCamera.addPipeline();
|
||||
allCamerasByName.put(cameraName, USBCamera);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static USBCamera getCamera(String cameraName) {
|
||||
return allCamerasByName.get(cameraName);
|
||||
}
|
||||
|
||||
public static USBCamera getCameraByIndex(int index) {
|
||||
return allCamerasByName.get( (allCamerasByName.keySet().toArray())[ index ] );
|
||||
}
|
||||
|
||||
public static USBCamera getCurrentCamera() throws CameraException {
|
||||
if (allCamerasByName.size() == 0) throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
||||
var curCam = allCamerasByName.get(SettingsManager.generalSettings.currentCamera);
|
||||
if (curCam == null) throw new CameraException(CameraException.CameraExceptionType.BAD_CAMERA);
|
||||
return curCam;
|
||||
}
|
||||
|
||||
public static Integer getCurrentCameraIndex() throws CameraException {
|
||||
if (allCamerasByName.size() == 0) throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
||||
List<String> arr = new ArrayList<>(allCamerasByName.keySet());
|
||||
for (var i = 0; i < allCamerasByName.size(); i++){
|
||||
if (SettingsManager.generalSettings.currentCamera.equals(arr.get(i))){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void setCurrentCamera(String cameraName) throws CameraException {
|
||||
if (!allCamerasByName.containsKey(cameraName))
|
||||
throw new CameraException(CameraException.CameraExceptionType.BAD_CAMERA);
|
||||
SettingsManager.generalSettings.currentCamera = cameraName;
|
||||
SettingsManager.updateCameraSetting(cameraName, getCurrentCamera().getCurrentPipelineIndex());
|
||||
}
|
||||
|
||||
public static void setCurrentCamera(int cameraIndex) throws CameraException {
|
||||
List<String> s = new ArrayList<String>(allCamerasByName.keySet());
|
||||
setCurrentCamera(s.get(cameraIndex));
|
||||
}
|
||||
|
||||
public static Pipeline getCurrentPipeline() throws CameraException {
|
||||
return getCurrentCamera().getCurrentPipeline();
|
||||
}
|
||||
|
||||
public static void setCurrentPipeline(int pipelineNumber) throws CameraException {
|
||||
if (pipelineNumber >= getCurrentCamera().getPipelines().size()){
|
||||
throw new CameraException(CameraException.CameraExceptionType.BAD_PIPELINE);
|
||||
}
|
||||
getCurrentCamera().setCurrentPipelineIndex(pipelineNumber);
|
||||
SettingsManager.updatePipelineSetting(pipelineNumber);
|
||||
}
|
||||
|
||||
public static VisionProcess getVisionProcessByCameraName(String cameraName) {
|
||||
return allVisionProcessesByName.get(cameraName);
|
||||
}
|
||||
|
||||
public static VisionProcess getCurrentVisionProcess() throws CameraException {
|
||||
if (!SettingsManager.generalSettings.currentCamera.equals("")){
|
||||
return allVisionProcessesByName.get(SettingsManager.generalSettings.currentCamera);
|
||||
}
|
||||
throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
||||
}
|
||||
|
||||
public static void saveCameras() {
|
||||
for (var entry : allCamerasByName.entrySet()) {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(USBCamera.class, new CameraSerializer()).create();
|
||||
FileWriter writer = new FileWriter(Paths.get(CamConfigPath.toString(), String.format("%s.json", entry.getKey())).toString());
|
||||
gson.toJson(entry.getValue(), writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +1,33 @@
|
||||
package com.chameleonvision.classabstraction.camera;
|
||||
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public interface CameraProcess {
|
||||
USBCameraProperties getProperties();
|
||||
|
||||
/**
|
||||
* Get the next camera frame
|
||||
* @return a Pair of the captured image and how long it took to grab the frame (in uS)
|
||||
*/
|
||||
Pair<Mat, Long> getFrame();
|
||||
|
||||
/**
|
||||
* Set the exposure of the camera
|
||||
* @param exposure the new exposure to set the camera to
|
||||
*/
|
||||
void setExposure(int exposure);
|
||||
|
||||
/**
|
||||
* Set the exposure of the camera
|
||||
* @param brightness the new brightness to set the camera to
|
||||
*/
|
||||
void setBrightness(int brightness);
|
||||
|
||||
/**
|
||||
* Set the video mode (fps and resolution) of the camera
|
||||
* @param mode the wanted mode
|
||||
*/
|
||||
void setVideoMode(VideoMode mode);
|
||||
}
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public interface CameraProcess {
|
||||
USBCameraProperties getProperties();
|
||||
|
||||
/**
|
||||
* Get the next camera frame
|
||||
* @return a Pair of the captured image and how long it took to grab the frame (in uS)
|
||||
*/
|
||||
Pair<Mat, Long> getFrame();
|
||||
|
||||
/**
|
||||
* Set the exposure of the camera
|
||||
* @param exposure the new exposure to set the camera to
|
||||
*/
|
||||
void setExposure(int exposure);
|
||||
|
||||
/**
|
||||
* Set the exposure of the camera
|
||||
* @param brightness the new brightness to set the camera to
|
||||
*/
|
||||
void setBrightness(int brightness);
|
||||
|
||||
/**
|
||||
* Set the video mode (fps and resolution) of the camera
|
||||
* @param mode the wanted mode
|
||||
*/
|
||||
void setVideoMode(VideoMode mode);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class CameraSerializer implements JsonSerializer<USBCamera> {
|
||||
@Override
|
||||
public JsonElement serialize(USBCamera USBCamera, Type type, JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("FOV", USBCamera.getFOV());
|
||||
obj.addProperty("path", USBCamera.path);
|
||||
obj.addProperty("name", USBCamera.name);
|
||||
obj.addProperty("nickname", USBCamera.getNickname());
|
||||
obj.addProperty("streamDivisor", USBCamera.getStreamDivisor().ordinal());
|
||||
var pipelines = context.serialize(USBCamera.getPipelines());
|
||||
obj.add("pipelines", pipelines);
|
||||
obj.addProperty("resolution", USBCamera.getVideoModeIndex());
|
||||
obj.add("camVideoMode", context.serialize(USBCamera.getVideoMode()));
|
||||
obj.add("isDriver",context.serialize(USBCamera.getDriverMode()));
|
||||
obj.add("driverExposure",context.serialize(USBCamera.getDriverExposure()));
|
||||
obj.add("driverBrightness",context.serialize(USBCamera.getDriverBrightness()));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
@@ -1,36 +1,36 @@
|
||||
package com.chameleonvision.classabstraction.camera;
|
||||
|
||||
import org.apache.commons.math3.fraction.Fraction;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
|
||||
public class CameraStaticProperties {
|
||||
|
||||
public final int imageWidth;
|
||||
public final int imageHeight;
|
||||
public final double fov;
|
||||
public final double imageArea;
|
||||
public final double centerX;
|
||||
public final double centerY;
|
||||
public final double horizontalFocalLength;
|
||||
public final double verticalFocalLength;
|
||||
|
||||
public CameraStaticProperties(int imageWidth, int imageHeight, double fov) {
|
||||
this.imageWidth = imageWidth;
|
||||
this.imageHeight = imageHeight;
|
||||
this.fov = fov;
|
||||
imageArea = this.imageWidth * this.imageHeight;
|
||||
centerX = ((double) this.imageWidth / 2) - 0.5;
|
||||
centerY = ((double) this.imageHeight / 2) - 0.5;
|
||||
|
||||
// pinhole model calculations
|
||||
double diagonalView = FastMath.toRadians(this.fov);
|
||||
Fraction aspectFraction = new Fraction(this.imageWidth, this.imageHeight);
|
||||
int horizontalRatio = aspectFraction.getNumerator();
|
||||
int verticalRatio = aspectFraction.getDenominator();
|
||||
double diagonalAspect = FastMath.hypot(horizontalRatio, verticalRatio);
|
||||
double horizontalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (horizontalRatio / diagonalAspect)) * 2;
|
||||
double verticalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (verticalRatio / diagonalAspect)) * 2;
|
||||
horizontalFocalLength = this.imageWidth / (2 * FastMath.tan(horizontalView /2));
|
||||
verticalFocalLength = this.imageHeight / (2 * FastMath.tan(verticalView /2));
|
||||
}
|
||||
}
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import org.apache.commons.math3.fraction.Fraction;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
|
||||
public class CameraStaticProperties {
|
||||
|
||||
public final int imageWidth;
|
||||
public final int imageHeight;
|
||||
public final double fov;
|
||||
public final double imageArea;
|
||||
public final double centerX;
|
||||
public final double centerY;
|
||||
public final double horizontalFocalLength;
|
||||
public final double verticalFocalLength;
|
||||
|
||||
public CameraStaticProperties(int imageWidth, int imageHeight, double fov) {
|
||||
this.imageWidth = imageWidth;
|
||||
this.imageHeight = imageHeight;
|
||||
this.fov = fov;
|
||||
imageArea = this.imageWidth * this.imageHeight;
|
||||
centerX = ((double) this.imageWidth / 2) - 0.5;
|
||||
centerY = ((double) this.imageHeight / 2) - 0.5;
|
||||
|
||||
// pinhole model calculations
|
||||
double diagonalView = FastMath.toRadians(this.fov);
|
||||
Fraction aspectFraction = new Fraction(this.imageWidth, this.imageHeight);
|
||||
int horizontalRatio = aspectFraction.getNumerator();
|
||||
int verticalRatio = aspectFraction.getDenominator();
|
||||
double diagonalAspect = FastMath.hypot(horizontalRatio, verticalRatio);
|
||||
double horizontalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (horizontalRatio / diagonalAspect)) * 2;
|
||||
double verticalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (verticalRatio / diagonalAspect)) * 2;
|
||||
horizontalFocalLength = this.imageWidth / (2 * FastMath.tan(horizontalView /2));
|
||||
verticalFocalLength = this.imageHeight / (2 * FastMath.tan(verticalView /2));
|
||||
}
|
||||
}
|
||||
@@ -1,65 +1,65 @@
|
||||
package com.chameleonvision.classabstraction.camera;
|
||||
|
||||
import com.chameleonvision.vision.camera.StreamDivisor;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import edu.wpi.cscore.CvSource;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public class CameraStreamer {
|
||||
private final CameraProcess cameraProcess;
|
||||
private final String name;
|
||||
private StreamDivisor divisor = StreamDivisor.NONE;
|
||||
private CvSource cvSource;
|
||||
private final Object streamBufferLock = new Object();
|
||||
private Mat streamBuffer = new Mat();
|
||||
|
||||
public CameraStreamer(CameraProcess cameraProcess, String name) {
|
||||
this.cameraProcess = cameraProcess;
|
||||
this.name = name;
|
||||
this.cvSource = CameraServer.getInstance().putVideo(name,
|
||||
cameraProcess.getProperties().staticProperties.imageWidth / divisor.value,
|
||||
cameraProcess.getProperties().staticProperties.imageHeight / divisor.value);
|
||||
setDivisor(divisor);
|
||||
}
|
||||
|
||||
public void setDivisor(StreamDivisor newDivisor) {
|
||||
this.divisor = newDivisor;
|
||||
var camValues = cameraProcess.getProperties();
|
||||
var newWidth = camValues.staticProperties.imageWidth / newDivisor.value;
|
||||
var newHeight = camValues.staticProperties.imageHeight / newDivisor.value;
|
||||
synchronized (streamBufferLock) {
|
||||
this.streamBuffer = new Mat(newWidth, newHeight, CvType.CV_8UC3);
|
||||
this.cvSource = CameraServer.getInstance().putVideo(this.name,
|
||||
cameraProcess.getProperties().staticProperties.imageWidth / divisor.value,
|
||||
cameraProcess.getProperties().staticProperties.imageHeight / divisor.value);
|
||||
}
|
||||
ServerHandler.sendFullSettings();
|
||||
}
|
||||
|
||||
public StreamDivisor getDivisor() {
|
||||
return divisor;
|
||||
}
|
||||
|
||||
public void setNewVideoMode(VideoMode newVideoMode) {
|
||||
// Trick to update cvSource and streamBuffer to the new resolution
|
||||
// Must change the cameraProcess resolution first
|
||||
setDivisor(divisor);
|
||||
}
|
||||
|
||||
public void runStream(Mat image) {
|
||||
synchronized (streamBufferLock) {
|
||||
streamBuffer = image;
|
||||
}
|
||||
// if (divisor.value != 1) {
|
||||
// var camVal = cameraProcess.getProperties().staticProperties;
|
||||
// var newWidth = camVal.imageWidth / divisor.value;
|
||||
// var newHeight = camVal.imageHeight / divisor.value;
|
||||
// Size newSize = new Size(newWidth, newHeight);
|
||||
// Imgproc.resize(streamBuffer, streamBuffer, newSize);
|
||||
// }
|
||||
cvSource.putFrame(streamBuffer);
|
||||
}
|
||||
}
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.vision.enums.StreamDivisor;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import edu.wpi.cscore.CvSource;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public class CameraStreamer {
|
||||
private final CameraProcess cameraProcess;
|
||||
private final String name;
|
||||
private StreamDivisor divisor = StreamDivisor.NONE;
|
||||
private CvSource cvSource;
|
||||
private final Object streamBufferLock = new Object();
|
||||
private Mat streamBuffer = new Mat();
|
||||
|
||||
public CameraStreamer(CameraProcess cameraProcess, String name) {
|
||||
this.cameraProcess = cameraProcess;
|
||||
this.name = name;
|
||||
this.cvSource = CameraServer.getInstance().putVideo(name,
|
||||
cameraProcess.getProperties().staticProperties.imageWidth / divisor.value,
|
||||
cameraProcess.getProperties().staticProperties.imageHeight / divisor.value);
|
||||
setDivisor(divisor);
|
||||
}
|
||||
|
||||
public void setDivisor(StreamDivisor newDivisor) {
|
||||
this.divisor = newDivisor;
|
||||
var camValues = cameraProcess.getProperties();
|
||||
var newWidth = camValues.staticProperties.imageWidth / newDivisor.value;
|
||||
var newHeight = camValues.staticProperties.imageHeight / newDivisor.value;
|
||||
synchronized (streamBufferLock) {
|
||||
this.streamBuffer = new Mat(newWidth, newHeight, CvType.CV_8UC3);
|
||||
this.cvSource = CameraServer.getInstance().putVideo(this.name,
|
||||
cameraProcess.getProperties().staticProperties.imageWidth / divisor.value,
|
||||
cameraProcess.getProperties().staticProperties.imageHeight / divisor.value);
|
||||
}
|
||||
ServerHandler.sendFullSettings();
|
||||
}
|
||||
|
||||
public StreamDivisor getDivisor() {
|
||||
return divisor;
|
||||
}
|
||||
|
||||
public void setNewVideoMode(VideoMode newVideoMode) {
|
||||
// Trick to update cvSource and streamBuffer to the new resolution
|
||||
// Must change the cameraProcess resolution first
|
||||
setDivisor(divisor);
|
||||
}
|
||||
|
||||
public void runStream(Mat image) {
|
||||
synchronized (streamBufferLock) {
|
||||
streamBuffer = image;
|
||||
}
|
||||
// if (divisor.value != 1) {
|
||||
// var camVal = cameraProcess.getProperties().staticProperties;
|
||||
// var newWidth = camVal.imageWidth / divisor.value;
|
||||
// var newHeight = camVal.imageHeight / divisor.value;
|
||||
// Size newSize = new Size(newWidth, newHeight);
|
||||
// Imgproc.resize(streamBuffer, streamBuffer, newSize);
|
||||
// }
|
||||
cvSource.putFrame(streamBuffer);
|
||||
}
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import org.apache.commons.math3.fraction.Fraction;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
|
||||
public class CameraValues {
|
||||
public final int ImageWidth;
|
||||
public final int ImageHeight;
|
||||
public final double FOV;
|
||||
public final double ImageArea;
|
||||
public final double CenterX;
|
||||
public final double CenterY;
|
||||
private final double HorizontalFocalLength;
|
||||
private final double VerticalFocalLength;
|
||||
|
||||
public CameraValues(USBCamera USBCamera) {
|
||||
this(USBCamera.getVideoMode().width, USBCamera.getVideoMode().height, USBCamera.getFOV());
|
||||
}
|
||||
|
||||
|
||||
public CameraValues(int imageWidth, int imageHeight, double fov) {
|
||||
ImageWidth = imageWidth;
|
||||
ImageHeight = imageHeight;
|
||||
FOV = fov;
|
||||
ImageArea = ImageWidth * ImageHeight;
|
||||
CenterX = ((double) ImageWidth / 2) - 0.5;
|
||||
CenterY = ((double) ImageHeight / 2) - 0.5;
|
||||
|
||||
// pinhole model calculations
|
||||
double diagonalView = FastMath.toRadians(FOV);
|
||||
Fraction aspectFraction = new Fraction(ImageWidth, ImageHeight);
|
||||
int horizontalRatio = aspectFraction.getNumerator();
|
||||
int verticalRatio = aspectFraction.getDenominator();
|
||||
double diagonalAspect = FastMath.hypot(horizontalRatio, verticalRatio);
|
||||
double horizontalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (horizontalRatio / diagonalAspect)) * 2;
|
||||
double verticalView = FastMath.atan(FastMath.tan(diagonalView / 2) * (verticalRatio / diagonalAspect)) * 2;
|
||||
HorizontalFocalLength = ImageWidth / (2 * FastMath.tan(horizontalView /2));
|
||||
VerticalFocalLength = ImageHeight / (2 * FastMath.tan(verticalView /2));
|
||||
}
|
||||
|
||||
public double CalculatePitch(double PixelY, double centerY) {
|
||||
double pitch = FastMath.toDegrees(FastMath.atan((PixelY - centerY) / VerticalFocalLength));
|
||||
return (pitch * -1);
|
||||
}
|
||||
|
||||
public double CalculateYaw(double PixelX, double centerX) {
|
||||
return FastMath.toDegrees(FastMath.atan((PixelX - centerX) / HorizontalFocalLength));
|
||||
}
|
||||
}
|
||||
@@ -1,347 +0,0 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import edu.wpi.cscore.*;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class USBCamera {
|
||||
|
||||
private static final double DEFAULT_FOV = 60.8;
|
||||
private static final StreamDivisor DEFAULT_STREAMDIVISOR = StreamDivisor.NONE;
|
||||
public static final int DEFAULT_EXPOSURE = 50;
|
||||
public static final int DEFAULT_BRIGHTNESS = 50;
|
||||
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;
|
||||
private static final List<VideoMode.PixelFormat> ALLOWED_PIXEL_FORMATS = Arrays.asList(VideoMode.PixelFormat.kYUYV, VideoMode.PixelFormat.kMJPEG);
|
||||
|
||||
public final String name;
|
||||
public final String path;
|
||||
|
||||
private String nickname;
|
||||
|
||||
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;
|
||||
private List<Pipeline> pipelines;
|
||||
|
||||
//Driver mode camera settings
|
||||
private int driverExposure;
|
||||
private int driverBrightness;
|
||||
private boolean isDriver;
|
||||
|
||||
public USBCamera(String cameraName) {
|
||||
this(cameraName, DEFAULT_FOV);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, double fov) {
|
||||
this(cameraName, CameraManager.allUsbCameraInfosByName.get(cameraName), fov);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCameraInfo, double fov) {
|
||||
this(cameraName, usbCameraInfo, fov, DEFAULT_STREAMDIVISOR);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCamInfo, double fov, StreamDivisor divisor) {
|
||||
this(cameraName, usbCamInfo, fov, new ArrayList<>(), 0, divisor, false);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
this(cameraName, CameraManager.allUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex, divisor, isDriver);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, double fov, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
this(cameraName, fov, new ArrayList<>(), videoModeIndex, divisor, isDriver);
|
||||
}
|
||||
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCamInfo, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
FOV = fov;
|
||||
name = cameraName;
|
||||
|
||||
if (Platform.CurrentPlatform.isWindows()) {
|
||||
path = usbCamInfo.path;
|
||||
} else {
|
||||
var truePath = Arrays.stream(usbCamInfo.otherPaths).filter(x -> x.contains("/dev/v4l/by-path")).findFirst();
|
||||
path = truePath.orElse(usbCamInfo.path);
|
||||
}
|
||||
|
||||
streamDivisor = divisor;
|
||||
UsbCam = new UsbCamera(name, path);
|
||||
|
||||
this.pipelines = pipelines;
|
||||
|
||||
// set up video modes according to minimums
|
||||
if (Platform.CurrentPlatform == 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("USBCameraProcess 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 && ALLOWED_PIXEL_FORMATS.contains(v.pixelFormat)).toArray(VideoMode[]::new);
|
||||
if (availableVideoModes.length == 0) {
|
||||
System.err.println("USBCameraProcess 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 / streamDivisor.value , camVals.ImageHeight / streamDivisor.value);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// update camera values
|
||||
camVals = new CameraValues(this);
|
||||
|
||||
if (prevVideoMode != null && !prevVideoMode.equals(newVideoMode)) { // if resolution changed
|
||||
UsbCam.setVideoMode(newVideoMode.getActualPixelFormat(), newVideoMode.width, newVideoMode.height, newVideoMode.fps);
|
||||
if (updateCvSource) {
|
||||
updateCvSource();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateCvSource() {
|
||||
synchronized (cvSourceLock) {
|
||||
var newWidth = camVideoMode.width / streamDivisor.value;
|
||||
var newHeight = camVideoMode.height / streamDivisor.value;
|
||||
cvSource = cs.putVideo(name, newWidth, newHeight);
|
||||
}
|
||||
CameraManager.getVisionProcessByCameraName(name).cameraProcess.updateFrameSize();
|
||||
ServerHandler.sendFullSettings();
|
||||
}
|
||||
|
||||
public void addPipeline() {
|
||||
Pipeline p = new Pipeline();
|
||||
p.nickname = "New pipeline " + pipelines.size();
|
||||
addPipeline(p);
|
||||
}
|
||||
|
||||
public void addPipeline(Pipeline p) {
|
||||
this.pipelines.add(p);
|
||||
}
|
||||
|
||||
public void deletePipeline(int index) {
|
||||
pipelines.remove(index);
|
||||
}
|
||||
|
||||
public void deletePipeline() {
|
||||
deletePipeline(getCurrentPipelineIndex());
|
||||
}
|
||||
|
||||
public Pipeline getCurrentPipeline() {
|
||||
return getPipelineByIndex(currentPipelineIndex);
|
||||
}
|
||||
|
||||
public Pipeline getPipelineByIndex(int pipelineIndex) {
|
||||
return pipelines.get(pipelineIndex);
|
||||
}
|
||||
|
||||
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, boolean updateCvSource) {
|
||||
streamDivisor = StreamDivisor.values()[divisor];
|
||||
if (updateCvSource) {
|
||||
updateCvSource();
|
||||
}
|
||||
}
|
||||
|
||||
public List<Pipeline> getPipelines() {
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
public List<String> getPipelinesNickname() {
|
||||
var pipelines = getPipelines();
|
||||
return pipelines.stream().map(pipeline -> pipeline.nickname).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public void setDriverMode(boolean state)
|
||||
{
|
||||
isDriver = state;
|
||||
if( isDriver ) {
|
||||
setBrightness(driverBrightness); //We call setBrightness because it updates after 2 calls
|
||||
setBrightness(driverBrightness); //Check it after we update to 2020 libraries
|
||||
setExposure(driverExposure);
|
||||
}
|
||||
else{
|
||||
UsbCam.setBrightness(getCurrentPipeline().brightness);
|
||||
UsbCam.setBrightness(getCurrentPipeline().brightness);
|
||||
try{UsbCam.setExposureManual(getCurrentPipeline().exposure);}
|
||||
catch (VideoException e)
|
||||
{
|
||||
System.out.println("Exposure change isn't supported");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getDriverMode()
|
||||
{
|
||||
return isDriver;
|
||||
}
|
||||
|
||||
public int getBrightness() {
|
||||
return UsbCam.getBrightness();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void setBrightness(int brightness) {
|
||||
if (isDriver) {
|
||||
driverBrightness = brightness;
|
||||
UsbCam.setBrightness(brightness); // set twice to reduce timeout
|
||||
}
|
||||
else {
|
||||
getCurrentPipeline().brightness = brightness;
|
||||
}
|
||||
UsbCam.setBrightness(brightness);
|
||||
}
|
||||
|
||||
public void setExposure(int exposure) {
|
||||
if (isDriver) {
|
||||
driverExposure = exposure;
|
||||
}
|
||||
else {
|
||||
getCurrentPipeline().exposure = exposure;
|
||||
}
|
||||
|
||||
try {
|
||||
UsbCam.setExposureManual(exposure);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("USBCameraProcess Does not support exposure change");
|
||||
}
|
||||
}
|
||||
|
||||
public long grabFrame(Mat image) {
|
||||
return cvSink.grabFrame(image);
|
||||
}
|
||||
|
||||
public CameraValues getCamVals() {
|
||||
return camVals;
|
||||
}
|
||||
|
||||
public void putFrame(Mat image) {
|
||||
synchronized (cvSourceLock) {
|
||||
cvSource.putFrame(image);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
//Deletes old camera nt table
|
||||
NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname).getInstance().deleteAllEntries();
|
||||
nickname = newNickname;
|
||||
if (CameraManager.allVisionProcessesByName.containsKey(this.name)) {
|
||||
NetworkTable newNT = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname);
|
||||
CameraManager.allVisionProcessesByName.get(this.name).resetNT(newNT);
|
||||
}
|
||||
}
|
||||
|
||||
public String getNickname() {
|
||||
return nickname == null ? name : nickname;
|
||||
}
|
||||
|
||||
public void setDriverExposure(int exposure) {
|
||||
driverExposure = exposure;
|
||||
|
||||
if (isDriver) {
|
||||
setExposure(exposure);
|
||||
}
|
||||
}
|
||||
|
||||
public void setDriverBrightness(int brightness) {
|
||||
driverBrightness = brightness;
|
||||
|
||||
if (isDriver) {
|
||||
setBrightness(brightness);
|
||||
}
|
||||
}
|
||||
|
||||
public int getDriverExposure() {
|
||||
return driverExposure;
|
||||
}
|
||||
|
||||
public int getDriverBrightness() {
|
||||
return driverBrightness;
|
||||
}
|
||||
}
|
||||
@@ -1,67 +1,67 @@
|
||||
package com.chameleonvision.classabstraction.camera;
|
||||
|
||||
import com.chameleonvision.classabstraction.config.CameraConfig;
|
||||
import edu.wpi.cscore.CvSink;
|
||||
import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.VideoException;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public class USBCameraProcess implements CameraProcess {
|
||||
private final UsbCamera baseCamera;
|
||||
private final CvSink cvSink;
|
||||
private Mat imageBuffer = new Mat();
|
||||
private USBCameraProperties properties;
|
||||
|
||||
public USBCameraProcess(CameraConfig config) {
|
||||
baseCamera = new UsbCamera(config.name, config.path);
|
||||
cvSink = CameraServer.getInstance().getVideo(baseCamera);
|
||||
properties = new USBCameraProperties(baseCamera, config);
|
||||
|
||||
setVideoMode(properties.videoModes.get(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public USBCameraProperties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> getFrame() {
|
||||
Long deltaTime;
|
||||
synchronized (cvSink) {
|
||||
deltaTime = cvSink.grabFrame(imageBuffer) * 1000L;
|
||||
}
|
||||
return Pair.of(imageBuffer, deltaTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExposure(int exposure) {
|
||||
try {
|
||||
baseCamera.setExposureManual(exposure);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support exposure change");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrightness(int brightness) {
|
||||
try {
|
||||
baseCamera.setBrightness(brightness);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support brightness change");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVideoMode(VideoMode mode) {
|
||||
try {
|
||||
baseCamera.setVideoMode(mode);
|
||||
properties.updateVideoMode(mode);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support resolution change");
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.config.CameraConfig;
|
||||
import edu.wpi.cscore.CvSink;
|
||||
import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.VideoException;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public class USBCameraProcess implements CameraProcess {
|
||||
private final UsbCamera baseCamera;
|
||||
private final CvSink cvSink;
|
||||
private Mat imageBuffer = new Mat();
|
||||
private USBCameraProperties properties;
|
||||
|
||||
public USBCameraProcess(CameraConfig config) {
|
||||
baseCamera = new UsbCamera(config.name, config.path);
|
||||
cvSink = CameraServer.getInstance().getVideo(baseCamera);
|
||||
properties = new USBCameraProperties(baseCamera, config);
|
||||
|
||||
setVideoMode(properties.videoModes.get(0));
|
||||
}
|
||||
|
||||
@Override
|
||||
public USBCameraProperties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<Mat, Long> getFrame() {
|
||||
Long deltaTime;
|
||||
synchronized (cvSink) {
|
||||
deltaTime = cvSink.grabFrame(imageBuffer) * 1000L;
|
||||
}
|
||||
return Pair.of(imageBuffer, deltaTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExposure(int exposure) {
|
||||
try {
|
||||
baseCamera.setExposureManual(exposure);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support exposure change");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrightness(int brightness) {
|
||||
try {
|
||||
baseCamera.setBrightness(brightness);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support brightness change");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setVideoMode(VideoMode mode) {
|
||||
try {
|
||||
baseCamera.setVideoMode(mode);
|
||||
properties.updateVideoMode(mode);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Current camera does not support resolution change");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.classabstraction.camera;
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.classabstraction.config.CameraConfig;
|
||||
import com.chameleonvision.settings.Platform;
|
||||
import com.chameleonvision.config.CameraConfig;
|
||||
import com.chameleonvision.util.Platform;
|
||||
import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.VideoMode;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum CalibrationMode {
|
||||
None,Single,Dual
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum ImageFlipMode {
|
||||
NONE(Integer.MIN_VALUE),
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
import org.opencv.core.Core;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum Orientation {
|
||||
//TODO: (low) add 90 and 270 deg rotation?
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum SortMode {
|
||||
Largest,Smallest,Highest,Lowest,Rightmost,Leftmost,Centermost
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum StreamDivisor {
|
||||
NONE(1),
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum TargetGroup {
|
||||
Single,
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.vision;
|
||||
package com.chameleonvision.vision.enums;
|
||||
|
||||
public enum TargetIntersection {
|
||||
None,Up,Down,Left,Right
|
||||
@@ -1,33 +1,30 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <R> Pipeline result type
|
||||
*/
|
||||
public abstract class CVPipeline<R extends CVPipelineResult, S extends CVPipelineSettings> {
|
||||
protected Mat outputMat = new Mat();
|
||||
CameraProcess cameraProcess;
|
||||
public final S settings;
|
||||
|
||||
protected CVPipeline(S settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
protected CVPipeline(String pipelineName, S settings) {
|
||||
this.settings = settings;
|
||||
settings.nickname = pipelineName;
|
||||
}
|
||||
|
||||
public void initPipeline(CameraProcess camera) {
|
||||
cameraProcess = camera;
|
||||
cameraProcess.setExposure((int) settings.exposure);
|
||||
cameraProcess.setBrightness((int) settings.brightness);
|
||||
}
|
||||
abstract public R runPipeline(Mat inputMat);
|
||||
}
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.camera.CameraProcess;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param <R> Pipeline result type
|
||||
*/
|
||||
public abstract class CVPipeline<R extends CVPipelineResult, S extends CVPipelineSettings> {
|
||||
protected Mat outputMat = new Mat();
|
||||
CameraProcess cameraProcess;
|
||||
public final S settings;
|
||||
|
||||
protected CVPipeline(S settings) {
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
protected CVPipeline(String pipelineName, S settings) {
|
||||
this.settings = settings;
|
||||
settings.nickname = pipelineName;
|
||||
}
|
||||
|
||||
public void initPipeline(CameraProcess camera) {
|
||||
cameraProcess = camera;
|
||||
cameraProcess.setExposure((int) settings.exposure);
|
||||
cameraProcess.setBrightness((int) settings.brightness);
|
||||
}
|
||||
abstract public R runPipeline(Mat inputMat);
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.classabstraction.pipeline.pipes.*;
|
||||
import com.chameleonvision.vision.ImageRotation;
|
||||
import com.chameleonvision.vision.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.vision.pipeline.pipes.*;
|
||||
import com.chameleonvision.vision.enums.ImageRotation;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.classabstraction.pipeline.CVPipeline2d.*;
|
||||
import static com.chameleonvision.vision.pipeline.CVPipeline2d.*;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSettings> {
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.CalibrationMode;
|
||||
import com.chameleonvision.vision.SortMode;
|
||||
import com.chameleonvision.vision.TargetGroup;
|
||||
import com.chameleonvision.vision.TargetIntersection;
|
||||
import com.chameleonvision.vision.enums.CalibrationMode;
|
||||
import com.chameleonvision.vision.enums.SortMode;
|
||||
import com.chameleonvision.vision.enums.TargetGroup;
|
||||
import com.chameleonvision.vision.enums.TargetIntersection;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -1,12 +1,10 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.chameleonvision.classabstraction.pipeline.CVPipeline3d.*;
|
||||
import static com.chameleonvision.vision.pipeline.CVPipeline3d.*;
|
||||
|
||||
public class CVPipeline3d extends CVPipeline<CVPipeline3dResult, CVPipeline3dSettings> {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
public class CVPipeline3dSettings extends CVPipeline2dSettings {
|
||||
// TODO: (2.1) define 3d-specific pipeline settings
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.*;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class CVPipelineSettings {
|
||||
public ImageFlipMode flipMode = ImageFlipMode.NONE;
|
||||
public String nickname = "New Pipeline";
|
||||
public double exposure = 50.0;
|
||||
public double brightness = 50.0;
|
||||
}
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.enums.ImageFlipMode;
|
||||
|
||||
@SuppressWarnings("ALL")
|
||||
public class CVPipelineSettings {
|
||||
public ImageFlipMode flipMode = ImageFlipMode.NONE;
|
||||
public String nickname = "New Pipeline";
|
||||
public double exposure = 50.0;
|
||||
public double brightness = 50.0;
|
||||
}
|
||||
@@ -1,39 +1,38 @@
|
||||
package com.chameleonvision.classabstraction.pipeline;
|
||||
|
||||
import com.chameleonvision.classabstraction.pipeline.pipes.Draw2dContoursPipe;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.chameleonvision.classabstraction.pipeline.DriverVisionPipeline.DriverPipelineResult;
|
||||
|
||||
public class DriverVisionPipeline extends CVPipeline<DriverPipelineResult, CVPipelineSettings> {
|
||||
|
||||
public DriverVisionPipeline(CVPipelineSettings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DriverPipelineResult runPipeline(Mat inputMat) {
|
||||
|
||||
outputMat = inputMat;
|
||||
|
||||
var camProps = cameraProcess.getProperties().staticProperties;
|
||||
|
||||
Draw2dContoursPipe.Draw2dContoursSettings draw2dContoursSettings = new Draw2dContoursPipe.Draw2dContoursSettings();
|
||||
draw2dContoursSettings.showCrosshair = true;
|
||||
Draw2dContoursPipe draw2dContoursPipe = new Draw2dContoursPipe(draw2dContoursSettings, camProps);
|
||||
|
||||
outputMat = draw2dContoursPipe.run(Pair.of(outputMat, null)).getLeft();
|
||||
|
||||
return new DriverPipelineResult(null, inputMat, 0);
|
||||
}
|
||||
|
||||
public static class DriverPipelineResult extends CVPipelineResult<Void> {
|
||||
public DriverPipelineResult(List<Void> targets, Mat outputMat, long processTime) {
|
||||
super(targets, outputMat, processTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.chameleonvision.vision.pipeline;
|
||||
|
||||
import com.chameleonvision.vision.pipeline.pipes.Draw2dContoursPipe;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.vision.pipeline.DriverVisionPipeline.DriverPipelineResult;
|
||||
|
||||
public class DriverVisionPipeline extends CVPipeline<DriverPipelineResult, CVPipelineSettings> {
|
||||
|
||||
public DriverVisionPipeline(CVPipelineSettings settings) {
|
||||
super(settings);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DriverPipelineResult runPipeline(Mat inputMat) {
|
||||
|
||||
outputMat = inputMat;
|
||||
|
||||
var camProps = cameraProcess.getProperties().staticProperties;
|
||||
|
||||
Draw2dContoursPipe.Draw2dContoursSettings draw2dContoursSettings = new Draw2dContoursPipe.Draw2dContoursSettings();
|
||||
draw2dContoursSettings.showCrosshair = true;
|
||||
Draw2dContoursPipe draw2dContoursPipe = new Draw2dContoursPipe(draw2dContoursSettings, camProps);
|
||||
|
||||
outputMat = draw2dContoursPipe.run(Pair.of(outputMat, null)).getLeft();
|
||||
|
||||
return new DriverPipelineResult(null, inputMat, 0);
|
||||
}
|
||||
|
||||
public static class DriverPipelineResult extends CVPipelineResult<Void> {
|
||||
public DriverPipelineResult(List<Void> targets, Mat outputMat, long processTime) {
|
||||
super(targets, outputMat, processTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipeline2d;
|
||||
import com.chameleonvision.vision.CalibrationMode;
|
||||
import com.chameleonvision.vision.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline2d;
|
||||
import com.chameleonvision.vision.enums.CalibrationMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.opencv.core.RotatedRect;
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.classabstraction.util.Helpers;
|
||||
import com.chameleonvision.vision.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.util.Helpers;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Point;
|
||||
import org.opencv.core.*;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.vision.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.util.MathHandler;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.MatOfPoint;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.util.MathHandler;
|
||||
import com.chameleonvision.vision.TargetGroup;
|
||||
import com.chameleonvision.vision.TargetIntersection;
|
||||
import com.chameleonvision.vision.enums.TargetGroup;
|
||||
import com.chameleonvision.vision.enums.TargetIntersection;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
@@ -1,10 +1,9 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class HsvPipe implements Pipe<Mat, Mat> {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.vision.ImageFlipMode;
|
||||
import com.chameleonvision.vision.ImageRotation;
|
||||
import com.chameleonvision.vision.enums.ImageFlipMode;
|
||||
import com.chameleonvision.vision.enums.ImageRotation;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.Mat;
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import com.chameleonvision.classabstraction.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.vision.SortMode;
|
||||
import com.chameleonvision.vision.camera.CameraStaticProperties;
|
||||
import com.chameleonvision.vision.enums.SortMode;
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.opencv.core.RotatedRect;
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.chameleonvision.classabstraction.pipeline.pipes;
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.MatOfPoint;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.vision.camera.CameraValues;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
public interface CVProcess {
|
||||
PipelineResult runPipeline(Pipeline currentPipeline, Mat inputImage, Mat outputImage, CameraValues cameraValues, boolean shouldFlip, boolean driverMode);
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.vision.camera.CamVideoMode;
|
||||
import com.chameleonvision.vision.camera.CameraValues;
|
||||
import org.opencv.core.Mat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CameraProcess extends Runnable {
|
||||
|
||||
long getLatestFrame(Mat outputFrame);
|
||||
|
||||
void updateFrame(Mat inputFrame);
|
||||
|
||||
void updateFrameSize();
|
||||
|
||||
String getCamName();
|
||||
CameraValues getCamVals();
|
||||
boolean getDriverMode();
|
||||
void setDriverMode(boolean isDriverMode);
|
||||
List<Pipeline> getPipelines();
|
||||
Pipeline getCurrentPipeline();
|
||||
int getCurrentPipelineIndex();
|
||||
void setExposure(int exposure);
|
||||
void setBrightness(int brightness);
|
||||
CamVideoMode getVideoMode();
|
||||
String getNickname();
|
||||
void setCurrentPipelineIndex(int ntPipelineIndex);
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import org.opencv.core.RotatedRect;
|
||||
|
||||
public class PipelineResult {
|
||||
public boolean IsValid = false;
|
||||
public double CalibratedX = 0.0;
|
||||
public double CalibratedY = 0.0;
|
||||
public double Pitch = 0.0;
|
||||
public double Yaw = 0.0;
|
||||
public double Area = 0.0;
|
||||
RotatedRect RawPoint;
|
||||
}
|
||||
@@ -1,345 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.vision.*;
|
||||
import com.chameleonvision.vision.camera.CameraValues;
|
||||
import com.chameleonvision.util.MathHandler;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
import org.opencv.imgproc.Moments;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
public class StandardCVProcess implements CVProcess {
|
||||
|
||||
private final CameraValues cameraValues;
|
||||
private Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
|
||||
private Size blur = new Size(3, 3);
|
||||
private Mat hsvImage = new Mat();
|
||||
private List<MatOfPoint> foundContours = new ArrayList<>();
|
||||
private Mat binaryMat = new Mat();
|
||||
private List<MatOfPoint> filteredContours = new ArrayList<>();
|
||||
private Comparator<RotatedRect> sortByCentermostComparator = Comparator.comparingDouble(this::calcDistance);
|
||||
private List<MatOfPoint> speckleRejectedContours = new ArrayList<>();
|
||||
private Comparator<MatOfPoint> sortByMomentsX = Comparator.comparingDouble(this::calcMomentsX);
|
||||
private List<RotatedRect> finalCountours = new ArrayList<>();
|
||||
private MatOfPoint2f intersectMatA = new MatOfPoint2f();
|
||||
private MatOfPoint2f intersectMatB = new MatOfPoint2f();
|
||||
|
||||
StandardCVProcess(CameraValues cameraValues) {
|
||||
this.cameraValues = cameraValues;
|
||||
}
|
||||
|
||||
private Mat cameraInputMat = new Mat();
|
||||
private Mat hsvThreshMat = new Mat();
|
||||
private Mat streamOutputMat = new Mat();
|
||||
|
||||
private List<MatOfPoint> foundContours_ = new ArrayList<>();
|
||||
private List<MatOfPoint> filteredContours_ = new ArrayList<>();
|
||||
private List<MatOfPoint> deSpeckledContours_ = new ArrayList<>();
|
||||
private List<RotatedRect> groupedContours_ = new ArrayList<>();
|
||||
|
||||
private static final Scalar contourRectColor = new Scalar(255, 0, 0);
|
||||
private static final Scalar BoxRectColor = new Scalar(0, 0, 233);
|
||||
|
||||
private void drawContour(Mat inputMat, RotatedRect contourRect) {
|
||||
if (contourRect == null) return;
|
||||
List<MatOfPoint> drawnContour = new ArrayList<>();
|
||||
Point[] vertices = new Point[4];
|
||||
contourRect.points(vertices);
|
||||
MatOfPoint contour = new MatOfPoint(vertices);
|
||||
drawnContour.add(contour);
|
||||
Rect box = Imgproc.boundingRect(contour);
|
||||
Imgproc.drawContours(inputMat, drawnContour, 0, contourRectColor, 3);
|
||||
Imgproc.circle(inputMat, contourRect.center, 3, contourRectColor);
|
||||
Imgproc.rectangle(inputMat, new Point(box.x, box.y), new Point((box.x + box.width), (box.y + box.height)), BoxRectColor, 2);
|
||||
}
|
||||
|
||||
public PipelineResult runPipeline(Pipeline currentPipeline, Mat inputImage, Mat outputImage, CameraValues cameraValues, boolean shouldFlip, boolean driverMode) {
|
||||
var pipelineResult = new PipelineResult();
|
||||
|
||||
if (currentPipeline == null) {
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
// flip the image
|
||||
if (shouldFlip) {
|
||||
Core.flip(inputImage, inputImage, -1);
|
||||
}
|
||||
|
||||
// if we're in driver mode don't do anything, and return a blank result
|
||||
if (driverMode) {
|
||||
inputImage.copyTo(outputImage);
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
foundContours_.clear();
|
||||
filteredContours_.clear();
|
||||
deSpeckledContours_.clear();
|
||||
groupedContours_.clear();
|
||||
|
||||
// HSV threshold the image
|
||||
Scalar hsvLower = new Scalar(currentPipeline.hue.get(0).intValue(), currentPipeline.saturation.get(0).intValue(), currentPipeline.value.get(0).intValue());
|
||||
Scalar hsvUpper = new Scalar(currentPipeline.hue.get(1).intValue(), currentPipeline.saturation.get(1).intValue(), currentPipeline.value.get(1).intValue());
|
||||
hsvThreshold(inputImage, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate);
|
||||
|
||||
// Make sure we're BFR
|
||||
if (currentPipeline.isBinary) {
|
||||
Imgproc.cvtColor(hsvThreshMat, outputImage, Imgproc.COLOR_GRAY2BGR, 3);
|
||||
} else {
|
||||
inputImage.copyTo(outputImage);
|
||||
}
|
||||
|
||||
// search for contours
|
||||
foundContours_ = findContours(hsvThreshMat);
|
||||
if (foundContours_.size() < 1) {
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
// filter contours by area, ratio and extent
|
||||
filteredContours_ = filterContours(foundContours_, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent);
|
||||
if (filteredContours_.size() < 1) {
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
// reject "speckle" contours
|
||||
deSpeckledContours_ = rejectSpeckles(filteredContours_, currentPipeline.speckle.doubleValue());
|
||||
if (deSpeckledContours_.size() < 1) {
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
// group targets
|
||||
groupedContours_ = groupTargets(deSpeckledContours_, currentPipeline.targetIntersection, currentPipeline.targetGroup);
|
||||
if (groupedContours_.size() < 1) {
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
// sort targets down to our final target
|
||||
var finalRect = sortTargetsToOne(groupedContours_, currentPipeline.sortMode);
|
||||
pipelineResult.RawPoint = finalRect;
|
||||
pipelineResult.IsValid = true;
|
||||
switch (currentPipeline.calibrationMode) {
|
||||
case None:
|
||||
///use the center of the USBCameraProcess to find the pitch and yaw difference
|
||||
pipelineResult.CalibratedX = cameraValues.CenterX;
|
||||
pipelineResult.CalibratedY = cameraValues.CenterY;
|
||||
break;
|
||||
case Single:
|
||||
// use the static point as a calibration method instead of the center
|
||||
pipelineResult.CalibratedX = currentPipeline.point.get(0).doubleValue();
|
||||
pipelineResult.CalibratedY = currentPipeline.point.get(1).doubleValue();
|
||||
break;
|
||||
case Dual:
|
||||
// use the calculated line to find the difference in length between the point and the line
|
||||
pipelineResult.CalibratedX = (finalRect.center.y - currentPipeline.b) / currentPipeline.m;
|
||||
pipelineResult.CalibratedY = (finalRect.center.x * currentPipeline.m) + currentPipeline.b;
|
||||
break;
|
||||
}
|
||||
|
||||
pipelineResult.Pitch = cameraValues.CalculatePitch(finalRect.center.y, pipelineResult.CalibratedY);
|
||||
pipelineResult.Yaw = cameraValues.CalculateYaw(finalRect.center.x, pipelineResult.CalibratedX);
|
||||
pipelineResult.Area = finalRect.size.area();
|
||||
drawContour(outputImage, finalRect);
|
||||
|
||||
return pipelineResult;
|
||||
}
|
||||
|
||||
void hsvThreshold(Mat srcImage, Mat dst, @NotNull Scalar hsvLower, @NotNull Scalar hsvUpper, boolean shouldErode, boolean shouldDilate) {
|
||||
Imgproc.cvtColor(srcImage, hsvImage, Imgproc.COLOR_RGB2HSV, 3);
|
||||
Imgproc.blur(hsvImage, hsvImage, blur);
|
||||
Core.inRange(hsvImage, hsvLower, hsvUpper, dst);
|
||||
if (shouldErode) {
|
||||
Imgproc.erode(dst, dst, kernel);
|
||||
}
|
||||
if (shouldDilate) {
|
||||
Imgproc.dilate(dst, dst, kernel);
|
||||
}
|
||||
hsvImage.release();
|
||||
}
|
||||
|
||||
List<MatOfPoint> findContours(Mat src) {
|
||||
src.copyTo(binaryMat);
|
||||
foundContours.clear();
|
||||
Imgproc.findContours(binaryMat, foundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
||||
binaryMat.release();
|
||||
return foundContours;
|
||||
}
|
||||
|
||||
List<MatOfPoint> filterContours(List<MatOfPoint> inputContours, List<Number> area, List<Number> ratio, List<Number> extent) {
|
||||
for (MatOfPoint Contour : inputContours) {
|
||||
try {
|
||||
double contourArea = Imgproc.contourArea(Contour);
|
||||
double AreaRatio = (contourArea / cameraValues.ImageArea) * 100;
|
||||
double minArea = (MathHandler.sigmoid(area.get(0)));
|
||||
double maxArea = (MathHandler.sigmoid(area.get(1)));
|
||||
if (AreaRatio < minArea || AreaRatio > maxArea) {
|
||||
continue;
|
||||
}
|
||||
var rect = Imgproc.minAreaRect(new MatOfPoint2f(Contour.toArray()));
|
||||
|
||||
var targetFullness = contourArea;
|
||||
double minExtent = (double) (extent.get(0).doubleValue() * rect.size.area()) / 100;
|
||||
double maxExtent = (double) (extent.get(1).doubleValue() * rect.size.area()) / 100;
|
||||
if (targetFullness <= minExtent || contourArea >= maxExtent) {
|
||||
continue;
|
||||
}
|
||||
Rect bb = Imgproc.boundingRect(Contour);
|
||||
double aspectRatio = (bb.width / bb.height);
|
||||
if (aspectRatio < ratio.get(0).doubleValue() || aspectRatio > ratio.get(1).doubleValue()) {
|
||||
continue;
|
||||
}
|
||||
filteredContours.add(Contour);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error while filtering contours");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return filteredContours;
|
||||
}
|
||||
|
||||
List<MatOfPoint> rejectSpeckles(List<MatOfPoint> inputContours, Double minimumPercentOfAverage) {
|
||||
double averageArea = 0.0;
|
||||
for (MatOfPoint c : inputContours) {
|
||||
averageArea += Imgproc.contourArea(c);
|
||||
}
|
||||
averageArea /= inputContours.size();
|
||||
var minimumAllowableArea = minimumPercentOfAverage / 100.0 * averageArea;
|
||||
speckleRejectedContours.clear();
|
||||
for (MatOfPoint c : inputContours) {
|
||||
if (Imgproc.contourArea(c) >= minimumAllowableArea) speckleRejectedContours.add(c);
|
||||
}
|
||||
return speckleRejectedContours;
|
||||
}
|
||||
|
||||
|
||||
private double calcDistance(RotatedRect rect) {
|
||||
return FastMath.sqrt(FastMath.pow(cameraValues.CenterX - rect.center.x, 2) + FastMath.pow(cameraValues.CenterY - rect.center.y, 2));
|
||||
}
|
||||
|
||||
private double calcMomentsX(MatOfPoint c) {
|
||||
Moments m = Imgproc.moments(c);
|
||||
return (m.get_m10() / m.get_m00());
|
||||
}
|
||||
|
||||
RotatedRect sortTargetsToOne(List<RotatedRect> inputRects, SortMode sortMode) {
|
||||
switch (sortMode) {
|
||||
case Largest:
|
||||
return Collections.max(inputRects, Comparator.comparing(rect -> rect.size.area()));
|
||||
case Smallest:
|
||||
return Collections.min(inputRects, Comparator.comparing(rect -> rect.size.area()));
|
||||
case Highest:
|
||||
return Collections.min(inputRects, Comparator.comparing(rect -> rect.center.y));
|
||||
case Lowest:
|
||||
return Collections.max(inputRects, Comparator.comparing(rect -> rect.center.y));
|
||||
case Leftmost:
|
||||
return Collections.min(inputRects, Comparator.comparing(rect -> rect.center.x));
|
||||
case Rightmost:
|
||||
return Collections.max(inputRects, Comparator.comparing(rect -> rect.center.x));
|
||||
case Centermost:
|
||||
return Collections.min(inputRects, sortByCentermostComparator);
|
||||
default:
|
||||
return inputRects.get(0); // default to whatever the first contour is, but this should never happen
|
||||
}
|
||||
}
|
||||
|
||||
List<RotatedRect> groupTargets(List<MatOfPoint> inputContours, TargetIntersection intersectionPoint, TargetGroup targetGroup) {
|
||||
finalCountours.clear();
|
||||
inputContours.sort(sortByMomentsX);
|
||||
Collections.reverse(inputContours);
|
||||
if (targetGroup.equals(TargetGroup.Dual)) {
|
||||
for (var i = 0; i < inputContours.size(); i++) {
|
||||
List<Point> FinalContourList = new ArrayList<>(inputContours.get(i).toList());
|
||||
try {
|
||||
MatOfPoint firstContour = inputContours.get(i);
|
||||
MatOfPoint secondContour = inputContours.get(i + 1);
|
||||
if (isIntersecting(firstContour, secondContour, intersectionPoint)) {
|
||||
FinalContourList.addAll(secondContour.toList());
|
||||
} else {
|
||||
FinalContourList.clear();
|
||||
continue;
|
||||
}
|
||||
firstContour.release();
|
||||
secondContour.release();
|
||||
MatOfPoint2f contour = new MatOfPoint2f();
|
||||
contour.fromList(FinalContourList);
|
||||
if (contour.cols() != 0 && contour.rows() != 0) {
|
||||
RotatedRect rect = Imgproc.minAreaRect(contour);
|
||||
finalCountours.add(rect);
|
||||
}
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
FinalContourList.clear();
|
||||
}
|
||||
}
|
||||
|
||||
} else if (targetGroup.equals(TargetGroup.Single)) {
|
||||
for (MatOfPoint inputContour : inputContours) {
|
||||
MatOfPoint2f contour = new MatOfPoint2f();
|
||||
contour.fromArray(inputContour.toArray());
|
||||
if (contour.cols() != 0 && contour.rows() != 0) {
|
||||
RotatedRect rect = Imgproc.minAreaRect(contour);
|
||||
finalCountours.add(rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
return finalCountours;
|
||||
}
|
||||
|
||||
private boolean isIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, TargetIntersection intersectionPoint) {
|
||||
if (intersectionPoint.equals(TargetIntersection.None)) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
intersectMatA.fromArray(ContourOne.toArray());
|
||||
intersectMatB.fromArray(ContourTwo.toArray());
|
||||
RotatedRect a = Imgproc.fitEllipse(intersectMatA);
|
||||
RotatedRect b = Imgproc.fitEllipse(intersectMatB);
|
||||
double mA = MathHandler.toSlope(a.angle);
|
||||
double mB = MathHandler.toSlope(b.angle);
|
||||
double x0A = a.center.x;
|
||||
double y0A = a.center.y;
|
||||
double x0B = b.center.x;
|
||||
double y0B = b.center.y;
|
||||
double intersectionX = ((mA * x0A) - y0A - (mB * x0B) + y0B) / (mA - mB);
|
||||
double intersectionY = (mA * (intersectionX - x0A)) + y0A;
|
||||
double massX = (x0A + x0B) / 2;
|
||||
double massY = (y0A + y0B) / 2;
|
||||
switch (intersectionPoint) {
|
||||
case Up: {
|
||||
if (intersectionY < massY) {
|
||||
if (mA > 0 && mB < 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Down: {
|
||||
if (intersectionY > massY) {
|
||||
if (mA < 0 && mB > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case Left: {
|
||||
if (intersectionX < massX) {
|
||||
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Right: {
|
||||
if (intersectionX > massX) {
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,144 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.vision.camera.CamVideoMode;
|
||||
import com.chameleonvision.vision.camera.CameraValues;
|
||||
import com.chameleonvision.vision.camera.USBCamera;
|
||||
import com.chameleonvision.vision.camera.StreamDivisor;
|
||||
import org.opencv.core.CvType;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class USBCameraProcess implements CameraProcess {
|
||||
|
||||
private final USBCamera usbCamera;
|
||||
private final int maxFPS;
|
||||
private final Object inputFrameLock = new Object();
|
||||
private final Object outputFrameLock = new Object();
|
||||
private Mat inputFrame;
|
||||
private Mat outputFrame;
|
||||
private long timestamp;
|
||||
private StreamDivisor divisor;
|
||||
|
||||
public USBCameraProcess(USBCamera usbCamera) {
|
||||
this.usbCamera = usbCamera;
|
||||
maxFPS = usbCamera.getVideoMode().fps;
|
||||
updateFrameSize();
|
||||
}
|
||||
|
||||
public void updateFrameSize() {
|
||||
var camVals = usbCamera.getCamVals();
|
||||
divisor = usbCamera.getStreamDivisor();
|
||||
var newWidth = camVals.ImageWidth / divisor.value;
|
||||
var newHeight = camVals.ImageHeight / divisor.value;
|
||||
synchronized (inputFrameLock) {
|
||||
inputFrame = new Mat(newWidth, newHeight, CvType.CV_8UC3);
|
||||
}
|
||||
synchronized (outputFrameLock) {
|
||||
outputFrame = new Mat(newWidth, newHeight, CvType.CV_8UC3);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateFrame(Mat inputFrame) {
|
||||
synchronized (inputFrameLock) {
|
||||
inputFrame.copyTo(this.inputFrame);
|
||||
}
|
||||
}
|
||||
|
||||
public long getLatestFrame(Mat outputFrame) {
|
||||
synchronized (outputFrameLock) {
|
||||
this.outputFrame.copyTo(outputFrame);
|
||||
return timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!Thread.interrupted()) {
|
||||
synchronized (outputFrameLock) {
|
||||
timestamp = usbCamera.grabFrame(outputFrame);
|
||||
}
|
||||
synchronized (inputFrameLock) {
|
||||
if (divisor.value != 1) {
|
||||
var camVals = usbCamera.getCamVals();
|
||||
var newWidth = camVals.ImageWidth / divisor.value;
|
||||
var newHeight = camVals.ImageHeight / divisor.value;
|
||||
Size newSize = new Size(newWidth, newHeight);
|
||||
Imgproc.resize(inputFrame, inputFrame, newSize);
|
||||
}
|
||||
usbCamera.putFrame(inputFrame);
|
||||
}
|
||||
var msToWait = (long) 1000 / maxFPS;
|
||||
try {
|
||||
Thread.sleep(msToWait);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// USBCameraProcess stuff
|
||||
|
||||
@Override
|
||||
public String getCamName() {
|
||||
return usbCamera.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CameraValues getCamVals() {
|
||||
return usbCamera.getCamVals();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getDriverMode() {
|
||||
return usbCamera.getDriverMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDriverMode(boolean isDriverMode) {
|
||||
usbCamera.setDriverMode(isDriverMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Pipeline> getPipelines() {
|
||||
return usbCamera.getPipelines();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pipeline getCurrentPipeline() {
|
||||
return usbCamera.getCurrentPipeline();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCurrentPipelineIndex() {
|
||||
return usbCamera.getCurrentPipelineIndex();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExposure(int exposure) {
|
||||
usbCamera.setExposure(exposure);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrightness(int brightness) {
|
||||
usbCamera.setBrightness(brightness);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CamVideoMode getVideoMode() {
|
||||
return usbCamera.getVideoMode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getNickname() {
|
||||
return usbCamera.getNickname();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCurrentPipelineIndex(int wantedIndex) {
|
||||
usbCamera.setCurrentPipelineIndex(wantedIndex);
|
||||
}
|
||||
}
|
||||
@@ -1,269 +0,0 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipeline2d;
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipeline2dSettings;
|
||||
import com.chameleonvision.classabstraction.pipeline.DriverVisionPipeline;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.chameleonvision.web.ServerHandler;
|
||||
import edu.wpi.cscore.VideoException;
|
||||
import edu.wpi.first.networktables.*;
|
||||
import org.opencv.core.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static com.chameleonvision.classabstraction.pipeline.CVPipeline2d.*;
|
||||
|
||||
public class VisionProcess implements Runnable {
|
||||
|
||||
private final String cameraName;
|
||||
public final CameraProcess cameraProcess;
|
||||
// NetworkTables
|
||||
public NetworkTableEntry ntPipelineEntry;
|
||||
public NetworkTableEntry ntDriverModeEntry;
|
||||
private int ntDriveModeListenerID;
|
||||
private int ntPipelineListenerID;
|
||||
private NetworkTableEntry ntYawEntry;
|
||||
private NetworkTableEntry ntPitchEntry;
|
||||
private NetworkTableEntry ntDistanceEntry;
|
||||
private NetworkTableEntry ntTimeStampEntry;
|
||||
private NetworkTableEntry ntValidEntry;
|
||||
// chameleon specific
|
||||
private Pipeline currentPipeline;
|
||||
private CVProcess cvProcess;
|
||||
|
||||
private CVPipeline2d cvPipeline2d;
|
||||
private DriverVisionPipeline driverVisionPipeline;
|
||||
|
||||
// pipeline process items
|
||||
// private List<MatOfPoint> foundContours = new ArrayList<>();
|
||||
// private List<MatOfPoint> filteredContours = new ArrayList<>();
|
||||
// private List<MatOfPoint> deSpeckledContours = new ArrayList<>();
|
||||
// private List<RotatedRect> groupedContours = new ArrayList<>();
|
||||
private Mat cameraInputMat = new Mat();
|
||||
private Mat hsvThreshMat = new Mat();
|
||||
private Mat streamOutputMat = new Mat();
|
||||
private long timeStamp = 0;
|
||||
|
||||
public VisionProcess(CameraProcess cameraProcess) {
|
||||
|
||||
cvProcess = new StandardCVProcess(cameraProcess.getCamVals());
|
||||
this.cameraProcess = cameraProcess; // new USBCameraProcess(cameraProcess);
|
||||
|
||||
this.cameraName = cameraProcess.getCamName();
|
||||
|
||||
initNT(NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + cameraProcess.getNickname()));
|
||||
}
|
||||
|
||||
private void driverModeListener(EntryNotification entryNotification) {
|
||||
cameraProcess.setDriverMode(entryNotification.value.getBoolean());
|
||||
}
|
||||
|
||||
private void pipelineListener(EntryNotification entryNotification) {
|
||||
var ntPipelineIndex = (int) entryNotification.value.getDouble();
|
||||
if (ntPipelineIndex >= cameraProcess.getPipelines().size()) {
|
||||
ntPipelineEntry.setNumber(cameraProcess.getCurrentPipelineIndex());
|
||||
} else {
|
||||
var pipeline = cameraProcess.getCurrentPipeline();
|
||||
cameraProcess.setCurrentPipelineIndex(ntPipelineIndex);
|
||||
try {
|
||||
cameraProcess.setExposure(pipeline.exposure);
|
||||
} catch (VideoException e) {
|
||||
System.err.println(e.toString());
|
||||
}
|
||||
cameraProcess.setBrightness(pipeline.brightness);
|
||||
if (SettingsManager.generalSettings.currentCamera.equals(cameraName)) {
|
||||
SettingsManager.generalSettings.currentPipeline = ntPipelineIndex;
|
||||
HashMap<String, Object> pipeChange = new HashMap<>();
|
||||
pipeChange.put("currentPipeline", ntPipelineIndex);
|
||||
ServerHandler.broadcastMessage(pipeChange);
|
||||
ServerHandler.sendFullSettings();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void updateNetworkTables(PipelineResult pipelineResult) {
|
||||
if (pipelineResult.IsValid) {
|
||||
ntValidEntry.setBoolean(true);
|
||||
ntYawEntry.setNumber(pipelineResult.Yaw);
|
||||
ntPitchEntry.setNumber(pipelineResult.Pitch);
|
||||
ntDistanceEntry.setNumber(pipelineResult.Area);
|
||||
ntTimeStampEntry.setNumber(timeStamp);
|
||||
NetworkTableInstance.getDefault().flush();
|
||||
} else {
|
||||
ntYawEntry.setNumber(0.0);
|
||||
ntPitchEntry.setNumber(0.0);
|
||||
ntDistanceEntry.setNumber(0.0);
|
||||
ntTimeStampEntry.setNumber(timeStamp);
|
||||
ntValidEntry.setBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
private CVPipeline2dSettings pipelineTo2dSettings(Pipeline pipeline) {
|
||||
CVPipeline2dSettings settings = new CVPipeline2dSettings();
|
||||
settings.hue = pipeline.hue;
|
||||
settings.saturation = pipeline.saturation;
|
||||
settings.value = pipeline.value;
|
||||
settings.erode = pipeline.erode;
|
||||
settings.dilate = pipeline.dilate;
|
||||
settings.area = pipeline.area;
|
||||
settings.ratio = pipeline.ratio;
|
||||
settings.extent = pipeline.extent;
|
||||
settings.speckle = pipeline.speckle;
|
||||
settings.isBinary = pipeline.isBinary;
|
||||
settings.sortMode = pipeline.sortMode;
|
||||
settings.targetGroup = pipeline.targetGroup;
|
||||
settings.targetIntersection = pipeline.targetIntersection;
|
||||
settings.point = pipeline.point;
|
||||
settings.calibrationMode = pipeline.calibrationMode;
|
||||
settings.nickname = pipeline.nickname;
|
||||
settings.exposure = pipeline.exposure;
|
||||
settings.brightness = pipeline.brightness;
|
||||
settings.dualTargetCalibrationM = pipeline.m;
|
||||
settings.dualTargetCalibrationB = pipeline.b;
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
private PipelineResult runVisionProcess(Mat inputImage, Mat outputImage) {
|
||||
|
||||
if (cvPipeline2d == null) {
|
||||
cvPipeline2d = new CVPipeline2d(pipelineTo2dSettings(currentPipeline));
|
||||
}
|
||||
CVPipeline2dResult result = cvPipeline2d.runPipeline(inputImage);
|
||||
result.outputMat.copyTo(outputImage);
|
||||
|
||||
PipelineResult pipeResult = new PipelineResult();
|
||||
pipeResult.IsValid = result.hasTarget;
|
||||
if (!pipeResult.IsValid) {
|
||||
pipeResult.CalibratedX = 0;
|
||||
pipeResult.CalibratedY = 0;
|
||||
pipeResult.Pitch = 0;
|
||||
pipeResult.Yaw = 0;
|
||||
pipeResult.Area = 0;
|
||||
} else {
|
||||
Target2d t = result.targets.get(0);
|
||||
pipeResult.CalibratedX = t.calibratedX;
|
||||
pipeResult.CalibratedY = t.calibratedY;
|
||||
pipeResult.Pitch = t.pitch;
|
||||
pipeResult.Yaw = t.yaw;
|
||||
pipeResult.Area = t.area;
|
||||
}
|
||||
|
||||
return pipeResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// processing time tracking
|
||||
long startTime;
|
||||
long fpsLastTime = 0;
|
||||
double processTimeMs;
|
||||
double fps = 0;
|
||||
double uiFps = 0;
|
||||
int maxFps = cameraProcess.getVideoMode().fps;
|
||||
|
||||
new Thread(cameraProcess).start();
|
||||
|
||||
long lastFrameEndNanosec = 0;
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
startTime = System.nanoTime();
|
||||
if ((startTime - lastFrameEndNanosec) * 1e-6 >= 1000.0 / (maxFps + 3)) { // 3 additional fps to allow for overhead
|
||||
// foundContours.clear();
|
||||
// filteredContours.clear();
|
||||
// groupedContours.clear();
|
||||
// deSpeckledContours.clear();
|
||||
|
||||
// update FPS for ui only every 0.5 seconds
|
||||
if ((startTime - fpsLastTime) * 1e-6 >= 500) {
|
||||
if (fps >= maxFps) {
|
||||
uiFps = maxFps;
|
||||
} else {
|
||||
uiFps = fps;
|
||||
}
|
||||
fpsLastTime = System.nanoTime();
|
||||
}
|
||||
|
||||
currentPipeline = cameraProcess.getCurrentPipeline();
|
||||
// start fps counter right before grabbing input frame
|
||||
timeStamp = cameraProcess.getLatestFrame(cameraInputMat);
|
||||
if (cameraInputMat.cols() == 0 && cameraInputMat.rows() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// get vision data
|
||||
var pipelineResult = runVisionProcess(cameraInputMat, streamOutputMat);
|
||||
updateNetworkTables(pipelineResult);
|
||||
if (cameraName.equals(SettingsManager.generalSettings.currentCamera)) {
|
||||
HashMap<String, Object> WebSend = new HashMap<>();
|
||||
HashMap<String, Object> point = new HashMap<>();
|
||||
HashMap<String, Object> calculated = new HashMap<>();
|
||||
List<Double> center = new ArrayList<>();
|
||||
if (pipelineResult.IsValid) {
|
||||
center.add(pipelineResult.RawPoint.center.x);
|
||||
center.add(pipelineResult.RawPoint.center.y);
|
||||
calculated.put("pitch", pipelineResult.Pitch);
|
||||
calculated.put("yaw", pipelineResult.Yaw);
|
||||
} else {
|
||||
center.add(0.0);
|
||||
center.add(0.0);
|
||||
calculated.put("pitch", 0);
|
||||
calculated.put("yaw", 0);
|
||||
}
|
||||
point.put("fps", uiFps);
|
||||
point.put("calculated", calculated);
|
||||
point.put("rawPoint", center);
|
||||
WebSend.put("point", point);
|
||||
ServerHandler.broadcastMessage(WebSend);
|
||||
}
|
||||
|
||||
cameraProcess.updateFrame(streamOutputMat);
|
||||
|
||||
cameraInputMat.release();
|
||||
hsvThreshMat.release();
|
||||
|
||||
// calculate FPS
|
||||
lastFrameEndNanosec = System.nanoTime();
|
||||
processTimeMs = (lastFrameEndNanosec - startTime) * 1e-6;
|
||||
fps = 1000 / processTimeMs;
|
||||
//please dont enable if you are not debugging
|
||||
// System.out.printf("%s - Process time: %-5.2fms, FPS: %-5.2f, FoundContours: %d, FilteredContours: %d, GroupedContours: %d\n", cameraName, processTimeMs, fps, FoundContours.size(), FilteredContours.size(), GroupedContours.size());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the old value change listeners
|
||||
* calls {@link #initNT}
|
||||
*
|
||||
* @param newTable passed to {@link #initNT}
|
||||
*/
|
||||
public void resetNT(NetworkTable newTable) {
|
||||
ntDriverModeEntry.removeListener(ntDriveModeListenerID);
|
||||
ntPipelineEntry.removeListener(ntPipelineListenerID);
|
||||
initNT(newTable);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rebases the writing location for the vision process - pipeline output
|
||||
*
|
||||
* @param newTable the new writing location
|
||||
*/
|
||||
private void initNT(NetworkTable newTable) {
|
||||
ntPipelineEntry = newTable.getEntry("pipeline");
|
||||
ntDriverModeEntry = newTable.getEntry("driver_mode");
|
||||
ntPitchEntry = newTable.getEntry("pitch");
|
||||
ntYawEntry = newTable.getEntry("yaw");
|
||||
ntDistanceEntry = newTable.getEntry("distance");
|
||||
ntTimeStampEntry = newTable.getEntry("timestamp");
|
||||
ntValidEntry = newTable.getEntry("is_valid");
|
||||
ntDriveModeListenerID = ntDriverModeEntry.addListener(this::driverModeListener, EntryListenerFlags.kUpdate);
|
||||
ntPipelineListenerID = ntPipelineEntry.addListener(this::pipelineListener, EntryListenerFlags.kUpdate);
|
||||
ntDriverModeEntry.setBoolean(false);
|
||||
ntPipelineEntry.setNumber(cameraProcess.getCurrentPipelineIndex());
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
package com.chameleonvision.web;
|
||||
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import io.javalin.Javalin;
|
||||
|
||||
|
||||
@@ -20,7 +20,8 @@ public class Server {
|
||||
ws.onClose(ctx -> {
|
||||
handler.onClose(ctx);
|
||||
System.out.println("Socket Disconnected");
|
||||
SettingsManager.saveSettings();
|
||||
// TODO: (HIGH) add generalSettingsSave
|
||||
ConfigManager.saveSettings();
|
||||
});
|
||||
ws.onBinaryMessage(ctx -> {
|
||||
handler.onBinaryMessage(ctx);
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package com.chameleonvision.web;
|
||||
|
||||
import com.chameleonvision.classabstraction.VisionManager;
|
||||
import com.chameleonvision.classabstraction.VisionProcess;
|
||||
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
||||
import com.chameleonvision.classabstraction.config.ConfigManager;
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipeline;
|
||||
import com.chameleonvision.classabstraction.pipeline.CVPipelineSettings;
|
||||
import com.chameleonvision.vision.*;
|
||||
import com.chameleonvision.vision.camera.StreamDivisor;
|
||||
import com.chameleonvision.vision.camera.USBCamera;
|
||||
import com.chameleonvision.vision.camera.CameraException;
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.VisionManager;
|
||||
import com.chameleonvision.vision.VisionProcess;
|
||||
import com.chameleonvision.vision.camera.CameraProcess;
|
||||
import com.chameleonvision.config.ConfigManager;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline;
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
import com.chameleonvision.vision.enums.StreamDivisor;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -58,7 +54,7 @@ public class ServerHandler {
|
||||
for (HashMap.Entry<String, Object> e : data.entrySet()) {
|
||||
setField(ConfigManager.settings, e.getKey(), e.getValue());
|
||||
}
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
sendFullSettings();
|
||||
break;
|
||||
}
|
||||
@@ -99,13 +95,13 @@ public class ServerHandler {
|
||||
case "changeCameraName": {
|
||||
currentCamera.getProperties().setNickname((String) entry.getValue());
|
||||
sendFullSettings();
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
break;
|
||||
}
|
||||
case "changePipelineName": {
|
||||
currentPipeline.settings.nickname = ((String) entry.getValue());
|
||||
sendFullSettings();
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
break;
|
||||
}
|
||||
case "duplicatePipeline": {
|
||||
@@ -124,7 +120,7 @@ public class ServerHandler {
|
||||
currentProcess.addPipeline(origPipeline);
|
||||
}
|
||||
// TODO: (HIGH) switch to ConfigManager
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
break;
|
||||
}
|
||||
case "command": {
|
||||
@@ -133,7 +129,7 @@ public class ServerHandler {
|
||||
currentProcess.addPipeline();
|
||||
sendFullSettings();
|
||||
// TODO: (HIGH) switch to ConfigManager
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
break;
|
||||
// TODO: (HIGH) this never worked before, re-visit now that VisionProcess is written sanely
|
||||
case "deleteCurrentPipeline":
|
||||
@@ -147,11 +143,11 @@ public class ServerHandler {
|
||||
// cam.deletePipeline();
|
||||
// cam.setCurrentPipelineIndex(nextIndex);
|
||||
// sendFullSettings();
|
||||
// SettingsManager.saveSettings();
|
||||
// ConfigManager.saveSettings();
|
||||
break;
|
||||
case "save":
|
||||
// TODO: (HIGH) switch to ConfigManager
|
||||
SettingsManager.saveSettings();
|
||||
ConfigManager.saveSettings();
|
||||
System.out.println("saved Settings");
|
||||
break;
|
||||
}
|
||||
@@ -237,9 +233,9 @@ public class ServerHandler {
|
||||
broadcastMessage(obj, null);//Broadcasts the message to every user
|
||||
}
|
||||
|
||||
private static HashMap<String, Object> getOrdinalPipeline() throws CameraException, IllegalAccessException {
|
||||
private static HashMap<String, Object> getOrdinalPipeline() throws IllegalAccessException {
|
||||
HashMap<String, Object> tmp = new HashMap<>();
|
||||
for (Field f : Pipeline.class.getFields()) {
|
||||
for (Field f : CVPipelineSettings.class.getFields()) {
|
||||
if (!f.getType().isEnum()) {
|
||||
tmp.put(f.getName(), f.get(VisionManager.getCurrentUIVisionProcess().getCurrentPipeline()));
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user