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;
}
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);
}
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);
}
}
}
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);
}
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;
}
Aggregations