From 1e250139ee37572569f47f0455d4664e0230e546 Mon Sep 17 00:00:00 2001 From: Alextopher Date: Fri, 9 Aug 2024 16:13:08 -0400 Subject: [PATCH] debugging jar --- .../dashboard/CameraAndPipelineSelectCard.vue | 2 +- .../NeuralNetworkModelManager.java | 76 +++++++------------ .../configuration/PhotonConfiguration.java | 4 +- .../photonvision/jni/RknnObjectDetector.java | 3 +- .../vision/pipe/impl/ObjectDetectionPipe.java | 2 +- photon-server/build.gradle | 4 +- .../src/main/resources/web/index.html | 16 +++- 7 files changed, 51 insertions(+), 56 deletions(-) diff --git a/photon-client/src/components/dashboard/CameraAndPipelineSelectCard.vue b/photon-client/src/components/dashboard/CameraAndPipelineSelectCard.vue index 38505dee0f..15511460b2 100644 --- a/photon-client/src/components/dashboard/CameraAndPipelineSelectCard.vue +++ b/photon-client/src/components/dashboard/CameraAndPipelineSelectCard.vue @@ -132,7 +132,7 @@ const validNewPipelineTypes = computed(() => { { name: "AprilTag", value: WebsocketPipelineType.AprilTag }, { name: "Aruco", value: WebsocketPipelineType.Aruco } ]; - if (useSettingsStore().general.rknnSupported) { + if (useSettingsStore().general.supportedBackends.length > 0) { pipelineTypes.push({ name: "Object Detection", value: WebsocketPipelineType.ObjectDetection }); } return pipelineTypes; diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkModelManager.java b/photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkModelManager.java index c69464545a..08f6866c1c 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkModelManager.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkModelManager.java @@ -19,17 +19,20 @@ import java.io.File; import java.io.IOException; -import java.net.URL; +import java.io.InputStream; +import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Arrays; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import org.photonvision.common.hardware.Platform; import org.photonvision.common.logging.LogGroup; import org.photonvision.common.logging.Logger; @@ -226,6 +229,8 @@ private void loadModel(File model) { * @param modelsFolder The folder where the models are stored */ public void loadModels(File modelsFolder) { + logger.info("Supported backends: " + supportedBackends); + if (!modelsFolder.exists()) { logger.error("Models folder " + modelsFolder.getAbsolutePath() + " does not exist."); return; @@ -265,58 +270,35 @@ public void loadModels(File modelsFolder) { * @param modelsDirectory the directory on disk to save models */ public void extractModels(File modelsDirectory) { - if (!modelsDirectory.exists()) { - modelsDirectory.mkdirs(); + if (!modelsDirectory.exists() && !modelsDirectory.mkdirs()) { + throw new RuntimeException("Failed to create directory: " + modelsDirectory); } - String resourcePath = "models"; - try { - URL resourceURL = NeuralNetworkModelManager.class.getClassLoader().getResource(resourcePath); - if (resourceURL == null) { - logger.error("Failed to find jar resource at " + resourcePath); - return; - } + String resource = "models"; - Path resourcePathResolved = Paths.get(resourceURL.toURI()); - Files.walk(resourcePathResolved) - .forEach(sourcePath -> copyResource(sourcePath, resourcePathResolved, modelsDirectory)); - } catch (Exception e) { - logger.error("Failed to extract models from JAR", e); - } - } - - /** - * Copies a resource from the source path to the target path. - * - * @param sourcePath The path of the resource to be copied. - * @param resourcePathResolved The resolved path of the resource. - * @param modelsFolder The folder where the resource will be copied to. - */ - private void copyResource(Path sourcePath, Path resourcePathResolved, File modelsFolder) { - Path targetPath = - Paths.get( - modelsFolder.getAbsolutePath(), resourcePathResolved.relativize(sourcePath).toString()); try { - if (Files.isDirectory(sourcePath)) { - Files.createDirectories(targetPath); - } else { - Path parentDir = targetPath.getParent(); - if (parentDir != null && !Files.exists(parentDir)) { - Files.createDirectories(parentDir); - } - - if (!Files.exists(targetPath)) { - Files.copy(sourcePath, targetPath); - } else { - long sourceSize = Files.size(sourcePath); - long targetSize = Files.size(targetPath); - if (sourceSize != targetSize) { - Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + String jarPath = + getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); + try (JarFile jarFile = new JarFile(jarPath)) { + Enumeration entries = jarFile.entries(); + while (entries.hasMoreElements()) { + JarEntry entry = entries.nextElement(); + if (!entry.getName().startsWith(resource + "/") || entry.isDirectory()) { + continue; + } + Path outputPath = + modelsDirectory.toPath().resolve(entry.getName().substring(resource.length() + 1)); + if (Files.exists(outputPath)) { + continue; + } + Files.createDirectories(outputPath.getParent()); + try (InputStream inputStream = jarFile.getInputStream(entry)) { + Files.copy(inputStream, outputPath, StandardCopyOption.REPLACE_EXISTING); } } } - } catch (IOException e) { - logger.error("Failed to copy " + sourcePath + " to " + targetPath, e); + } catch (IOException | URISyntaxException e) { + logger.error("Error extracting models", e); } } } diff --git a/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java b/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java index 7ea3d62d36..614c00135c 100644 --- a/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java +++ b/photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java @@ -28,7 +28,6 @@ import org.photonvision.common.networking.NetworkManager; import org.photonvision.common.networking.NetworkUtils; import org.photonvision.common.util.SerializationUtils; -import org.photonvision.jni.RknnDetectorJNI; import org.photonvision.mrcal.MrCalJNILoader; import org.photonvision.raspi.LibCameraJNILoader; import org.photonvision.vision.calibration.UICameraCalibrationCoefficients; @@ -140,7 +139,8 @@ public Map toHashMap() { : ""); // TODO add support for other types of GPU accel generalSubmap.put("mrCalWorking", MrCalJNILoader.getInstance().isLoaded()); generalSubmap.put("availableModels", NeuralNetworkModelManager.getInstance().getModels()); - generalSubmap.put("supportedBackends", NeuralNetworkModelManager.getInstance().getSupportedBackends()); + generalSubmap.put( + "supportedBackends", NeuralNetworkModelManager.getInstance().getSupportedBackends()); generalSubmap.put("hardwareModel", hardwareConfig.deviceName); generalSubmap.put("hardwarePlatform", Platform.getPlatformName()); settingsSubmap.put("general", generalSubmap); diff --git a/photon-core/src/main/java/org/photonvision/jni/RknnObjectDetector.java b/photon-core/src/main/java/org/photonvision/jni/RknnObjectDetector.java index 1ef3acbd8c..b11699074a 100644 --- a/photon-core/src/main/java/org/photonvision/jni/RknnObjectDetector.java +++ b/photon-core/src/main/java/org/photonvision/jni/RknnObjectDetector.java @@ -93,8 +93,7 @@ public List detect(Mat in, double nmsThresh, double box // Resize the frame to the input size of the model Mat letterboxed = new Mat(); Letterbox scale = - Letterbox.letterbox( - in, letterboxed, this.inputSize, ColorHelper.colorToScalar(Color.GRAY)); + Letterbox.letterbox(in, letterboxed, this.inputSize, ColorHelper.colorToScalar(Color.GRAY)); if (!letterboxed.size().equals(this.inputSize)) { throw new RuntimeException("Letterboxed frame is not the right size!"); } diff --git a/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ObjectDetectionPipe.java b/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ObjectDetectionPipe.java index 00cfa191c8..f6ae7de602 100644 --- a/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ObjectDetectionPipe.java +++ b/photon-core/src/main/java/org/photonvision/vision/pipe/impl/ObjectDetectionPipe.java @@ -33,7 +33,7 @@ public class ObjectDetectionPipe CVMat, List, ObjectDetectionPipe.ObjectDetectionPipeParams> implements Releasable { - private ObjectDetector detector;; + private ObjectDetector detector; public ObjectDetectionPipe() { Optional defaultModel = NeuralNetworkModelManager.getInstance().getDefaultModel(); diff --git a/photon-server/build.gradle b/photon-server/build.gradle index 7c8c2ba078..d84b1111bd 100644 --- a/photon-server/build.gradle +++ b/photon-server/build.gradle @@ -62,8 +62,8 @@ run { } remotes { - pi { - host = 'photonvision.local' + home { + host = '192.168.1.220' user = 'pi' password = 'raspberry' knownHosts = allowAnyHosts diff --git a/photon-server/src/main/resources/web/index.html b/photon-server/src/main/resources/web/index.html index 988f55e6a3..5c33c978fc 100644 --- a/photon-server/src/main/resources/web/index.html +++ b/photon-server/src/main/resources/web/index.html @@ -1 +1,15 @@ -

UI has not been copied!

+ + + + + + + Photon Client + + + + +
+ + +