mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Fix timing calculations, add OpenCV exception catching to some pipes
This commit is contained in:
@@ -362,13 +362,11 @@ public class VisionProcess {
|
||||
result = currentPipeline.runPipeline(streamBuffer);
|
||||
lastPipelineResult = result;
|
||||
|
||||
var yes = lastPipelineResult==null;
|
||||
if (result != null) {
|
||||
updateNetworkTableData(lastPipelineResult);
|
||||
updateUI(lastPipelineResult);
|
||||
}
|
||||
|
||||
updateNetworkTableData(lastPipelineResult);
|
||||
updateUI(lastPipelineResult);
|
||||
|
||||
} else {
|
||||
// System.err.println("Bad streambuffer mat");
|
||||
}
|
||||
|
||||
var deltaTimeNanos = lastUpdateTimeNanos - System.nanoTime();
|
||||
|
||||
@@ -31,6 +31,8 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
|
||||
@Override
|
||||
public CVPipeline2dResult runPipeline(Mat inputMat) {
|
||||
long totalProcessTimeNanos = 0;
|
||||
long processStartTimeNanos = System.nanoTime();
|
||||
|
||||
if (cameraProcess == null) {
|
||||
throw new RuntimeException("Pipeline was not initialized before being run!");
|
||||
@@ -39,12 +41,13 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
throw new RuntimeException("uwu uwu ");
|
||||
}
|
||||
|
||||
long totalProcessTimeNanos = 0;
|
||||
StringBuilder procTimeStringBuilder = new StringBuilder();
|
||||
|
||||
CameraStaticProperties camProps = cameraProcess.getProperties().staticProperties;
|
||||
|
||||
rawCameraMat = inputMat;
|
||||
inputMat.copyTo(rawCameraMat);
|
||||
|
||||
// rawCameraMat = inputMat;
|
||||
|
||||
// prepare pipes
|
||||
RotateFlipPipe rotateFlipPipe = new RotateFlipPipe(ImageRotation.DEG_0, settings.flipMode);
|
||||
@@ -75,67 +78,77 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
|
||||
Draw2dContoursPipe draw2dContoursPipe = new Draw2dContoursPipe(draw2dContoursSettings, camProps);
|
||||
|
||||
long pipeInitTimeNanos = System.nanoTime() - processStartTimeNanos;
|
||||
totalProcessTimeNanos += pipeInitTimeNanos;
|
||||
procTimeStringBuilder.append(String.format("PipeInit: %.2fms, ", pipeInitTimeNanos / 1000000.0));
|
||||
|
||||
// run pipes
|
||||
Pair<Mat, Long> rotateFlipResult = rotateFlipPipe.run(inputMat);
|
||||
totalProcessTimeNanos += rotateFlipResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("RotateFlip: %.2fms, ", rotateFlipResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("RotateFlip: %.2fms, ", rotateFlipResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<Mat, Long> blurResult = blurPipe.run(rotateFlipResult.getLeft());
|
||||
totalProcessTimeNanos += blurResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("Blur: %.2fms, ", blurResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("Blur: %.2fms, ", blurResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<Mat, Long> erodeDilateResult = erodeDilatePipe.run(blurResult.getLeft());
|
||||
totalProcessTimeNanos += erodeDilateResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("ErodeDilate: %.2fms, ", erodeDilateResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("ErodeDilate: %.2fms, ", erodeDilateResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<Mat, Long> hsvResult = hsvPipe.run(erodeDilateResult.getLeft());
|
||||
totalProcessTimeNanos += hsvResult.getRight();
|
||||
Imgproc.cvtColor(hsvResult.getLeft(), hsvOutputMat, Imgproc.COLOR_GRAY2BGR, 3);
|
||||
procTimeStringBuilder.append(String.format("HSV: %.2fms, ", hsvResult.getRight() / 1000.0));
|
||||
|
||||
try {
|
||||
Imgproc.cvtColor(hsvResult.getLeft(), hsvOutputMat, Imgproc.COLOR_GRAY2BGR, 3);
|
||||
} catch (CvException e) {
|
||||
System.err.println("(CVPipeline2d) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
|
||||
procTimeStringBuilder.append(String.format("HSV: %.2fms, ", hsvResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<MatOfPoint>, Long> findContoursResult = findContoursPipe.run(hsvResult.getLeft());
|
||||
totalProcessTimeNanos += findContoursResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("FindContours: %.2fms, ", findContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("FindContours: %.2fms, ", findContoursResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<MatOfPoint>, Long> filterContoursResult = filterContoursPipe.run(findContoursResult.getLeft());
|
||||
totalProcessTimeNanos += filterContoursResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("FilterContours: %.2fms, ", filterContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("FilterContours: %.2fms, ", filterContoursResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<MatOfPoint>, Long> speckleRejectResult = speckleRejectPipe.run(filterContoursResult.getLeft());
|
||||
totalProcessTimeNanos += speckleRejectResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("SpeckleReject: %.2fms, ", speckleRejectResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("SpeckleReject: %.2fms, ", speckleRejectResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<RotatedRect>, Long> groupContoursResult = groupContoursPipe.run(speckleRejectResult.getLeft());
|
||||
totalProcessTimeNanos += groupContoursResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("GroupContours: %.2fms, ", groupContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("GroupContours: %.2fms, ", groupContoursResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<RotatedRect>, Long> sortContoursResult = sortContoursPipe.run(groupContoursResult.getLeft());
|
||||
totalProcessTimeNanos += sortContoursResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("SortContours: %.2fms, ", sortContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("SortContours: %.2fms, ", sortContoursResult.getRight() / 1000000.0));
|
||||
|
||||
Pair<List<Target2d>, Long> collect2dTargetsResult = collect2dTargetsPipe.run(sortContoursResult.getLeft());
|
||||
totalProcessTimeNanos += collect2dTargetsResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("SortContours: %.2fms, ", sortContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("SortContours: %.2fms, ", sortContoursResult.getRight() / 1000000.0));
|
||||
|
||||
// takes pair of (Mat of original camera image, Mat of HSV thresholded image)
|
||||
Pair<Mat, Long> outputMatResult = outputMatPipe.run(Pair.of(rawCameraMat, hsvOutputMat));
|
||||
totalProcessTimeNanos += outputMatResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("OutputMat: %.2fms, ", outputMatResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("OutputMat: %.2fms, ", outputMatResult.getRight() / 1000000.0));
|
||||
|
||||
// takes pair of (Mat to draw on, List<RotatedRect> of sorted contours)
|
||||
Pair<Mat, Long> draw2dContoursResult = draw2dContoursPipe.run(Pair.of(outputMatResult.getLeft(), sortContoursResult.getLeft()));
|
||||
totalProcessTimeNanos += draw2dContoursResult.getRight();
|
||||
procTimeStringBuilder.append(String.format("Draw2dContours: %.2fms, ", draw2dContoursResult.getRight() / 1000.0));
|
||||
procTimeStringBuilder.append(String.format("Draw2dContours: %.2fms, ", draw2dContoursResult.getRight() / 1000000.0));
|
||||
|
||||
System.out.println(procTimeStringBuilder.toString());
|
||||
System.out.printf("Pipeline ran in %.3fms\n", totalProcessTimeNanos / 1000.0);
|
||||
System.out.printf("Pipeline ran in %.3fms\n", totalProcessTimeNanos / 1000000.0);
|
||||
|
||||
return new CVPipeline2dResult(collect2dTargetsResult.getLeft(), draw2dContoursResult.getLeft(), totalProcessTimeNanos / 1000);
|
||||
return new CVPipeline2dResult(collect2dTargetsResult.getLeft(), draw2dContoursResult.getLeft(), totalProcessTimeNanos);
|
||||
}
|
||||
|
||||
public static class CVPipeline2dResult extends CVPipelineResult<Target2d> {
|
||||
public CVPipeline2dResult(List<Target2d> targets, Mat outputMat, long processTime) {
|
||||
super(targets, outputMat, processTime);
|
||||
public CVPipeline2dResult(List<Target2d> targets, Mat outputMat, long processTimeNanos) {
|
||||
super(targets, outputMat, processTimeNanos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.CvException;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Size;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
|
||||
public class BlurPipe implements Pipe<Mat, Mat> {
|
||||
|
||||
@@ -18,15 +21,15 @@ public class BlurPipe implements Pipe<Mat, Mat> {
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
// TODO (HIGH) make this blur
|
||||
input.copyTo(processBuffer);
|
||||
// if (blurSize > 0) {
|
||||
// Imgproc.blur(outputMat, outputMat, new Size(blurSize, blurSize));
|
||||
// }
|
||||
try {
|
||||
if (blurSize > 0) {
|
||||
Imgproc.blur(outputMat, outputMat, new Size(blurSize, blurSize));
|
||||
}
|
||||
} catch (CvException e) {
|
||||
System.err.println("(BlurPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
processBuffer.release();
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class Collect2dTargetsPipe implements Pipe<List<RotatedRect>, List<CVPipe
|
||||
targets.add(t);
|
||||
});
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(targets, processTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
outputMat.release();
|
||||
return output;
|
||||
|
||||
@@ -35,8 +35,7 @@ public class ErodeDilatePipe implements Pipe<Mat, Mat> {
|
||||
input.copyTo(processBuffer);
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
processBuffer.copyTo(outputMat);
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
|
||||
processBuffer.release();
|
||||
|
||||
@@ -56,7 +56,7 @@ public class FilterContoursPipe implements Pipe<List<MatOfPoint>, List<MatOfPoin
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(filteredContours, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class FindContoursPipe implements Pipe<Mat, List<MatOfPoint>> {
|
||||
|
||||
Imgproc.findContours(input, foundContours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_TC89_L1);
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(foundContours, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,7 +85,7 @@ public class GroupContoursPipe implements Pipe<List<MatOfPoint>, List<RotatedRec
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(groupedContours, processTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.chameleonvision.vision.pipeline.pipes;
|
||||
|
||||
import org.apache.commons.lang3.tuple.Pair;
|
||||
import org.opencv.core.Core;
|
||||
import org.opencv.core.CvException;
|
||||
import org.opencv.core.Mat;
|
||||
import org.opencv.core.Scalar;
|
||||
import org.opencv.imgproc.Imgproc;
|
||||
@@ -22,16 +23,14 @@ public class HsvPipe implements Pipe<Mat, Mat> {
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
// convert from rgb to hsv
|
||||
if(input.empty()) {
|
||||
throw new RuntimeException("HSV input cannot be empty!");
|
||||
try {
|
||||
Imgproc.cvtColor(input, outputMat, Imgproc.COLOR_RGB2HSV, 3);
|
||||
Core.inRange(outputMat, hsvLower, hsvUpper, outputMat);
|
||||
} catch (CvException e) {
|
||||
System.err.println("(HsvPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
Imgproc.cvtColor(input, processBuffer, Imgproc.COLOR_RGB2HSV, 3);
|
||||
|
||||
Core.inRange(processBuffer, hsvLower, hsvUpper, processBuffer);
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
processBuffer.copyTo(outputMat);
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
processBuffer.release();
|
||||
return output;
|
||||
|
||||
@@ -19,7 +19,7 @@ public class OutputMatPipe implements Pipe<Pair<Mat, Mat>, Mat> {
|
||||
|
||||
outputMat = showThresholded ? input.getRight() : input.getLeft();
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
outputMat.release();
|
||||
return output;
|
||||
|
||||
@@ -26,8 +26,7 @@ public class RotateFlipPipe implements Pipe<Mat, Mat> {
|
||||
Core.flip(input, processBuffer, flip.value);
|
||||
Core.rotate(processBuffer, processBuffer, rotation.value);
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
processBuffer.copyTo(outputMat);
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
Pair<Mat, Long> output = Pair.of(outputMat, processTime);
|
||||
processBuffer.release();
|
||||
return output;
|
||||
|
||||
@@ -64,7 +64,7 @@ public class SortContoursPipe implements Pipe<List<RotatedRect>, List<RotatedRec
|
||||
break;
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(sortedContours, processTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ public class SpeckleRejectPipe implements Pipe<List<MatOfPoint>, List<MatOfPoint
|
||||
}
|
||||
}
|
||||
|
||||
long processTime = processStartNanos - System.nanoTime();
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(despeckledContours, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user