File size: 5,273 Bytes
c5ec220 1528081 c5ec220 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#include <vector>
#include <string>
#include <utility>
#include <opencv2/opencv.hpp>
#include "labelsimagenet1k.h"
using namespace std;
using namespace cv;
using namespace dnn;
vector< pair<dnn::Backend, dnn::Target> > backendTargetPairs = {
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_OPENCV, dnn::DNN_TARGET_CPU),
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA),
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CUDA, dnn::DNN_TARGET_CUDA_FP16),
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_TIMVX, dnn::DNN_TARGET_NPU),
std::make_pair<dnn::Backend, dnn::Target>(dnn::DNN_BACKEND_CANN, dnn::DNN_TARGET_NPU) };
std::string keys =
"{ help h | | Print help message. }"
"{ model m | image_classification_mobilenetv1_2022apr.onnx | Usage: Set model type, defaults to image_classification_mobilenetv1_2022apr.onnx (v1) }"
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
"{ initial_width | 0 | Preprocess input image by initial resizing to a specific width.}"
"{ initial_height | 0 | Preprocess input image by initial resizing to a specific height.}"
"{ rgb | true | swap R and B plane.}"
"{ crop | false | Preprocess input image by center cropping.}"
"{ vis v | true | Usage: Specify to open a new window to show results.}"
"{ backend bt | 0 | Choose one of computation backends: "
"0: (default) OpenCV implementation + CPU, "
"1: CUDA + GPU (CUDA), "
"2: CUDA + GPU (CUDA FP16), "
"3: TIM-VX + NPU, "
"4: CANN + NPU}";
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run classification deep learning networks in opencv Zoo using OpenCV.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
int rszWidth = parser.get<int>("initial_width");
int rszHeight = parser.get<int>("initial_height");
bool swapRB = parser.get<bool>("rgb");
bool crop = parser.get<bool>("crop");
bool vis = parser.get<bool>("vis");
String model = parser.get<String>("model");
int backendTargetid = parser.get<int>("backend");
if (model.empty())
{
CV_Error(Error::StsError, "Model file " + model + " not found");
}
vector<string> labels = getLabelsImagenet1k();
Net net = readNet(samples::findFile(model));
net.setPreferableBackend(backendTargetPairs[backendTargetid].first);
net.setPreferableTarget(backendTargetPairs[backendTargetid].second);
//! [Open a video file or an image file or a camera stream]
VideoCapture cap;
if (parser.has("input"))
cap.open(samples::findFile(parser.get<String>("input")));
else
cap.open(0);
if (!cap.isOpened())
CV_Error(Error::StsError, "Cannot open video or file");
Mat frame, blob;
static const std::string kWinName = model;
int nbInference = 0;
while (waitKey(1) < 0)
{
cap >> frame;
if (frame.empty())
{
cout << "Frame is empty" << endl;
waitKey();
break;
}
if (rszWidth != 0 && rszHeight != 0)
{
resize(frame, frame, Size(rszWidth, rszHeight));
}
Image2BlobParams paramMobilenet;
paramMobilenet.datalayout = DNN_LAYOUT_NCHW;
paramMobilenet.ddepth = CV_32F;
paramMobilenet.mean = Scalar(123.675, 116.28, 103.53);
paramMobilenet.scalefactor = Scalar(1 / (255. * 0.229), 1 / (255. * 0.224), 1 / (255. * 0.225));
paramMobilenet.size = Size(224, 224);
paramMobilenet.swapRB = swapRB;
if (crop)
paramMobilenet.paddingmode = DNN_PMODE_CROP_CENTER;
else
paramMobilenet.paddingmode = DNN_PMODE_NULL;
//! [Create a 4D blob from a frame]
blobFromImageWithParams(frame, blob, paramMobilenet);
//! [Set input blob]
net.setInput(blob);
Mat prob = net.forward();
//! [Get a class with a highest score]
Point classIdPoint;
double confidence;
minMaxLoc(prob.reshape(1, 1), 0, &confidence, 0, &classIdPoint);
int classId = classIdPoint.x;
std::string label = format("%s: %.4f", (labels.empty() ? format("Class #%d", classId).c_str() :
labels[classId].c_str()),
confidence);
if (vis)
{
putText(frame, label, Point(0, 55), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
imshow(kWinName, frame);
}
else
{
cout << label << endl;
nbInference++;
if (nbInference > 100)
{
cout << nbInference << " inference made. Demo existing" << endl;
break;
}
}
}
return 0;
}
|