Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
CHILD INTELLIGENT ASSESSMENT TOOL
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2020-046
CHILD INTELLIGENT ASSESSMENT TOOL
Commits
9e52d9ad
Commit
9e52d9ad
authored
May 17, 2020
by
Gunasinghe M.D.
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Test the ANN
parent
0ba37063
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
103 additions
and
0 deletions
+103
-0
TestANN.java
TestANN.java
+103
-0
No files found.
TestANN.java
0 → 100644
View file @
9e52d9ad
package
opencvapp
;
import
java.io.File
;
import
java.util.Arrays
;
import
org.opencv.core.Core
;
import
org.opencv.core.CvType
;
import
org.opencv.core.Mat
;
import
org.opencv.core.MatOfFloat
;
import
org.opencv.core.MatOfInt
;
import
org.opencv.imgcodecs.Imgcodecs
;
import
org.opencv.imgproc.Imgproc
;
import
org.opencv.ml.ANN_MLP
;
public
class
PredictANN
{
public
static
void
main
(
String
[]
args
)
{
String
currentDirectory
=
System
.
getProperty
(
"user.dir"
);
System
.
load
(
currentDirectory
+
"\\OpenCVDLL\\x64\\"
+
Core
.
NATIVE_LIBRARY_NAME
+
".dll"
);
Mat
datasetHist
=
new
Mat
();
Mat
datasetLabels
=
new
Mat
();
String
[]
classesNames
=
{
"a"
,
"b"
,
"c"
,
"d"
};
for
(
int
classIdx
=
0
;
classIdx
<
classesNames
.
length
;
classIdx
++){
String
currClassName
=
classesNames
[
classIdx
];
String
currClassDir
=
currentDirectory
+
"\\Dataset\\Test\\"
+
currClassName
+
"\\"
;
System
.
out
.
println
(
"Current Class Directory : "
+
currClassDir
);
File
folder
=
new
File
(
currClassDir
);
File
[]
listOfFiles
=
folder
.
listFiles
();
int
imgCount
=
0
;
for
(
File
listOfFile
:
listOfFiles
)
{
// Make sure we are working with a file and its extension is JPG
if
(
listOfFile
.
isFile
()
&&
(
currClassDir
+
listOfFile
.
getName
()).
endsWith
(
".jpg"
))
{
System
.
out
.
println
(
"Class Index "
+
classIdx
+
"("
+
currClassName
+
")"
+
", Image Index "
+
imgCount
+
"("
+
listOfFile
.
getName
()
+
")"
);
String
currImgPath
=
currClassDir
+
listOfFile
.
getName
();
System
.
out
.
println
(
currImgPath
);
Mat
imgBGR
=
Imgcodecs
.
imread
(
currImgPath
);
// int numRows = imgBGR.rows();
// int numCols = imgBGR.cols();
// int numChannels = imgRimgBGRGB.channels();
// System.out.println("Image Size : (" + numRows + ", " + numCols + ", " + numChannels + ")");
Mat
imgHSV
=
new
Mat
();
Imgproc
.
cvtColor
(
imgBGR
,
imgHSV
,
Imgproc
.
COLOR_BGR2HSV
);
// Preparing parameters of Imgproc.calcHist().
MatOfInt
selectedChannels
=
new
MatOfInt
(
0
);
Mat
imgHist
=
new
Mat
();
MatOfInt
histSize
=
new
MatOfInt
(
180
);
MatOfFloat
ranges
=
new
MatOfFloat
(
0
f
,
180
f
);
// Doc: https://docs.opencv.org/3.1.0/d6/dc7/group__imgproc__hist.html#ga4b2b5fd75503ff9e6844cc4dcdaed35d
Imgproc
.
calcHist
(
Arrays
.
asList
(
imgHSV
),
selectedChannels
,
new
Mat
(),
imgHist
,
histSize
,
ranges
);
// Transposing the histogram Mat from being 1D column vector to be 1D row vector.
imgHist
=
imgHist
.
t
();
System
.
out
.
println
(
"Hue Channel Hist : "
+
imgHist
.
dump
());
System
.
out
.
println
(
"Image Hist Size : ("
+
imgHist
.
rows
()
+
", "
+
imgHist
.
cols
()+
")\n"
);
// Inserting the extracted histogram of the current image into the Mat collecting the histograms of all images.
datasetHist
.
push_back
(
imgHist
);
datasetLabels
.
push_back
(
new
MatOfInt
(
classIdx
));
imgCount
++;
}
}
}
// Converting the type of the features & labels Mats into CV_32F because ANN accepts data of this type.
datasetHist
.
convertTo
(
datasetHist
,
CvType
.
CV_32F
);
datasetLabels
.
convertTo
(
datasetLabels
,
CvType
.
CV_32F
);
System
.
out
.
println
(
"Dataset Hist Size : ("
+
datasetHist
.
rows
()
+
", "
+
datasetHist
.
cols
()+
")"
);
System
.
out
.
println
(
"Dataset Label Size : ("
+
datasetLabels
.
rows
()
+
", "
+
datasetLabels
.
cols
()+
")"
);
ANN_MLP
ANN
=
ANN_MLP
.
load
(
currentDirectory
+
"\\OpenCV_ANN_Fruits.yml"
);
double
num_correct_predictions
=
0
;
for
(
int
i
=
0
;
i
<
datasetHist
.
rows
();
i
++)
{
Mat
sample
=
datasetHist
.
row
(
i
);
double
correct_label
=
datasetLabels
.
get
(
i
,
0
)[
0
];
Mat
results
=
new
Mat
();
ANN
.
predict
(
sample
,
results
,
0
);
double
response
=
results
.
get
(
0
,
0
)[
0
];
int
predicted_label
=
(
int
)
Math
.
round
(
response
);
System
.
out
.
println
(
"Predicted Score : "
+
response
+
", Predicted Label : "
+
predicted_label
+
", Correct Label : "
+
correct_label
);
if
(
predicted_label
==
correct_label
)
{
num_correct_predictions
+=
1
;
}
}
double
accuracy
=
(
num_correct_predictions
/
datasetHist
.
rows
())
*
100
;
System
.
out
.
println
(
"Accuracy : "
+
accuracy
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment