#include<opencv2/opencv.hpp>
#include<opencv2/dnn.hpp>
#include <iostream>
using namespace std;
using namespace cv;
const size_t inWidth = 300;
const size_t inHeight = 300;
const float WHRatio = inWidth / (float)inHeight;
const char* classNames[] = { "background","face" };
int main() {
String weights = "/root/ssd_mayun_mobilenet_v1_coco_2018_01_28/frozen_inference_graph.pb";
//String prototxt = "/root/ssd_mayun_mobilenet_v1_coco_2018_01_28/mayun.pbtxt";
//ssd-v1.pbtxt 这个模型是通过opencv源码提供的转换工具转化的
String prototxt = "/root/lvyunxiangoutput/ssd-v1.pbtxt";
//String weights = "/root/my_tensorflow_object_detech_api/ssd_mobilenet_v1_coco_2018_01_28/frozen_inference_graph.pb";
//String prototxt = "/root/my_tensorflow_object_detech_api/models-master/research/object_detection/data/mscoco_label_map.pbtxt";
dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt);
cout << "lvyunxiang test 1" << endl;
Mat frame = cv::imread("/root/my_tensorflow_object_detech_api/test_mayun.jpg");
Size frame_size = frame.size();
Size cropSize;
if (frame_size.width / (float)frame_size.height > WHRatio)
{
cropSize = Size(static_cast<int>(frame_size.height * WHRatio),
frame_size.height);
}
else
{
cropSize = Size(frame_size.width,
static_cast<int>(frame_size.width / WHRatio));
}
cout << "lvyunxiang test 2\n"<< endl;
imshow("wwwww",frame);
waitKey(0);
Rect crop(Point((frame_size.width - cropSize.width) / 2,
(frame_size.height - cropSize.height) / 2),
cropSize);
cout << "lvyunxiang test3\n"<< endl;
cv::Mat blob = cv::dnn::blobFromImage(frame,1./255,Size(300,300));
//imshow("bbbbb",blob);
//cout << "lvyunxiang test 4\n" << endl;
cout << "blob size: " << blob.size << endl;
net.setInput(blob);
cout <<"lvyunxiang test 4\n"<< endl;
Mat output = net.forward();
cout << "output size: " << output.size << endl;
Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>());
frame = frame(crop);
float confidenceThreshold = 0.20;
for (int i = 0; i < detectionMat.rows; i++)
{
float confidence = detectionMat.at<float>(i, 2);
if (confidence > confidenceThreshold)
{
size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols);
int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows);
int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols);
int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows);
ostringstream ss;
ss << confidence;
String conf(ss.str());
Rect object((int)xLeftBottom, (int)yLeftBottom,
(int)(xRightTop - xLeftBottom),
(int)(yRightTop - yLeftBottom));
rectangle(frame, object, Scalar(0, 255, 0),2);
String label = String(classNames[objectClass]) + ": " + conf;
int baseLine = 0;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
//rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
// Size(labelSize.width, labelSize.height + baseLine)),
// Scalar(0, 255, 0), CV_FILLED);
rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height),
Size(labelSize.width, labelSize.height + baseLine)),
Scalar(0, 255, 0), -1);
putText(frame, label, Point(xLeftBottom, yLeftBottom),
FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
}
}
// namedWindow("image", CV_WINDOW_NORMAL);
imshow("image", frame);
waitKey(0);
return 0;
}