mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Adjusted folder structure to sit under com.chameleonvision package
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.chameleonvision;
|
||||
|
||||
public class NoCameraException extends Exception {
|
||||
public NoCameraException(){
|
||||
super("No Camera Connected");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
package com.chameleonvision.settings;
|
||||
import com.chameleonvision.NoCameraException;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
|
||||
import com.chameleonvision.vision.CamVideoMode;
|
||||
import com.chameleonvision.vision.Camera;
|
||||
import com.chameleonvision.vision.GeneralSettings;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import com.google.gson.Gson;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import edu.wpi.cscore.*;
|
||||
import org.opencv.videoio.VideoCapture;
|
||||
|
||||
public class SettingsManager {
|
||||
private static SettingsManager instance;
|
||||
private SettingsManager() {
|
||||
InitiateGeneralSettings();
|
||||
InitiateCamerasInfo();
|
||||
InitiateUsbCameras();
|
||||
InitiateCameras();
|
||||
InitiateUsbCamerasSettings();
|
||||
}
|
||||
public static synchronized SettingsManager getInstance(){
|
||||
if(instance == null){
|
||||
synchronized (SettingsManager.class) {
|
||||
if(instance == null){
|
||||
instance = new SettingsManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
public static HashMap<String, Camera> Cameras = new HashMap<String, Camera>();
|
||||
public static HashMap<String,UsbCamera> UsbCameras = new HashMap<String, UsbCamera>();
|
||||
public static HashMap<String,UsbCameraInfo> USBCamerasInfo = new HashMap<String,UsbCameraInfo>();
|
||||
public static com.chameleonvision.vision.GeneralSettings GeneralSettings;
|
||||
public static HashMap<String, String> CamerasCurrentPipeline = new HashMap<String, String>();
|
||||
public static HashMap<String,String> CameraPort = new HashMap<String, String>();
|
||||
private Path SettingsPath = Paths.get(System.getProperty("user.dir"),"Settings");
|
||||
private Path CamsPath = Paths.get(SettingsPath.toString(),"Cams");
|
||||
|
||||
|
||||
|
||||
private void InitiateGeneralSettings(){
|
||||
CheckPath(SettingsPath);
|
||||
try {
|
||||
GeneralSettings = new Gson().fromJson(new FileReader(Paths.get(SettingsPath.toString(),"Settings.json").toString()), com.chameleonvision.vision.GeneralSettings.class);
|
||||
} catch (FileNotFoundException e) {
|
||||
GeneralSettings = new GeneralSettings();
|
||||
}
|
||||
}
|
||||
|
||||
private void InitiateCamerasInfo(){
|
||||
List<Integer> TrueCameras = new ArrayList<Integer>();
|
||||
UsbCameraInfo[] UsbDevices = UsbCamera.enumerateUsbCameras();
|
||||
for (var i=0; i < UsbDevices.length; i++){
|
||||
var cap = new VideoCapture(UsbDevices[i].dev);
|
||||
if (cap.isOpened()){
|
||||
TrueCameras.add(i);
|
||||
cap.release();
|
||||
}
|
||||
|
||||
}
|
||||
for (var i: TrueCameras){
|
||||
var DeviceName = UsbDevices[i].name;
|
||||
var suffix = 0;
|
||||
while (USBCamerasInfo.containsKey(DeviceName)){
|
||||
suffix++;
|
||||
DeviceName = String.format("%s(%s)",UsbDevices[i].name,suffix);
|
||||
}
|
||||
USBCamerasInfo.put(DeviceName,UsbDevices[i]);
|
||||
}
|
||||
}
|
||||
private void InitiateUsbCameras(){
|
||||
for(Map.Entry<String,UsbCameraInfo> entry : USBCamerasInfo.entrySet()){
|
||||
var device = entry.getValue();
|
||||
var camera = new UsbCamera(device.name, device.dev);
|
||||
UsbCameras.put(device.name,camera);
|
||||
}
|
||||
}
|
||||
private void InitiateCameras(){
|
||||
CheckPath(CamsPath);
|
||||
for(Map.Entry<String,UsbCameraInfo> entry: USBCamerasInfo.entrySet()){
|
||||
var camPath = Paths.get(CamsPath.toString(),String.format("%s.json",entry.getKey()));
|
||||
if(Files.exists(camPath)){
|
||||
try {
|
||||
Camera cam = new Gson().fromJson(new FileReader(camPath.toString()),Camera.class);
|
||||
Cameras.put(entry.getKey(),cam);
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else{
|
||||
CreateNewCam(entry.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
private void InitiateUsbCamerasSettings(){
|
||||
for(Map.Entry<String,UsbCamera> entry: UsbCameras.entrySet()){
|
||||
var cam = entry.getValue();
|
||||
var camName = entry.getKey();
|
||||
var camInfo = Cameras.get(camName);
|
||||
cam.setPixelFormat(VideoMode.PixelFormat.valueOf(camInfo.camVideoMode.pixel_format));
|
||||
cam.setFPS(camInfo.camVideoMode.fps);
|
||||
cam.setResolution(camInfo.camVideoMode.width, camInfo.camVideoMode.heigh);
|
||||
}
|
||||
}
|
||||
private void CheckPath(Path path){
|
||||
if (!Files.exists(path)){
|
||||
try {
|
||||
Files.createDirectories(path);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
// Creators
|
||||
private void CreateNewCam(String CameraName){
|
||||
Camera cam = new Camera();
|
||||
var caminfo =USBCamerasInfo.get(CameraName);
|
||||
cam.path = caminfo.path;
|
||||
var videomode = UsbCameras.get(CameraName).enumerateVideoModes()[0];
|
||||
CamVideoMode CamVideoMode = new CamVideoMode();
|
||||
CamVideoMode.fps = videomode.fps;
|
||||
CamVideoMode.heigh = videomode.height;
|
||||
CamVideoMode.width = videomode.width;
|
||||
CamVideoMode.pixel_format = videomode.pixelFormat.name();
|
||||
cam.camVideoMode = CamVideoMode;
|
||||
cam.pipelines = new HashMap<String, Pipeline>();
|
||||
cam.resolution = 0;
|
||||
cam.FOV = 60.8;
|
||||
Cameras.put(CameraName,cam);
|
||||
|
||||
CreateNewPipeline(null,CameraName);
|
||||
|
||||
}
|
||||
private void CreateNewPipeline(String PipeName, String CamName){
|
||||
if (CamName == null){
|
||||
CamName = GeneralSettings.curr_camera;
|
||||
}
|
||||
var cam = Cameras.get(CamName);
|
||||
if (PipeName == null){
|
||||
var suffix = 0;
|
||||
PipeName = "pipeline" + suffix;
|
||||
while (cam.pipelines.containsKey(PipeName)){
|
||||
suffix ++;
|
||||
PipeName = "pipeline"+suffix;
|
||||
}
|
||||
}
|
||||
else if (cam.pipelines.containsKey(PipeName)){
|
||||
System.err.println("Pipeline Already Exists");
|
||||
}
|
||||
cam.pipelines.put(PipeName,new Pipeline());
|
||||
}
|
||||
//Access Methods
|
||||
public Pipeline GetCurrentPipeline() throws Exception {
|
||||
if (!GeneralSettings.curr_pipeline.equals("")){
|
||||
return Cameras.get(GeneralSettings.curr_camera).pipelines.get(GeneralSettings.curr_pipeline);
|
||||
}
|
||||
throw new NoCameraException();
|
||||
}
|
||||
public Camera GetCurrentCamera() throws Exception {
|
||||
if (!GeneralSettings.curr_camera.equals("")){
|
||||
return Cameras.get(GeneralSettings.curr_camera);
|
||||
}
|
||||
throw new NoCameraException();
|
||||
}
|
||||
public List<String> GetResolutionList() throws NoCameraException {
|
||||
if (!GeneralSettings.curr_camera.equals("")){
|
||||
List<String> list = new ArrayList<String>();
|
||||
var cam = UsbCameras.get(GeneralSettings.curr_camera);
|
||||
for (var res : cam.enumerateVideoModes()){
|
||||
list.add(String.format("%s X %s at %s fps", res.width, res.height, res.fps));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
throw new NoCameraException();
|
||||
}
|
||||
public void SetCurrentCamera(String CamName) throws Exception {
|
||||
if (Cameras.containsKey(CamName)){
|
||||
GeneralSettings.curr_camera = CamName;
|
||||
GeneralSettings.curr_pipeline = GetCurrentCamera().pipelines.keySet().stream().findFirst().toString();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
public void SetCurrentPipeline(String PipelineName) throws Exception {
|
||||
if (GetCurrentCamera().pipelines.containsKey(PipelineName)){
|
||||
GeneralSettings.curr_pipeline = PipelineName;
|
||||
}
|
||||
}
|
||||
//Savers
|
||||
public void SaveSettings(){
|
||||
SaveCameras();
|
||||
SaveGeneralSettings();
|
||||
}
|
||||
private void SaveCameras(){
|
||||
for(Map.Entry<String,Camera> entry: Cameras.entrySet()){
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
FileWriter writer = new FileWriter(Paths.get(CamsPath.toString(),String.format("%s.json",entry.getKey())).toString());
|
||||
gson.toJson(entry.getValue(),writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
private void SaveGeneralSettings(){
|
||||
try {
|
||||
Gson gson = new Gson();
|
||||
FileWriter writer = new FileWriter(Paths.get(SettingsPath.toString(),"Settings.json").toString());
|
||||
new Gson().toJson(GeneralSettings, writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
public class CamVideoMode {
|
||||
public int fps;
|
||||
public int width;
|
||||
public int heigh;
|
||||
public String pixel_format;
|
||||
|
||||
}
|
||||
12
Main/src/main/java/com/chameleonvision/vision/Camera.java
Normal file
12
Main/src/main/java/com/chameleonvision/vision/Camera.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Camera {
|
||||
public Double FOV = 60.8;
|
||||
public String path = "";
|
||||
public HashMap<String, Pipeline> pipelines;
|
||||
public int resolution = 0;
|
||||
public CamVideoMode camVideoMode;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.chameleonvision.vision;
|
||||
|
||||
public class GeneralSettings {
|
||||
public int team_number = 1577;
|
||||
public String connection_type = "DHCP";
|
||||
public String ip = "";
|
||||
public String gateway = "";
|
||||
public String netmask = "";
|
||||
public String hostname = "Chameleon-vision";
|
||||
public String curr_camera = "";
|
||||
public String curr_pipeline = "";
|
||||
|
||||
|
||||
}
|
||||
23
Main/src/main/java/com/chameleonvision/vision/Pipeline.java
Normal file
23
Main/src/main/java/com/chameleonvision/vision/Pipeline.java
Normal file
@@ -0,0 +1,23 @@
|
||||
package com.chameleonvision.vision;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Pipeline {
|
||||
public int exposure = 50;
|
||||
public int brightness = 50;
|
||||
public String orientation = "Normal";
|
||||
public List<Integer> hue = Arrays.asList(0,100);
|
||||
public List<Integer> saturation = Arrays.asList(0,100);
|
||||
public List<Integer> value = Arrays.asList(0,100);
|
||||
public boolean erode = false;
|
||||
public boolean dilate = false;
|
||||
public List<Integer> area = Arrays.asList(0,100);
|
||||
public List<Integer> ratio = Arrays.asList(0,20);
|
||||
public List<Integer> extent = Arrays.asList(0,100);
|
||||
public boolean is_binary = false;
|
||||
public String sort_mode = "Largest";
|
||||
public String target_group = "Single";
|
||||
public String target_intersection = "Largest";
|
||||
public double M = 1;
|
||||
public double B = 0;
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import com.chameleonvision.vision.Pipeline;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.opencv.core.Mat;
|
||||
import org.apache.commons.math3.fraction.Fraction;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
|
||||
public class CameraProcess implements Runnable{
|
||||
private String CameraName;
|
||||
public CameraProcess(String CameraName){
|
||||
this.CameraName = CameraName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
//calling all classes
|
||||
CameraServer cs = CameraServer.getInstance();
|
||||
NetworkTableInstance networkTableInstance = NetworkTableInstance.getDefault();
|
||||
SettingsManager manager = SettingsManager.getInstance();
|
||||
SettingsManager.CamerasCurrentPipeline.put(CameraName, SettingsManager.Cameras.get(CameraName).pipelines.keySet().toArray()[0].toString());
|
||||
//Setting up camera and network table
|
||||
var Table = networkTableInstance.getTable("/Chameleon-vision/" + CameraName);
|
||||
var PipeLineEntry = Table.getEntry("Pipeline");
|
||||
var DriverModeEntry = Table.getEntry("Driver_Mode");
|
||||
var cv_sink = cs.getVideo(manager.UsbCameras.get(CameraName));
|
||||
|
||||
|
||||
int Width = manager.Cameras.get(CameraName).camVideoMode.width;
|
||||
int Height = manager.Cameras.get(CameraName).camVideoMode.heigh;
|
||||
var cv_publish = cs.putVideo(CameraName,Width,Height);
|
||||
//initial math setup for camera
|
||||
double DiagonalView = FastMath.toRadians(manager.Cameras.get(CameraName).FOV);
|
||||
Fraction AspectFraction = new Fraction(Width,Height);
|
||||
int HorizontalRatio = AspectFraction.getNumerator();
|
||||
int VerticalRatio = AspectFraction.getDenominator();
|
||||
double HorizontalView = FastMath.atan(FastMath.tan(DiagonalView/2) * (HorizontalRatio / DiagonalView)) * 2;
|
||||
double VerticalView = FastMath.atan(FastMath.tan(DiagonalView/2) * (VerticalRatio / DiagonalView)) * 2;
|
||||
double H_FOCAL_LENGTH = Width / (2 * FastMath.tan(HorizontalView /2));
|
||||
double V_FOCAL_LENGTH = Width / (2 * FastMath.tan(VerticalView /2));
|
||||
double CenterX = ((double) Width / 2) - 0.5;
|
||||
double CenterY = ((double) Height/2) - 0.5;
|
||||
double CamArea = (double)(Width * Height);
|
||||
VisionProcess visionProcess = new VisionProcess(CenterX,CenterY,CamArea);
|
||||
Mat mat = new Mat();
|
||||
long time;
|
||||
|
||||
while (!Thread.interrupted()){
|
||||
Pipeline pipeline = manager.Cameras.get(CameraName).pipelines.get(manager.CamerasCurrentPipeline.get(CameraName));
|
||||
time = cv_sink.grabFrame(mat);
|
||||
if (mat.cols() !=0 && mat.rows() != 0) {
|
||||
Mat HSVImage = visionProcess.HSVThreshold(pipeline.hue, pipeline.saturation, pipeline.value, mat, pipeline.erode, pipeline.dilate);
|
||||
// List<MatOfPoint> Contours = visionProcess.FindContours(HSVImage);
|
||||
// List<MatOfPoint> FilterdContours = visionProcess.FilterContours(Contours, pipeline.area, pipeline.ratio, pipeline.extent, pipeline.sort_mode, pipeline.target_intersection, pipeline.target_group);
|
||||
cv_publish.putFrame(mat);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.chameleonvision.vision.process;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class VisionProcess {
|
||||
private HashMap<String, Integer>TargetGrouping= new HashMap<String, Integer>(){{
|
||||
put("Single",1);
|
||||
put("Dual",2);
|
||||
put("Triple",3);
|
||||
put("Quadruple",4);
|
||||
put("Quintuple",5);
|
||||
}};
|
||||
private double CamArea,CenterX, CenterY;
|
||||
VisionProcess(double CenterX, double CenterY, double CamArea){
|
||||
this.CenterX = CenterX;
|
||||
this.CenterY = CenterY;
|
||||
this.CamArea = CamArea;
|
||||
}
|
||||
private Mat Kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(5, 5));
|
||||
|
||||
public Mat HSVThreshold(@NotNull List<Integer> hue, @NotNull List<Integer> saturation , @NotNull List<Integer> value, Mat image, boolean IsErode, boolean IsDilate){
|
||||
Mat hsv = new Mat();
|
||||
Imgproc.cvtColor(image,hsv,Imgproc.COLOR_BGR2HSV,3);
|
||||
new Scalar(hue.get(0),saturation.get(0),value.get(0));
|
||||
Mat threshold = new Mat();
|
||||
Core.inRange(hsv,new Scalar(hue.get(0),saturation.get(0),value.get(0)),new Scalar(hue.get(1),saturation.get(1),value.get(1)),threshold);
|
||||
if (IsErode){
|
||||
Imgproc.erode(threshold,threshold, Kernel);
|
||||
}
|
||||
if (IsDilate){
|
||||
Imgproc.dilate(threshold,threshold, Kernel);
|
||||
}
|
||||
return threshold;
|
||||
}
|
||||
public List<MatOfPoint> FindContours(Mat BinaryImage){
|
||||
List<MatOfPoint> Contours = new ArrayList<>();
|
||||
Imgproc.findContours(BinaryImage,Contours,new Mat(),Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_TC89_L1);
|
||||
return Contours;
|
||||
}
|
||||
public List<MatOfPoint> FilterContours(List<MatOfPoint> InputContours, List<Integer> area, List<Integer> ratio, List<Integer> extent, String SortMode,String TargetIntersection , String TargetGrouping){
|
||||
List<MatOfPoint> FilteredContours = new ArrayList<MatOfPoint>();
|
||||
for (MatOfPoint Contour : InputContours){
|
||||
try{
|
||||
var contourArea = Imgproc.contourArea(Contour);
|
||||
double targetArea = (contourArea / CamArea) * 100;
|
||||
if (targetArea >= area.get(0) || targetArea <= area.get(1)){
|
||||
continue;
|
||||
}
|
||||
var rect = Imgproc.minAreaRect(new MatOfPoint2f(Contour.toArray()));
|
||||
var targetFullness = (contourArea/rect.size.area())*100;
|
||||
if (targetFullness <= extent.get(0) || targetArea >= extent.get(1)){
|
||||
continue;
|
||||
}
|
||||
var aspectRatio = rect.size.width / rect.size.height;
|
||||
if (aspectRatio <= ratio.get(0) || aspectRatio >= ratio.get(1)){
|
||||
continue;
|
||||
}
|
||||
FilteredContours.add(Contour);
|
||||
} catch (Exception e){
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return FilteredContours;
|
||||
}
|
||||
private List<MatOfPoint> GroupTargets(List<MatOfPoint> InputContours, String IntersectionPoint,String TargetGroup){
|
||||
if (!TargetGroup.equals("Single")){
|
||||
List<MatOfPoint> FinalCountours = new ArrayList<MatOfPoint>();
|
||||
for (var i = 0; i < InputContours.size(); i++){
|
||||
var FinalContour = InputContours.get(i);
|
||||
for (var c = 0; c < (TargetGrouping.get(TargetGroup)-1);c++){
|
||||
try{
|
||||
MatOfPoint firstContour = InputContours.get(i + c);
|
||||
MatOfPoint secoundContour = InputContours.get(i+c+1);
|
||||
if (IsIntersecting(firstContour,secoundContour, IntersectionPoint)){
|
||||
System.out.println("");
|
||||
}
|
||||
} catch (IndexOutOfBoundsException e){
|
||||
FinalContour = new MatOfPoint();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return InputContours;
|
||||
}
|
||||
private boolean IsIntersecting(MatOfPoint ContourOne, MatOfPoint ContourTwo, String IntersectionPoint){
|
||||
Mat LineA = new Mat();
|
||||
Imgproc.fitLine(ContourOne,LineA,Imgproc.CV_DIST_L2,0,0.01,0.01);
|
||||
Mat LineB = new Mat();
|
||||
Imgproc.fitLine(ContourTwo,LineB,Imgproc.CV_DIST_L2,0,0.01,0.01);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
39
Main/src/main/java/com/chameleonvision/web/Server.java
Normal file
39
Main/src/main/java/com/chameleonvision/web/Server.java
Normal file
@@ -0,0 +1,39 @@
|
||||
package com.chameleonvision.web;
|
||||
|
||||
import com.chameleonvision.settings.SettingsManager;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.websocket.WsContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Server {
|
||||
private static List<WsContext> users = new ArrayList<WsContext>();
|
||||
public static void main(int port) {
|
||||
Javalin app = Javalin.create();
|
||||
app.config.addStaticFiles("web");
|
||||
app.ws("/websocket", ws ->{
|
||||
ws.onConnect(ctx -> {
|
||||
users.add(ctx);
|
||||
System.out.println("Socket Connected");
|
||||
});
|
||||
ws.onClose(ctx -> {
|
||||
users.remove(ctx);
|
||||
System.out.println("Socket Disconnected");
|
||||
SettingsManager.getInstance().SaveSettings();
|
||||
});
|
||||
ws.onMessage(ctx -> {
|
||||
broadcastMessage(ctx, ctx.message());
|
||||
});
|
||||
});
|
||||
app.start(port);
|
||||
}
|
||||
private static void broadcastMessage(WsContext sendingUser, String message){
|
||||
for (var user : users)
|
||||
{
|
||||
if (user != sendingUser){
|
||||
user.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user