Skip to content

Commit

Permalink
[api] Adds sam2 model to onnxruntime model zoo (#3492)
Browse files Browse the repository at this point in the history
* [api] alternative NDArray should not be closed in NDScope

* [api] Adds sam2 model to onnxruntime model zoo
  • Loading branch information
frankfliu authored Oct 4, 2024
1 parent e623210 commit 3059991
Show file tree
Hide file tree
Showing 13 changed files with 559 additions and 107 deletions.
12 changes: 12 additions & 0 deletions api/src/main/java/ai/djl/modality/cv/BufferedImageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,18 @@ public void drawBoundingBoxes(DetectedObjects detections, float opacity) {
g.dispose();
}

/** {@inheritDoc} */
@Override
public void drawRectangle(Rectangle rect, int rgb, int thickness) {
Graphics2D g = (Graphics2D) image.getGraphics();
g.setStroke(new BasicStroke(thickness));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setPaint(new Color(rgb));
int x = (int) rect.getX();
int y = (int) rect.getY();
g.drawRect(x, y, (int) rect.getWidth(), (int) rect.getHeight());
}

/** {@inheritDoc} */
@Override
public void drawMarks(List<Point> points, int radius) {
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/ai/djl/modality/cv/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.modality.cv.output.Joints;
import ai.djl.modality.cv.output.Point;
import ai.djl.modality.cv.output.Rectangle;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDManager;

Expand Down Expand Up @@ -137,6 +138,15 @@ default void drawBoundingBoxes(DetectedObjects detections) {
*/
void drawBoundingBoxes(DetectedObjects detections, float opacity);

/**
* Draws a rectangle on the image.
*
* @param rect the rectangle to draw
* @param rgb the color
* @param thickness the thickness
*/
void drawRectangle(Rectangle rect, int rgb, int thickness);

/**
* Draws a mark on the image.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.modality.cv.translator;

import ai.djl.modality.Input;
import ai.djl.modality.Output;
import ai.djl.modality.cv.output.DetectedObjects;
import ai.djl.modality.cv.translator.Sam2Translator.Sam2Input;
import ai.djl.ndarray.BytesSupplier;
import ai.djl.ndarray.NDList;
import ai.djl.translate.Batchifier;
import ai.djl.translate.TranslateException;
import ai.djl.translate.Translator;
import ai.djl.translate.TranslatorContext;

import java.io.IOException;

/** A {@link Translator} that can serve SAM2 model. */
public class Sam2ServingTranslator implements Translator<Input, Output> {

private Sam2Translator translator;

/**
* Constructs a new {@code Sam2ServingTranslator} instance.
*
* @param translator a {@code Sam2Translator}
*/
public Sam2ServingTranslator(Sam2Translator translator) {
this.translator = translator;
}

/** {@inheritDoc} */
@Override
public Batchifier getBatchifier() {
return translator.getBatchifier();
}

/** {@inheritDoc} */
@Override
public Output processOutput(TranslatorContext ctx, NDList list) throws Exception {
Output output = new Output();
output.addProperty("Content-Type", "application/json");
DetectedObjects obj = translator.processOutput(ctx, list);
output.add(BytesSupplier.wrapAsJson(obj));
return output;
}

/** {@inheritDoc} */
@Override
public NDList processInput(TranslatorContext ctx, Input input) throws Exception {
BytesSupplier data = input.getData();
try {
if (data == null) {
throw new TranslateException("Input data is empty.");
}
Sam2Input sam2 = Sam2Input.fromJson(data.getAsString());
return translator.processInput(ctx, sam2);
} catch (IOException e) {
throw new TranslateException("Input is not an Image data type", e);
}
}

/** {@inheritDoc} */
@Override
public void prepare(TranslatorContext ctx) throws Exception {
translator.prepare(ctx);
}
}
Loading

0 comments on commit 3059991

Please sign in to comment.