Skip to content

Commit

Permalink
Hack in aruco nano
Browse files Browse the repository at this point in the history
  • Loading branch information
mcm001 committed Jan 22, 2024
1 parent a3e1dda commit 30dcd25
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions photon-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ dependencies {
}

testImplementation group: 'org.junit-pioneer' , name: 'junit-pioneer', version: '2.2.0'

def arucoVer = "dev-v2024.0.1-15-g0d7210c"
implementation "org.photonvision:photonaruconano-jni:$arucoVer:$jniPlatform"
implementation "org.photonvision:photonaruconano-java:$arucoVer"
}

task writeCurrentVersion {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.photonvision.jni;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.photonvision.ArucoNanoV5Detector;
import org.photonvision.ArucoNanoV5Detector.DetectionResult;
import org.photonvision.common.util.TestUtils;
import org.photonvision.vision.aruco.ArucoDetectionResult;
import org.photonvision.vision.opencv.CVMat;

public class ArucoNanoDetector extends PhotonJNICommon {

private boolean isLoaded;
private static ArucoNanoDetector instance = null;

private ArucoNanoDetector() {
isLoaded = false;
}

public static ArucoNanoDetector getInstance() {
if (instance == null) instance = new ArucoNanoDetector();

return instance;
}

public static synchronized void forceLoad() throws IOException {
forceLoad(getInstance(), ArucoNanoDetector.class, List.of("photonmiscjnijni"));
}

@Override
public boolean isLoaded() {
return isLoaded;
}

@Override
public void setLoaded(boolean state) {
isLoaded = state;
}

public static List<ArucoDetectionResult> detect(CVMat in) {
DetectionResult[] ret = ArucoNanoV5Detector.detect(in.getMat().getNativeObjAddr(), 0);

return List.of(ret).stream()
.map(it -> new ArucoDetectionResult(it.xCorners, it.yCorners, it.id))
.collect(Collectors.toList());
}

public static void main(String[] args) throws IOException {
TestUtils.loadLibraries();
forceLoad();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.opencv.core.TermCriteria;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.Objdetect;
import org.photonvision.jni.ArucoNanoDetector;
import org.photonvision.vision.aruco.ArucoDetectionResult;
import org.photonvision.vision.aruco.PhotonArucoDetector;
import org.photonvision.vision.opencv.CVMat;
Expand All @@ -43,6 +44,8 @@ public class ArucoDetectionPipe

@Override
protected List<ArucoDetectionResult> process(CVMat in) {
if (in.getMat().empty()) return List.of();

var imgMat = in.getMat();

// Sanity check -- image should not be empty
Expand All @@ -51,8 +54,10 @@ protected List<ArucoDetectionResult> process(CVMat in) {
return List.of();
}

var detections = photonDetector.detect(imgMat);
// manually do corner refinement ourselves
// var detections = photonDetector.detect(imgMat);
var detections = ArucoNanoDetector.detect(in);

// manually do corner refinement ourselves (todo do we have to with aruco-nano?)
if (params.useCornerRefinement) {
for (var detection : detections) {
double[] xCorners = detection.getXCorners();
Expand Down Expand Up @@ -93,7 +98,9 @@ protected List<ArucoDetectionResult> process(CVMat in) {
}
}
}
return List.of(detections);

// return List.of(detections);
return (detections);
}

@Override
Expand Down
8 changes: 8 additions & 0 deletions photon-server/src/main/java/org/photonvision/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.photonvision.common.networking.NetworkManager;
import org.photonvision.common.util.TestUtils;
import org.photonvision.common.util.numbers.IntegerCouple;
import org.photonvision.jni.ArucoNanoDetector;
import org.photonvision.jni.RknnDetectorJNI;
import org.photonvision.mrcal.MrCalJNILoader;
import org.photonvision.raspi.LibCameraJNILoader;
Expand Down Expand Up @@ -343,6 +344,13 @@ public static void main(String[] args) {
logger.error("Failed to load native libraries!", e);
}

try {
ArucoNanoDetector.forceLoad();
logger.info("Native libraries loaded.");
} catch (Exception e) {
logger.error("Failed to load native libraries!", e);
}

try {
if (Platform.isRaspberryPi()) {
LibCameraJNILoader.forceLoad();
Expand Down

0 comments on commit 30dcd25

Please sign in to comment.