1 #include <opencv2/opencv.hpp> 2 #include <opencv2/dnn.hpp> 3 #include <iostream> 4 5 using namespace cv; 6 using namespace cv::dnn; 7 using namespace std; 8 9 const size_t width = 300; 10 const size_t height = 300; 11 const float meanVal = 127.5;//均值 12 const float scaleFactor = 0.007843f; 13 const char* classNames[] = { "background", 14 "aeroplane", "bicycle", "bird", "boat", 15 "bottle", "bus", "car", "cat", "chair", 16 "cow", "diningtable", "dog", "horse", 17 "motorbike", "person", "pottedplant", 18 "sheep", "sofa", "train", "tvmonitor" }; 19 //模型文件 20 String modelFile = "D:/opencv3.3/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.caffemodel"; 21 //二进制描述文件 22 String model_text_file = "D:/opencv3.3/opencv/sources/samples/data/dnn/MobileNetSSD_deploy.prototxt"; 23 24 int main(int argc, char** argv) { 25 VideoCapture capture;//读取视频 26 capture.open("01.mp4"); 27 namedWindow("input", CV_WINDOW_AUTOSIZE); 28 int w = capture.get(CAP_PROP_FRAME_WIDTH);//获取视频宽度 29 int h = capture.get(CAP_PROP_FRAME_HEIGHT );//获取视频高度 30 printf("frame width : %d, frame height : %d", w, h); 31 32 // set up net 33 Net net = readNetFromCaffe(model_text_file, modelFile); 34 35 Mat frame; 36 while (capture.read(frame)) { 37 imshow("input", frame); 38 39 // 预测 40 Mat inputblob = blobFromImage(frame, scaleFactor, Size(width, height), meanVal, false); 41 net.setInput(inputblob, "data"); 42 Mat detection = net.forward("detection_out"); 43 44 // 绘制 45 Mat detectionMat(detection.size[2], detection.size[3], CV_32F, detection.ptr<float>()); 46 float confidence_threshold = 0.25;//自信区间,越小检测到的物体越多(>=0.25) 47 for (int i = 0; i < detectionMat.rows; i++) { 48 float confidence = detectionMat.at<float>(i, 2); 49 if (confidence > confidence_threshold) { 50 size_t objIndex = (size_t)(detectionMat.at<float>(i, 1)); 51 float tl_x = detectionMat.at<float>(i, 3) * frame.cols; 52 float tl_y = detectionMat.at<float>(i, 4) * frame.rows; 53 float br_x = detectionMat.at<float>(i, 5) * frame.cols; 54 float br_y = detectionMat.at<float>(i, 6) * frame.rows; 55 56 Rect object_box((int)tl_x, (int)tl_y, (int)(br_x - tl_x), (int)(br_y - tl_y)); 57 rectangle(frame, object_box, Scalar(0, 0, 255), 2, 8, 0); 58 putText(frame, format("%s", classNames[objIndex]), Point(tl_x, tl_y), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(255, 0, 0), 2); 59 } 60 } 61 imshow("ssd-video-demo", frame); 62 char c = waitKey(5); 63 if (c == 27) { // 如果ESC按下 64 break; 65 } 66 } 67 capture.release(); 68 waitKey(0); 69 return 0; 70 }标签:detectionMat,float,MobileNet,int,frame,DNN,tl,SSD,size From: https://www.cnblogs.com/ybqjymy/p/17639511.html