mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-22 01:11:40 +00:00
Rename Camera and CamProcess to USBCamera and USBCameraProcess
Further changes: - Add CameraProcess interface - Change VisionProcess to pass the interface as parameter
This commit is contained in:
@@ -4,19 +4,16 @@ 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.MapType;
|
||||
import com.fasterxml.jackson.databind.type.ArrayType;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class CameraDeserializer implements JsonDeserializer<Camera> {
|
||||
public class CameraDeserializer implements JsonDeserializer<USBCamera> {
|
||||
@Override
|
||||
public Camera deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
public USBCamera deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
try {
|
||||
var jsonObj = jsonElement.getAsJsonObject();
|
||||
var camFOV = jsonObj.get("FOV").getAsDouble();
|
||||
@@ -32,8 +29,8 @@ public class CameraDeserializer implements JsonDeserializer<Camera> {
|
||||
|
||||
// always null-check new features
|
||||
boolean isDriver = isDriverObj != null && isDriverObj.getAsBoolean();
|
||||
int driverExposure = driverExposureObj == null ? Camera.DEFAULT_EXPOSURE : driverExposureObj.getAsInt();
|
||||
int driverBrightness = driverBrightnessObj == null ? Camera.DEFAULT_BRIGHTNESS : driverBrightnessObj.getAsInt();
|
||||
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");
|
||||
@@ -47,7 +44,7 @@ public class CameraDeserializer implements JsonDeserializer<Camera> {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
var newCamera = actualPipelines != null ? new Camera(camName, camFOV, actualPipelines, videoModeIndex, divisor, isDriver) : new Camera(camName, camFOV, videoModeIndex, divisor, isDriver);
|
||||
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);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.settings.GeneralSettings;
|
||||
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;
|
||||
@@ -21,10 +21,10 @@ public class CameraManager {
|
||||
|
||||
private static final Path CamConfigPath = Paths.get(SettingsManager.SettingsPath.toString(), "cameras");
|
||||
|
||||
private static LinkedHashMap<String, Camera> AllCamerasByName = new LinkedHashMap<>();
|
||||
public static HashMap<String, VisionProcess> AllVisionProcessesByName = new HashMap<>();
|
||||
private static LinkedHashMap<String, USBCamera> allCamerasByName = new LinkedHashMap<>();
|
||||
public static HashMap<String, VisionProcess> allVisionProcessesByName = new HashMap<>();
|
||||
|
||||
static HashMap<String, UsbCameraInfo> AllUsbCameraInfosByName = new HashMap<>() {{
|
||||
static HashMap<String, UsbCameraInfo> allUsbCameraInfosByName = new HashMap<>() {{
|
||||
var suffix = 0;
|
||||
for (var info : UsbCamera.enumerateUsbCameras()) {
|
||||
var cap = new VideoCapture(info.dev);
|
||||
@@ -40,31 +40,31 @@ public class CameraManager {
|
||||
}
|
||||
}};
|
||||
|
||||
public static HashMap<String, Camera> getAllCamerasByName() {
|
||||
return AllCamerasByName;
|
||||
public static HashMap<String, USBCamera> getAllCamerasByName() {
|
||||
return allCamerasByName;
|
||||
}
|
||||
public static List<String> getAllCameraByNickname(){
|
||||
var cameras = getAllCamerasByName();
|
||||
return cameras.values().stream().map(Camera::getNickname).collect(Collectors.toList());
|
||||
return cameras.values().stream().map(USBCamera::getNickname).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static boolean initializeCameras() {
|
||||
if (AllUsbCameraInfosByName.size() == 0) return false;
|
||||
if (allUsbCameraInfosByName.size() == 0) return false;
|
||||
FileHelper.CheckPath(CamConfigPath);
|
||||
AllUsbCameraInfosByName.forEach((key, value) -> {
|
||||
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(Camera.class, new CameraDeserializer()).create();
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(USBCamera.class, new CameraDeserializer()).create();
|
||||
var camJsonFileReader = new FileReader(camPath.toString());
|
||||
var gsonRead = gson.fromJson(camJsonFileReader, Camera.class);
|
||||
AllCamerasByName.put(key, gsonRead);
|
||||
var gsonRead = gson.fromJson(camJsonFileReader, USBCamera.class);
|
||||
allCamerasByName.put(key, gsonRead);
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
if (!addCamera(new Camera(key), key)) {
|
||||
if (!addCamera(new USBCamera(key), key)) {
|
||||
System.err.println("Failed to add camera! Already exists!");
|
||||
}
|
||||
}
|
||||
@@ -73,39 +73,39 @@ public class CameraManager {
|
||||
}
|
||||
|
||||
public static void initializeThreads(){
|
||||
AllCamerasByName.forEach((key, value) -> {
|
||||
VisionProcess visionProcess = new VisionProcess(value);
|
||||
AllVisionProcessesByName.put(key, visionProcess);
|
||||
allCamerasByName.forEach((name, camera) -> {
|
||||
VisionProcess visionProcess = new VisionProcess(new USBCameraProcess(camera));
|
||||
allVisionProcessesByName.put(name, visionProcess);
|
||||
new Thread(visionProcess).start();
|
||||
});
|
||||
}
|
||||
|
||||
private static boolean addCamera(Camera camera, String cameraName) {
|
||||
if (AllCamerasByName.containsKey(cameraName)) return false;
|
||||
camera.addPipeline();
|
||||
AllCamerasByName.put(cameraName, camera);
|
||||
private static boolean addCamera(USBCamera USBCamera, String cameraName) {
|
||||
if (allCamerasByName.containsKey(cameraName)) return false;
|
||||
USBCamera.addPipeline();
|
||||
allCamerasByName.put(cameraName, USBCamera);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Camera getCamera(String cameraName) {
|
||||
return AllCamerasByName.get(cameraName);
|
||||
private static USBCamera getCamera(String cameraName) {
|
||||
return allCamerasByName.get(cameraName);
|
||||
}
|
||||
|
||||
public static Camera getCameraByIndex(int index) {
|
||||
return AllCamerasByName.get( (AllCamerasByName.keySet().toArray())[ index ] );
|
||||
public static USBCamera getCameraByIndex(int index) {
|
||||
return allCamerasByName.get( (allCamerasByName.keySet().toArray())[ index ] );
|
||||
}
|
||||
|
||||
public static Camera getCurrentCamera() throws CameraException {
|
||||
if (AllCamerasByName.size() == 0) throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
||||
var curCam = AllCamerasByName.get(SettingsManager.GeneralSettings.currentCamera);
|
||||
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))){
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -113,13 +113,13 @@ public class CameraManager {
|
||||
}
|
||||
|
||||
public static void setCurrentCamera(String cameraName) throws CameraException {
|
||||
if (!AllCamerasByName.containsKey(cameraName))
|
||||
if (!allCamerasByName.containsKey(cameraName))
|
||||
throw new CameraException(CameraException.CameraExceptionType.BAD_CAMERA);
|
||||
SettingsManager.GeneralSettings.currentCamera = cameraName;
|
||||
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());
|
||||
List<String> s = new ArrayList<String>(allCamerasByName.keySet());
|
||||
setCurrentCamera(s.get(cameraIndex));
|
||||
}
|
||||
|
||||
@@ -136,20 +136,20 @@ public class CameraManager {
|
||||
}
|
||||
|
||||
public static VisionProcess getVisionProcessByCameraName(String cameraName) {
|
||||
return AllVisionProcessesByName.get(cameraName);
|
||||
return allVisionProcessesByName.get(cameraName);
|
||||
}
|
||||
|
||||
public static VisionProcess getCurrentVisionProcess() throws CameraException {
|
||||
if (!SettingsManager.GeneralSettings.currentCamera.equals("")){
|
||||
return AllVisionProcessesByName.get(SettingsManager.GeneralSettings.currentCamera);
|
||||
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()) {
|
||||
for (var entry : allCamerasByName.entrySet()) {
|
||||
try {
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Camera.class, new CameraSerializer()).create();
|
||||
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();
|
||||
|
||||
@@ -3,22 +3,22 @@ import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class CameraSerializer implements JsonSerializer<Camera> {
|
||||
public class CameraSerializer implements JsonSerializer<USBCamera> {
|
||||
@Override
|
||||
public JsonElement serialize(Camera camera, Type type, JsonSerializationContext context) {
|
||||
public JsonElement serialize(USBCamera USBCamera, Type type, JsonSerializationContext context) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.addProperty("FOV", camera.getFOV());
|
||||
obj.addProperty("path", camera.path);
|
||||
obj.addProperty("name", camera.name);
|
||||
obj.addProperty("nickname", camera.getNickname());
|
||||
obj.addProperty("streamDivisor", camera.getStreamDivisor().ordinal());
|
||||
var pipelines = context.serialize(camera.getPipelines());
|
||||
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", camera.getVideoModeIndex());
|
||||
obj.add("camVideoMode", context.serialize(camera.getVideoMode()));
|
||||
obj.add("isDriver",context.serialize(camera.getDriverMode()));
|
||||
obj.add("driverExposure",context.serialize(camera.getDriverExposure()));
|
||||
obj.add("driverBrightness",context.serialize(camera.getDriverBrightness()));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,8 +21,8 @@ public class CameraValues {
|
||||
public final double HorizontalFocalLength;
|
||||
public final double VerticalFocalLength;
|
||||
|
||||
public CameraValues(Camera camera) {
|
||||
this(camera.getVideoMode().width, camera.getVideoMode().height, camera.getFOV());
|
||||
public CameraValues(USBCamera USBCamera) {
|
||||
this(USBCamera.getVideoMode().width, USBCamera.getVideoMode().height, USBCamera.getFOV());
|
||||
}
|
||||
|
||||
public CameraValues(int imageWidth, int imageHeight, double fov) {
|
||||
|
||||
@@ -13,7 +13,7 @@ import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class Camera {
|
||||
public class USBCamera {
|
||||
|
||||
private static final double DEFAULT_FOV = 60.8;
|
||||
private static final StreamDivisor DEFAULT_STREAMDIVISOR = StreamDivisor.none;
|
||||
@@ -49,31 +49,31 @@ public class Camera {
|
||||
private int driverBrightness;
|
||||
private boolean isDriver;
|
||||
|
||||
public Camera(String cameraName) {
|
||||
public USBCamera(String cameraName) {
|
||||
this(cameraName, DEFAULT_FOV);
|
||||
}
|
||||
|
||||
public Camera(String cameraName, double fov) {
|
||||
this(cameraName, CameraManager.AllUsbCameraInfosByName.get(cameraName), fov);
|
||||
public USBCamera(String cameraName, double fov) {
|
||||
this(cameraName, CameraManager.allUsbCameraInfosByName.get(cameraName), fov);
|
||||
}
|
||||
|
||||
public Camera(String cameraName, UsbCameraInfo usbCameraInfo, double fov) {
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCameraInfo, double fov) {
|
||||
this(cameraName, usbCameraInfo, fov, DEFAULT_STREAMDIVISOR);
|
||||
}
|
||||
|
||||
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov, StreamDivisor divisor) {
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCamInfo, double fov, StreamDivisor divisor) {
|
||||
this(cameraName, usbCamInfo, fov, new ArrayList<>(), 0, divisor, false);
|
||||
}
|
||||
|
||||
public Camera(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, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
this(cameraName, CameraManager.allUsbCameraInfosByName.get(cameraName), fov, pipelines, videoModeIndex, divisor, isDriver);
|
||||
}
|
||||
|
||||
public Camera(String cameraName, double fov, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
public USBCamera(String cameraName, double fov, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
this(cameraName, fov, new ArrayList<>(), videoModeIndex, divisor, isDriver);
|
||||
}
|
||||
|
||||
public Camera(String cameraName, UsbCameraInfo usbCamInfo, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
public USBCamera(String cameraName, UsbCameraInfo usbCamInfo, double fov, List<Pipeline> pipelines, int videoModeIndex, StreamDivisor divisor, boolean isDriver) {
|
||||
FOV = fov;
|
||||
name = cameraName;
|
||||
|
||||
@@ -99,13 +99,13 @@ public class Camera {
|
||||
}
|
||||
}
|
||||
var initTimeMs = (System.nanoTime() - initTimeout) / 1e6;
|
||||
System.out.printf("Camera initialized in %.2fms\n", initTimeMs);
|
||||
System.out.printf("USBCamera 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("Camera not supported!");
|
||||
System.err.println("USBCamera not supported!");
|
||||
throw new RuntimeException(new CameraException(CameraException.CameraExceptionType.BAD_CAMERA));
|
||||
}
|
||||
if (videoModeIndex <= availableVideoModes.length - 1) {
|
||||
@@ -283,7 +283,7 @@ public class Camera {
|
||||
try {
|
||||
UsbCam.setExposureManual(exposure);
|
||||
} catch (VideoException e) {
|
||||
System.err.println("Camera Does not support exposure change");
|
||||
System.err.println("USBCamera Does not support exposure change");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -311,9 +311,9 @@ public class Camera {
|
||||
//Deletes old camera nt table
|
||||
NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname).getInstance().deleteAllEntries();
|
||||
nickname = newNickname;
|
||||
if (CameraManager.AllVisionProcessesByName.containsKey(this.name)) {
|
||||
if (CameraManager.allVisionProcessesByName.containsKey(this.name)) {
|
||||
NetworkTable newNT = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + this.nickname);
|
||||
CameraManager.AllVisionProcessesByName.get(this.name).resetNT(newNT);
|
||||
CameraManager.allVisionProcessesByName.get(this.name).resetNT(newNT);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user