mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Start work on pipelineconfig
This commit is contained in:
@@ -1,17 +1,14 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.util.JacksonHelper;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline2dSettings;
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class CameraConfig {
|
||||
@@ -49,14 +46,15 @@ public class CameraConfig {
|
||||
}
|
||||
|
||||
List<CVPipelineSettings> loadPipelines() {
|
||||
List<CVPipelineSettings> pipelines = new ArrayList<>();
|
||||
try {
|
||||
var pipelineArray = JacksonHelper.deserializer(getPipelinesPath(), CVPipelineSettings[].class);
|
||||
// pipelines = Arrays.asList(pipelineArray);
|
||||
} catch (Exception e) {
|
||||
System.err.println("Failed to load camera pipelines: " + getPipelinesPath().toString());
|
||||
}
|
||||
return pipelines;
|
||||
// List<CVPipelineSettings> pipelines = new ArrayList<>();
|
||||
// try {
|
||||
// var pipelineArray = JacksonHelper.deserializer(getPipelinesPath(), CVPipelineSettings[].class);
|
||||
//// pipelines = Arrays.asList(pipelineArray);
|
||||
// } catch (Exception e) {
|
||||
// System.err.println("Failed to load camera pipelines: " + getPipelinesPath().toString());
|
||||
// }
|
||||
// return pipelines;
|
||||
return pipelineConfig.load();
|
||||
}
|
||||
|
||||
CVPipelineSettings loadDriverMode() {
|
||||
@@ -79,11 +77,12 @@ public class CameraConfig {
|
||||
}
|
||||
|
||||
void savePipelines(List<CVPipelineSettings> pipelines) {
|
||||
try {
|
||||
JacksonHelper.serializer(getPipelinesPath(), pipelines.toArray());
|
||||
} catch (IOException e) {
|
||||
System.err.println("Failed to save camera pipelines file: " + getConfigPath().toString());
|
||||
}
|
||||
// try {
|
||||
// JacksonHelper.serializer(getPipelinesPath(), pipelines.toArray());
|
||||
// } catch (IOException e) {
|
||||
// System.err.println("Failed to save camera pipelines file: " + getConfigPath().toString());
|
||||
// }
|
||||
pipelineConfig.save(pipelines);
|
||||
}
|
||||
|
||||
void saveDriverMode(CVPipelineSettings driverMode) {
|
||||
@@ -94,14 +93,14 @@ public class CameraConfig {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkFolder() {
|
||||
if (!folderExists()) {
|
||||
void checkFolder() {
|
||||
if (!getConfigFolderExists()) {
|
||||
try {
|
||||
if (!(new File(getFolderPath().toUri()).mkdirs())) {
|
||||
System.err.println("Failed to create camera config folder: " + getFolderPath().toString());
|
||||
if (!(new File(getConfigFolderPath().toUri()).mkdirs())) {
|
||||
System.err.println("Failed to create camera config folder: " + getConfigFolderPath().toString());
|
||||
}
|
||||
} catch(Exception e) {
|
||||
System.err.println("Failed to create camera config folder: " + getFolderPath().toString());
|
||||
System.err.println("Failed to create camera config folder: " + getConfigFolderPath().toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,35 +129,27 @@ public class CameraConfig {
|
||||
}
|
||||
}
|
||||
|
||||
private Path getFolderPath() {
|
||||
Path getConfigFolderPath() {
|
||||
return Paths.get(camerasConfigFolderPath.toString(), cameraConfigName);
|
||||
}
|
||||
|
||||
private Path getConfigPath() {
|
||||
return Paths.get(getFolderPath().toString(), "camera.json");
|
||||
}
|
||||
|
||||
Path getPipelinesPath() {
|
||||
return Paths.get(getFolderPath().toString(), "pipelines.json");
|
||||
return Paths.get(getConfigFolderPath().toString(), "camera.json");
|
||||
}
|
||||
|
||||
private Path getDriverModePath() {
|
||||
return Paths.get(getFolderPath().toString(), "drivermode.json");
|
||||
return Paths.get(getConfigFolderPath().toString(), "drivermode.json");
|
||||
}
|
||||
|
||||
boolean folderExists() {
|
||||
return Files.exists(getFolderPath());
|
||||
boolean getConfigFolderExists() {
|
||||
return Files.exists(getConfigFolderPath());
|
||||
}
|
||||
|
||||
private boolean configExists() {
|
||||
return folderExists() && Files.exists(getConfigPath());
|
||||
return getConfigFolderExists() && Files.exists(getConfigPath());
|
||||
}
|
||||
|
||||
private boolean driverModeExists() {
|
||||
return folderExists() && Files.exists(getDriverModePath());
|
||||
}
|
||||
|
||||
boolean pipelinesExists() {
|
||||
return folderExists() && Files.exists(getPipelinesPath());
|
||||
return getConfigFolderExists() && Files.exists(getDriverModePath());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,11 +86,9 @@ public class ConfigManager {
|
||||
CameraConfig cameraConfiguration = new CameraConfig(preliminaryConfig);
|
||||
cameraConfigs.put(preliminaryConfig.name, cameraConfiguration);
|
||||
|
||||
CameraJsonConfig camJsonConfig = cameraConfiguration.load();
|
||||
List<CVPipelineSettings> pipelines = cameraConfiguration.loadPipelines();
|
||||
CVPipelineSettings driverMode = cameraConfiguration.loadDriverMode();
|
||||
FullCameraConfiguration camJsonConfig = cameraConfiguration.load();
|
||||
|
||||
configList.add(new FullCameraConfiguration(camJsonConfig, pipelines, driverMode));
|
||||
configList.add(camJsonConfig);
|
||||
}
|
||||
|
||||
return configList;
|
||||
|
||||
@@ -1,35 +1,65 @@
|
||||
package com.chameleonvision.config;
|
||||
|
||||
import com.chameleonvision.util.JacksonHelper;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline2dSettings;
|
||||
import com.chameleonvision.vision.pipeline.CVPipeline3dSettings;
|
||||
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
||||
import com.chameleonvision.vision.pipeline.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PipelineConfig {
|
||||
|
||||
public static final String CVPipeline2DPrefix = "CV2D";
|
||||
public static final String CVPipeline3DPrefix = "CV3D";
|
||||
private final Path pipelineFolderPath;
|
||||
|
||||
private final CameraConfig cameraConfig;
|
||||
|
||||
/**
|
||||
* Construct a new PipelineConfig
|
||||
* @param cameraConfig the CameraConfig (parent folder, kinda?)
|
||||
*/
|
||||
public PipelineConfig(CameraConfig cameraConfig) {
|
||||
this.cameraConfig = cameraConfig;
|
||||
pipelineFolderPath = Paths.get(cameraConfig.getConfigFolderPath().toString(), "pipelines");
|
||||
}
|
||||
|
||||
void check() {
|
||||
if (!cameraConfig.pipelinesExists()) {
|
||||
cameraConfig.checkFolder();
|
||||
// Check if there's at least one pipe
|
||||
if (!pipelinesExists()) {
|
||||
save(new CVPipeline2dSettings());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pipelinesExists() {
|
||||
cameraConfig.checkFolder();
|
||||
return cameraConfig.getConfigFolderExists()
|
||||
&& Objects.requireNonNull(new File(pipelineFolderPath.toUri()).listFiles()).length > 0;
|
||||
}
|
||||
|
||||
private void save(CVPipelineSettings settings) {
|
||||
if (settings instanceof CVPipeline2dSettings) {
|
||||
|
||||
} else if (settings instanceof CVPipeline3dSettings) {
|
||||
|
||||
if (settings instanceof CVPipeline3dSettings) {
|
||||
Path settingJsonPath = Paths.get(pipelineFolderPath.toString(), CVPipeline3DPrefix + settings.nickname);
|
||||
try {
|
||||
JacksonHelper.serializer(settingJsonPath, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if (settings instanceof CVPipeline2dSettings) {
|
||||
Path settingJsonPath = Paths.get(pipelineFolderPath.toString(), CVPipeline2DPrefix + settings.nickname);
|
||||
try {
|
||||
JacksonHelper.serializer(settingJsonPath, settings);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
throw new RuntimeException("saving non-2d and non-3d pipelines not implemented~");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +70,37 @@ public class PipelineConfig {
|
||||
}
|
||||
|
||||
public List<CVPipelineSettings> load() {
|
||||
return null;
|
||||
check();
|
||||
var pipelineDir = new File(pipelineFolderPath.toUri());
|
||||
|
||||
File[] files = pipelineDir.listFiles();
|
||||
List<CVPipelineSettings> deserializedList = new ArrayList<>();
|
||||
if(files == null || files.length < 1) {
|
||||
// TODO handle no pipelines to load
|
||||
System.err.println("no pipes to load! loading default");
|
||||
} else {
|
||||
for(File file : files) {
|
||||
var name = file.getName();
|
||||
if(name.startsWith(CVPipeline3DPrefix)) {
|
||||
// try to load 3d pipe
|
||||
try {
|
||||
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline3dSettings.class);
|
||||
deserializedList.add(pipe);
|
||||
} catch (IOException e) {
|
||||
System.err.println("couldn't load cvpipeline3d");
|
||||
}
|
||||
} else if(name.startsWith(CVPipeline2DPrefix)) {
|
||||
// try to load 2d pipe
|
||||
try {
|
||||
var pipe = JacksonHelper.deserializer(Paths.get(file.getPath()), CVPipeline2dSettings.class);
|
||||
deserializedList.add(pipe);
|
||||
} catch (IOException e) {
|
||||
System.err.println("couldn't load cvpipeline2d");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return deserializedList;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user