refactor OD model UID to path (#2486)

This commit is contained in:
Sam Freund
2026-06-22 23:15:57 -07:00
committed by GitHub
parent 0a07263f74
commit 0e2563111c
6 changed files with 23 additions and 14 deletions

View File

@@ -280,10 +280,10 @@ public class NeuralNetworkModelManager {
*
* <p>If this method returns `Optional.of(..)` then the model should be safe to load.
*
* @param modelUID the unique identifier of the model to retrieve
* @param modelPath the unique identifier of the model to retrieve
* @return an Optional containing the model if found, or an empty Optional if not found
*/
public Optional<Model> getModel(String modelUID) {
public Optional<Model> getModel(Path modelPath) {
if (models == null) {
return Optional.empty();
}
@@ -292,7 +292,7 @@ public class NeuralNetworkModelManager {
for (Family backend : supportedBackends) {
if (models.containsKey(backend)) {
Optional<Model> model =
models.get(backend).stream().filter(m -> m.getUID().equals(modelUID)).findFirst();
models.get(backend).stream().filter(m -> m.getPath().equals(modelPath)).findFirst();
if (model.isPresent()) {
return model;
}
@@ -406,7 +406,8 @@ public class NeuralNetworkModelManager {
// After loading all of the models, sort them by name to ensure a consistent
// ordering
models.forEach(
(backend, backendModels) -> backendModels.sort((a, b) -> a.getUID().compareTo(b.getUID())));
(backend, backendModels) ->
backendModels.sort((a, b) -> a.getPath().compareTo(b.getPath())));
// Log
StringBuilder sb = new StringBuilder();
@@ -414,7 +415,7 @@ public class NeuralNetworkModelManager {
models.forEach(
(backend, backendModels) -> {
sb.append(backend).append(" [");
backendModels.forEach(model -> sb.append(model.getUID()).append(", "));
backendModels.forEach(model -> sb.append(model.getPath()).append(", "));
sb.append("] ");
});
}

View File

@@ -17,13 +17,19 @@
package org.photonvision.vision.objects;
import java.nio.file.Path;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Family;
import org.photonvision.common.configuration.NeuralNetworkModelsSettings.ModelProperties;
public interface Model {
public ObjectDetector load();
public String getUID();
/**
* Gets the path to the model file. This is being used as a UID.
*
* @return the path to the model file (for use as a UID)
*/
public Path getPath();
public String getNickname();

View File

@@ -17,6 +17,7 @@
package org.photonvision.vision.objects;
import java.nio.file.Path;
import java.util.List;
import org.opencv.core.Mat;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Family;
@@ -43,8 +44,8 @@ public class NullModel implements Model, ObjectDetector {
}
@Override
public String getUID() {
return "NullModel";
public Path getPath() {
return Path.of("null");
}
@Override

View File

@@ -18,6 +18,7 @@
package org.photonvision.vision.objects;
import java.io.File;
import java.nio.file.Path;
import org.opencv.core.Size;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Family;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Version;
@@ -61,8 +62,8 @@ public class RknnModel implements Model {
}
/** Return the unique identifier for the model. In this case, it's the model's path. */
public String getUID() {
return properties.modelPath().toString();
public Path getPath() {
return properties.modelPath();
}
public String getNickname() {

View File

@@ -18,6 +18,7 @@
package org.photonvision.vision.objects;
import java.io.File;
import java.nio.file.Path;
import org.opencv.core.Size;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Family;
import org.photonvision.common.configuration.NeuralNetworkModelManager.Version;
@@ -59,8 +60,8 @@ public class RubikModel implements Model {
}
/** Return the unique identifier for the model. In this case, it's the model's path. */
public String getUID() {
return properties.modelPath().toString();
public Path getPath() {
return properties.modelPath();
}
public String getNickname() {

View File

@@ -57,8 +57,7 @@ public class ObjectDetectionPipeline
protected void setPipeParamsImpl() {
Optional<Model> selectedModel =
settings.model != null
? NeuralNetworkModelManager.getInstance()
.getModel(settings.model.modelPath().toString())
? NeuralNetworkModelManager.getInstance().getModel(settings.model.modelPath())
: Optional.empty();
// If the desired model couldn't be found, log an error and try to use the default model