mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Added SortMode rect filtering, Added exposure/brightness websocket adjustment
This commit is contained in:
@@ -8,6 +8,7 @@ import edu.wpi.first.networktables.NetworkTable;
|
||||
import edu.wpi.first.networktables.NetworkTableEntry;
|
||||
import edu.wpi.first.networktables.NetworkTableInstance;
|
||||
import edu.wpi.first.cameraserver.CameraServer;
|
||||
import org.apache.commons.math3.stat.descriptive.moment.Mean;
|
||||
import org.opencv.core.*;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
@@ -25,7 +26,7 @@ public class CameraProcess implements Runnable {
|
||||
private int imgWidth, imgHeight;
|
||||
|
||||
|
||||
public CameraProcess(String CameraName){
|
||||
public CameraProcess(String CameraName) {
|
||||
this.CameraName = CameraName;
|
||||
|
||||
// add pipeline
|
||||
@@ -34,16 +35,15 @@ public class CameraProcess implements Runnable {
|
||||
// NetworkTables
|
||||
NetworkTable ntTable = NetworkTableInstance.getDefault().getTable("/chameleon-vision/" + CameraName);
|
||||
ntPipelineEntry = ntTable.getEntry("Pipeline");
|
||||
ntDriverModeEntry = ntTable.getEntry("Driver_Mode");
|
||||
ntDriverModeEntry = ntTable.getEntry("Driver_Mode");
|
||||
|
||||
imgWidth = SettingsManager.Cameras.get(CameraName).camVideoMode.width;
|
||||
imgHeight = SettingsManager.Cameras.get(CameraName).camVideoMode.height;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
// camera values
|
||||
var cv_sink = cs.getVideo(SettingsManager.UsbCameras.get(CameraName));
|
||||
var cv_publish = cs.putVideo(CameraName, imgWidth, imgHeight);
|
||||
double fov = SettingsManager.Cameras.get(CameraName).FOV;
|
||||
@@ -51,69 +51,72 @@ public class CameraProcess implements Runnable {
|
||||
VisionProcess visionProcess = new VisionProcess(camVals);
|
||||
Pipeline currentPipeline;
|
||||
|
||||
// actual OpenCV objects
|
||||
List<MatOfPoint> FoundContours = new ArrayList<>();
|
||||
List<MatOfPoint> FilteredContours = new ArrayList<>();
|
||||
List<RotatedRect> GroupedContours = new ArrayList<>();
|
||||
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;
|
||||
startTime = System.nanoTime();
|
||||
int duration = 1;
|
||||
int counter = 0;
|
||||
double fps = 0;
|
||||
while (!Thread.interrupted()) {
|
||||
|
||||
// processing time tracking
|
||||
long startTime;
|
||||
double processTimeMs;
|
||||
double fps;
|
||||
|
||||
while (!Thread.interrupted()) {
|
||||
FoundContours.clear();
|
||||
FilteredContours.clear();
|
||||
GroupedContours.clear();
|
||||
|
||||
currentPipeline = SettingsManager.Cameras.get(CameraName).pipelines.get(SettingsManager.CamerasCurrentPipeline.get(CameraName));
|
||||
|
||||
// start fps counter right before grabbing input frame
|
||||
startTime = System.nanoTime();
|
||||
cv_sink.grabFrame(inputMat);
|
||||
if (inputMat.cols() == 0 && inputMat.rows() == 0) { continue; }
|
||||
|
||||
// Imgproc.cvtColor(inputMat, bgrMat, Imgproc.COLOR_RGB2BGR, 3);
|
||||
if (inputMat.cols() == 0 && inputMat.rows() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
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;
|
||||
Imgproc.cvtColor(hsvThreshMat, outputMat, Imgproc.COLOR_GRAY2BGR, 3);
|
||||
} 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);
|
||||
FoundContours = visionProcess.FindContours(hsvThreshMat);
|
||||
if (FoundContours.size() > 0) {
|
||||
FilteredContours = visionProcess.FilterContours(FoundContours, currentPipeline.area, currentPipeline.ratio, currentPipeline.extent, currentPipeline.sort_mode, currentPipeline.target_intersection, currentPipeline.target_group);
|
||||
if (FilteredContours.size() > 0) {
|
||||
GroupedContours = visionProcess.GroupTargets(FilteredContours, currentPipeline.target_intersection, currentPipeline.target_group);
|
||||
if (GroupedContours.size() > 0) {
|
||||
var finalRect = visionProcess.SortTargetsToOne(GroupedContours, currentPipeline.sort_mode);
|
||||
if (finalRect != null) {
|
||||
List<MatOfPoint> a = new ArrayList<>();
|
||||
Point[] vertices = new Point[4];
|
||||
finalRect.points(vertices);
|
||||
a.add(new MatOfPoint(vertices));
|
||||
Imgproc.drawContours(outputMat, a, 0, contourColor, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cv_publish.putFrame(outputMat);
|
||||
System.out.println("fps: " + fps);
|
||||
|
||||
// calculate FPS after publishing output frame
|
||||
processTimeMs = (System.nanoTime() - startTime) * 1e-6;
|
||||
fps = 1000 / processTimeMs;
|
||||
System.out.printf("Process time: %fms, FPS: %.2f, FoundContours: %d, FilteredContours: %d, GroupedContours: %d\n", processTimeMs, fps, FoundContours.size(), FilteredContours.size(), GroupedContours.size());
|
||||
|
||||
inputMat.release();
|
||||
hsvThreshMat.release();
|
||||
for (MatOfPoint oldMat : FoundContours) { oldMat.release(); }
|
||||
for (MatOfPoint oldMat1 : FilteredContours) { oldMat1.release(); }
|
||||
memManager.run();
|
||||
counter++;
|
||||
if ((System.nanoTime() - startTime)*1e-9 > duration){
|
||||
fps = (counter / ((System.nanoTime() - startTime)*1e-9 ));
|
||||
counter = 0;
|
||||
startTime = System.nanoTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user