Hello Samuel,
I understand, that we can get an example in Python, Is there an example for this in Java?. Could you please point me to that?. It would be great to know how to do operations in JAVA as shown below python code.
net = cv2.dnn.readNetFromTensorflow(weightsPath, configPath)
# construct a blob from the input image and then perform a forward
# pass of the Mask R-CNN, giving us (1) the bounding box coordinates
# of the objects in the image along with (2) the pixel-wise segmentation
# for each specific object
blob = cv2.dnn.blobFromImage(image, swapRB=True, crop=False)
Hello Samuel,
I found the API readNetFromTensorflow when I tried to use, it is throwing an exception as
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.opencv.dnn.Dnn.readNetFromTensorflow_1(Ljava/lang/String;)J
at org.opencv.dnn.Dnn.readNetFromTensorflow_1(Native Method)
at org.opencv.dnn.Dnn.readNetFromTensorflow(Dnn.java:654)
Can you help me how to correct it?
With Regards,
Suresh Jeyachandran
Hello Florain,
Thank you so much for introducing me to your work. I don’t know how to thank you.
I tried a small example but I could not make it to work. I have pasted the my code below.
import static org.bytedeco.opencv.global.opencv_imgcodecs.IMREAD_COLOR;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.bytedeco.opencv.opencv_core.Mat;
import ch.bildspur.vision.MaskRCNN;
import ch.bildspur.vision.result.ObjectDetectionResult;
import ch.bildspur.vision.result.ResultList;
public class MaskRCNNTest {
public static void main(String[] args) {
String configPath = "C:\\Users\\sures\\OpenCV\\mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
String modelPath = "C:\\Users\\sures\\OpenCV\\mask_rcnn_inception_v2_coco_2018_01_28.pb";
Path config = Paths.get(configPath);
Path model = Paths.get(modelPath);
MaskRCNN maskrcnn = new MaskRCNN(config, model);
maskrcnn.setup();
try {
Mat image = load(new File("data/group.jpg"), IMREAD_COLOR);
ResultList<ObjectDetectionResult> detections = maskrcnn.run(image);
if (detections == null) {
return;
}
float confidenceSum = 0;
for (ObjectDetectionResult detection : detections) {
System.out.println(detection.getClassName() + "\t[" + detection.getConfidence() + "]");
confidenceSum += detection.getConfidence();
}
System.out.println(
"found " + detections.size() + " objects. avg conf: " + (confidenceSum / detections.size()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Could you please let me know what correction I need to make?
With Regards,
Lariena
From: flo...@nexpose.ch <flo...@nexpose.ch>
Sent: mardi 21 avril 2020 09:25
To: larien...@gmail.com; jav...@googlegroups.com; Samuel Audet <samuel...@gmail.com>
Cc: Jan-Philipp Buck <coole....@gmail.com>
Subject: Re: [javacv] Instance segmentation with OpenCV
Loading the Tensorflow MaskRCNN files works, I just tested it here:
I used the following models:
Please be aware that for loading tensorflow models into opencv dnn, you have to create a new config file which is explained here:
For SSD and R-CNN networks, there are even some pre-built.
best regards
Florian
Hello Florian and Samuel,
I took inspiration looking what Florian have implemented and started to implement MaskRCNN but I am facing a problem hope you can help.
import static org.bytedeco.opencv.global.opencv_core.minMaxLoc;
import static org.bytedeco.opencv.global.opencv_dnn.blobFromImage;
import static org.bytedeco.opencv.global.opencv_dnn.readNetFromTensorflow;
import static org.bytedeco.opencv.global.opencv_imgcodecs.IMREAD_COLOR;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.bytedeco.javacpp.DoublePointer;
import org.bytedeco.javacpp.FloatPointer;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.MatVector;
import org.bytedeco.opencv.opencv_core.Point;
import org.bytedeco.opencv.opencv_core.Rect;
import org.bytedeco.opencv.opencv_core.RectVector;
import org.bytedeco.opencv.opencv_core.StringVector;
import org.bytedeco.opencv.opencv_dnn.Net;
import org.bytedeco.opencv.opencv_text.FloatVector;
import org.bytedeco.opencv.opencv_text.IntVector;
import ch.bildspur.vision.result.ObjectDetectionResult;
import ch.bildspur.vision.result.ResultList;
public class MaskRCNNTest {
public static void main(String[] args) throws IOException {
String configPath = "mask_rcnn_inception_v2_coco_2018_01_28.pbtxt";
String modelPath = "frozen_inference_graph.pb";
Path config = Paths.get(configPath);
Path model = Paths.get(modelPath);
Net net = readNetFromTensorflow(model.toAbsolutePath().toString(), config.toAbsolutePath().toString());
if (net.empty()) {
System.out.println("Can't load network!");
return;
}
Mat image = load(new File("example_01.jpg"), IMREAD_COLOR);
Mat inputBlob = blobFromImage(image);
// set input
net.setInput(inputBlob);
// create output layers
StringVector outNames = new StringVector("detection_out_final", "detection_masks");
MatVector outs = new MatVector(outNames.size());
// run detection
net.forward(outs, outNames);
// evaluate result
postprocess(image, outs);
}
private static void postprocess(Mat frame, MatVector outs) {
IntVector classIds = new IntVector();
FloatVector confidences = new FloatVector();
RectVector boxes = new RectVector();
for (int i = 0; i < outs.size(); ++i) {
Mat result = outs.get(i);
for (int j = 0; j < result.rows(); j++) {
}
}
}
In postprocess function when I get the result “Mat result = outs.get(i)" the row value is always -1. Am I doing anything wrong?
Can you please guide me?
With Regards,
Lariena
From: flo...@nexpose.ch <flo...@nexpose.ch>
Sent: mercredi 22 avril 2020 10:17
To: 'Samuel Audet' <samuel...@gmail.com>; larien...@gmail.com
Cc: 'Jan-Philipp Buck' <coole....@gmail.com>
Subject: RE: [javacv] Instance segmentation with OpenCV
Hi Lariena
I think you misunderstood my answer, it was just a proof that loading the model works. As you see in the source code, I have not yet implemented the algorithm and the result fetching.
Best regards
Florian
Hi Florian,
Thanks a lot 😊 Much appreciated.
How do I extract just the detect images. I am trying to
Mat cloneImage = originalImg.clone();
Rect roi = new Rect(x, y, width, height);
Mat cropped = new Mat(cloneImage, roi);
//Mask
Mat mask = detection.getMask();
I need to “bitwise and” the mask and region of interest something like “cv2.bitwise_and(roi, roi, mask)”. I don’t know how to do it with Mat of type “org.bytedeco.opencv.opencv_core”.
Any guidance on this?
With Regards,
Lariena
From: flo...@bildspur.ch <flo...@bildspur.ch>
Sent: lundi 4 mai 2020 21:47
To: larien...@gmail.com; Samuel Audet <samuel...@gmail.com>
Cc: 'Jan-Philipp Buck' <coole....@gmail.com>; javacv <jav...@googlegroups.com>
Subject: Re: [javacv] Instance segmentation with OpenCV
Dear All
I found a bit of time today to implement a working version. Please have a look here as a reference in JavaCV:
https://github.com/cansik/deep-vision-processing/blob/master/src/main/java/ch/bildspur/vision/MaskRCNN.java
@Samuel: I am still looking for time to port all my examples to the javacv examples
Best regards
Florian