2019-11-10 11:47:56 -05:00
|
|
|
package com.chameleonvision.classabstraction;
|
|
|
|
|
|
2019-11-19 21:29:15 -08:00
|
|
|
import com.chameleonvision.classabstraction.camera.USBCameraProcess;
|
|
|
|
|
import com.chameleonvision.classabstraction.config.CameraConfig;
|
2019-11-19 12:06:37 -08:00
|
|
|
import com.chameleonvision.classabstraction.config.ConfigManager;
|
2019-11-10 11:47:56 -05:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
|
|
|
|
import com.chameleonvision.util.FileHelper;
|
|
|
|
|
import edu.wpi.cscore.UsbCamera;
|
|
|
|
|
import edu.wpi.cscore.UsbCameraInfo;
|
|
|
|
|
import org.opencv.videoio.VideoCapture;
|
|
|
|
|
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
2019-11-19 12:06:37 -08:00
|
|
|
import java.util.ArrayList;
|
2019-11-10 11:47:56 -05:00
|
|
|
import java.util.LinkedHashMap;
|
2019-11-19 21:29:15 -08:00
|
|
|
import java.util.List;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
|
|
|
|
public class VisionManager {
|
|
|
|
|
|
2019-11-16 14:30:44 -05:00
|
|
|
private VisionManager() {}
|
|
|
|
|
|
2019-11-10 11:47:56 -05:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-16 14:30:44 -05:00
|
|
|
FileHelper.CheckPath(CamConfigPath);
|
|
|
|
|
|
2019-11-19 12:06:37 -08:00
|
|
|
// load the config
|
2019-11-19 21:29:15 -08:00
|
|
|
List<CameraConfig> loadedConfigs = ConfigManager.initializeCameraConfig(new ArrayList<>(UsbCameraInfosByCameraName.keySet()));
|
|
|
|
|
loadedConfigs.forEach(config -> {
|
|
|
|
|
var camera = new USBCameraProcess(
|
|
|
|
|
new edu.wpi.cscore.UsbCamera(config.name, config.path),
|
|
|
|
|
config
|
|
|
|
|
);
|
|
|
|
|
VisionProcessesByCameraName.put(config.name, new VisionProcess(camera, config.name));
|
|
|
|
|
});
|
|
|
|
|
|
2019-11-16 14:30:44 -05:00
|
|
|
|
2019-11-19 12:06:37 -08:00
|
|
|
// UsbCameraInfosByCameraName.forEach((cameraName, cameraInfo) -> {
|
|
|
|
|
// Path cameraConfigFolder = Paths.get(CamConfigPath.toString(), String.format("%s\\", cameraName));
|
|
|
|
|
// Path cameraConfigPath = Paths.get(cameraConfigFolder.toString(), String.format("%s.json", cameraName));
|
|
|
|
|
// Path cameraPipelinesPath = Paths.get(cameraConfigFolder.toString(), "pipelines.json");
|
|
|
|
|
// Path cameraDrivermodePath = Paths.get(cameraConfigFolder.toString(), "drivermode.json");
|
|
|
|
|
//
|
|
|
|
|
// try {
|
|
|
|
|
//
|
|
|
|
|
// boolean cameraFolderExists = Files.exists(cameraConfigFolder);
|
|
|
|
|
//
|
|
|
|
|
// if (!cameraFolderExists) {
|
|
|
|
|
// Files.createDirectory(cameraConfigFolder);
|
|
|
|
|
// }
|
|
|
|
|
// boolean cameraConfigExists = cameraFolderExists && Files.exists(cameraConfigPath);
|
|
|
|
|
//
|
|
|
|
|
// if (Files.exists(cameraConfigFolder)) {
|
|
|
|
|
// if (Files.exists(cameraConfigPath)) {
|
|
|
|
|
// File cameraConfigFile = new File(cameraConfigPath.toString());
|
|
|
|
|
// if (cameraConfigFile.length() != 0) {
|
|
|
|
|
// try {
|
|
|
|
|
// Gson gson = new GsonBuilder().create();
|
|
|
|
|
// } catch (Exception e) {
|
|
|
|
|
// e.printStackTrace();
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// Files.createFile(cameraConfigPath);
|
|
|
|
|
// }
|
|
|
|
|
// } else {
|
|
|
|
|
// Files.createDirectory(cameraConfigFolder);
|
|
|
|
|
// }
|
|
|
|
|
// } catch (IOException ex) {
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// });
|
2019-11-16 14:30:44 -05:00
|
|
|
|
2019-11-15 16:01:50 -05:00
|
|
|
// 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) {
|
|
|
|
|
//// try {
|
|
|
|
|
//// Gson gson = new GsonBuilder().registerTypeAdapter(USBCameraProcess.class, new CameraDeserializer());
|
|
|
|
|
//// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// TODO: implement new camera JSON loads
|
2019-11-12 19:28:46 +02:00
|
|
|
return true;
|
2019-11-11 17:26:32 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-16 14:30:44 -05:00
|
|
|
public static boolean initializeProcesses() {
|
2019-11-10 11:47:56 -05:00
|
|
|
|
2019-11-16 14:30:44 -05:00
|
|
|
return true;
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|