Skip to content

Commit 3cd644c

Browse files
author
Anastasia Murzova
committed
Minor Java code correction
1 parent 98e6725 commit 3cd644c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,21 @@ public static Mat getPreprocessedImage(String imagePath) {
8282
return blob;
8383
}
8484

85-
public static void getPredictedClass(Mat classificationResult) {
85+
public static String getPredictedClass(Mat classificationResult) {
8686
ArrayList<String> imgLabels = new ArrayList<String>();
8787
try {
8888
imgLabels = getImgLabels(IMAGENET_CLASSES);
8989
} catch (IOException ex) {
90-
90+
System.out.printf("Could not read %s file:%n", IMAGENET_CLASSES);
91+
ex.printStackTrace();
92+
}
93+
if (imgLabels.isEmpty()) {
94+
return "";
9195
}
9296
// obtain max prediction result
9397
Core.MinMaxLocResult mm = Core.minMaxLoc(classificationResult);
9498
double maxValIndex = mm.maxLoc.x;
95-
System.out.println("Predicted Class: " + imgLabels.get((int) maxValIndex));
99+
return imgLabels.get((int) maxValIndex);
96100
}
97101

98102
public static void main(String[] args) {
@@ -115,6 +119,7 @@ public static void main(String[] args) {
115119
Mat classification = dnnNet.forward();
116120

117121
// decode classification results
118-
DnnOpenCV.getPredictedClass(classification);
122+
String label = DnnOpenCV.getPredictedClass(classification);
123+
System.out.println("Predicted Class: " + label);
119124
}
120125
}

0 commit comments

Comments
 (0)