mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
Modify pipes to pass around same Mat, fix Streamer leak
This commit is contained in:
@@ -246,6 +246,7 @@ public class VisionProcess {
|
||||
Mat camFrame = camData.getLeft();
|
||||
if (camFrame.cols() > 0 && camFrame.rows() > 0) {
|
||||
CVPipelineResult result = pipelineManager.getCurrentPipeline().runPipeline(camFrame);
|
||||
camFrame.release();
|
||||
|
||||
if (result != null) {
|
||||
result.setTimestamp(camData.getRight());
|
||||
@@ -258,7 +259,6 @@ public class VisionProcess {
|
||||
try {
|
||||
streamFrameQueue.clear();
|
||||
streamFrameQueue.add(lastPipelineResult.outputMat);
|
||||
camFrame.release();
|
||||
} catch (Exception e) {
|
||||
Debug.printInfo("Vision running faster than stream.");
|
||||
}
|
||||
@@ -295,7 +295,12 @@ public class VisionProcess {
|
||||
protected void process() {
|
||||
if (!streamFrameQueue.isEmpty()) {
|
||||
try {
|
||||
streamer.runStream(streamFrameQueue.take());
|
||||
Mat tempMat = new Mat();
|
||||
Mat tempMat2 = streamFrameQueue.take();
|
||||
tempMat2.copyTo(tempMat);
|
||||
tempMat2.release();
|
||||
streamer.runStream(tempMat);
|
||||
tempMat.release();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ public class USBCameraCapture implements CameraCapture {
|
||||
public Pair<Mat, Long> getFrame() {
|
||||
Long deltaTime;
|
||||
// TODO: Why multiply by 1000 here?
|
||||
deltaTime = cvSink.grabFrame(imageBuffer) * 1000L;
|
||||
Mat tempMat = new Mat();
|
||||
deltaTime = cvSink.grabFrame(tempMat) * 1000L;
|
||||
tempMat.copyTo(imageBuffer);
|
||||
tempMat.release();
|
||||
return Pair.of(imageBuffer, deltaTime);
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +79,7 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
outputMatPipe = new OutputMatPipe(settings.isBinary);
|
||||
}
|
||||
|
||||
private final MemoryManager memManager = new MemoryManager(120);
|
||||
private final MemoryManager memManager = new MemoryManager(120, 20000);
|
||||
|
||||
@Override
|
||||
public CVPipeline2dResult runPipeline(Mat inputMat) {
|
||||
@@ -125,10 +125,10 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
Pair<Mat, Long> rotateFlipResult = rotateFlipPipe.run(inputMat);
|
||||
totalPipelineTimeNanos += rotateFlipResult.getRight();
|
||||
|
||||
Pair<Mat, Long> blurResult = blurPipe.run(rotateFlipResult.getLeft());
|
||||
totalPipelineTimeNanos += blurResult.getRight();
|
||||
// Pair<Mat, Long> blurResult = blurPipe.run(rotateFlipResult.getLeft());
|
||||
// totalPipelineTimeNanos += blurResult.getRight();
|
||||
|
||||
Pair<Mat, Long> erodeDilateResult = erodeDilatePipe.run(blurResult.getLeft());
|
||||
Pair<Mat, Long> erodeDilateResult = erodeDilatePipe.run(rotateFlipResult.getLeft());
|
||||
totalPipelineTimeNanos += erodeDilateResult.getRight();
|
||||
|
||||
Pair<Mat, Long> hsvResult = hsvPipe.run(erodeDilateResult.getLeft());
|
||||
@@ -163,7 +163,7 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
if (Main.testMode) {
|
||||
pipelineTimeString += String.format("PipeInit: %.2fms, ", pipeInitTimeNanos / 1000000.0);
|
||||
pipelineTimeString += String.format("RotateFlip: %.2fms, ", rotateFlipResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("Blur: %.2fms, ", blurResult.getRight() / 1000000.0);
|
||||
// pipelineTimeString += String.format("Blur: %.2fms, ", blurResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("ErodeDilate: %.2fms, ", erodeDilateResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("HSV: %.2fms, ", hsvResult.getRight() / 1000000.0);
|
||||
pipelineTimeString += String.format("FindContours: %.2fms, ", findContoursResult.getRight() / 1000000.0);
|
||||
@@ -184,7 +184,7 @@ public class CVPipeline2d extends CVPipeline<CVPipeline2dResult, CVPipeline2dSet
|
||||
System.out.printf("full pipeline run time was %.3fms (%.2fFPS)\n", truePipelineTimeMillis, truePipelineFPS);
|
||||
}
|
||||
|
||||
memManager.run(Main.testMode);
|
||||
memManager.run();
|
||||
|
||||
return new CVPipeline2dResult(collect2dTargetsResult.getLeft(), draw2dContoursResult.getLeft(), totalPipelineTimeNanos);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class DriverVisionPipeline extends CVPipeline<DriverPipelineResult, CVPip
|
||||
private Draw2dContoursPipe.Draw2dContoursSettings draw2dContoursSettings = new Draw2dContoursPipe.Draw2dContoursSettings();
|
||||
private final List<RotatedRect> blankList = List.of();
|
||||
|
||||
private final MemoryManager memoryManager = new MemoryManager(50);
|
||||
private final MemoryManager memoryManager = new MemoryManager(200, 20000);
|
||||
|
||||
public DriverVisionPipeline(CVPipelineSettings settings) {
|
||||
super(settings);
|
||||
@@ -46,7 +46,7 @@ public class DriverVisionPipeline extends CVPipeline<DriverPipelineResult, CVPip
|
||||
Pair<Mat, Long> rotateFlipResult = rotateFlipPipe.run(inputMat);
|
||||
Pair<Mat, Long> draw2dContoursResult = draw2dContoursPipe.run(Pair.of(rotateFlipResult.getLeft(), blankList));
|
||||
|
||||
memoryManager.run(Main.testMode);
|
||||
memoryManager.run();
|
||||
|
||||
return new DriverPipelineResult(null, draw2dContoursResult.getLeft(), 0);
|
||||
}
|
||||
|
||||
@@ -25,20 +25,20 @@ public class BlurPipe implements Pipe<Mat, Mat> {
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (blurSize > 0) {
|
||||
input.copyTo(processBuffer);
|
||||
try {
|
||||
Imgproc.blur(processBuffer, processBuffer, new Size(blurSize, blurSize));
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
} catch (CvException e) {
|
||||
System.err.println("(BlurPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
} else {
|
||||
input.copyTo(outputMat);
|
||||
}
|
||||
// if (blurSize > 0) {
|
||||
// input.copyTo(processBuffer);
|
||||
// try {
|
||||
// Imgproc.blur(processBuffer, processBuffer, new Size(blurSize, blurSize));
|
||||
// processBuffer.copyTo(outputMat);
|
||||
// processBuffer.release();
|
||||
// } catch (CvException e) {
|
||||
// System.err.println("(BlurPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
// }
|
||||
// } else {
|
||||
// input.copyTo(outputMat);
|
||||
// }
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
return Pair.of(input, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
|
||||
private Point[] vertices = new Point[4];
|
||||
private List<MatOfPoint> drawnContours = new ArrayList<>();
|
||||
private MatOfPoint contour = new MatOfPoint();
|
||||
@SuppressWarnings("FieldCanBeLocal")
|
||||
private Point xMax = new Point(), xMin = new Point(), yMax = new Point(), yMin = new Point();
|
||||
|
||||
@@ -41,7 +42,7 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
|
||||
if (settings.showCrosshair || settings.showCentroid || settings.showMaximumBox || settings.showRotatedBox) {
|
||||
// input.getLeft().copyTo(processBuffer);
|
||||
processBuffer = input.getLeft();
|
||||
// processBuffer = input.getLeft();
|
||||
|
||||
if (input.getRight().size() > 0) {
|
||||
for (int i = 0; i < input.getRight().size(); i++) {
|
||||
@@ -55,21 +56,24 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
drawnContours.clear();
|
||||
|
||||
r.points(vertices);
|
||||
MatOfPoint contour = new MatOfPoint(vertices);
|
||||
contour.fromArray(vertices);
|
||||
// MatOfPoint contour = new MatOfPoint(vertices);
|
||||
drawnContours.add(contour);
|
||||
|
||||
if (settings.showCentroid) {
|
||||
Imgproc.circle(processBuffer, r.center, 3, Helpers.colorToScalar(settings.centroidColor));
|
||||
Imgproc.circle(input.getLeft(), r.center, 3, Helpers.colorToScalar(settings.centroidColor));
|
||||
}
|
||||
|
||||
if (settings.showRotatedBox) {
|
||||
Imgproc.drawContours(processBuffer, drawnContours, 0, Helpers.colorToScalar(settings.rotatedBoxColor), settings.boxOutlineSize);
|
||||
Imgproc.drawContours(input.getLeft(), drawnContours, 0, Helpers.colorToScalar(settings.rotatedBoxColor), settings.boxOutlineSize);
|
||||
}
|
||||
|
||||
if (settings.showMaximumBox) {
|
||||
Rect box = Imgproc.boundingRect(contour);
|
||||
Imgproc.rectangle(processBuffer, new Point(box.x, box.y), new Point((box.x + box.width), (box.y + box.height)), Helpers.colorToScalar(settings.maximumBoxColor), settings.boxOutlineSize);
|
||||
Imgproc.rectangle(input.getLeft(), new Point(box.x, box.y), new Point((box.x + box.width), (box.y + box.height)), Helpers.colorToScalar(settings.maximumBoxColor), settings.boxOutlineSize);
|
||||
}
|
||||
|
||||
// contour.release();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,8 +82,8 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
xMin.set(new double[] {camProps.centerX - 10, camProps.centerY});
|
||||
yMax.set(new double[] {camProps.centerX, camProps.centerY + 10});
|
||||
yMin.set(new double[] {camProps.centerX, camProps.centerY - 10});
|
||||
Imgproc.line(processBuffer, xMax, xMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
Imgproc.line(processBuffer, yMax, yMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
Imgproc.line(input.getLeft(), xMax, xMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
Imgproc.line(input.getLeft(), yMax, yMin, Helpers.colorToScalar(settings.crosshairColor), 2);
|
||||
}
|
||||
|
||||
// processBuffer.copyTo(outputMat);
|
||||
@@ -89,7 +93,7 @@ public class Draw2dContoursPipe implements Pipe<Pair<Mat, List<RotatedRect>>, Ma
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(processBuffer, processTime);
|
||||
return Pair.of(input.getLeft(), processTime);
|
||||
}
|
||||
|
||||
public static class Draw2dContoursSettings {
|
||||
|
||||
@@ -31,23 +31,23 @@ public class ErodeDilatePipe implements Pipe<Mat, Mat> {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
if (erode || dilate) {
|
||||
input.copyTo(processBuffer);
|
||||
// input.copyTo(processBuffer);
|
||||
|
||||
if (erode) {
|
||||
Imgproc.erode(processBuffer, processBuffer, kernel);
|
||||
Imgproc.erode(input, input, kernel);
|
||||
}
|
||||
|
||||
if (dilate) {
|
||||
Imgproc.dilate(processBuffer, processBuffer, kernel);
|
||||
Imgproc.dilate(input, input, kernel);
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
// processBuffer.copyTo(outputMat);
|
||||
// processBuffer.release();
|
||||
} else {
|
||||
input.copyTo(outputMat);
|
||||
// input.copyTo(outputMat);
|
||||
}
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
return Pair.of(input, processTime);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,18 +29,15 @@ public class HsvPipe implements Pipe<Mat, Mat> {
|
||||
public Pair<Mat, Long> run(Mat input) {
|
||||
long processStartNanos = System.nanoTime();
|
||||
|
||||
input.copyTo(processBuffer);
|
||||
input.copyTo(outputMat);
|
||||
|
||||
try {
|
||||
Imgproc.cvtColor(processBuffer, processBuffer, Imgproc.COLOR_BGR2HSV, 3);
|
||||
Core.inRange(processBuffer, hsvLower, hsvUpper, processBuffer);
|
||||
Imgproc.cvtColor(outputMat, outputMat, Imgproc.COLOR_BGR2HSV, 3);
|
||||
Core.inRange(outputMat, hsvLower, hsvUpper, outputMat);
|
||||
} catch (CvException e) {
|
||||
System.err.println("(HsvPipe) Exception thrown by OpenCV: \n" + e.getMessage());
|
||||
}
|
||||
|
||||
processBuffer.copyTo(outputMat);
|
||||
processBuffer.release();
|
||||
|
||||
long processTime = System.nanoTime() - processStartNanos;
|
||||
return Pair.of(outputMat, processTime);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user