2019-09-15 15:03:44 -04:00
|
|
|
package com.chameleonvision.vision.process;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
import com.chameleonvision.MemoryManager;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.settings.SettingsManager;
|
2019-09-16 04:08:23 -04:00
|
|
|
import com.chameleonvision.vision.CameraValues;
|
2019-09-15 15:03:44 -04:00
|
|
|
import com.chameleonvision.vision.Pipeline;
|
2019-09-16 04:08:23 -04:00
|
|
|
import edu.wpi.first.networktables.NetworkTable;
|
|
|
|
|
import edu.wpi.first.networktables.NetworkTableEntry;
|
2019-09-14 17:14:49 +03:00
|
|
|
import edu.wpi.first.networktables.NetworkTableInstance;
|
|
|
|
|
import edu.wpi.first.cameraserver.CameraServer;
|
2019-09-16 04:08:23 -04:00
|
|
|
import org.opencv.core.*;
|
|
|
|
|
import org.opencv.imgproc.Imgproc;
|
2019-09-14 17:14:49 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class CameraProcess implements Runnable {
|
2019-09-14 17:14:49 +03:00
|
|
|
private String CameraName;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
private CameraServer cs = CameraServer.getInstance();
|
|
|
|
|
private NetworkTableEntry ntPipelineEntry, ntDriverModeEntry;
|
|
|
|
|
|
|
|
|
|
private MemoryManager memManager = new MemoryManager(125);
|
|
|
|
|
|
|
|
|
|
private int imgWidth, imgHeight;
|
|
|
|
|
|
|
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
public CameraProcess(String CameraName){
|
|
|
|
|
this.CameraName = CameraName;
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
// add pipeline
|
|
|
|
|
SettingsManager.CamerasCurrentPipeline.put(CameraName, SettingsManager.Cameras.get(CameraName).pipelines.keySet().toArray()[0].toString());
|
|
|
|
|
|
|
|
|
|
// NetworkTables
|
|
|
|
|
NetworkTable ntTable = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + CameraName);
|
|
|
|
|
ntPipelineEntry = ntTable.getEntry("Pipeline");
|
|
|
|
|
ntDriverModeEntry = ntTable.getEntry("Driver_Mode");
|
|
|
|
|
|
|
|
|
|
imgWidth = SettingsManager.Cameras.get(CameraName).camVideoMode.width;
|
|
|
|
|
imgHeight = SettingsManager.Cameras.get(CameraName).camVideoMode.height;
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
|
|
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
2019-09-16 04:08:23 -04:00
|
|
|
var cv_sink = cs.getVideo(SettingsManager.UsbCameras.get(CameraName));
|
|
|
|
|
var cv_publish = cs.putVideo(CameraName, imgWidth, imgHeight);
|
|
|
|
|
double fov = SettingsManager.Cameras.get(CameraName).FOV;
|
|
|
|
|
CameraValues camVals = new CameraValues(imgWidth, imgHeight, fov);
|
|
|
|
|
VisionProcess visionProcess = new VisionProcess(camVals);
|
|
|
|
|
Pipeline currentPipeline;
|
2019-09-14 20:38:50 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
List<MatOfPoint> FoundContours = new ArrayList<>();
|
|
|
|
|
List<MatOfPoint> FilteredContours = new ArrayList<>();
|
2019-09-16 21:45:04 +03:00
|
|
|
List<RotatedRect> GroupedContours = new ArrayList<>();
|
2019-09-16 04:08:23 -04:00
|
|
|
Mat inputMat = new Mat();
|
|
|
|
|
Mat bgrMat = new Mat();
|
|
|
|
|
Mat hsvThreshMat = new Mat();
|
|
|
|
|
Mat outputMat = new Mat();
|
|
|
|
|
Mat contourBoxPointsMat = new Mat();
|
|
|
|
|
Scalar contourColor = new Scalar(255, 0, 0);
|
|
|
|
|
long startTime, endTime;
|
2019-09-16 22:36:30 +03:00
|
|
|
startTime = System.nanoTime();
|
|
|
|
|
int duration = 1;
|
|
|
|
|
int counter = 0;
|
|
|
|
|
double fps = 0;
|
2019-09-16 04:08:23 -04:00
|
|
|
while (!Thread.interrupted()) {
|
|
|
|
|
|
|
|
|
|
FoundContours.clear();
|
|
|
|
|
FilteredContours.clear();
|
|
|
|
|
|
|
|
|
|
currentPipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
2019-09-16 21:45:04 +03:00
|
|
|
|
2019-09-16 04:08:23 -04:00
|
|
|
cv_sink.grabFrame(inputMat);
|
2019-09-16 21:45:04 +03:00
|
|
|
if (inputMat.cols() == 0 && inputMat.rows() == 0) { continue; }
|
|
|
|
|
|
|
|
|
|
// Imgproc.cvtColor(inputMat, bgrMat, Imgproc.COLOR_RGB2BGR, 3);
|
|
|
|
|
|
|
|
|
|
Scalar hsvLower = new Scalar(currentPipeline.hue.get(0), currentPipeline.saturation.get(0), currentPipeline.value.get(0));
|
|
|
|
|
Scalar hsvUpper = new Scalar(currentPipeline.hue.get(1), currentPipeline.saturation.get(1), currentPipeline.value.get(1));
|
|
|
|
|
|
|
|
|
|
visionProcess.HSVThreshold(inputMat, hsvThreshMat, hsvLower, hsvUpper, currentPipeline.erode, currentPipeline.dilate);
|
|
|
|
|
FoundContours = visionProcess.FindContours(hsvThreshMat);
|
|
|
|
|
FilteredContours = visionProcess.FilterContours(FoundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent, currentPipeline.sort_mode, currentPipeline.target_intersection, currentPipeline.target_group);
|
|
|
|
|
GroupedContours = visionProcess.GroupTargets(FilteredContours,currentPipeline.target_intersection,currentPipeline.target_group);
|
|
|
|
|
|
|
|
|
|
if (currentPipeline.is_binary == 1) {
|
|
|
|
|
Imgproc.cvtColor(hsvThreshMat, hsvThreshMat, Imgproc.COLOR_GRAY2BGR, 3);
|
|
|
|
|
outputMat = hsvThreshMat;
|
|
|
|
|
} else {
|
|
|
|
|
outputMat = inputMat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (GroupedContours.size() > 0) {
|
|
|
|
|
List<MatOfPoint> a = new ArrayList<>();
|
|
|
|
|
Point[] vertices = new Point[4];
|
|
|
|
|
GroupedContours.get(0).points(vertices);
|
|
|
|
|
a.add(new MatOfPoint(vertices));
|
|
|
|
|
Imgproc.drawContours(outputMat,a, 0, contourColor, 3);
|
2019-09-16 04:08:23 -04:00
|
|
|
}
|
2019-09-16 21:45:04 +03:00
|
|
|
cv_publish.putFrame(outputMat);
|
2019-09-16 22:36:30 +03:00
|
|
|
System.out.println("fps: " + fps);
|
2019-09-16 21:45:04 +03:00
|
|
|
inputMat.release();
|
|
|
|
|
hsvThreshMat.release();
|
|
|
|
|
for (MatOfPoint oldMat : FoundContours) { oldMat.release(); }
|
|
|
|
|
for (MatOfPoint oldMat1 : FilteredContours) { oldMat1.release(); }
|
2019-09-16 04:08:23 -04:00
|
|
|
memManager.run();
|
2019-09-16 22:36:30 +03:00
|
|
|
counter++;
|
|
|
|
|
if ((System.nanoTime() - startTime)*1e-9 > duration){
|
|
|
|
|
fps = (counter / ((System.nanoTime() - startTime)*1e-9 ));
|
|
|
|
|
counter = 0;
|
|
|
|
|
startTime = System.nanoTime();
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-14 17:14:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|