Search in sources :

Example 16 with Rectangle

use of ai.djl.modality.cv.output.Rectangle in project djl by deepjavalibrary.

the class CocoDetection method getLabels.

private PairList<Long, Rectangle> getLabels(CocoUtils coco, long imageId) {
    List<Long> annotationIds = coco.getAnnotationIdByImageId(imageId);
    if (annotationIds == null) {
        return new PairList<>();
    }
    PairList<Long, Rectangle> label = new PairList<>(annotationIds.size());
    for (long annotationId : annotationIds) {
        CocoMetadata.Annotation annotation = coco.getAnnotationById(annotationId);
        if (annotation.getArea() > 0) {
            double[] box = annotation.getBoundingBox();
            long labelClass = coco.mapCategoryId(annotation.getCategoryId());
            Rectangle objectLocation = new Rectangle(new Point(box[0], box[1]), box[2], box[3]);
            label.add(labelClass, objectLocation);
        }
    }
    return label;
}
Also used : Rectangle(ai.djl.modality.cv.output.Rectangle) PairList(ai.djl.util.PairList) Point(ai.djl.modality.cv.output.Point)

Example 17 with Rectangle

use of ai.djl.modality.cv.output.Rectangle in project djl by deepjavalibrary.

the class ObjectDetectionDataset method get.

/**
 * {@inheritDoc}
 */
@Override
public Record get(NDManager manager, long index) throws IOException {
    NDList data = new NDList(getRecordImage(manager, index));
    PairList<Long, Rectangle> objects = getObjects(index);
    float[][] labelsSplit = new float[objects.size()][5];
    for (int i = 0; i < objects.size(); i++) {
        Pair<Long, Rectangle> obj = objects.get(i);
        labelsSplit[i][0] = obj.getKey();
        Rectangle location = obj.getValue();
        labelsSplit[i][1] = (float) location.getX();
        labelsSplit[i][2] = (float) location.getY();
        labelsSplit[i][3] = (float) location.getWidth();
        labelsSplit[i][4] = (float) location.getHeight();
    }
    NDList labels = new NDList(manager.create(labelsSplit));
    return new Record(data, labels);
}
Also used : NDList(ai.djl.ndarray.NDList) Rectangle(ai.djl.modality.cv.output.Rectangle) Record(ai.djl.training.dataset.Record)

Example 18 with Rectangle

use of ai.djl.modality.cv.output.Rectangle in project djl by deepjavalibrary.

the class OpenCVImage method drawBoundingBoxes.

/**
 * {@inheritDoc}
 */
@Override
public void drawBoundingBoxes(DetectedObjects detections) {
    int imageWidth = image.width();
    int imageHeight = image.height();
    List<DetectedObjects.DetectedObject> list = detections.items();
    for (DetectedObjects.DetectedObject result : list) {
        String className = result.getClassName();
        BoundingBox box = result.getBoundingBox();
        Rectangle rectangle = box.getBounds();
        int x = (int) (rectangle.getX() * imageWidth);
        int y = (int) (rectangle.getY() * imageHeight);
        Rect rect = new Rect(x, y, (int) (rectangle.getWidth() * imageWidth), (int) (rectangle.getHeight() * imageHeight));
        Scalar color = new Scalar(RandomUtils.nextInt(178), RandomUtils.nextInt(178), RandomUtils.nextInt(178));
        Imgproc.rectangle(image, rect.tl(), rect.br(), color, 2);
        Size size = Imgproc.getTextSize(className, Imgproc.FONT_HERSHEY_PLAIN, 1.3, 1, null);
        Point br = new Point(x + size.width + 4, y + size.height + 4);
        Imgproc.rectangle(image, rect.tl(), br, color, -1);
        Point point = new Point(x, y + size.height + 2);
        color = new Scalar(255, 255, 255);
        Imgproc.putText(image, className, point, Imgproc.FONT_HERSHEY_PLAIN, 1.3, color, 1);
        // If we have a mask instead of a plain rectangle, draw tha mask
        if (box instanceof Mask) {
            Mask mask = (Mask) box;
            BufferedImage img = mat2Image(image);
            drawMask(img, mask);
            image = image2Mat(img);
        } else if (box instanceof Landmark) {
            drawLandmarks(box);
        }
    }
}
Also used : Rect(org.opencv.core.Rect) Size(org.opencv.core.Size) Landmark(ai.djl.modality.cv.output.Landmark) Rectangle(ai.djl.modality.cv.output.Rectangle) Mask(ai.djl.modality.cv.output.Mask) DetectedObjects(ai.djl.modality.cv.output.DetectedObjects) Point(org.opencv.core.Point) Point(org.opencv.core.Point) BufferedImage(java.awt.image.BufferedImage) Scalar(org.opencv.core.Scalar) BoundingBox(ai.djl.modality.cv.output.BoundingBox)

Example 19 with Rectangle

use of ai.djl.modality.cv.output.Rectangle in project djl by deepjavalibrary.

the class TfSsdTranslator method processOutput.

/**
 * {@inheritDoc}
 */
