Skip to content

Instantly share code, notes, and snippets.

@nitinkgp23
Created May 28, 2017 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nitinkgp23/ffa8314ae30e778058752d5a6f7cd038 to your computer and use it in GitHub Desktop.
Save nitinkgp23/ffa8314ae30e778058752d5a6f7cd038 to your computer and use it in GitHub Desktop.
/**M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace cv::dnn;
#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;
/* Find best class for the blob (i. e. class with maximal probability) */
void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
{
Mat probMat = probBlob.reshape(1, 1); //reshape the blob to 1x1000 matrix
Point classNumber;
minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
*classId = classNumber.x;
}
std::vector<String> readClassNames(const char *filename = "/home/anomaly_/Dewinter_anurag/differentmodelnitin/AB-detection-cnn-master/labels.txt")
{
std::vector<String> classNames;
std::ifstream fp(filename);
if (!fp.is_open())
{
std::cerr << "File with classes labels not found: " << filename << std::endl;
exit(-1);
}
std::string name;
while (!fp.eof())
{
std::getline(fp, name);
if (name.length())
classNames.push_back( name.substr(name.find(' ')+1) );
}
fp.close();
return classNames;
}
int main(int argc, char **argv)
{
cv::dnn::initModule(); //Required if OpenCV is built as static libs
String modelTxt = "/home/anomaly_/Dewinter_anurag/differentmodelnitin/AB-detection-cnn-master/results/model_modified.net";
// String imageFile = (argc > 1) ? argv[1] : "space_shuttle.jpg";
//! [Read and initialize network]
Net net = dnn::readNetFromTorch(modelTxt);
//! [Read and initialize network]
//! [Check that network was read successfully]
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "prototxt: " << modelTxt << std::endl;
exit(-1);
}
//! [Check that network was read successfully]
String imageFile = "/home/anomaly_/dewinter/resizedimage/A1.jpg";
Mat img = imread(imageFile);
Mat result;
//Mat mean, stddev;
//meanStdDev(img, mean, stddev);
//cout << "M = "<< endl << " " << mean << endl << endl;
//cout << "M = "<< endl << " " << stddev << endl << endl;
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
/*Mat ch1, ch2, ch3;
// "channels" is a vector of 3 Mat arrays:
vector<Mat> channels(3);
// split img:
split(img, channels);
// get the channels (dont forget they follow BGR order in OpenCV)
ch1 = channels[0];
ch2 = channels[1];
ch3 = channels[2];
Mat ch1_;
ch1.convertTo(ch1_, CV_64F);
// cout << "ch1 = "<< endl << " " << ch1_ << endl << endl;
// cout << mean.at<double>(0) << endl;
subtract(ch1_, mean.at<double>(0), ch1_);
divide(ch1_,stddev.at<double>(0), ch1_);
Mat tmp[] = { ch1_, ch1_, ch1_ };
Mat img_new;
img_new.convertTo(img_new, CV_64F);
vector<Mat> channel_new( tmp, tmp+3 );
merge(img_new, channel_new);
cout << "Merge done" << endl ;
// cout << "ch1 = "<< endl << " " << ch1_ << endl << endl;
*/
//Mat img;
//cvtColor( img1, img, CV_BGR2GRAY ); // Converting to grayscale
// cout << img.size().width << ' ' << img.size().height << endl;
//resize(img, img, Size(256, 256)); //GoogLeNet accepts only 224x224 RGB-images
Mat inputBlob = blobFromImage(img); //Convert Mat to batch of images
//! [Prepare blob]
std::vector<String> v = net.getLayerNames();
for (std::vector<String>::const_iterator i = v.begin(); i != v.end(); ++i)
std::cout << *i << ' ' << net.getLayerId(*i) << endl;
// CV_Assert(inputBlob.dims == 4 && (inputBlob.type() == CV_32F || inputBlob.type() == CV_64F));
//! [Set input blob]
net.setBlob("", inputBlob); //set the network input
//! [Set input blob]
//! [Make forward pass]
net.forward(); //compute output
//! [Make forward pass]
//! [Gather output]
String oBlob = net.getLayerNames().back();
Mat prob = net.getBlob(oBlob); //gather output of "prob" layer
/*
int classId;
double classProb;
getMaxClass(prob, &classId, &classProb);//find the best class
//! [Gather output]
//! [Print results]
std::vector<String> classNames = readClassNames();
std::cout << "Best class: #" << classId << " '" << classNames.at(classId) << "'" << std::endl;
std::cout << "Probability: " << classProb * 100 << "%" << std::endl;
//! [Print results] */
return 0;
} //main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment