2019-11-10 11:47:56 -05:00
|
|
|
package com.chameleonvision.classabstraction;
|
|
|
|
|
|
2019-11-23 04:05:37 -05:00
|
|
|
import com.chameleonvision.classabstraction.camera.CameraProcess;
|
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-23 18:29:16 +02:00
|
|
|
import com.chameleonvision.classabstraction.pipeline.CVPipelineSettings;
|
2019-11-21 05:32:19 -05:00
|
|
|
import com.chameleonvision.settings.Platform;
|
2019-11-23 04:05:37 -05:00
|
|
|
import com.chameleonvision.vision.camera.USBCamera;
|
2019-11-10 11:47:56 -05:00
|
|
|
import edu.wpi.cscore.UsbCamera;
|
|
|
|
|
import edu.wpi.cscore.UsbCameraInfo;
|
2019-11-23 04:05:37 -05:00
|
|
|
import org.apache.commons.lang3.tuple.Pair;
|
2019-11-10 11:47:56 -05:00
|
|
|
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-23 04:05:37 -05:00
|
|
|
private static final LinkedHashMap<String, UsbCameraInfo> UsbCameraInfosByCameraName = new LinkedHashMap<>();
|
2019-11-21 05:32:19 -05:00
|
|
|
private static final LinkedList<CameraConfig> LoadedCameraConfigs = new LinkedList<>();
|
2019-11-23 04:05:37 -05:00
|
|
|
private static final LinkedHashMap<Integer, Pair<VisionProcess, String>> VisionProcessesByIndex = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
while (UsbCameraInfosByCameraName.containsKey(name)) {
|
|
|
|
|
suffix++;
|
|
|
|
|
name = String.format("%s (%d)", name, suffix);
|
|
|
|
|
}
|
|
|
|
|
UsbCameraInfosByCameraName.put(name, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (UsbCameraInfosByCameraName.isEmpty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-19 12:06:37 -08:00
|
|
|
// load the config
|
2019-11-21 05:32:19 -05:00
|
|
|
List<CameraConfig> preliminaryConfigs = new ArrayList<>();
|
2019-11-19 21:29:15 -08:00
|
|
|
|
2019-11-21 05:32:19 -05:00
|
|
|
UsbCameraInfosByCameraName.values().forEach((cameraInfo) -> {
|
|
|
|
|
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-23 18:29:16 +02:00
|
|
|
preliminaryConfigs.add(new CameraConfig(truePath, cameraInfo.name));
|
2019-11-21 05:32:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
LoadedCameraConfigs.addAll(ConfigManager.initializeCameraConfig(preliminaryConfigs));
|
2019-11-23 04:05:37 -05:00
|
|
|
|
|
|
|
|
// TODO: (HIGH) Load pipelines from json
|
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");
|
2019-11-21 05:32:19 -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-23 04:05:37 -05:00
|
|
|
for (int i = 0; i < LoadedCameraConfigs.size(); i++) {
|
|
|
|
|
CameraConfig config = LoadedCameraConfigs.get(i);
|
|
|
|
|
CameraProcess camera = new USBCameraProcess(config);
|
|
|
|
|
VisionProcess process = new VisionProcess(camera, config.name);
|
|
|
|
|
VisionProcessesByIndex.put(i, Pair.of(process, config.name));
|
|
|
|
|
}
|
|
|
|
|
currentUIVisionProcess = VisionProcessesByIndex.get(0).getLeft();
|
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-23 04:05:37 -05:00
|
|
|
VisionProcessesByIndex.forEach((index, processNamePair) -> {
|
|
|
|
|
processNamePair.getLeft().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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setCurrentProcessByIndex(int processIndex) {
|
|
|
|
|
if (processIndex > VisionProcessesByIndex.size() - 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentUIVisionProcess = VisionProcessesByIndex.get(processIndex).getLeft();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static VisionProcess getVisionProcessByIndex(int processIndex) {
|
|
|
|
|
if (processIndex > VisionProcessesByIndex.size() - 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return VisionProcessesByIndex.get(0).getLeft();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> getAllCameraNicknames() {
|
|
|
|
|
return VisionProcessesByIndex.values().stream().map(processNamePair -> processNamePair.getLeft().getCamera().getProperties().getNickname()).collect(Collectors.toList());
|
2019-11-22 14:34:21 -08:00
|
|
|
}
|
2019-11-23 18:29:16 +02:00
|
|
|
|
|
|
|
|
public static void saveCameras() {
|
|
|
|
|
VisionProcessesByIndex.forEach((index, process) -> {
|
|
|
|
|
VisionProcess p = process.getLeft();
|
|
|
|
|
String name = process.getRight();
|
|
|
|
|
List<CVPipelineSettings> pipelines = p.getPipelines().stream().map(cvPipeline -> cvPipeline.settings).collect(Collectors.toList());
|
|
|
|
|
CVPipelineSettings driverMode = p.getDriverModeSettings();
|
|
|
|
|
//TODO: get camera config and serialize into folder with camera name
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-11-10 11:47:56 -05:00
|
|
|
}
|