// This example is derived from the ssd_mobilenet_object_detection opencv demo
// and adapted to be used with Intel RealSense Cameras
// Please see https://github.com/opencv/opencv/blob/master/LICENSE
//程式簡化,不用太新的C++ 語法 本版本修改Size參數(原300x300),可辨識更小比例模型
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp>
#include <iostream>
#include <string>
using namespace cv::dnn;
using namespace cv;
using namespace std;

const int inWidth = 1280;
const int inHeight = 720;
const float inScaleFactor = 0.007843f;
const float meanVal       = 127.5;
const char* classNames[]  = { 
    "background", "aeroplane", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow", 
    "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"
};

int main(int argc, char** argv)
try
{   
    cout << "本版本修改Size參數,用cv::VideoCapture簡化非必要參數,可辨識同時多個小比例模型\n";   
    Net net = readNetFromCaffe("MobileNetSSD_deploy.prototxt",
        "MobileNetSSD_deploy.caffemodel");
    // Start streaming from Intel RealSense Camera
    VideoCapture cap(0);
    cap.set(CAP_PROP_FRAME_WIDTH, inWidth);
    cap.set(CAP_PROP_FRAME_HEIGHT, inHeight);
    const auto window_name = "1280*720顯示辨識";
    namedWindow(window_name);

    while (getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
    {
  
        Mat color_mat;
        cap >> color_mat;
                
        //Convert Mat to batch of images
        Mat inputBlob = blobFromImage(color_mat, inScaleFactor,Size(inWidth, inHeight), meanVal, false); 
        net.setInput(inputBlob, "data"); //set the network input
        Mat detection = net.forward(); //compute output

        Mat detectionMat(detection.size[2], detection.size[3], CV_32F, (float*)detection.data);

		float confidenceThreshold = 0.7;
        Vec3b color[] = {Vec3b(255,255,0), Vec3b(0, 255, 0), Vec3b(0,255, 255), Vec3b(255, 255, 127)};
		for (int i = 0; i < detectionMat.rows; i++)
		{
            putText(color_mat, "Max size of Detections: "+ to_string(detection.size[2]),
                Size(30, 30), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255));
			float confidence = detectionMat.at<float>(i, 2);

			if (confidence > confidenceThreshold)
			{
				size_t objectClass = (size_t)(detectionMat.at<float>(i, 1));
                cout <<"i="<<i<<", #"<<objectClass << "\t";
				int xLeftBottom = (int)(detectionMat.at<float>(i, 3) * color_mat.cols);
				int yLeftBottom = (int)(detectionMat.at<float>(i, 4) * color_mat.rows);
				int xRightTop = (int)(detectionMat.at<float>(i, 5) * color_mat.cols);
				int yRightTop = (int)(detectionMat.at<float>(i, 6) * color_mat.rows);

				Rect object((int)xLeftBottom, (int)yLeftBottom,
					(int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom));             
                cout << object;
				string ss = classNames[objectClass];
			    ss += " Prob=" + to_string(confidence);
				cout << ss << endl;

				rectangle(color_mat, object, color[i%4], 2);
				int baseLine = 0;
				Size labelSize = getTextSize(ss, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);

				auto center = (object.br() + object.tl()) * 0.5;
				center.x = center.x - labelSize.width / 2;

				rectangle(color_mat, Rect(Point(center.x, center.y - labelSize.height),
					Size(labelSize.width, labelSize.height + baseLine)), color[i%4], -1);
				putText(color_mat, ss, center, FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));
			}
		}       
        imshow(window_name, color_mat);

        int k=waitKey(1);
        if (k == 27) break;
        else if (k == (int)'+') imwrite("photo.png", color_mat);
    }
    destroyAllWindows();
    system("Pause");
    return 0;
}
catch (exception& e)
{
    cerr << e.what();
    return 1;
}
附件列表