Skip to content

Commit fddbf99

Browse files
committed
Hack in aruco nano
1 parent a3e1dda commit fddbf99

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

photon-core/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ dependencies {
5555
}
5656

5757
testImplementation group: 'org.junit-pioneer' , name: 'junit-pioneer', version: '2.2.0'
58+
59+
def arucoVer = "dev-v2024.0.1-15-g0d7210c"
60+
implementation "org.photonvision:photonaruconano-jni:$arucoVer:$jniPlatform"
61+
implementation "org.photonvision:photonaruconano-java:$arucoVer"
5862
}
5963

6064
task writeCurrentVersion {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) Photon Vision.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package org.photonvision.jni;
19+
20+
import java.io.IOException;
21+
import java.util.List;
22+
import java.util.stream.Collectors;
23+
import org.photonvision.ArucoNanoV5Detector;
24+
import org.photonvision.ArucoNanoV5Detector.DetectionResult;
25+
import org.photonvision.common.util.TestUtils;
26+
import org.photonvision.vision.aruco.ArucoDetectionResult;
27+
import org.photonvision.vision.opencv.CVMat;
28+
29+
public class ArucoNanoDetectorJNI extends PhotonJNICommon {
30+
private boolean isLoaded;
31+
private static ArucoNanoDetectorJNI instance = null;
32+
33+
private ArucoNanoDetectorJNI() {
34+
isLoaded = false;
35+
}
36+
37+
public static ArucoNanoDetectorJNI getInstance() {
38+
if (instance == null) instance = new ArucoNanoDetectorJNI();
39+
40+
return instance;
41+
}
42+
43+
public static synchronized void forceLoad() throws IOException {
44+
forceLoad(getInstance(), ArucoNanoDetectorJNI.class, List.of("photonmiscjnijni"));
45+
}
46+
47+
@Override
48+
public boolean isLoaded() {
49+
return isLoaded;
50+
}
51+
52+
@Override
53+
public void setLoaded(boolean state) {
54+
isLoaded = state;
55+
}
56+
57+
public static List<ArucoDetectionResult> detect(CVMat in) {
58+
DetectionResult[] ret = ArucoNanoV5Detector.detect(in.getMat().getNativeObjAddr(), 0);
59+
60+
return List.of(ret).stream()
61+
.map(it -> new ArucoDetectionResult(it.xCorners, it.yCorners, it.id))
62+
.collect(Collectors.toList());
63+
}
64+
65+
public static void main(String[] args) throws IOException {
66+
TestUtils.loadLibraries();
67+
forceLoad();
68+
}
69+
}

photon-core/src/main/java/org/photonvision/vision/pipe/impl/ArucoDetectionPipe.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.opencv.core.TermCriteria;
2727
import org.opencv.imgproc.Imgproc;
2828
import org.opencv.objdetect.Objdetect;
29+
import org.photonvision.jni.ArucoNanoDetectorJNI;
2930
import org.photonvision.vision.aruco.ArucoDetectionResult;
3031
import org.photonvision.vision.aruco.PhotonArucoDetector;
3132
import org.photonvision.vision.opencv.CVMat;
@@ -43,6 +44,8 @@ public class ArucoDetectionPipe
4344

4445
@Override
4546
protected List<ArucoDetectionResult> process(CVMat in) {
47+
if (in.getMat().empty()) return List.of();
48+
4649
var imgMat = in.getMat();
4750

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

54-
var detections = photonDetector.detect(imgMat);
55-
// manually do corner refinement ourselves
57+
// var detections = photonDetector.detect(imgMat);
58+
var detections = ArucoNanoDetectorJNI.detect(in);
59+
60+
// manually do corner refinement ourselves (todo do we have to with aruco-nano?)
5661
if (params.useCornerRefinement) {
5762
for (var detection : detections) {
5863
double[] xCorners = detection.getXCorners();
@@ -93,7 +98,9 @@ protected List<ArucoDetectionResult> process(CVMat in) {
9398
}
9499
}
95100
}
96-
return List.of(detections);
101+
102+
// return List.of(detections);
103+
return (detections);
97104
}
98105

99106
@Override

photon-server/src/main/java/org/photonvision/Main.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.photonvision.common.networking.NetworkManager;
3939
import org.photonvision.common.util.TestUtils;
4040
import org.photonvision.common.util.numbers.IntegerCouple;
41+
import org.photonvision.jni.ArucoNanoDetectorJNI;
4142
import org.photonvision.jni.RknnDetectorJNI;
4243
import org.photonvision.mrcal.MrCalJNILoader;
4344
import org.photonvision.raspi.LibCameraJNILoader;
@@ -343,6 +344,13 @@ public static void main(String[] args) {
343344
logger.error("Failed to load native libraries!", e);
344345
}
345346

347+
try {
348+
ArucoNanoDetectorJNI.forceLoad();
349+
logger.info("Native libraries loaded.");
350+
} catch (Exception e) {
351+
logger.error("Failed to load native libraries!", e);
352+
}
353+
346354
try {
347355
if (Platform.isRaspberryPi()) {
348356
LibCameraJNILoader.forceLoad();

0 commit comments

Comments
 (0)