Java Code Examples for org.deeplearning4j.nn.layers.objdetect.DetectedObject#getBottomRightXY()

The following examples show how to use org.deeplearning4j.nn.layers.objdetect.DetectedObject#getBottomRightXY() . 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: YOLOApp.java    From java-ml-projects with Apache License 2.0 6 votes vote down vote up
private void drawBoxes(List<DetectedObject> predictedObjects) {
	ctx.setLineWidth(3);
	int w = yoloModel.getInputWidth();
	int h = yoloModel.getInputHeight();
	int gridW = yoloModel.getGridW();
	int gridH = yoloModel.getGridH();
	ctx.setTextAlign(TextAlignment.CENTER);
	for (DetectedObject obj : predictedObjects) {
		String cl = yoloModel.getModelClasses()[obj.getPredictedClass()];
		double[] xy1 = obj.getTopLeftXY();
		double[] xy2 = obj.getBottomRightXY();
		int x1 = (int) Math.round(w * xy1[0] / gridW);
		int y1 = (int) Math.round(h * xy1[1] / gridH);
		int x2 = (int) Math.round(w * xy2[0] / gridW);
		int y2 = (int) Math.round(h * xy2[1] / gridH);
		int rectW = x2 - x1;
		int rectH = y2 - y1;
		ctx.setStroke(colors.get(cl));
		ctx.strokeRect(x1, y1, rectW, rectH);
		ctx.strokeText(cl, x1 + (rectW / 2), y1 - 2);
		ctx.setFill(Color.WHITE);
		ctx.fillText(cl, x1 + (rectW / 2), y1 - 2);
	}
}
 
Example 2
Source File: Yolo.java    From Java-Machine-Learning-for-Computer-Vision with MIT License 5 votes vote down vote up
private void createBoundingBoxRectangle(Mat file, int w, int h, DetectedObject obj) {

        double[] xy1 = obj.getTopLeftXY();
        double[] xy2 = obj.getBottomRightXY();
        int predictedClass = obj.getPredictedClass();
        int x1 = (int) Math.round(w * xy1[0] / selectedSpeed.gridWidth);
        int y1 = (int) Math.round(h * xy1[1] / selectedSpeed.gridHeight);
        int x2 = (int) Math.round(w * xy2[0] / selectedSpeed.gridWidth);
        int y2 = (int) Math.round(h * xy2[1] / selectedSpeed.gridHeight);
        rectangle(file, new Point(x1, y1), new Point(x2, y2), Scalar.RED);
        putText(file, groupMap.get(map.get(predictedClass)), new Point(x1 + 2, y2 - 2), FONT_HERSHEY_DUPLEX, 1, Scalar.GREEN);
    }
 
Example 3
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");

    }