使用OpenCV调用Caffe-SSD训练好的模型
生活随笔
收集整理的這篇文章主要介紹了
使用OpenCV调用Caffe-SSD训练好的模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
1.OpenCV3之后的dnn模型可以可以調用好多用深度學習框架的訓練好的模型。
2.這里我演示是我自己訓練的一個圍棋棋盤識別模型,使用的神經網絡是VGG16,OpenCV3.3,IDE是VS2015。
3.訓練好模型之后把標簽文件,模型文件,配置文件復制到當前工程目錄方便調用。
代碼
1.標簽文件內容:
label: 0 display_name: "background" label:1 display_name: "B" label:2 display_name: "H"2.代碼
#pragma once #include <opencv2/opencv.hpp> #include <opencv2/dnn.hpp> #include <iostream>using namespace cv; using namespace cv::dnn; using namespace std; const int meanValues[3] = { 104, 117, 123 }; const size_t width = 300; const size_t height = 300; //得到標簽名 static Mat getMean(const size_t &w, const size_t &h); static Mat preprocess(const Mat &frame); vector<string> readLabels(string label_file); //得到棋盤 void getBarod(Mat &src, Mat &go_barod, string model_file, string model_text_file, string label_file);static Mat getMean(const size_t &w, const size_t &h) {Mat mean;vector<Mat> channels;for (int i = 0; i < 3; i++){Mat channel(h, w, CV_32F, Scalar(meanValues[i]));channels.push_back(channel);}merge(channels, mean);return mean; }static Mat preprocess(const Mat &frame) {Mat preprocessed;frame.convertTo(preprocessed, CV_32F);resize(preprocessed, preprocessed, Size(width, height));Mat mean = getMean(width, height);subtract(preprocessed, mean, preprocessed);return preprocessed; }void getBarod(Mat &src, Mat &go_barod, string model_file, string model_text_file, string label_file) {Rect rect;vector<string> obj_names = readLabels(label_file);Ptr<dnn::Importer> importer;try{importer = createCaffeImporter(model_text_file, model_file);}catch (const cv::Exception &err){cerr << err.msg << endl;}Net net;importer->populateNet(net);importer.release();Mat input_image = preprocess(src);Mat blobImage = blobFromImage(input_image);net.setInput(blobImage, "data");Mat detection = net.forward("detection_out");Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>());float confidence_threshold = 0.2;for (int i = 0; i < detectionMat.rows; i++){float confidence = detectionMat.at<float>(i, 2);if (confidence > confidence_threshold){size_t objIndex = (size_t)(detectionMat.at<float>(i, 1));float tl_x = detectionMat.at<float>(i, 3) * src.cols;float tl_y = detectionMat.at<float>(i, 4) * src.rows;float br_x = detectionMat.at<float>(i, 5) * src.cols;float br_y = detectionMat.at<float>(i, 6) * src.rows;rect = Rect((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y));rectangle(src, rect, Scalar(i * 10, 0, 255), 2, 8, 0);putText(src, format("%s", obj_names[objIndex].c_str()), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2);}}namedWindow("src", 0);imshow("src",src); }//得到標簽名 void readLabels(vector<string> obj_names, string label_file) {ifstream fp(label_file);if (!fp.is_open()){printf("could not open the file...\n");exit(-1);}string name;while (!fp.eof()){getline(fp, name);if (name.length() && (name.find("display_name:") == 0)){string temp = name.substr(15);temp.replace(temp.end() - 1, temp.end(), "");obj_names.push_back(temp);}} }void adaptiveLogarithmicMapping(const Mat& img, Mat &dst) {Mat ldrDrago;img.convertTo(ldrDrago, CV_32FC3, 1.0f / 255);cvtColor(ldrDrago, ldrDrago, cv::COLOR_BGR2XYZ);Ptr<TonemapDrago> tonemapDrago = createTonemapDrago(1.f, 1.f, 0.85f);tonemapDrago->process(ldrDrago, dst);cvtColor(dst, dst, cv::COLOR_XYZ2BGR);dst.convertTo(dst, CV_8UC3, 255); }vector<string> readLabels(string label_file) {vector<string> objNames;ifstream fp(label_file);if (!fp.is_open()) {printf("could not open the file...\n");exit(-1);}string name;while (!fp.eof()){getline(fp, name);if (name.length() && (name.find("display_name:") == 0)){string temp = name.substr(15);temp.replace(temp.end() - 1, temp.end(), "");objNames.push_back(temp);}}return objNames; }主函數:
#include "function.h"string label_file = "weiqi.txt"; string model_file = "weiqi.caffemodel"; string model_text_file = "weiqi.prototxt";int main(void) {Mat backbackground = imread("b1.jpg");if (backbackground.empty()){cout<<"could not load image...\n")<<endl;return -1;}Mat frame, go_barod,standard_barod;getBarod(backbackground, go_barod,model_file,model_text_file, label_file);waitKey(0);return 0; }測試結果:
總結
以上是生活随笔為你收集整理的使用OpenCV调用Caffe-SSD训练好的模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux编程——入门级Makefile
- 下一篇: Linux 常用命令与设置