Ask Your Question
0

Java DNN Help

asked 2019-12-29 02:53:36 -0600

aditya_mangalampalli gravatar image

updated 2019-12-29 05:40:08 -0600

supra56 gravatar image

Hello all,

I was recently working on a project that used the OpenCV DNN module in order to correctly localize objects. However I have a Python version which was working very well as can be seen here:

import cv2 as cv
from collections import defaultdict
cvNet = cv.dnn.readNetFromTensorflow('/Users/Tinku/Desktop/RoboticsShit/Skystone-Vision/Final/image_tensor/PB_file/model.pb', '/Users/Tinku/Desktop/RoboticsShit/Skystone-Vision/Final/image_tensor/Pbtxt/model.pbtxt')

cap = cv.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, img = cap.read()
    #img = cv.imread('/Users/Tinku/Desktop/TestStuff/pls.jpeg')
    rows = img.shape[0]
    cols = img.shape[1]
    cvNet.setInput(cv.dnn.blobFromImage(img, size=(300, 300), swapRB=True, crop=False))
    cvOut = cvNet.forward()
    numOfSkystone = 0
    objects_dict = {}
    StoneOrder = []
    for detection in cvOut[0,0,:,:]:
        score = float(detection[2])
        if score > 0.85:
            objectClass = detection[1]
            left = detection[3] * cols
            top = detection[4] * rows
            right = detection[5] * cols
            bottom = detection[6] * rows
            objects_dict[score]=[left,top,right,bottom, objectClass]


    l=list(objects_dict.keys())
    l.sort(reverse=True)
    for i in l[:6] :
        #print(i,objects_dict[i])
        if objects_dict[i][4] == 3.0:
            detectedObject = "Skystone"
            numOfSkystone +=1
        elif objects_dict[i][4] == 4.0:
            detectedObject = "Stone"
        elif objects_dict[i][4] == 2.0:
            detectedObject = "Red Foundation"
        elif objects_dict[i][4] == 1.0:
            detectedObject = "Blue Foundation"
        cv.rectangle(img, (int(objects_dict[i][0]), int(objects_dict[i][1])), (int(objects_dict[i][2]), int(objects_dict[i][3])), (23, 230, 210), thickness=5)
        cv.putText(img, detectedObject, (int(objects_dict[i][0]), int(objects_dict[i][1])), cv.FONT_HERSHEY_SIMPLEX, 2, (0,0,0), 7, cv.LINE_AA)
        #print(i, objects_dict)
        StoneOrder.append(detectedObject)
        if numOfSkystone==2:

            #print(StoneOrder)
            SkystonePosition = [i for i, value in enumerate(StoneOrder) if value == "Skystone"]
            print('The Skystone is located in positions: ' + str(int(SkystonePosition[0]) + 1) + ' and ' + str(int(SkystonePosition[1]) + 1))

    cv.imshow('img', img)
    cv.waitKey(1)
    cv.destroyAllWindows()

However, I now need to make a Java version but I have no clue where to get started in order to translate the above code. Any pointers or help would be greatly appreciated on how I were to tackle this task.

Thanks so much in advance!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2019-12-29 05:54:59 -0600

berak gravatar image

updated 2019-12-29 06:38:44 -0600

first note, that above code will only work with SSD style detection models (yolo or RCNN will be a bit more complicated)

you could take the face detection as an example:

public class FaceDetect {
    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img = Imgcodecs.imread("../img/face.jpg"); // your data here !
        Net net = Dnn.readNetFromTensorflow("c:/data/mdl/opencv_face_detector_uint8.pb",
                                            "c:/data/mdl/opencv_face_detector.pbtxt");
        // you want a downscaled Size of your input img, 
        // but keep the aspect ratio. 128x96 is 1/5 of a vga img
        Mat inputBlob = Dnn.blobFromImage(img, 1.0f, new Size(128,96), 
                                          new Scalar(104, 177, 123, 0), false, false);
        net.setInput(inputBlob);
        Mat res = net.forward("detection_out");
        // the net's output is a list of [n#,id,conf,t,l,b,r], one row per face found
        Mat faces = res.reshape(1, res.size(2));
        System.out.println("faces" + faces);
        float [] data = new float[7];
        for (int i=0; i<faces.rows(); i++)
        {
            faces.get(i, 0, data);
            int classID = data[1]
            float confidence = data[2];
            if (confidence > 0.4f)
            {
                int left   = (int)(data[3] * img.cols());
                int top    = (int)(data[4] * img.rows());
                int right  = (int)(data[5] * img.cols());
                int bottom = (int)(data[6] * img.rows());
                System.out.println("("+left + "," + top + ")("+right+","+bottom+") " 
                                    + confidence);
                Imgproc.rectangle(img, new Point(left,top), new Point(right,bottom),
                                   new Scalar(0,200,0), 3);
            }
        }
        Imgcodecs.imwrite("facedet.png", img);
    }
}

more here

android tutorial

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2019-12-29 02:53:36 -0600

Seen: 336 times

Last updated: Dec 29 '19