Create FPS limited streamers (#97)

This commit is contained in:
Matt
2020-08-20 16:42:39 -07:00
committed by GitHub
parent 072075508c
commit fea72e18bf
3 changed files with 28 additions and 35 deletions

View File

@@ -25,10 +25,9 @@ import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgproc.Imgproc;
import org.photonvision.vision.frame.Frame;
import org.photonvision.vision.frame.FrameConsumer;
import org.photonvision.vision.frame.FrameDivisor;
public class MJPGFrameConsumer implements FrameConsumer {
public class MJPGFrameConsumer {
private final CvSource cvSource;
private final MjpegServer mjpegServer;
@@ -48,7 +47,6 @@ public class MJPGFrameConsumer implements FrameConsumer {
this.divisor = divisor;
}
@Override
public void accept(Frame frame) {
if (frame != null && !frame.image.getMat().empty()) {
if (divisor != FrameDivisor.NONE) {

View File

@@ -36,6 +36,21 @@ public class Draw2dTargetsPipe
@Override
protected Void process(Triple<Mat, List<TrackedTarget>, Integer> in) {
// Always draw FPS
var imageSize = Math.sqrt(in.getLeft().rows() * in.getLeft().cols());
var fps = in.getRight();
var textSize = params.kPixelsToText * imageSize;
var thickness = params.kPixelsToThickness * imageSize;
Imgproc.putText(
in.getLeft(),
fps.toString(),
new Point(10, 10 + textSize * 25),
0,
textSize,
ColorHelper.colorToScalar(params.textColor),
(int) thickness);
if (!params.shouldDraw) return null;
if (!in.getMiddle().isEmpty()
@@ -44,9 +59,6 @@ public class Draw2dTargetsPipe
|| params.showRotatedBox
|| params.showShape)) {
var fps = in.getRight();
var imageSize = Math.sqrt(in.getLeft().rows() * in.getLeft().cols());
var centroidColour = ColorHelper.colorToScalar(params.centroidColor);
var maximumBoxColour = ColorHelper.colorToScalar(params.maximumBoxColor);
var rotatedBoxColour = ColorHelper.colorToScalar(params.rotatedBoxColor);
@@ -102,8 +114,6 @@ public class Draw2dTargetsPipe
}
if (params.showContourNumber) {
var textSize = params.kPixelsToText * imageSize;
var thickness = params.kPixelsToThickness * imageSize;
var center = target.m_mainContour.getCenterPoint();
var textPos =
new Point(
@@ -144,18 +154,6 @@ public class Draw2dTargetsPipe
centroidColour,
(int) Math.ceil(imageSize * params.kPixelsToBoxThickness));
}
// Draw FPS
var textSize = params.kPixelsToText * imageSize;
var thickness = params.kPixelsToThickness * imageSize;
Imgproc.putText(
in.getLeft(),
fps.toString(),
new Point(10, 10 + textSize * 25),
0,
textSize,
ColorHelper.colorToScalar(params.textColor),
(int) thickness);
}
}

View File

@@ -37,8 +37,6 @@ import org.photonvision.vision.calibration.CameraCalibrationCoefficients;
import org.photonvision.vision.camera.CameraQuirk;
import org.photonvision.vision.camera.QuirkyCamera;
import org.photonvision.vision.camera.USBCameraSource;
import org.photonvision.vision.frame.Frame;
import org.photonvision.vision.frame.FrameConsumer;
import org.photonvision.vision.frame.consumer.MJPGFrameConsumer;
import org.photonvision.vision.pipeline.UICalibrationData;
import org.photonvision.vision.pipeline.result.CVPipelineResult;
@@ -58,7 +56,7 @@ public class VisionModule {
protected final VisionSource visionSource;
private final VisionRunner visionRunner;
private final LinkedList<CVPipelineResultConsumer> resultConsumers = new LinkedList<>();
private final LinkedList<FrameConsumer> frameConsumers = new LinkedList<>();
private final LinkedList<CVPipelineResultConsumer> fpsLimitedResultConsumers = new LinkedList<>();
private final NTDataPublisher ntConsumer;
private final UIDataPublisher uiDataConsumer;
protected final int moduleIndex;
@@ -99,8 +97,8 @@ public class VisionModule {
dashboardInputStreamer =
new MJPGFrameConsumer(visionSource.getSettables().getConfiguration().uniqueName + "-input");
addResultConsumer(result -> dashboardInputStreamer.accept(result.inputFrame));
addResultConsumer(result -> dashboardOutputStreamer.accept(result.outputFrame));
fpsLimitedResultConsumers.add(result -> dashboardInputStreamer.accept(result.inputFrame));
fpsLimitedResultConsumers.add(result -> dashboardOutputStreamer.accept(result.outputFrame));
ntConsumer =
new NTDataPublisher(
@@ -250,15 +248,16 @@ public class VisionModule {
ntConsumer.updateCameraNickname(newName);
// rename streams
frameConsumers.remove(dashboardOutputStreamer);
frameConsumers.remove(dashboardInputStreamer);
fpsLimitedResultConsumers.clear();
dashboardOutputStreamer =
new MJPGFrameConsumer(
visionSource.getSettables().getConfiguration().uniqueName + "-output");
dashboardInputStreamer =
new MJPGFrameConsumer(visionSource.getSettables().getConfiguration().uniqueName + "-input");
frameConsumers.add(dashboardOutputStreamer);
frameConsumers.add(dashboardInputStreamer);
fpsLimitedResultConsumers.add(result -> dashboardInputStreamer.accept(result.inputFrame));
fpsLimitedResultConsumers.add(result -> dashboardOutputStreamer.accept(result.outputFrame));
}
public PhotonConfiguration.UICameraConfiguration toUICameraConfig() {
@@ -327,9 +326,7 @@ public class VisionModule {
private void consumeResult(CVPipelineResult result) {
consumePipelineResult(result);
var frame = result.outputFrame;
consumeFrame(frame);
consumeFpsLimitedResult(result);
result.release();
}
@@ -340,10 +337,10 @@ public class VisionModule {
}
}
private void consumeFrame(Frame frame) {
private void consumeFpsLimitedResult(CVPipelineResult result) {
if (System.currentTimeMillis() - lastFrameConsumeMillis > 1000 / StreamFPSCap) {
for (var frameConsumer : frameConsumers) {
frameConsumer.accept(frame);
for (var c : fpsLimitedResultConsumers) {
c.accept(result);
}
lastFrameConsumeMillis = System.currentTimeMillis();
}