void MainWindow::tensorflow()
{
qDebug()<<"startAutoRecognize";
QStringList classes;
float confThreshold = 0.8;
float scale = 1;
Scalar mean = { 0, 0, 0 };
bool swapRB = true;
int inpWidth = 200;
int inpHeight = 200;
QString strPath = QCoreApplication::applicationDirPath();
String modelPath = strPath.toStdString()+"/frozen_inference_graph.pb";
String configPath = strPath.toStdString()+"/ssdlite.pbtxt";
String framework = "";
const char* classNames[] = {"苹果","白象多半桶","4元薯愿","8元薯愿","农夫山泉小瓶" };
int backendId = cv::dnn::DNN_BACKEND_OPENCV;
int targetId = cv::dnn::DNN_TARGET_CPU;
String classesFile = strPath.toStdString()+"/predefined_classes.txt";
// Open file with classes names.
if (!classesFile.empty()) {
const std::string& file = classesFile;
std::ifstream ifs(file.c_str());
if (!ifs.is_open())
CV_Error(Error::StsError, "File " + file + " not found");
std::string line;
while (std::getline(ifs, line)) {
classes.push_back(QString::fromStdString(line));
}
}
// Load a model.
Net net = readNet(modelPath, configPath, framework);
net.setPreferableBackend(backendId);
net.setPreferableTarget(targetId);
std::vector<String> outNames = net.getUnconnectedOutLayersNames();
Mat frame, blob;
cv::VideoCapture capture;
if(!capture.open(0))
{
qDebug()<<"capture open error!";
}
while(1)
{
capture >> frame;
if(frame.empty())
{
qDebug()<<"frame is empty";
}
//QImage img = Mat2QImage(frame);
//emit sendFruitImageInfo(img);
//Create a 4D blob from a frame.
Size inpSize(inpWidth > 0 ? inpWidth : frame.cols,
inpHeight > 0 ? inpHeight : frame.rows);
blobFromImage(frame, blob, scale, inpSize, mean, swapRB, false);
// Run a model.
net.setInput(blob);
// if (net.getLayer(0)->outputNameToIndex("im_info") != -1) // Faster-RCNN or R-FCN
// {
// cv::resize(frame, frame, inpSize);
// Mat imInfo = (Mat_<float>(1, 3) << inpSize.height, inpSize.width, 1.6f);
// net.setInput(imInfo, "im_info");
// }
std::vector<Mat> outs;
net.forward(outs, outNames);
std::vector<int> classIds;
CV_Assert(outs.size() == 1);
float* data = (float*)outs[0].data;
for (size_t i = 0; i < outs[0].total(); i += 7) {
float confidence = data[i + 2];
if (confidence > confThreshold)
{
classIds.push_back((int)(data[i + 1]) - 1); // Skip 0th background class id.
qDebug() << "类别:" << QString::number((int)(data[i + 1]) - 1) << "confidence:" << confidence;
QString strDisplay = (classes[(int)(data[i + 1]) - 1]);
qDebug()<<"strDisplay:"<<strDisplay;
}
}
imshow("image",frame);
waitKey(30);
}
}