@Override
public DetectedObjects processOutput(TranslatorContext ctx, NDList list) {
    int len = (int) list.get(0).getShape().get(0);
    for (NDArray array : list) {
        if (numDetectionsOutputName.equals(array.getName())) {
            len = array.toArray()[0].intValue();
            break;
        }
    }
    float[] scores = new float[len];
    long[] classIds = new long[len];
    NDArray boundingBoxes = list.get(0);
    for (NDArray array : list) {
        if (scoresOutputName.equals(array.getName())) {
            scores = array.toFloatArray();
        } else if (boundingBoxOutputName.equals(array.getName())) {
            boundingBoxes = array;
        } else if (classLabelOutputName.equals(array.getName())) {
            classIds = Arrays.stream(array.toArray()).mapToLong(Number::longValue).toArray();
        }
    }
    List<String> retNames = new ArrayList<>();
    List<Double> retProbs = new ArrayList<>();
    List<BoundingBox> retBB = new ArrayList<>();
    // results are already sorted according to scores
    for (int i = 0; i < Math.min(classIds.length, maxBoxes); ++i) {
        long classId = classIds[i];
        double score = scores[i];
        // classId starts from 0, -1 means background
        if (classId >= 0 && score > threshold) {
            if (classId >= classes.size()) {
                throw new AssertionError("Unexpected index: " + classId);
            }
            String className = classes.get((int) classId - 1);
            float[] box = boundingBoxes.get(i).toFloatArray();
            float yMin = box[0];
            float xMin = box[1];
            float yMax = box[2];
            float xMax = box[3];
            double w = xMax - xMin;
            double h = yMax - yMin;
            Rectangle rect = new Rectangle(xMin, yMin, w, h);
            retNames.add(className);
            retProbs.add(score);
            retBB.add(rect);
        }
    }
    return new DetectedObjects(retNames, retProbs, retBB);
}
Also used : ArrayList(java.util.ArrayList) Rectangle(ai.djl.modality.cv.output.Rectangle) DetectedObjects(ai.djl.modality.cv.output.DetectedObjects) BoundingBox(ai.djl.modality.cv.output.BoundingBox) NDArray(ai.djl.ndarray.NDArray)

Example 20 with Rectangle

use of ai.djl.modality.cv.output.Rectangle in project djl-demo by deepjavalibrary.

the class FaceDetectionActivity method detectSelectedImage.

private String detectSelectedImage(Context mContext, ImageView imageView) throws IOException, TranslateException {
    String msg = "";
    msg = msg + "Image size = " + selectedImage.getWidth() + "x" + selectedImage.getHeight() + "\n";
    long startTime = System.currentTimeMillis();
    String imgPath = ImageUtil.getRealPathFromUri(mContext, selectedImageUri);
    Path facePath = Paths.get(imgPath);
    Image img = BitmapImageFactory.getInstance().fromFile(facePath);
    DetectedObjects detection = predictor.predict(img);
    msg = msg + "Face detected = " + detection.getNumberOfObjects() + "\n";
    msg = msg + "Time: " + (System.currentTimeMillis() - startTime) + " ms";
    Canvas canvas = new Canvas(selectedImage);
    Paint paint = new Paint();
    paint.setColor(Color.GREEN);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(5);
    List<DetectedObjects.DetectedObject> list = detection.items();
    for (DetectedObjects.DetectedObject face : list) {
        BoundingBox box = face.getBoundingBox();
        Rectangle rectangle = box.getBounds();
        int left = (int) (rectangle.getX() * (double) img.getWidth());
        int top = (int) (rectangle.getY() * (double) img.getHeight());
        int right = left + (int) (rectangle.getWidth() * (double) img.getWidth());
        int bottom = top + (int) (rectangle.getHeight() * (double) img.getHeight());
        canvas.drawRect(left, top, right, bottom, paint);
    }
    imageView.post(() -> imageView.setImageBitmap(selectedImage));
    return msg;
}
Also used : Path(java.nio.file.Path) Canvas(android.graphics.Canvas) Rectangle(ai.djl.modality.cv.output.Rectangle) FaceDetectedObjects(ai.djl.examples.detection.domain.FaceDetectedObjects) DetectedObjects(ai.djl.modality.cv.output.DetectedObjects) Paint(android.graphics.Paint) Image(ai.djl.modality.cv.Image) SuppressLint(android.annotation.SuppressLint) Paint(android.graphics.Paint) BoundingBox(ai.djl.modality.cv.output.BoundingBox)

Aggregations

Rectangle (ai.djl.modality.cv.output.Rectangle)21 DetectedObjects (ai.djl.modality.cv.output.DetectedObjects)12 BoundingBox (ai.djl.modality.cv.output.BoundingBox)11 ArrayList (java.util.ArrayList)11 NDArray (ai.djl.ndarray.NDArray)7 Point (ai.djl.modality.cv.output.Point)5 List (java.util.List)5 Image (ai.djl.modality.cv.Image)4 NDList (ai.djl.ndarray.NDList)4 Path (java.nio.file.Path)4 FaceDetectedObjects (ai.djl.examples.detection.domain.FaceDetectedObjects)3 Artifact (ai.djl.repository.Artifact)3 PairList (ai.djl.util.PairList)3 Landmark (ai.djl.modality.cv.output.Landmark)2 ProgressBar (ai.djl.training.util.ProgressBar)2 SuppressLint (android.annotation.SuppressLint)2 Canvas (android.graphics.Canvas)2 Paint (android.graphics.Paint)2 Reader (java.io.Reader)2 Type (java.lang.reflect.Type)2