Java Code Examples for org.deeplearning4j.nn.layers.objdetect.YoloUtils#getPredictedObjects()

The following examples show how to use org.deeplearning4j.nn.layers.objdetect.YoloUtils#getPredictedObjects() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YOLOOutputAdapter.java    From konduit-serving with Apache License 2.0 5 votes vote down vote up
private DetectedObjectsBatch[] getPredictedObjects(INDArray[] outputs, double threshold, String[] outputNames, int originalHeight, int originalWidth) {
    // assuming "standard" output from TensorFlow using a "normal" YOLOv2 model
    //INDArray permuted = outputs[0].permute(0, 3, 1, 2);
    INDArray permuted = outputs[0];
    INDArray activated = YoloUtils.activate(boundingBoxPriors, permuted);

    List<DetectedObject> predictedObjects1 = YoloUtils.getPredictedObjects(boundingBoxPriors, activated, threshold, 0.4);


    DetectedObjectsBatch[] detectedObjects = new DetectedObjectsBatch[predictedObjects1.size()];

    int n = numLabels; // an arbitrary number of classes returned per object
    for (int i = 0; i < detectedObjects.length; i++) {
        DetectedObject detectedObject = predictedObjects1.get(i);
        long x = Math.round(originalWidth * predictedObjects1.get(i).getCenterX() / gridWidth);
        long y = Math.round(originalHeight * predictedObjects1.get(i).getCenterY() / gridHeight);
        long w = Math.round(originalWidth * predictedObjects1.get(i).getWidth() / gridWidth);
        long h = Math.round(originalHeight * predictedObjects1.get(i).getHeight() / gridHeight);

        detectedObjects[i] = new DetectedObjectsBatch();
        detectedObjects[i].setCenterX(x);
        detectedObjects[i].setCenterY(y);
        detectedObjects[i].setWidth(w);
        detectedObjects[i].setHeight(h);
        detectedObjects[i].setPredictedClasses(new String[]{labels.getLabel(detectedObject.getPredictedClass())});
        detectedObjects[i].setPredictedClassNumbers(new int[]{detectedObject.getPredictedClass()});
        detectedObjects[i].setConfidences(new float[]{detectedObject.getClassPredictions().getFloat(detectedObject.getPredictedClass())});

    }

    return detectedObjects;


}
 
Example 2
Source File: YOLO2_TF_Client.java    From SKIL_Examples with Apache License 2.0 4 votes vote down vote up
private void renderJavaFXStyle(GraphicsContext ctx) throws Exception {

        INDArray boundingBoxPriors = Nd4j.create(YOLO2.DEFAULT_PRIOR_BOXES);

        ctx.setLineWidth(3);
        ctx.setTextAlign(TextAlignment.LEFT);

        long start = System.nanoTime();
        for (int i = 0; i < 1; i++) {
            INDArray permuted = networkGlobalOutput.permute(0, 3, 1, 2);
            INDArray activated = YoloUtils.activate(boundingBoxPriors, permuted);
            List<DetectedObject> predictedObjects = YoloUtils.getPredictedObjects(boundingBoxPriors, activated, 0.6, 0.4);
            
            //System.out.println( "width: " + imageWidth );
            //System.out.println( "height: " + imageHeight );

            for (DetectedObject o : predictedObjects) {
                ClassPrediction classPrediction = labels.decodePredictions(o.getClassPredictions(), 1).get(0).get(0);
                String label = classPrediction.getLabel();
                long x = Math.round(imageWidth  * o.getCenterX() / gridWidth);
                long y = Math.round(imageHeight * o.getCenterY() / gridHeight);
                long w = Math.round(imageWidth  * o.getWidth()   / gridWidth);
                long h = Math.round(imageHeight * o.getHeight()  / gridHeight);

                System.out.println("\"" + label + "\" at [" + x + "," + y + ";" + w + "," + h + "], score = "
                                + o.getConfidence() * classPrediction.getProbability());

                double[] xy1 = o.getTopLeftXY();
                double[] xy2 = o.getBottomRightXY();
                int x1 = (int) Math.round(imageWidth  * xy1[0] / gridWidth);
                int y1 = (int) Math.round(imageHeight * xy1[1] / gridHeight);
                int x2 = (int) Math.round(imageWidth  * xy2[0] / gridWidth);
                int y2 = (int) Math.round(imageHeight * xy2[1] / gridHeight);

                int rectW = x2 - x1;
                int rectH = y2 - y1;
                //System.out.printf("%s - %d, %d, %d, %d \n", label, x1, x2, y1, y2);
                //System.out.println( "color: " + colors.get(label) );
                ctx.setStroke(colors.get(label));
                ctx.strokeRect(x1, y1, rectW, rectH);
                
                int labelWidth = label.length() * 10;
                int labelHeight = 14;
                
                ctx.setFill( colors.get(label) );
                ctx.strokeRect(x1, y1-labelHeight, labelWidth, labelHeight);
                ctx.fillRect(x1, y1 - labelHeight, labelWidth, labelHeight);
                ctx.setFill( Color.WHITE );
                ctx.fillText(label, x1 + 3, y1 - 3 );

            }

        }

        globalEndTime = System.nanoTime();
        long end = System.nanoTime();
        System.out.println("Rendering code took: " + (end - start) / 1000000 + " ms");

        System.out.println("Overall Program Time: " + (globalEndTime - globalStartTime) / 1000000 + " ms");

    }