2019-11-23 11:55:20 -05:00
|
|
|
package com.chameleonvision.vision;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
2019-11-29 00:30:29 -05:00
|
|
|
import com.chameleonvision.config.CameraConfig;
|
2019-11-26 17:57:51 -05:00
|
|
|
import com.chameleonvision.config.CameraJsonConfig;
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.config.ConfigManager;
|
2019-11-26 17:57:51 -05:00
|
|
|
import com.chameleonvision.config.FullCameraConfiguration;
|
2019-11-23 20:02:21 -05:00
|
|
|
import com.chameleonvision.util.Helpers;
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.util.Platform;
|
2019-11-25 05:34:04 -05:00
|
|
|
import com.chameleonvision.vision.camera.CameraCapture;
|
|
|
|
|
import com.chameleonvision.vision.camera.USBCameraCapture;
|
2019-11-23 11:55:20 -05:00
|
|
|
import com.chameleonvision.vision.pipeline.CVPipelineSettings;
|
2019-11-10 11:47:56 -05:00
|
|
|
import edu.wpi.cscore.UsbCamera;
|
|
|
|
|
import edu.wpi.cscore.UsbCameraInfo;
|
|
|
|
|
import org.opencv.videoio.VideoCapture;
|
|
|
|
|
|
2019-11-21 05:32:19 -05:00
|
|
|
import java.util.*;
|
2019-11-23 04:05:37 -05:00
|
|
|
import java.util.stream.Collectors;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
|
|
|
|
public class VisionManager {
|
2019-11-23 18:29:16 +02:00
|
|
|
private VisionManager() {
|
|
|
|
|
}
|
2019-11-16 14:30:44 -05:00
|
|
|
|
2019-11-24 20:46:25 -08:00
|
|
|
private static final LinkedHashMap<String, UsbCameraInfo> usbCameraInfosByCameraName = new LinkedHashMap<>();
|
2019-11-26 17:57:51 -05:00
|
|
|
private static final LinkedList<FullCameraConfiguration> loadedCameraConfigs = new LinkedList<>();
|
2019-11-24 20:46:25 -08:00
|
|
|
private static final LinkedList<VisionProcessManageable> visionProcesses = new LinkedList<>();
|
2019-11-23 20:02:21 -05:00
|
|
|
|
|
|
|
|
private static class VisionProcessManageable {
|
|
|
|
|
public final int index;
|
|
|
|
|
public final String name;
|
|
|
|
|
public final VisionProcess visionProcess;
|
|
|
|
|
|
|
|
|
|
public VisionProcessManageable(int index, String name, VisionProcess visionProcess) {
|
|
|
|
|
this.index = index;
|
|
|
|
|
this.name = name;
|
|
|
|
|
this.visionProcess = visionProcess;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-23 04:05:37 -05:00
|
|
|
|
|
|
|
|
private static VisionProcess currentUIVisionProcess;
|
2019-11-10 11:47:56 -05:00
|
|
|
|
|
|
|
|
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;
|
2019-11-24 20:46:25 -08:00
|
|
|
while (usbCameraInfosByCameraName.containsKey(name)) {
|
2019-11-10 11:47:56 -05:00
|
|
|
suffix++;
|
|
|
|
|
name = String.format("%s (%d)", name, suffix);
|
|
|
|
|
}
|
2019-11-24 20:46:25 -08:00
|
|
|
usbCameraInfosByCameraName.put(name, info);
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:46:25 -08:00
|
|
|
if (usbCameraInfosByCameraName.isEmpty()) {
|
2019-11-10 11:47:56 -05:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 12:06:37 -08:00
|
|
|
// load the config
|
2019-11-26 17:57:51 -05:00
|
|
|
List<CameraJsonConfig> preliminaryConfigs = new ArrayList<>();
|
2019-11-19 21:29:15 -08:00
|
|
|
|
2019-11-24 20:46:25 -08:00
|
|
|
usbCameraInfosByCameraName.values().forEach((cameraInfo) -> {
|
2019-11-21 05:32:19 -05:00
|
|
|
String truePath;
|
|
|
|
|
|
|
|
|
|
if (Platform.CurrentPlatform.isWindows()) {
|
|
|
|
|
truePath = cameraInfo.path;
|
|
|
|
|
} else {
|
|
|
|
|
truePath = Arrays.stream(cameraInfo.otherPaths).filter(x -> x.contains("/dev/v4l/by-path")).findFirst().orElse(cameraInfo.path);
|
|
|
|
|
}
|
2019-11-16 14:30:44 -05:00
|
|
|
|
2019-11-26 17:57:51 -05:00
|
|
|
preliminaryConfigs.add(new CameraJsonConfig(truePath, cameraInfo.name));
|
2019-11-21 05:32:19 -05:00
|
|
|
});
|
|
|
|
|
|
2019-11-26 17:57:51 -05:00
|
|
|
loadedCameraConfigs.addAll(ConfigManager.initializeCameras(preliminaryConfigs));
|
2019-11-23 04:05:37 -05:00
|
|
|
|
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-24 20:46:25 -08:00
|
|
|
for (int i = 0; i < loadedCameraConfigs.size(); i++) {
|
2019-11-26 17:57:51 -05:00
|
|
|
FullCameraConfiguration config = loadedCameraConfigs.get(i);
|
|
|
|
|
|
|
|
|
|
CameraJsonConfig cameraJsonConfig = config.cameraConfig;
|
|
|
|
|
|
|
|
|
|
CameraCapture camera = new USBCameraCapture(cameraJsonConfig);
|
2019-11-27 19:39:08 -05:00
|
|
|
VisionProcess process = new VisionProcess(camera, cameraJsonConfig.name, config.pipelines);
|
2019-11-26 17:57:51 -05:00
|
|
|
process.setDriverModeSettings(config.drivermode);
|
|
|
|
|
visionProcesses.add(new VisionProcessManageable(i, cameraJsonConfig.name, process));
|
2019-11-23 04:05:37 -05:00
|
|
|
}
|
2019-11-23 20:02:21 -05:00
|
|
|
currentUIVisionProcess = getVisionProcessByIndex(0);
|
2019-11-27 23:53:41 +02:00
|
|
|
ConfigManager.settings.currentCamera = visionProcesses.get(0).name;
|
2019-11-16 14:30:44 -05:00
|
|
|
return true;
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|
2019-11-21 05:32:19 -05:00
|
|
|
|
|
|
|
|
public static void startProcesses() {
|
2019-11-29 00:30:29 -05:00
|
|
|
visionProcesses.forEach((vpm) -> vpm.visionProcess.start());
|
2019-11-21 05:32:19 -05:00
|
|
|
}
|
2019-11-22 14:34:21 -08:00
|
|
|
|
2019-11-23 04:05:37 -05:00
|
|
|
public static VisionProcess getCurrentUIVisionProcess() {
|
|
|
|
|
return currentUIVisionProcess;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-29 00:30:29 -05:00
|
|
|
private static CameraConfig getCameraConfig(VisionProcess process) {
|
|
|
|
|
String cameraName = process.getCamera().getProperties().name;
|
|
|
|
|
return Objects.requireNonNull(loadedCameraConfigs.stream().filter(x -> x.cameraConfig.name.equals(cameraName)).findFirst().orElse(null)).fileConfig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void addPipelineToCamera(CVPipelineSettings newPipeline, VisionProcess process) {
|
|
|
|
|
getCameraConfig(process).pipelineConfig.save(newPipeline);
|
|
|
|
|
process.addPipeline(newPipeline);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 04:05:37 -05:00
|
|
|
public static void setCurrentProcessByIndex(int processIndex) {
|
2019-11-24 20:46:25 -08:00
|
|
|
if (processIndex > visionProcesses.size() - 1) {
|
2019-11-23 04:05:37 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 23:53:41 +02:00
|
|
|
currentUIVisionProcess = getVisionProcessByIndex(processIndex);
|
|
|
|
|
ConfigManager.settings.currentCamera = visionProcesses.get(processIndex).name;
|
2019-11-23 04:05:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static VisionProcess getVisionProcessByIndex(int processIndex) {
|
2019-11-24 20:46:25 -08:00
|
|
|
if (processIndex > visionProcesses.size() - 1) {
|
2019-11-23 04:05:37 -05:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-24 20:46:25 -08:00
|
|
|
VisionProcessManageable vpm = visionProcesses.stream().filter(manageable -> manageable.index == processIndex).findFirst().orElse(null);
|
2019-11-23 20:02:21 -05:00
|
|
|
return vpm != null ? vpm.visionProcess : null;
|
2019-11-23 04:05:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> getAllCameraNicknames() {
|
2019-11-24 20:46:25 -08:00
|
|
|
return visionProcesses.stream().map(vpm -> vpm.visionProcess.getCamera()
|
2019-11-23 20:02:21 -05:00
|
|
|
.getProperties().getNickname()).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> getCurrentCameraPipelineNicknames() {
|
|
|
|
|
return currentUIVisionProcess.getPipelines().stream().map(cvPipeline -> cvPipeline.settings.nickname).collect(Collectors.toList());
|
2019-11-22 14:34:21 -08:00
|
|
|
}
|
2019-11-23 18:29:16 +02:00
|
|
|
|
2019-11-26 23:03:07 -05:00
|
|
|
|
2019-11-27 14:59:36 -05:00
|
|
|
public static void saveAllCameras() {
|
2019-11-24 20:46:25 -08:00
|
|
|
visionProcesses.forEach((vpm) -> {
|
2019-11-23 20:02:21 -05:00
|
|
|
VisionProcess process = vpm.visionProcess;
|
|
|
|
|
String cameraName = process.getCamera().getProperties().name;
|
|
|
|
|
List<CVPipelineSettings> pipelines = process.getPipelines().stream().map(cvPipeline -> cvPipeline.settings).collect(Collectors.toList());
|
|
|
|
|
CVPipelineSettings driverMode = process.getDriverModeSettings();
|
2019-11-26 17:57:51 -05:00
|
|
|
CameraJsonConfig config = CameraJsonConfig.fromUSBCameraProcess((USBCameraCapture) process.getCamera());
|
|
|
|
|
ConfigManager.saveCameraPipelines(cameraName, pipelines);
|
|
|
|
|
ConfigManager.saveCameraDriverMode(cameraName, driverMode);
|
|
|
|
|
ConfigManager.saveCameraConfig(cameraName, config);
|
2019-11-23 18:29:16 +02:00
|
|
|
});
|
|
|
|
|
}
|
2019-11-23 20:02:21 -05:00
|
|
|
|
2019-11-26 23:03:07 -05:00
|
|
|
private static String getCurrentCameraName() {
|
|
|
|
|
return currentUIVisionProcess.getCamera().getProperties().name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveCurrentCameraSettings() {
|
|
|
|
|
CameraJsonConfig config = CameraJsonConfig.fromUSBCameraProcess((USBCameraCapture) currentUIVisionProcess.getCamera());
|
|
|
|
|
ConfigManager.saveCameraConfig(getCurrentCameraName(), config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveCurrentCameraPipelines() {
|
|
|
|
|
List<CVPipelineSettings> pipelineSettings = currentUIVisionProcess.getPipelines().stream().map(pipeline -> pipeline.settings).collect(Collectors.toList());
|
|
|
|
|
ConfigManager.saveCameraPipelines(getCurrentCameraName(), pipelineSettings);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void saveCurrentCameraDriverMode() {
|
|
|
|
|
CVPipelineSettings driverModeSettings = currentUIVisionProcess.getDriverModeSettings();
|
|
|
|
|
ConfigManager.saveCameraDriverMode(getCurrentCameraName(), driverModeSettings);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 19:39:08 -05:00
|
|
|
public static List<String> getCameraResolutionList(CameraCapture capture) {
|
|
|
|
|
return capture.getProperties().getVideoModes().stream().map(Helpers::VideoModeToString).collect(Collectors.toList());
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-23 20:02:21 -05:00
|
|
|
public static List<String> getCurrentCameraResolutionList() {
|
2019-11-27 19:39:08 -05:00
|
|
|
return getCameraResolutionList(currentUIVisionProcess.getCamera());
|
2019-11-23 20:02:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getCurrentUIVisionProcessIndex() {
|
2019-11-24 20:46:25 -08:00
|
|
|
VisionProcessManageable vpm = visionProcesses.stream().filter(v -> v.visionProcess == currentUIVisionProcess).findFirst().orElse(null);
|
2019-11-23 20:02:21 -05:00
|
|
|
return vpm != null ? vpm.index : -1;
|
|
|
|
|
}
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|