Skip to content

Commit

Permalink
Should save default choice better now
Browse files Browse the repository at this point in the history
  • Loading branch information
levyishai committed Feb 6, 2025
1 parent 088da24 commit d95e50a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,17 @@ public RknnModel getModel() {
* @param model The model to create the detector from.
* @param inputSize The required image dimensions for the model. Images will be {@link
* Letterbox}ed to this shape.
* @param useAllCores Whether the model should use all possible NPU cores, or use an automatic assignment.
*/
public RknnObjectDetector(RknnModel model, Size inputSize) {
public RknnObjectDetector(RknnModel model, Size inputSize, boolean useAllCores) {
this.model = model;
this.inputSize = inputSize;

// Create the detector
isUsingAllCores = useAllCores;
objPointer =
RknnJNI.create(
model.modelFile.getPath(), model.labels.size(), model.version.ordinal(), 210);
model.modelFile.getPath(), model.labels.size(), model.version.ordinal(), determineCoreNum(useAllCores));
if (objPointer <= 0) {
throw new RuntimeException(
"Failed to create detector from path " + model.modelFile.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.photonvision.vision.objects;

public interface Model {
public ObjectDetector load();
public ObjectDetector load(boolean useAllCores);

public String getName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static NullModel getInstance() {
}

@Override
public ObjectDetector load() {
public ObjectDetector load(boolean useAllCores) {
return this;
}

Expand Down Expand Up @@ -66,5 +66,7 @@ public List<NeuralNetworkPipeResult> detect(Mat in, double nmsThresh, double box
}

@Override
public void setUseAllCores(boolean useAllCores) {}
public void setUseAllCores(boolean useAllCores) {
// Do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public String getName() {
return modelFile.getName();
}

public ObjectDetector load() {
return new RknnObjectDetector(this, inputSize);
public ObjectDetector load(boolean useAllCores) {
return new RknnObjectDetector(this, inputSize, useAllCores);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

package org.photonvision.vision.pipe.impl;

import java.util.List;
import java.util.Optional;
import org.opencv.core.Mat;
import org.photonvision.common.configuration.NeuralNetworkModelManager;
import org.photonvision.vision.objects.Model;
Expand All @@ -28,15 +26,18 @@
import org.photonvision.vision.opencv.Releasable;
import org.photonvision.vision.pipe.CVPipe;

import java.util.List;
import java.util.Optional;

public class ObjectDetectionPipe
extends CVPipe<
CVMat, List<NeuralNetworkPipeResult>, ObjectDetectionPipe.ObjectDetectionPipeParams>
CVMat, List<NeuralNetworkPipeResult>, ObjectDetectionPipe.ObjectDetectionPipeParams>
implements Releasable {
private ObjectDetector detector;

public ObjectDetectionPipe() {
public ObjectDetectionPipe(boolean useAllCores) {
Optional<Model> defaultModel = NeuralNetworkModelManager.getInstance().getDefaultModel();
detector = defaultModel.map(Model::load).orElse(NullModel.getInstance());
detector = defaultModel.map((model) -> model.load(useAllCores)).orElse(NullModel.getInstance());
}

public void setUseAllCores(boolean useAllCores) {
Expand All @@ -48,9 +49,11 @@ protected List<NeuralNetworkPipeResult> process(CVMat in) {
// Check if the model has changed
if (detector.getModel() != params.model) {
detector.release();
detector = params.model.load();
detector = params.model.load(params.useAllCores);
}

detector.setUseAllCores(params.useAllCores);

Mat frame = in.getMat();
if (frame.empty()) {
return List.of();
Expand All @@ -64,8 +67,10 @@ public static class ObjectDetectionPipeParams {
public double nms;
public int max_detections;
public Model model;
public boolean useAllCores;

public ObjectDetectionPipeParams() {}
public ObjectDetectionPipeParams() {
}
}

public List<String> getClassNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

package org.photonvision.vision.pipeline;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.photonvision.common.configuration.NeuralNetworkModelManager;
import org.photonvision.vision.frame.Frame;
import org.photonvision.vision.frame.FrameThresholdType;
Expand All @@ -34,26 +31,28 @@
import org.photonvision.vision.target.TargetOrientation;
import org.photonvision.vision.target.TrackedTarget;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ObjectDetectionPipeline
extends CVPipeline<CVPipelineResult, ObjectDetectionPipelineSettings> {
private final CalculateFPSPipe calculateFPSPipe = new CalculateFPSPipe();
private final ObjectDetectionPipe objectDetectorPipe = new ObjectDetectionPipe();
private final SortContoursPipe sortContoursPipe = new SortContoursPipe();
private final Collect2dTargetsPipe collect2dTargetsPipe = new Collect2dTargetsPipe();
private final FilterObjectDetectionsPipe filterContoursPipe = new FilterObjectDetectionsPipe();
private final ObjectDetectionPipe objectDetectorPipe;

private static final FrameThresholdType PROCESSING_TYPE = FrameThresholdType.NONE;

public ObjectDetectionPipeline() {
super(PROCESSING_TYPE);
settings = new ObjectDetectionPipelineSettings();
objectDetectorPipe.setUseAllCores(settings.useAllCores);
this(new ObjectDetectionPipelineSettings());
}

public ObjectDetectionPipeline(ObjectDetectionPipelineSettings settings) {
super(PROCESSING_TYPE);
this.settings = settings;
objectDetectorPipe.setUseAllCores(settings.useAllCores);
objectDetectorPipe = new ObjectDetectionPipe(settings.useAllCores);
}

@Override
Expand All @@ -76,8 +75,9 @@ protected void setPipeParamsImpl() {

params.model = selectedModel.get();

params.useAllCores = settings.useAllCores;

objectDetectorPipe.setParams(params);
objectDetectorPipe.setUseAllCores(settings.useAllCores);

DualOffsetValues dualOffsetValues =
new DualOffsetValues(
Expand Down

0 comments on commit d95e50a

Please sign in to comment.