Skip to content

Commit 373ab1a

Browse files
committed
backend is done?
1 parent 322532f commit 373ab1a

File tree

10 files changed

+166
-20
lines changed

10 files changed

+166
-20
lines changed

photon-core/src/main/java/org/photonvision/common/configuration/ConfigManager.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ public boolean saveUploadedAprilTagFieldLayout(Path uploadPath) {
294294
return m_provider.saveUploadedAprilTagFieldLayout(uploadPath);
295295
}
296296

297+
public boolean saveUploadedNeuralNetworkProperties(Path uploadPath) {
298+
return m_provider.saveUploadedNeuralNetworkProperties(uploadPath);
299+
}
300+
297301
public void requestSave() {
298302
logger.trace("Requesting save...");
299303
saveRequestTimestamp = System.currentTimeMillis();

photon-core/src/main/java/org/photonvision/common/configuration/ConfigProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,6 @@ public void clearConfig() {
4141
public abstract boolean saveUploadedNetworkConfig(Path uploadPath);
4242

4343
public abstract boolean saveUploadedAprilTagFieldLayout(Path uploadPath);
44+
45+
public abstract boolean saveUploadedNeuralNetworkProperties(Path uploadPath);
4446
}

photon-core/src/main/java/org/photonvision/common/configuration/LegacyConfigProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,12 @@ public void load() {
214214

215215
this.config =
216216
new PhotonConfiguration(
217-
hardwareConfig, hardwareSettings, networkConfig, atfl, cameraConfigurations);
217+
hardwareConfig,
218+
hardwareSettings,
219+
networkConfig,
220+
atfl,
221+
new NeuralNetworkProperties(),
222+
cameraConfigurations);
218223
}
219224

220225
@Override
@@ -484,4 +489,11 @@ private void saveAndWriteTask() {
484489
public void unloadCameraConfigs() {
485490
this.config.getCameraConfigurations().clear();
486491
}
492+
493+
@Override
494+
public boolean saveUploadedNeuralNetworkProperties(Path uploadPath) {
495+
// TODO I'm not implementing this cause nobody with the legacy config is gonna have one of these
496+
throw new UnsupportedOperationException(
497+
"Unimplemented method 'saveUploadedNeuralNetworkProperties'");
498+
}
487499
}

photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkModelManager.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Enumeration;
2929
import java.util.HashMap;
30+
import java.util.LinkedList;
3031
import java.util.List;
3132
import java.util.Map;
3233
import java.util.Optional;
@@ -36,6 +37,7 @@
3637
import org.photonvision.common.hardware.Platform;
3738
import org.photonvision.common.logging.LogGroup;
3839
import org.photonvision.common.logging.Logger;
40+
import org.photonvision.rknn.RknnJNI.ModelVersion;
3941
import org.photonvision.vision.objects.Model;
4042
import org.photonvision.vision.objects.RknnModel;
4143

