Skip to content

Commit

Permalink
Properly handle empty frames from cscore (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 authored Mar 22, 2024
1 parent 15da06b commit 2d8b1ec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
package org.photonvision.vision.frame.provider;

import edu.wpi.first.cscore.CvSink;
import org.photonvision.common.util.math.MathUtils;
import org.photonvision.common.logging.LogGroup;
import org.photonvision.common.logging.Logger;
import org.photonvision.vision.opencv.CVMat;
import org.photonvision.vision.processes.VisionSourceSettables;

public class USBFrameProvider extends CpuImageProcessor {
private static final Logger logger = new Logger(USBFrameProvider.class, LogGroup.Camera);

private final CvSink cvSink;

@SuppressWarnings("SpellCheckingInspection")
Expand All @@ -43,9 +46,9 @@ public CapturedFrame getInputMat() {
cvSink.grabFrame(mat.getMat())
* 1000; // Units are microseconds, epoch is the same as the Unix epoch

// Sometimes CSCore gives us a zero frametime.
if (time <= 1e-6) {
time = MathUtils.wpiNanoTime();
if (time == 0) {
var error = cvSink.getError();
logger.error("Error grabbing image: " + error);
}

return new CapturedFrame(mat, settables.getFrameStaticProperties(), time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ private void update() {
// Grab the new camera frame
var frame = frameSupplier.get();

// Frame empty -- no point in trying to do anything more?
if (frame.processedImage.getMat().empty() && frame.colorImage.getMat().empty()) {
// give up without increasing loop count
continue;
}

// There's no guarantee the processing type change will occur this tick, so pipelines should
// check themselves
try {
Expand Down

0 comments on commit 2d8b1ec

Please sign in to comment.