Skip to content

Commit

Permalink
debugging jar
Browse files Browse the repository at this point in the history
  • Loading branch information
Alextopher committed Sep 11, 2024
1 parent be116a7 commit 1e25013
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<JarEntry> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -140,7 +139,8 @@ public Map<String, Object> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ public List<NeuralNetworkPipeResult> 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!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ObjectDetectionPipe
CVMat, List<NeuralNetworkPipeResult>, ObjectDetectionPipe.ObjectDetectionPipeParams>
implements Releasable {

private ObjectDetector detector;;
private ObjectDetector detector;

public ObjectDetectionPipe() {
Optional<Model> defaultModel = NeuralNetworkModelManager.getInstance().getDefaultModel();
Expand Down
4 changes: 2 additions & 2 deletions photon-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ run {
}

remotes {
pi {
host = 'photonvision.local'
home {
host = '192.168.1.220'
user = 'pi'
password = 'raspberry'
knownHosts = allowAnyHosts
Expand Down
16 changes: 15 additions & 1 deletion photon-server/src/main/resources/web/index.html
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
<p>UI has not been copied!</p>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="./favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Photon Client</title>
<script type="module" crossorigin src="./assets/index-50ab8a2c.js"></script>
<link rel="stylesheet" href="./assets/index-69a5f457.css">
</head>
<body>
<div id="app"></div>

</body>
</html>

0 comments on commit 1e25013

Please sign in to comment.