@@ -55,6 +57,27 @@ public class NeuralNetworkModelManager {
5557
/** Singleton instance of the NeuralNetworkModelManager */
5658
private static NeuralNetworkModelManager INSTANCE;
5759

60+
/**
61+
* This function stores the properties of the shipped object detection models. It is stored as a
62+
* function so that it can be dynamic, to adjust for the models directory.
63+
*/
64+
private NeuralNetworkProperties getShippedProperties(File modelsDirectory) {
65+
NeuralNetworkProperties nnProps = new NeuralNetworkProperties();
66+
67+
nnProps.addModelProperties(
68+
nnProps
69+
.new RknnModelProperties(
70+
Path.of(modelsDirectory.getAbsolutePath(), "NAMEHERE.rknn"),
71+
"foo",
72+
new LinkedList<String>(),
73+
0,
74+
0,
75+
Family.RKNN,
76+
ModelVersion.YOLO_V8));
77+
78+
return nnProps;
79+
}
80+
5881
/**
5982
* Private constructor to prevent instantiation
6083
*
@@ -177,6 +200,12 @@ private void loadModel(RknnModelProperties properties) {
177200
models = new HashMap<>();
178201
}
179202

203+
if (properties == null) {
204+
logger.error(
205+
"Model properties are null, this could mean the models config was unable to be found in the database");
206+
return;
207+
}
208+
180209
if (!supportedBackends.contains(properties.family)) {
181210
logger.warn(
182211
"Model "
@@ -208,23 +237,33 @@ private void loadModel(RknnModelProperties properties) {
208237
/**
209238
* Discovers DNN models from the specified folder.
210239
*
240+
* <p>This makes the assumption that
241+
*
211242
* @param modelsDirectory The folder where the models are stored
212243
*/
213-
public void discoverModels(File modelsDirectory) {
244+
public void discoverModels() {
214245
logger.info("Supported backends: " + supportedBackends);
215246

247+
File modelsDirectory = ConfigManager.getInstance().getModelsDirectory();
248+
216249
if (!modelsDirectory.exists()) {
217250
logger.error("Models folder " + modelsDirectory.getAbsolutePath() + " does not exist.");
218251
return;
219252
}
220253

221254
models = new HashMap<>();
222255

223-
// TODO: figure out how to get the JSON to object for each model
256+
// TODO: Load neural network properties from json/create new one
224257
try {
225258
Files.walk(modelsDirectory.toPath())
226259
.filter(Files::isRegularFile)
227-
.forEach(path -> loadModel(path.toFile()));
260+
.forEach(
261+
path ->
262+
loadModel(
263+
ConfigManager.getInstance()
264+
.getConfig()
265+
.getNeuralNetworkProperties()
266+
.getModelProperties(path)));
228267
} catch (IOException e) {
229268
logger.error("Failed to discover models at " + modelsDirectory.getAbsolutePath(), e);
230269
}
@@ -253,7 +292,9 @@ public void discoverModels(File modelsDirectory) {
253292
*
254293
* @param modelsDirectory the directory on disk to save models
255294
*/
256-
public void extractModels(File modelsDirectory) {
295+
public void extractModels() {
296+
File modelsDirectory = ConfigManager.getInstance().getModelsDirectory();
297+
257298
if (!modelsDirectory.exists() && !modelsDirectory.mkdirs()) {
258299
throw new RuntimeException("Failed to create directory: " + modelsDirectory);
259300
}
@@ -290,5 +331,11 @@ public void extractModels(File modelsDirectory) {
290331
} catch (IOException | URISyntaxException e) {
291332
logger.error("Error extracting models", e);
292333
}
334+
335+
ConfigManager.getInstance()
336+
.getConfig()
337+
.setNeuralNetworkProperties(
338+
getShippedProperties(modelsDirectory)
339+
.add(ConfigManager.getInstance().getConfig().getNeuralNetworkProperties()));
293340
}
294341
}

photon-core/src/main/java/org/photonvision/common/configuration/NeuralNetworkProperties.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import com.fasterxml.jackson.annotation.JsonCreator;
2121
import com.fasterxml.jackson.annotation.JsonProperty;
22+
import java.nio.file.Path;
23+
import java.util.HashMap;
2224
import java.util.LinkedList;
2325
import org.photonvision.common.configuration.NeuralNetworkModelManager.Family;
2426
import org.photonvision.rknn.RknnJNI;
@@ -30,7 +32,7 @@ public class NeuralNetworkProperties {
3032
* expand this modelProperties object, or create separate objects for each family.
3133
*/
3234
public class RknnModelProperties {
33-
public String modelPath;
35+
public Path modelPath;
3436
public String nickname;
3537
public LinkedList<String> labels;
3638
public double resolutionWidth;
@@ -50,7 +52,7 @@ public class RknnModelProperties {
5052
* @param rknnVersion
5153
*/
5254
public RknnModelProperties(
53-
String modelPath,
55+
Path modelPath,
5456
String nickname,
5557
LinkedList<String> labels,
5658
double resolutionWidth,
@@ -67,16 +69,18 @@ public RknnModelProperties(
6769
}
6870
}
6971

70-
public LinkedList<RknnModelProperties> modelProperties = new LinkedList<RknnModelProperties>();
72+
protected HashMap<Path, RknnModelProperties> properties =
73+
new HashMap<Path, RknnModelProperties>();
7174

7275
public NeuralNetworkProperties() {}
7376

7477
@JsonCreator
7578
public NeuralNetworkProperties(
76-
@JsonProperty("modelPropertiesList") LinkedList<RknnModelProperties> modelPropertiesList) {}
79+
@JsonProperty("modelPropertiesList")
80+
HashMap<Path, RknnModelProperties> modelPropertiesList) {}
7781

7882
public NeuralNetworkProperties(NeuralNetworkProperties NNMProperties) {
79-
this(NNMProperties.modelProperties);
83+
this(NNMProperties.properties);
8084
}
8185

8286
@Override
@@ -85,8 +89,34 @@ public String toString() {
8589

8690
toReturn += "NeuralNetworkProperties [";
8791

88-
toReturn += modelProperties.toString() + "]";
92+
toReturn += properties.toString() + "]";
8993

9094
return toReturn;
9195
}
96+
97+
public void addModelProperties(RknnModelProperties modelProperties) {
98+
properties.put(modelProperties.modelPath, modelProperties);
99+
}
100+
101+
/**
102+
* Add two Neural Network Properties together.
103+
*
104+
* <p>Any keys in the object passed in will override those from the other object
105+
*
106+
* @param nnProps
107+
* @return itself, so it can be chained and for adding where you want to pass into a function
108+
*/
109+
public NeuralNetworkProperties add(NeuralNetworkProperties nnProps) {
110+
properties.putAll(nnProps.properties);
111+
112+
return this;
113+
}
114+
115+
public boolean removeModelProperties(Path modelPath) {
116+
return properties.remove(modelPath) != null;
117+
}
118+
119+
public RknnModelProperties getModelProperties(Path modelPath) {
120+
return properties.get(modelPath);
121+
}
92122
}

photon-core/src/main/java/org/photonvision/common/configuration/PhotonConfiguration.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,35 @@ public class PhotonConfiguration {
2828
private final HardwareSettings hardwareSettings;
2929
private NetworkConfig networkConfig;
3030
private AprilTagFieldLayout atfl;
31+
private NeuralNetworkProperties neuralNetworkProperties;
3132
private HashMap<String, CameraConfiguration> cameraConfigurations;
3233

3334
public PhotonConfiguration(
3435
HardwareConfig hardwareConfig,
3536
HardwareSettings hardwareSettings,
3637
NetworkConfig networkConfig,
37-
AprilTagFieldLayout atfl) {
38-
this(hardwareConfig, hardwareSettings, networkConfig, atfl, new HashMap<>());
38+
AprilTagFieldLayout atfl,
39+
NeuralNetworkProperties neuralNetworkProperties) {
40+
this(
41+
hardwareConfig,
42+
hardwareSettings,
43+
networkConfig,
44+
atfl,
45+
neuralNetworkProperties,
46+
new HashMap<>());
3947
}
4048

4149
public PhotonConfiguration(
4250
HardwareConfig hardwareConfig,
4351
HardwareSettings hardwareSettings,
4452
NetworkConfig networkConfig,
4553
AprilTagFieldLayout atfl,
54+
NeuralNetworkProperties neuralNetworkProperties,
4655
HashMap<String, CameraConfiguration> cameraConfigurations) {
4756
this.hardwareConfig = hardwareConfig;
4857
this.hardwareSettings = hardwareSettings;
4958
this.networkConfig = networkConfig;
59+
this.neuralNetworkProperties = neuralNetworkProperties;
5060
this.cameraConfigurations = cameraConfigurations;
5161
this.atfl = atfl;
5262
}
@@ -56,7 +66,8 @@ public PhotonConfiguration() {
5666
new HardwareConfig(),
5767
new HardwareSettings(),
5868
new NetworkConfig(),
59-
new AprilTagFieldLayout(List.of(), 0, 0));
69+
new AprilTagFieldLayout(List.of(), 0, 0),
70+
new NeuralNetworkProperties());
6071
}
6172

6273
public HardwareConfig getHardwareConfig() {
@@ -75,6 +86,10 @@ public AprilTagFieldLayout getApriltagFieldLayout() {
7586
return atfl;
7687
}
7788

89+
public NeuralNetworkProperties getNeuralNetworkProperties() {
90+
return neuralNetworkProperties;
91+
}
92+
7893
public void setApriltagFieldLayout(AprilTagFieldLayout atfl) {
7994
this.atfl = atfl;
8095
}
@@ -83,6 +98,10 @@ public void setNetworkConfig(NetworkConfig networkConfig) {
8398
this.networkConfig = networkConfig;
8499
}
85100

101+
public void setNeuralNetworkProperties(NeuralNetworkProperties neuralNetworkProperties) {
102+
this.neuralNetworkProperties = neuralNetworkProperties;
103+
}
104+
86105
public HashMap<String, CameraConfiguration> getCameraConfigurations() {
87106
return cameraConfigurations;
88107
}
@@ -121,6 +140,8 @@ public String toString() {
121140
+ networkConfig
122141
+ "\n atfl="
123142
+ atfl
143+
+ "\n neuralNetworkProperties="
144+
+ neuralNetworkProperties
124145
+ "\n cameraConfigurations="
125146
+ cameraConfigurations
126147
+ "\n]";

0 commit comments

Comments
 (0)