2019-11-10 11:47:56 -05:00
|
|
|
package com.chameleonvision.classabstraction;
|
|
|
|
|
|
|
|
|
|
import com.chameleonvision.classabstraction.camera.USBCameraProcess;
|
2019-11-11 17:26:32 +02:00
|
|
|
import com.chameleonvision.classabstraction.pipeline.CVPipelineSettings;
|
2019-11-10 11:47:56 -05:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
|
|
|
|
import com.chameleonvision.util.FileHelper;
|
|
|
|
|
import com.chameleonvision.vision.camera.CameraDeserializer;
|
2019-11-11 17:26:32 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-11-10 11:47:56 -05:00
|
|
|
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.File;
|
2019-11-11 17:26:32 +02:00
|
|
|
import java.io.IOException;
|
2019-11-10 11:47:56 -05:00
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
2019-11-11 17:26:32 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
2019-11-10 11:47:56 -05:00
|
|
|
import java.util.LinkedHashMap;
|
2019-11-11 17:26:32 +02:00
|
|
|
import java.util.List;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
|
|
|
|
public class VisionManager {
|
|
|
|
|
|
|
|
|
|
private static final Path CamConfigPath = Paths.get(SettingsManager.SettingsPath.toString(), "cameras");
|
|
|
|
|
|
|
|
|
|
public static final LinkedHashMap<String, UsbCameraInfo> UsbCameraInfosByCameraName = new LinkedHashMap<>();
|
|
|
|
|
public static final LinkedHashMap<String, VisionProcess> VisionProcessesByCameraName = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static boolean initializeSources() {
|
|
|
|
|
int suffix = 0;
|
|
|
|
|
for (UsbCameraInfo info : UsbCamera.enumerateUsbCameras()) {
|
|
|
|
|
VideoCapture cap = new VideoCapture(info.dev);
|
|
|
|
|
if (cap.isOpened()) {
|
|
|
|
|
cap.release();
|
|
|
|
|
String name = info.name;
|
|
|
|
|
while (UsbCameraInfosByCameraName.containsKey(name)) {
|
|
|
|
|
suffix++;
|
|
|
|
|
name = String.format("%s (%d)", name, suffix);
|
|
|
|
|
}
|
|
|
|
|
UsbCameraInfosByCameraName.put(name, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UsbCameraInfosByCameraName.isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileHelper.CheckPath(CamConfigPath);
|
|
|
|
|
UsbCameraInfosByCameraName.forEach((cameraName, cameraInfo) -> {
|
|
|
|
|
Path cameraConfigPath = Paths.get(CamConfigPath.toString(), String.format("%s.json", cameraName));
|
|
|
|
|
File cameraConfigFile = new File(cameraConfigPath.toString());
|
|
|
|
|
if (cameraConfigFile.exists() && cameraConfigFile.length() != 0) {
|
2019-11-10 17:55:28 -05:00
|
|
|
// try {
|
|
|
|
|
// Gson gson = new GsonBuilder().registerTypeAdapter(USBCameraProcess.class, new CameraDeserializer());
|
|
|
|
|
// }
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 17:26:32 +02:00
|
|
|
public void PipelineSerializer(List<CVPipelineSettings> list, Path path) throws IOException {
|
|
|
|
|
File pipelineFile = new File(path.toString());
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper().enableDefaultTyping();
|
|
|
|
|
objectMapper.writeValue(pipelineFile,list);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CVPipelineSettings> PipelineDESerializer(Path path) throws IOException {
|
|
|
|
|
File pipelineFile = new File(path.toString());
|
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper().enableDefaultTyping();
|
|
|
|
|
List<CVPipelineSettings> list = Arrays.asList(objectMapper.readValue(pipelineFile, CVPipelineSettings[].class));
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-10 11:47:56 -05:00
|
|
|
public static void initializeProcesses() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|