Skip to content

Instantly share code, notes, and snippets.

@berak
Created January 13, 2019 16:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berak/68905442fa25416075b769906f94cc37 to your computer and use it in GitHub Desktop.
Save berak/68905442fa25416075b769906f94cc37 to your computer and use it in GitHub Desktop.
face boxes
#include <opencv2/dnn.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace cv::dnn;
using namespace std;
int main(int argc, char **argv)
{
String modelTxt = "c:/data/mdl/faceboxes/deploy.prototxt";
String modelBin = "c:/data/mdl/faceboxes/FaceBoxes_1024x1024.caffemodel";
String imageFile = (argc > 1) ? argv[1] : "../demo/img/h1.png";
Net net = dnn::readNetFromCaffe(modelTxt,modelBin);
if (net.empty())
{
std::cerr << "Can't load network by using the following files: " << std::endl;
std::cerr << "caffemodel: " << modelBin << std::endl;
exit(-1);
}
Mat img = imread(imageFile);
if (img.empty())
{
std::cerr << "Can't read image from the file: " << imageFile << std::endl;
exit(-1);
}
int s = 1024;
Mat inputBlob = blobFromImage(img, 1.0/128, Size(s,s), Scalar::all(127),false, true);
net.setInput(inputBlob); //set the network input
Mat out = net.forward();
MatShape ms1 { inputBlob.size[0], inputBlob.size[1] , inputBlob.size[2], inputBlob.size[3] };
size_t nlayers = net.getLayerNames().size() + 1; // one off for the hidden input layer
for (size_t i=0; i<nlayers; i++) {
Ptr<Layer> lyr = net.getLayer((unsigned)i);
vector<MatShape> in,out;
net.getLayerShapes(ms1,i,in,out);
cout << format("%-38s %-13s ", (i==0?"data":lyr->name.c_str()), (i==0?"Input":lyr->type.c_str()));
for (auto j:in) cout << "i" << Mat(j).t() << " "; // input(s) size
for (auto j:out) cout << "o" << Mat(j).t() << " "; // output(s) size
for (auto b:lyr->blobs) { // what the net trains on, e.g. weights and bias
cout << "b[" << b.size[0];
for (size_t d=1; d<b.dims; d++) cout << ", " << b.size[d];
cout << "] ";
}
cout << endl;
}
cout << out.size << endl;
out = out.reshape(1,out.size[2]); // 7 cols
for (int r=0; r<out.rows; r++) {
float cls = out.at<float>(r,1);
float conf = out.at<float>(r,2);
int x = out.at<float>(r,3) * img.cols;
int y = out.at<float>(r,4) * img.rows;
int X = out.at<float>(r,5) * img.cols;
int Y = out.at<float>(r,6) * img.rows;
Rect rc(x,y,X-x,Y-y);
cout << cls << " " << conf << " " << rc << endl;
if (conf > 1) {
rectangle(img, rc, Scalar(0,200,0), 1);
}
}
imshow("I",img);
waitKey();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment