#include <iostream> #include<opencv2/dnn.hpp> #include <opencv.hpp> #include <fstream> using namespace std; using namespace cv; using namespace cv::dnn; String objNames[] = { "background","aeroplane","bicycle" ,"bird" ,"boat" ,"bottle" ,"bus" ,"car" ,"cat" , "chair" ,"cow" ,"diningtable" ,"dog" ,"horse" ,"motorbike" ,"person" ,"pottedplant" ,"sheep" ,"sofa" , "train" ,"tvmoitor" }; int main() { string model_path = R"(model\ssd\)"; string model_cfg_file = model_path + "MobileNetSSD_deploy.caffemodel"; string model_pro_file = model_path + "MobileNetSSD_deploy.prototxt"; string model_label_file = model_path + "labelmap_det.txt"; Net net = readNetFromCaffe(model_pro_file, model_cfg_file); net.setPreferableBackend(DNN_BACKEND_OPENCV); vector<string> layer_names = net.getLayerNames(); for (auto i : layer_names) { int id = net.getLayerId(i); auto layer = net.getLayer(id); } Mat src = imread(R"(Pictures\1.jpg)"); imshow("input", src); Mat blob = blobFromImage(src, 0.007843, Size(300, 300), Scalar(127.5, 127.5, 127.5), false, false); net.setInput(blob, "data"); Mat detection = net.forward("detection_out"); Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); float confidence_trhreshold = 0.5; for (int i = 0; i < detectionMat.rows; i++) { float score = detectionMat.at<float>(i, 2); if (score > confidence_trhreshold) { 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 box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y)); rectangle(src, box, Scalar(0, 0, 255), 2, 8, 0); putText(src, format("score:%.2f,index:%s",score, objNames[objIndex].c_str()), box.tl(), FONT_HERSHEY_PLAIN, 0.75, Scalar(255, 0, 25), 1, 8); } } imshow("res", src); waitKey(0); return 0; }
|