File size: 11,943 Bytes
ea63088 1528081 ea63088 1528081 ea63088 ab90c04 ea63088 |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
#include <vector>
#include <string>
#include <utility>
#include <opencv2/opencv.hpp>
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) };
vector<string> labelYolox = {
"person", "bicycle", "car", "motorcycle", "airplane", "bus",
"train", "truck", "boat", "traffic light", "fire hydrant",
"stop sign", "parking meter", "bench", "bird", "cat", "dog",
"horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe",
"backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat",
"baseball glove", "skateboard", "surfboard", "tennis racket",
"bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl",
"banana", "apple", "sandwich", "orange", "broccoli", "carrot",
"hot dog", "pizza", "donut", "cake", "chair", "couch",
"potted plant", "bed", "dining table", "toilet", "tv", "laptop",
"mouse", "remote", "keyboard", "cell phone", "microwave",
"oven", "toaster", "sink", "refrigerator", "book", "clock",
"vase", "scissors", "teddy bear", "hair drier", "toothbrush" };
class YoloX {
private:
Net net;
string modelPath;
Size inputSize;
float confThreshold;
float nmsThreshold;
float objThreshold;
dnn::Backend backendId;
dnn::Target targetId;
int num_classes;
vector<int> strides;
Mat expandedStrides;
Mat grids;
public:
YoloX(string modPath, float confThresh = 0.35, float nmsThresh = 0.5, float objThresh = 0.5, dnn::Backend bId = DNN_BACKEND_DEFAULT, dnn::Target tId = DNN_TARGET_CPU) :
modelPath(modPath), confThreshold(confThresh),
nmsThreshold(nmsThresh), objThreshold(objThresh),
backendId(bId), targetId(tId)
{
this->num_classes = int(labelYolox.size());
this->net = readNet(modelPath);
this->inputSize = Size(640, 640);
this->strides = vector<int>{ 8, 16, 32 };
this->net.setPreferableBackend(this->backendId);
this->net.setPreferableTarget(this->targetId);
this->generateAnchors();
}
Mat preprocess(Mat img)
{
Mat blob;
Image2BlobParams paramYolox;
paramYolox.datalayout = DNN_LAYOUT_NCHW;
paramYolox.ddepth = CV_32F;
paramYolox.mean = Scalar::all(0);
paramYolox.scalefactor = Scalar::all(1);
paramYolox.size = Size(img.cols, img.rows);
paramYolox.swapRB = true;
blob = blobFromImageWithParams(img, paramYolox);
return blob;
}
Mat infer(Mat srcimg)
{
Mat inputBlob = this->preprocess(srcimg);
this->net.setInput(inputBlob);
vector<Mat> outs;
this->net.forward(outs, this->net.getUnconnectedOutLayersNames());
Mat predictions = this->postprocess(outs[0]);
return predictions;
}
Mat postprocess(Mat outputs)
{
Mat dets = outputs.reshape(0,outputs.size[1]);
Mat col01;
add(dets.colRange(0, 2), this->grids, col01);
Mat col23;
exp(dets.colRange(2, 4), col23);
vector<Mat> col = { col01, col23 };
Mat boxes;
hconcat(col, boxes);
float* ptr = this->expandedStrides.ptr<float>(0);
for (int r = 0; r < boxes.rows; r++, ptr++)
{
boxes.rowRange(r, r + 1) = *ptr * boxes.rowRange(r, r + 1);
}
// get boxes
Mat boxes_xyxy(boxes.rows, boxes.cols, CV_32FC1, Scalar(1));
Mat scores = dets.colRange(5, dets.cols).clone();
vector<float> maxScores(dets.rows);
vector<int> maxScoreIdx(dets.rows);
vector<Rect2d> boxesXYXY(dets.rows);
for (int r = 0; r < boxes_xyxy.rows; r++, ptr++)
{
boxes_xyxy.at<float>(r, 0) = boxes.at<float>(r, 0) - boxes.at<float>(r, 2) / 2.f;
boxes_xyxy.at<float>(r, 1) = boxes.at<float>(r, 1) - boxes.at<float>(r, 3) / 2.f;
boxes_xyxy.at<float>(r, 2) = boxes.at<float>(r, 0) + boxes.at<float>(r, 2) / 2.f;
boxes_xyxy.at<float>(r, 3) = boxes.at<float>(r, 1) + boxes.at<float>(r, 3) / 2.f;
// get scores and class indices
scores.rowRange(r, r + 1) = scores.rowRange(r, r + 1) * dets.at<float>(r, 4);
double minVal, maxVal;
Point maxIdx;
minMaxLoc(scores.rowRange(r, r+1), &minVal, &maxVal, nullptr, &maxIdx);
maxScoreIdx[r] = maxIdx.x;
maxScores[r] = float(maxVal);
boxesXYXY[r].x = boxes_xyxy.at<float>(r, 0);
boxesXYXY[r].y = boxes_xyxy.at<float>(r, 1);
boxesXYXY[r].width = boxes_xyxy.at<float>(r, 2);
boxesXYXY[r].height = boxes_xyxy.at<float>(r, 3);
}
vector<int> keep;
NMSBoxesBatched(boxesXYXY, maxScores, maxScoreIdx, this->confThreshold, this->nmsThreshold, keep);
Mat candidates(int(keep.size()), 6, CV_32FC1);
int row = 0;
for (auto idx : keep)
{
boxes_xyxy.rowRange(idx, idx + 1).copyTo(candidates(Rect(0, row, 4, 1)));
candidates.at<float>(row, 4) = maxScores[idx];
candidates.at<float>(row, 5) = float(maxScoreIdx[idx]);
row++;
}
if (keep.size() == 0)
return Mat();
return candidates;
}
void generateAnchors()
{
vector< tuple<int, int, int> > nb;
int total = 0;
for (auto v : this->strides)
{
int w = this->inputSize.width / v;
int h = this->inputSize.height / v;
nb.push_back(tuple<int, int, int>(w * h, w, v));
total += w * h;
}
this->grids = Mat(total, 2, CV_32FC1);
this->expandedStrides = Mat(total, 1, CV_32FC1);
float* ptrGrids = this->grids.ptr<float>(0);
float* ptrStrides = this->expandedStrides.ptr<float>(0);
int pos = 0;
for (auto le : nb)
{
int r = get<1>(le);
for (int i = 0; i < get<0>(le); i++, pos++)
{
*ptrGrids++ = float(i % r);
*ptrGrids++ = float(i / r);
*ptrStrides++ = float((get<2>(le)));
}
}
}
};
std::string keys =
"{ help h | | Print help message. }"
"{ model m | object_detection_yolox_2022nov.onnx | Usage: Path to the model, defaults to object_detection_yolox_2022nov.onnx }"
"{ input i | | Path to input image or video file. Skip this argument to capture frames from a camera.}"
"{ confidence | 0.5 | Class confidence }"
"{ obj | 0.5 | Enter object threshold }"
"{ nms | 0.5 | Enter nms IOU threshold }"
"{ save s | true | Specify to save results. This flag is invalid when using camera. }"
"{ vis v | 1 | Specify to open a window for result visualization. This flag is invalid when using camera. }"
"{ 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}";
pair<Mat, double> letterBox(Mat srcimg, Size targetSize = Size(640, 640))
{
Mat paddedImg(targetSize.height, targetSize.width, CV_32FC3, Scalar::all(114.0));
Mat resizeImg;
double ratio = min(targetSize.height / double(srcimg.rows), targetSize.width / double(srcimg.cols));
resize(srcimg, resizeImg, Size(int(srcimg.cols * ratio), int(srcimg.rows * ratio)), INTER_LINEAR);
resizeImg.copyTo(paddedImg(Rect(0, 0, int(srcimg.cols * ratio), int(srcimg.rows * ratio))));
return pair<Mat, double>(paddedImg, ratio);
}
Mat unLetterBox(Mat bbox, double letterboxScale)
{
return bbox / letterboxScale;
}
Mat visualize(Mat dets, Mat srcimg, double letterbox_scale, double fps = -1)
{
Mat resImg = srcimg.clone();
if (fps > 0)
putText(resImg, format("FPS: %.2f", fps), Size(10, 25), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255), 2);
for (int row = 0; row < dets.rows; row++)
{
Mat boxF = unLetterBox(dets(Rect(0, row, 4, 1)), letterbox_scale);
Mat box;
boxF.convertTo(box, CV_32S);
float score = dets.at<float>(row, 4);
int clsId = int(dets.at<float>(row, 5));
int x0 = box.at<int>(0, 0);
int y0 = box.at<int>(0, 1);
int x1 = box.at<int>(0, 2);
int y1 = box.at<int>(0, 3);
string text = format("%s : %f", labelYolox[clsId].c_str(), score * 100);
int font = FONT_HERSHEY_SIMPLEX;
int baseLine = 0;
Size txtSize = getTextSize(text, font, 0.4, 1, &baseLine);
rectangle(resImg, Point(x0, y0), Point(x1, y1), Scalar(0, 255, 0), 2);
rectangle(resImg, Point(x0, y0 + 1), Point(x0 + txtSize.width + 1, y0 + int(1.5 * txtSize.height)), Scalar(255, 255, 255), -1);
putText(resImg, text, Point(x0, y0 + txtSize.height), font, 0.4, Scalar(0, 0, 0), 1);
}
return resImg;
}
int main(int argc, char** argv)
{
CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run Yolox deep learning networks in opencv_zoo using OpenCV.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string model = parser.get<String>("model");
float confThreshold = parser.get<float>("confidence");
float objThreshold = parser.get<float>("obj");
float nmsThreshold = parser.get<float>("nms");
bool vis = parser.get<bool>("vis");
bool save = parser.get<bool>("save");
int backendTargetid = parser.get<int>("backend");
if (model.empty())
{
CV_Error(Error::StsError, "Model file " + model + " not found");
}
YoloX modelNet(model, confThreshold, nmsThreshold, objThreshold,
backendTargetPairs[backendTargetid].first, 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, inputBlob;
double letterboxScale;
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;
}
pair<Mat, double> w = letterBox(frame);
inputBlob = get<0>(w);
letterboxScale = get<1>(w);
TickMeter tm;
tm.start();
Mat predictions = modelNet.infer(inputBlob);
tm.stop();
cout << "Inference time: " << tm.getTimeMilli() << " ms\n";
Mat img = visualize(predictions, frame, letterboxScale, tm.getFPS());
if (save && parser.has("input"))
{
imwrite("result.jpg", img);
}
if (vis)
{
imshow(kWinName, img);
}
}
return 0;
}
|