Skip to content

Commit

Permalink
feat: added confidence threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
trancee committed Sep 9, 2023
1 parent dedc5b1 commit cc4d9fa
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/selfie-segmentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ Only available on Android and iOS.

#### ProcessImageOptions

| Prop | Type | Description | Since |
| ------------ | ------------------- | --------------------------------- | ----- |
| **`path`** | <code>string</code> | The local path to the image file. | 5.2.0 |
| **`width`** | <code>number</code> | Scale the image to this width. | 5.2.0 |
| **`height`** | <code>number</code> | Scale the image to this height. | 5.2.0 |
| Prop | Type | Description | Default | Since |
| ---------------- | ------------------- | --------------------------------- | ---------------- | ----- |
| **`path`** | <code>string</code> | The local path to the image file. | | 5.2.0 |
| **`width`** | <code>number</code> | Scale the image to this width. | | 5.2.0 |
| **`height`** | <code>number</code> | Scale the image to this height. | | 5.2.0 |
| **`confidence`** | <code>number</code> | Sets the confidence threshold. | <code>0.9</code> | 5.2.0 |

</docgen-api>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public InputImage createInputImageFromFilePath(@NonNull String path) {

public void processImage(ProcessImageOptions options, ProcessImageResultCallback callback) {
InputImage inputImage = options.getInputImage();
Float threshold = options.getConfidence();

SelfieSegmenterOptions.Builder builder = new SelfieSegmenterOptions.Builder();
builder.setDetectorMode(SelfieSegmenterOptions.SINGLE_IMAGE_MODE);
Expand Down Expand Up @@ -75,7 +76,7 @@ public void processImage(ProcessImageOptions options, ProcessImageResultCallback
for (int i = 0; i < pixels.capacity() >> 2; i++) {
float confidence = mask.getFloat();

if (confidence >= 0.9f) {
if (confidence >= threshold) {
// byte alpha = pixels.get((i << 2) + ALPHA);
byte red = pixels.get((i << 2) + RED);
byte green = pixels.get((i << 2) + GREEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public void processImage(PluginCall call) {
Integer width = call.getInt("width", null);
Integer height = call.getInt("height", null);

Float confidence = call.getFloat("confidence", 0.9f);

InputImage image = implementation.createInputImageFromFilePath(path);
if (image == null) {
call.reject(ERROR_LOAD_IMAGE_FAILED);
return;
}
ProcessImageOptions options = new ProcessImageOptions(image, width, height);
ProcessImageOptions options = new ProcessImageOptions(image, width, height, confidence);

implementation.processImage(
options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
public class ProcessImageOptions {

private InputImage inputImage;
private Float confidence;

public ProcessImageOptions(InputImage inputImage, Integer width, Integer height) {
public ProcessImageOptions(InputImage inputImage, Integer width, Integer height, Float confidence) {
this.inputImage = scaledImage(inputImage, width, height);

this.confidence = confidence;
}

public InputImage getInputImage() {
return inputImage;
}
public Float getConfidence() { return confidence; };

private InputImage scaledImage(InputImage inputImage, Integer width, Integer height) {
float scaleX = (width != null) ? width * 1f / inputImage.getWidth() : 0f;
Expand Down
8 changes: 8 additions & 0 deletions packages/selfie-segmentation/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export interface ProcessImageOptions {
* @since 5.2.0
*/
height?: number;

/**
* Sets the confidence threshold.
*
* @since 5.2.0
* @default 0.9
*/
confidence?: number;
}

/**
Expand Down

0 comments on commit cc4d9fa

Please sign in to comment.