Skip to content

Commit b6cf6db

Browse files
authored
Fix: Early return when no detections in SpatialLocationCalculator (#1824)
* Fix early return * Move segmask passthrough to SpatialLocationCalculator instead * Bump FW * Bump FW
1 parent a5a25bd commit b6cf6db

4 files changed

Lines changed: 56 additions & 12 deletions

File tree

cmake/Depthai/DepthaiDeviceRVC4Config.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
set(DEPTHAI_DEVICE_RVC4_MATURITY "snapshot")
44

55
# "version if applicable"
6-
set(DEPTHAI_DEVICE_RVC4_VERSION "0.0.1+ffaaa351f971c291311f8ab5d8d0753f327799b3")
6+
set(DEPTHAI_DEVICE_RVC4_VERSION "0.0.1+44b5f9a40174dac9670e4f6f411e9b4daf6e6f76")

src/pipeline/node/SpatialLocationCalculator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ void SpatialLocationCalculator::run() {
100100
auto stop = high_resolution_clock::now();
101101
auto timeToComputeSpatialDetections = duration_cast<microseconds>(stop - start);
102102
logger->trace("Time to compute spatial detections: {} us", timeToComputeSpatialDetections.count());
103+
104+
if(calculationConfig->segmentationPassthrough && !imgDetections->getData().empty()) {
105+
outputSpatialImgDetections->data = imgDetections->data;
106+
outputSpatialImgDetections->segmentationMaskWidth = imgDetections->getSegmentationMaskWidth();
107+
outputSpatialImgDetections->segmentationMaskHeight = imgDetections->getSegmentationMaskHeight();
108+
}
109+
103110
outputSpatialImgDetections->setSequenceNum(imgDetections->getSequenceNum());
104111
outputSpatialImgDetections->setTimestampDevice(imgDetections->getTimestampDevice());
105112
outputSpatialImgDetections->setTimestamp(imgDetections->getTimestamp());

src/pipeline/utilities/SpatialLocationCalculator/SpatialUtils.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,6 @@ dai::SpatialImgDetection computeSpatialDetection(const dai::ImgFrame& depthFrame
350350
if(!areAligned) centerPoint = depthTransformation->remapPointTo(detectionsTransformation, centerPoint);
351351
dai::Point3f spatialCoordinates =
352352
calculateSpatialCoordinates(depthStats.calculateDepth(calculationAlgorithm), detectionsTransformation.getIntrinsicMatrix(), centerPoint);
353-
logger->trace("Calculated spatial coordinates: {} {} {}", spatialCoordinates.x, spatialCoordinates.y, spatialCoordinates.z);
354353

355354
dai::SpatialImgDetection spatialDetection = createSpatialDetection(detection, spatialCoordinates);
356355

@@ -418,9 +417,6 @@ void computeSpatialDetections(const dai::ImgFrame& depthFrame,
418417
}
419418

420419
std::vector<dai::ImgDetection> imgDetectionsVector = imgDetections.detections;
421-
if(imgDetectionsVector.size() == 0) {
422-
return;
423-
}
424420
spatialDetections.detections.resize(imgDetectionsVector.size());
425421

426422
const SpatialLocationCalculatorAlgorithm calculationAlgorithm = config.globalCalculationAlgorithm;
@@ -432,7 +428,11 @@ void computeSpatialDetections(const dai::ImgFrame& depthFrame,
432428
const std::size_t segmentationMaskHeight = imgDetections.getSegmentationMaskHeight();
433429

434430
span<const std::uint8_t> maskSpan = imgDetections.getData();
435-
const bool passthroughSegmentation = config.segmentationPassthrough && !maskSpan.empty();
431+
432+
if(imgDetectionsVector.size() == 0) {
433+
return;
434+
}
435+
436436
if(!config.useSegmentation) { // ignore segmentation mask even if provided
437437
maskSpan = span<const std::uint8_t>{};
438438
}
@@ -458,12 +458,6 @@ void computeSpatialDetections(const dai::ImgFrame& depthFrame,
458458
logger);
459459
spatialDetections.detections[i] = spatialImgDetection;
460460
}
461-
462-
if(passthroughSegmentation) {
463-
spatialDetections.data = imgDetections.data;
464-
spatialDetections.segmentationMaskWidth = imgDetections.getSegmentationMaskWidth();
465-
spatialDetections.segmentationMaskHeight = imgDetections.getSegmentationMaskHeight();
466-
}
467461
}
468462

469463
} // namespace SpatialUtils

tests/src/ondevice_tests/pipeline/node/spatial_location_calculator_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,49 @@ TEST_CASE("Segmentation passthrough can be toggled") {
604604
}
605605
}
606606

607+
TEST_CASE("Segmentation passthrough preserves mask when detections are empty") {
608+
constexpr unsigned width = 320;
609+
constexpr unsigned height = 240;
610+
const std::array<std::array<float, 3>, 3> intrinsics = {{
611+
{{5.0F, 0.0F, 1.0F}},
612+
{{0.0F, 5.0F, 1.0F}},
613+
{{0.0F, 0.0F, 1.0F}},
614+
}};
615+
616+
cv::Mat depthMat(height, width, CV_16UC1, cv::Scalar(2000));
617+
auto depthFrame = createDepthFrame(depthMat, intrinsics);
618+
619+
std::vector<std::uint8_t> mask(width * height, 255);
620+
for(int y = 20; y < 60; ++y) {
621+
for(int x = 30; x < 90; ++x) {
622+
mask.at(y * static_cast<int>(width) + x) = 0;
623+
}
624+
}
625+
626+
dai::SpatialLocationCalculatorConfig initialConfig;
627+
initialConfig.setCalculationAlgorithm(dai::SpatialLocationCalculatorAlgorithm::AVERAGE);
628+
initialConfig.setDepthThresholds(0, 10000);
629+
initialConfig.setUseSegmentation(true);
630+
initialConfig.setSegmentationPassthrough(true);
631+
initialConfig.setCalculateSpatialKeypoints(false);
632+
633+
auto detectionMsg = std::make_shared<dai::ImgDetections>();
634+
detectionMsg->setSegmentationMask(mask, width, height);
635+
detectionMsg->transformation = depthFrame->transformation;
636+
detectionMsg->setTimestamp(depthFrame->getTimestamp());
637+
detectionMsg->setSequenceNum(depthFrame->getSequenceNum());
638+
auto [unusedLegacy, unusedPassthrough, spatialDetections] = processDepthFrame(initialConfig, depthFrame, std::nullopt, detectionMsg);
639+
static_cast<void>(unusedLegacy);
640+
static_cast<void>(unusedPassthrough);
641+
642+
CHECK(spatialDetections.detections.empty());
643+
const auto outMask = spatialDetections.getMaskData();
644+
REQUIRE(outMask.has_value());
645+
CHECK(*outMask == mask);
646+
CHECK(spatialDetections.getSegmentationMaskWidth() == width);
647+
CHECK(spatialDetections.getSegmentationMaskHeight() == height);
648+
}
649+
607650
TEST_CASE("Spatial detections remap depth to detection transformations") {
608651
using std::chrono::steady_clock;
609652
const auto RESET_REMOTE_TIMEOUT_MS = std::chrono::milliseconds(2000);

0 commit comments

Comments
 (0)