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 fddbf99
Show file tree
Hide file tree
Showing 4 changed files with 91 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,69 @@
/*
* Copyright (C) Photon Vision.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

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 ArucoNanoDetectorJNI extends PhotonJNICommon {
private boolean isLoaded;
private static ArucoNanoDetectorJNI instance = null;

private ArucoNanoDetectorJNI() {
isLoaded = false;
}

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

return instance;
}

public static synchronized void forceLoad() throws IOException {
forceLoad(getInstance(), ArucoNanoDetectorJNI.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.ArucoNanoDetectorJNI;
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 = ArucoNanoDetectorJNI.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.ArucoNanoDetectorJNI;
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 {
ArucoNanoDetectorJNI.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 fddbf99

Please sign in to comment.