-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageanalyzer.cpp
More file actions
57 lines (43 loc) · 1.46 KB
/
imageanalyzer.cpp
File metadata and controls
57 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "imageanalyzer.h"
#include <QDebug>
#include <opencv2/opencv.hpp>
ImageAnalyzer::ImageAnalyzer()
{
}
void ImageAnalyzer::analyzeImage(QImage image)
{
qDebug() <<"analyzeImage";
cv::Mat testImage(image.height(), image.width(),CV_8UC4, image.bits());
cv::Ptr<cv::ml::ANN_MLP> mlp = cv::ml::ANN_MLP::load("/home/cvlab/trained_digit_model.xml");
cv::cvtColor(testImage,testImage,cv::COLOR_RGBA2GRAY);
// Load the image you want to classify
cv::resize(testImage,testImage,cv::Size(28,28));
// Flatten the image
cv::Mat flattenedImage = testImage.reshape(1, 1);
cv::Mat input;
flattenedImage.convertTo(input, CV_32F);
// Perform prediction
cv::Mat output;
mlp->predict(input, output);
std::cout<<output<<std::endl;
// Find the class with the highest probability
cv::Point classIdPoint;
double confidence;
cv::minMaxLoc(output, nullptr, &confidence, nullptr, &classIdPoint);
int predictedClass = classIdPoint.x;
// Display the result
std::cout << "Predicted class: " << predictedClass << " with confidence: " << confidence << std::endl;
QString result = "Recognized digit is " + QString::number(predictedClass);
setResultStr(result);
}
QString ImageAnalyzer::resultStr() const
{
return m_resultStr;
}
void ImageAnalyzer::setResultStr(const QString &newResultStr)
{
if (m_resultStr == newResultStr)
return;
m_resultStr = newResultStr;
Q_EMIT resultStrChanged();
}