mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Add CameraDeserializer, fix empty camera.json crash
This commit is contained in:
@@ -7,12 +7,15 @@ import com.chameleonvision.web.Server;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
CameraManager.initializeCameras();
|
||||
SettingsManager manager = SettingsManager.getInstance();
|
||||
for (var camSet : CameraManager.getAllCamerasByName().entrySet()) {
|
||||
new Thread(new CameraProcess(camSet.getValue())).start();
|
||||
if (CameraManager.initializeCameras()) {
|
||||
SettingsManager manager = SettingsManager.getInstance();
|
||||
for (var camSet : CameraManager.getAllCamerasByName().entrySet()) {
|
||||
new Thread(new CameraProcess(camSet.getValue())).start();
|
||||
}
|
||||
// NetworkTableInstance.getDefault().startClientTeam(SettingsManager.GeneralSettings.team_number);
|
||||
Server.main(8888);
|
||||
} else {
|
||||
System.err.println("No cameras connected!");
|
||||
}
|
||||
// NetworkTableInstance.getDefault().startClientTeam(SettingsManager.GeneralSettings.team_number);
|
||||
Server.main(8888);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,8 +15,7 @@ public class Camera {
|
||||
public final String name;
|
||||
public final String path;
|
||||
|
||||
public final UsbCamera UsbCam;
|
||||
private final UsbCameraInfo UsbCamInfo;
|
||||
final UsbCamera UsbCam;
|
||||
private final VideoMode[] availableVideoModes;
|
||||
|
||||
private final CameraServer cs = CameraServer.getInstance();
|
||||
@@ -29,7 +28,7 @@ public class Camera {
|
||||
private CamVideoMode camVideoMode;
|
||||
|
||||
private int currentPipelineIndex;
|
||||
private HashMap<Integer, Pipeline> pipelines = new HashMap<>();
|
||||
private HashMap<Integer, Pipeline> pipelines;
|
||||
|
||||
public Camera(String cameraName) {
|
||||
this(cameraName, defaultFOV);
|
||||
@@ -44,13 +43,22 @@ public class Camera {
|
||||
}
|
||||
|
||||
public Camera(UsbCameraInfo usbCamInfo, double fov) {
|
||||
UsbCamInfo = usbCamInfo;
|
||||
this(usbCamInfo, fov, new HashMap<>());
|
||||
}
|
||||
|
||||
public Camera(String cameraName, double fov, HashMap<Integer, Pipeline> pipelines) {
|
||||
this(CameraManager.AllUsbCameraInfosByName.get(cameraName), fov, pipelines);
|
||||
}
|
||||
|
||||
public Camera(UsbCameraInfo usbCamInfo, double fov, HashMap<Integer, Pipeline> pipelines) {
|
||||
FOV = fov;
|
||||
name = usbCamInfo.name;
|
||||
path = usbCamInfo.path;
|
||||
|
||||
UsbCam = new UsbCamera(name, path);
|
||||
|
||||
this.pipelines = pipelines;
|
||||
|
||||
// set up video mode
|
||||
availableVideoModes = UsbCam.enumerateVideoModes();
|
||||
setCamVideoMode(new CamVideoMode(availableVideoModes[0]));
|
||||
@@ -81,7 +89,7 @@ public class Camera {
|
||||
}
|
||||
}
|
||||
|
||||
public void addPipeline() {
|
||||
void addPipeline() {
|
||||
addPipeline(pipelines.size());
|
||||
}
|
||||
|
||||
@@ -98,7 +106,7 @@ public class Camera {
|
||||
return currentPipelineIndex;
|
||||
}
|
||||
|
||||
public void setCurrentPipelineIndex(int pipelineNumber) {
|
||||
void setCurrentPipelineIndex(int pipelineNumber) {
|
||||
if (pipelineNumber - 1 > pipelines.size()) return;
|
||||
currentPipelineIndex = pipelineNumber;
|
||||
}
|
||||
@@ -106,7 +114,7 @@ public class Camera {
|
||||
return pipelines;
|
||||
}
|
||||
|
||||
public CamVideoMode getVideoMode() {
|
||||
CamVideoMode getVideoMode() {
|
||||
return camVideoMode;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.chameleonvision.vision.camera;
|
||||
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.type.MapType;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.gson.*;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class CameraDeserializer implements JsonDeserializer<Camera> {
|
||||
@Override
|
||||
public Camera deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
||||
var jsonObj = jsonElement.getAsJsonObject();
|
||||
var camFOV = jsonObj.get("FOV").getAsDouble();
|
||||
var camPath = jsonObj.get("path").getAsString();
|
||||
var camName = jsonObj.get("name").getAsString();
|
||||
|
||||
var pipelines = jsonObj.get("pipelines");
|
||||
HashMap<Integer, Pipeline> actualPipelines = new HashMap<>();
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
TypeFactory typeFactory = mapper.getTypeFactory();
|
||||
MapType mapType = typeFactory.constructMapType(HashMap.class, Integer.class, Pipeline.class);
|
||||
try {
|
||||
actualPipelines = mapper.readValue(pipelines.toString(), mapType);
|
||||
} catch (JsonProcessingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return actualPipelines != null ? new Camera(camName, camFOV, actualPipelines) : new Camera(camName, camFOV);
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,7 @@ import edu.wpi.cscore.UsbCamera;
|
||||
import edu.wpi.cscore.UsbCameraInfo;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
@@ -25,7 +21,6 @@ public class CameraManager {
|
||||
|
||||
private static final Path CamConfigPath = Paths.get(SettingsManager.SettingsPath.toString(), "Cams");
|
||||
|
||||
// TODO: throw a camera Exception if no camera is connected
|
||||
static HashMap<String, UsbCameraInfo> AllUsbCameraInfosByName = new HashMap<>() {{
|
||||
var suffix = 0;
|
||||
for (var info : UsbCamera.enumerateUsbCameras()) {
|
||||
@@ -46,25 +41,28 @@ public class CameraManager {
|
||||
|
||||
public static HashMap<String, Camera> getAllCamerasByName() { return AllCamerasByName; }
|
||||
|
||||
public static void initializeCameras() {
|
||||
public static boolean initializeCameras() {
|
||||
if (AllUsbCameraInfosByName.size() == 0) return false;
|
||||
FileHelper.CheckPath(CamConfigPath);
|
||||
for (var entry : AllUsbCameraInfosByName.entrySet()) {
|
||||
var camPath = Paths.get(CamConfigPath.toString(), String.format("%s.json", entry.getKey()));
|
||||
if (Files.exists(camPath)) {
|
||||
File camJsonFile = new File(camPath.toString());
|
||||
if (camJsonFile.exists() && camJsonFile.length() != 0) {
|
||||
try {
|
||||
// TODO: Check if deserializing correctly, if not, add CameraDeserializer
|
||||
var camJsonFile = new FileReader(camPath.toString());
|
||||
var gsonRead = new Gson().fromJson(camJsonFile, Camera.class);
|
||||
Gson gson = new GsonBuilder().registerTypeAdapter(Camera.class, new CameraDeserializer()).create();
|
||||
var camJsonFileReader = new FileReader(camPath.toString());
|
||||
var gsonRead = gson.fromJson(camJsonFileReader, Camera.class);
|
||||
AllCamerasByName.put(entry.getKey(), gsonRead);
|
||||
} catch (FileNotFoundException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
if (!addCamera(new Camera(entry.getKey()),entry.getKey())) {
|
||||
if (!addCamera(new Camera(entry.getKey()), entry.getKey())) {
|
||||
System.err.println("Failed to add camera! Already exists!");
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static boolean addCamera(Camera camera, String cameraName) {
|
||||
|
||||
@@ -14,6 +14,7 @@ public class CameraSerializer implements JsonSerializer<Camera> {
|
||||
var pipelines = context.serialize(camera.getPipelines());
|
||||
obj.add("pipelines", pipelines);
|
||||
|
||||
var videoModeIndex = camera.getVideoModeIndex();
|
||||
obj.addProperty("resolution", camera.getVideoModeIndex());
|
||||
obj.add("camVideoMode", context.serialize(camera.getVideoMode()));
|
||||
|
||||
|
||||
@@ -60,9 +60,10 @@ public class CameraProcess implements Runnable {
|
||||
}
|
||||
|
||||
private void PipelineListener(EntryNotification entryNotification) {
|
||||
if (camera.getPipelines().containsKey(entryNotification.value.getString())) {
|
||||
// camera.setPntryNotification.value.getString());
|
||||
Pipeline pipeline = camera.getCurrentPipeline();
|
||||
var ntPipelineIndex = Integer.parseInt(entryNotification.value.getString().replace("pipeline", ""));
|
||||
if (camera.getPipelines().containsKey(ntPipelineIndex)) {
|
||||
// camera.setEntryNotification.value.getString());
|
||||
var pipeline = camera.getCurrentPipeline();
|
||||
|
||||
camera.setExposure(pipeline.exposure);
|
||||
camera.setBrightness(pipeline.brightness);
|
||||
@@ -206,8 +207,6 @@ public class CameraProcess implements Runnable {
|
||||
|
||||
cameraInputMat.release();
|
||||
hsvThreshMat.release();
|
||||
|
||||
memManager.run(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user