Skip to content

Commit 1556b41

Browse files
Merge pull request #513 from xlabd/patch-1
Displaying the image
2 parents 7a28ca9 + 2f8eff5 commit 1556b41

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

DNN-OpenCV-Classification-with-Java/DnnOpenCV.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import org.opencv.core.Core;
22
import org.opencv.core.Mat;
3+
import org.opencv.core.Point;
34
import org.opencv.core.Rect;
45
import org.opencv.core.Scalar;
56
import org.opencv.core.Size;
67
import org.opencv.dnn.Net;
78
import org.opencv.dnn.Dnn;
89
import org.opencv.imgproc.Imgproc;
10+
import org.opencv.highgui.HighGui;
911
import org.opencv.imgcodecs.Imgcodecs;
1012

1113
import java.io.IOException;
@@ -30,6 +32,8 @@ public class DnnOpenCV {
3032
private static final Scalar MEAN = new Scalar(0.485, 0.456, 0.406);
3133
private static final Scalar STD = new Scalar(0.229, 0.224, 0.225);
3234

35+
private static Mat imageRead;
36+
3337
public static ArrayList<String> getImgLabels(String imgLabelsFilePath) throws IOException {
3438
ArrayList<String> imgLabels;
3539
try (Stream<String> lines = Files.lines(Paths.get(imgLabelsFilePath))) {
@@ -52,10 +56,13 @@ public static Mat centerCrop(Mat inputImage) {
5256

5357
public static Mat getPreprocessedImage(String imagePath) {
5458
// get the image from the internal resource folder
55-
Mat image = Imgcodecs.imread(imagePath);
56-
59+
imageRead = Imgcodecs.imread(imagePath);
60+
61+
// this object will store the preprocessed image
62+
Mat image = new Mat();
63+
5764
// resize input image
58-
Imgproc.resize(image, image, new Size(256, 256));
65+
Imgproc.resize(imageRead, image, new Size(256, 256));
5966

6067
// create empty Mat images for float conversions
6168
Mat imgFloat = new Mat(image.rows(), image.cols(), CvType.CV_32FC3);
@@ -121,5 +128,14 @@ public static void main(String[] args) {
121128
// decode classification results
122129
String label = DnnOpenCV.getPredictedClass(classification);
123130
System.out.println("Predicted Class: " + label);
131+
132+
// displaying the photo and putting the text on it
133+
Point pos = new Point (50, 50);
134+
Scalar colour = new Scalar(255, 255, 255);
135+
Imgproc.putText(imageRead, "Predicted class is: " +label, pos, Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, colour, 2);
136+
HighGui.imshow("Input Image", imageRead);
137+
if (HighGui.waitKey(0) == 27){
138+
System.exit(0);
139+
}
124140
}
125-
}
141+
}

0 commit comments

Comments
 (0)