1
1
import org .opencv .core .Core ;
2
2
import org .opencv .core .Mat ;
3
+ import org .opencv .core .Point ;
3
4
import org .opencv .core .Rect ;
4
5
import org .opencv .core .Scalar ;
5
6
import org .opencv .core .Size ;
6
7
import org .opencv .dnn .Net ;
7
8
import org .opencv .dnn .Dnn ;
8
9
import org .opencv .imgproc .Imgproc ;
10
+ import org .opencv .highgui .HighGui ;
9
11
import org .opencv .imgcodecs .Imgcodecs ;
10
12
11
13
import java .io .IOException ;
@@ -30,6 +32,8 @@ public class DnnOpenCV {
30
32
private static final Scalar MEAN = new Scalar (0.485 , 0.456 , 0.406 );
31
33
private static final Scalar STD = new Scalar (0.229 , 0.224 , 0.225 );
32
34
35
+ private static Mat imageRead ;
36
+
33
37
public static ArrayList <String > getImgLabels (String imgLabelsFilePath ) throws IOException {
34
38
ArrayList <String > imgLabels ;
35
39
try (Stream <String > lines = Files .lines (Paths .get (imgLabelsFilePath ))) {
@@ -52,10 +56,13 @@ public static Mat centerCrop(Mat inputImage) {
52
56
53
57
public static Mat getPreprocessedImage (String imagePath ) {
54
58
// 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
+
57
64
// resize input image
58
- Imgproc .resize (image , image , new Size (256 , 256 ));
65
+ Imgproc .resize (imageRead , image , new Size (256 , 256 ));
59
66
60
67
// create empty Mat images for float conversions
61
68
Mat imgFloat = new Mat (image .rows (), image .cols (), CvType .CV_32FC3 );
@@ -121,5 +128,14 @@ public static void main(String[] args) {
121
128
// decode classification results
122
129
String label = DnnOpenCV .getPredictedClass (classification );
123
130
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
+ }
124
140
}
125
- }
141
+ }
0 commit comments