2019-09-19 14:07:42 -04:00
|
|
|
package com.chameleonvision.vision.camera;
|
|
|
|
|
|
2019-10-04 15:55:45 -04:00
|
|
|
import com.chameleonvision.util.FileHelper;
|
2019-09-19 14:07:42 -04:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
|
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-29 17:39:58 +03:00
|
|
|
import com.chameleonvision.vision.process.VisionProcess;
|
2019-09-19 14:07:42 -04: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;
|
|
|
|
|
|
2019-09-20 15:42:13 -04:00
|
|
|
import java.io.*;
|
2019-09-19 14:07:42 -04:00
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
2019-10-01 02:18:55 -04:00
|
|
|
import java.util.Arrays;
|
2019-09-19 14:07:42 -04:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
2019-10-01 02:18:55 -04:00
|
|
|
import java.util.stream.Collectors;
|
2019-09-19 14:07:42 -04:00
|
|
|
|
|
|
|
|
public class CameraManager {
|
|
|
|
|
|
2019-09-26 15:01:51 -04:00
|
|
|
private static final Path CamConfigPath = Paths.get(SettingsManager.SettingsPath.toString(), "cameras");
|
2019-09-23 18:26:53 -04:00
|
|
|
|
|
|
|
|
private static HashMap<String, Camera> AllCamerasByName = new HashMap<>();
|
2019-09-29 17:39:58 +03:00
|
|
|
private static HashMap<String, VisionProcess> AllVisionProcessesByName = new HashMap<>();
|
2019-09-23 18:26:53 -04:00
|
|
|
|
2019-09-22 02:49:30 -04:00
|
|
|
static HashMap<String, UsbCameraInfo> AllUsbCameraInfosByName = new HashMap<>() {{
|
|
|
|
|
var suffix = 0;
|
|
|
|
|
for (var info : UsbCamera.enumerateUsbCameras()) {
|
|
|
|
|
var cap = new VideoCapture(info.dev);
|
|
|
|
|
if (cap.isOpened()) {
|
|
|
|
|
cap.release();
|
|
|
|
|
var name = info.name;
|
|
|
|
|
while (this.containsKey(name)) {
|
|
|
|
|
suffix++;
|
|
|
|
|
name = String.format("%s(%s)", info.name, suffix);
|
|
|
|
|
}
|
|
|
|
|
put(name, info);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
public static HashMap<String, Camera> getAllCamerasByName() {
|
|
|
|
|
return AllCamerasByName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static boolean initializeCameras() {
|
|
|
|
|
if (AllUsbCameraInfosByName.size() == 0) return false;
|
|
|
|
|
FileHelper.CheckPath(CamConfigPath);
|
2019-10-01 02:18:55 -04:00
|
|
|
AllUsbCameraInfosByName.forEach((key, value) -> {
|
|
|
|
|
var camPath = Paths.get(CamConfigPath.toString(), String.format("%s.json", key));
|
2019-09-22 02:49:30 -04:00
|
|
|
File camJsonFile = new File(camPath.toString());
|
|
|
|
|
if (camJsonFile.exists() && camJsonFile.length() != 0) {
|
|
|
|
|
try {
|
|
|
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(Camera.class, new CameraDeserializer()).create();
|
|
|
|
|
var camJsonFileReader = new FileReader(camPath.toString());
|
|
|
|
|
var gsonRead = gson.fromJson(camJsonFileReader, Camera.class);
|
2019-10-01 02:18:55 -04:00
|
|
|
AllCamerasByName.put(key, gsonRead);
|
2019-09-22 02:49:30 -04:00
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-10-01 02:18:55 -04:00
|
|
|
if (!addCamera(new Camera(key), key)) {
|
2019-09-22 02:49:30 -04:00
|
|
|
System.err.println("Failed to add camera! Already exists!");
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-01 02:18:55 -04:00
|
|
|
});
|
2019-09-22 02:49:30 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-29 17:39:58 +03:00
|
|
|
public static void initializeThreads(){
|
2019-10-01 02:18:55 -04:00
|
|
|
AllCamerasByName.forEach((key, value) -> {
|
|
|
|
|
VisionProcess visionProcess = new VisionProcess(value);
|
|
|
|
|
AllVisionProcessesByName.put(key, visionProcess);
|
2019-09-29 17:39:58 +03:00
|
|
|
new Thread(visionProcess).start();
|
2019-10-01 02:18:55 -04:00
|
|
|
});
|
2019-09-29 17:39:58 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-22 02:49:30 -04:00
|
|
|
private static boolean addCamera(Camera camera, String cameraName) {
|
|
|
|
|
if (AllCamerasByName.containsKey(cameraName)) return false;
|
2019-10-01 02:18:55 -04:00
|
|
|
for (int i = 0; i < 10; i++){
|
2019-09-25 10:10:11 -07:00
|
|
|
camera.addPipeline(); // simple fix to create more pipelines for now
|
|
|
|
|
}
|
2019-09-22 02:49:30 -04:00
|
|
|
AllCamerasByName.put(cameraName, camera);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Camera getCamera(String cameraName) {
|
|
|
|
|
return AllCamerasByName.get(cameraName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Camera getCurrentCamera() throws CameraException {
|
|
|
|
|
if (AllCamerasByName.size() == 0) throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
|
|
|
|
var curCam = AllCamerasByName.get(SettingsManager.GeneralSettings.curr_camera);
|
|
|
|
|
if (curCam == null) throw new CameraException(CameraException.CameraExceptionType.BAD_CAMERA);
|
|
|
|
|
return curCam;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setCurrentCamera(String cameraName) throws CameraException {
|
|
|
|
|
if (!AllCamerasByName.containsKey(cameraName))
|
|
|
|
|
throw new CameraException(CameraException.CameraExceptionType.BAD_CAMERA);
|
|
|
|
|
SettingsManager.GeneralSettings.curr_camera = cameraName;
|
|
|
|
|
SettingsManager.updateCameraSetting(cameraName, getCurrentCamera().getCurrentPipelineIndex());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Pipeline getCurrentPipeline() throws CameraException {
|
|
|
|
|
return getCurrentCamera().getCurrentPipeline();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void setCurrentPipeline(int pipelineNumber) throws CameraException {
|
|
|
|
|
if (!getCurrentCamera().getPipelines().containsKey(pipelineNumber))
|
|
|
|
|
throw new CameraException(CameraException.CameraExceptionType.BAD_PIPELINE);
|
|
|
|
|
getCurrentCamera().setCurrentPipelineIndex(pipelineNumber);
|
|
|
|
|
SettingsManager.updatePipelineSetting(pipelineNumber);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> getResolutionList() throws CameraException {
|
|
|
|
|
if (!SettingsManager.GeneralSettings.curr_camera.equals("")) {
|
2019-10-01 02:18:55 -04:00
|
|
|
return Arrays.stream(CameraManager.getCamera(SettingsManager.GeneralSettings.curr_camera).getAvailableVideoModes())
|
|
|
|
|
.map(res -> String.format("%s X %s at %s fps", res.width, res.height, res.fps)).collect(Collectors.toList());
|
2019-09-22 02:49:30 -04:00
|
|
|
}
|
|
|
|
|
throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
|
|
|
|
}
|
2019-09-29 17:39:58 +03:00
|
|
|
public static VisionProcess getCurrentCameraProcess() throws CameraException{
|
|
|
|
|
if (!SettingsManager.GeneralSettings.curr_camera.equals("")){
|
|
|
|
|
return AllVisionProcessesByName.get(SettingsManager.GeneralSettings.curr_camera);
|
|
|
|
|
}
|
|
|
|
|
throw new CameraException(CameraException.CameraExceptionType.NO_CAMERA);
|
|
|
|
|
}
|
2019-09-22 02:49:30 -04:00
|
|
|
|
|
|
|
|
public static void saveCameras() {
|
|
|
|
|
for (var entry : AllCamerasByName.entrySet()) {
|
|
|
|
|
try {
|
|
|
|
|
Gson gson = new GsonBuilder().setPrettyPrinting().registerTypeAdapter(Camera.class, new CameraSerializer()).create();
|
|
|
|
|
FileWriter writer = new FileWriter(Paths.get(CamConfigPath.toString(), String.format("%s.json", entry.getKey())).toString());
|
|
|
|
|
gson.toJson(entry.getValue(), writer);
|
|
|
|
|
writer.flush();
|
|
|
|
|
writer.close();
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
ex.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-19 14:07:42 -04:00
|
|
|
}
|