Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api] Adds sam2 model to onnxruntime model zoo #3492

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading