{"id":409096,"date":"2024-06-29T20:34:58","date_gmt":"2024-06-29T20:34:58","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=409096"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=409096","title":{"rendered":"<span>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 C++<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a17\/f5c\/2dd\/a17f5c2ddde26f979e48cf9472e9eb7c.png\" width=\"780\" height=\"440\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a17\/f5c\/2dd\/a17f5c2ddde26f979e48cf9472e9eb7c.png\"\/><\/figure>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 Python. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438 \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, C++. \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043d\u0430 C++ \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 <em>YOLOv8 \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446<\/em> \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <em>PyTorch<\/em>.<\/p>\n<h2>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/h2>\n<p>PyTorch \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 C++-\u0432\u0435\u0440\u0441\u0438\u044e \u0441\u0432\u043e\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <strong>LibTorch<\/strong>. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/pytorch.org\/get-started\/locally\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0430\u0442\u044c<\/a> (<em>cxx11 ABI<\/em>) \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u044f\u0434\u043e\u043c \u0441 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439:<\/p>\n<pre><code class=\"bash\">wget https:\/\/download.pytorch.org\/libtorch\/cpu\/libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip unzip libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <strong>OpenCV<\/strong>. \u0415\u0451 \u0441\u0442\u043e\u0438\u0442 <a href=\"https:\/\/docs.opencv.org\/4.x\/df\/d65\/tutorial_table_of_content_introduction.html\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0430\u0442\u044c<\/a> \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"bash\">wget https:\/\/github.com\/opencv\/opencv\/archive\/4.7.0.zip unzip opencv-4.7.0.zip &amp;&amp; cd opencv-4.7.0 mkdir build &amp;&amp; cd build cmake .. cmake --build . sudo make install<\/code><\/pre>\n<h2>\u0412\u0435\u0441\u0430<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0432\u0435\u0441\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 PyTorch \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 <code>TorchScript<\/code>. \u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\"># model_export.py import torch  MODEL_PATH = 'model.pt' MODEL_INPUT_SHAPE = (1,3,640,640)  model = torch.load(MODEL_PATH) example = torch.randn(MODEL_INPUT_SHAPE) model(example) # model warmup model_traced = torch.jit.trace(model, example) model_traced.save('model_traced.torchscript')<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c YOLOv8 \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446 \u043e\u0442 Ultralytics, \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439:<\/p>\n<pre><code class=\"bash\">pip3 install ultralytics pip3 install gdown gdown 1jG8_C_P0SbnzYROZORe7CiJO6oMgp7eZ # yolov8n-face.pt<\/code><\/pre>\n<pre><code class=\"python\"># model_export_yolov8face.py import torch from ultralytics import YOLO  model = YOLO('yolov8n-face.pt') model.export(format='torchscript')<\/code><\/pre>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/h2>\n<h3>\u0418\u043d\u043a\u043b\u044e\u0434\u044b, \u043b\u0438\u0431\u044b \u0438 \u043b\u0438\u043d\u043a\u043e\u0432\u043a\u0430<\/h3>\n<p>\u0414\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0443\u0442\u044c \u0434\u043e icnlude- \u0438 lib-\u0444\u0430\u0439\u043b\u043e\u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a LibTorch \u0438 OpenCV, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0438\u043d\u043a\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 lib-\u0444\u0430\u0439\u043b\u044b (\u043e\u043d\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438; \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c IDE, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Visual Studio Code):<\/p>\n<pre><code class=\"bash\"># ... -I\/usr\/local\/include\/opencv4 -I\/path\/to\/libtorch\/include -L\/path\/to\/libtorch\/lib # ... -o main.o # ... -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -ltorch -ltorch_cpu -lc10<\/code><\/pre>\n<h3>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/h3>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp #include &lt;torch\/script.h> #include &lt;opencv2\/opencv.hpp>  int main() {    torch::jit::script::Module     model = torch::jit::load(\"yolov8n-face.torchscript\");      return 0; }<\/code><\/pre>\n<h3>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438<\/h3>\n<p>LibTorch \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438\u0437 <code>torch::jit::IValue<\/code> \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>torch::jit::IValue<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (\u0442\u0435\u043d\u0437\u043e\u0440 <code>.toTensor()<\/code>, \u043a\u043e\u0440\u0442\u0435\u0436 <code>.toTuple()<\/code> \u0438\u043b\u0438 \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439).<\/p>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c-\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0442\u0435\u043d\u0437\u043e\u0440 \u0441 \u0444\u043e\u0440\u043c\u043e\u0439 <code>(B, 3, 640, 640)<\/code> \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043d\u0437\u043e\u0440 \u0441 \u0444\u043e\u0440\u043c\u043e\u0439 <code>(1, 5, 8400)<\/code>, \u0433\u0434\u0435 5 \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 8400 \u0446\u0435\u043b\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <em>cx<\/em>, <em>cy<\/em>, <em>w<\/em>, <em>h<\/em> \u0438 \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 <em>conf<\/em> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043e\u043a\u0441\u0430 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   cv::Mat img = cv::imread(source);   cv::cvtColor(img, img, cv::COLOR_BGR2RGB);   cv::normalize(img, img, 0.0, 1.0, cv::NORM_MINMAX, CV_32F);   vector&lt;torch::jit::IValue> inputs = {     torch::from_blob(       imgNorm.data,       {640,640,3},       torch::kFloat32     ).permute({2,0,1}).unsqueeze(0)   };   at::Tensor outputs = model.forward(inputs).toTensor();   \/\/ ...<\/code><\/pre>\n<h3>\u0414\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0438\u0446<\/h3>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u0438-\u043f\u0440\u0438\u043c\u0435\u0440\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 <code>Box<\/code> \u0434\u043b\u044f \u0431\u043e\u043a\u0441\u043e\u0432 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... class Box {     public:         int x1, y1, x2, y2;         float conf;         Box(int x1, int y1, int x2,  int y2, float conf) {             this->x1 = x1;             this->y1 = y1;             this->x2 = x2;             this->y2 = y2;             this->conf = conf;         } }; \/\/ ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>getBoxes<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0431\u043e\u043a\u0441\u044b \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <em>conf<\/em> \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e <em>Non Maximum Supression<\/em> (NMS):<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... vector&lt;Box> getBoxes(     at::Tensor &amp;outputs,     float confThres = 0.25,     float iouThres = 0.15 ) {     vector&lt;Box> candidates;     for (unsigned short ibatch = 0; ibatch &lt; outputs.sizes()[0]; ibatch++) {         for (unsigned short ibox = 0; ibox &lt; outputs.sizes()[2]; ibox++) {             float conf = outputs[ibatch][4][ibox].item&lt;float>();             if (conf >= confThres) {                 unsigned short                     cx = outputs[ibatch][0][ibox].item&lt;int>(),                     cy = outputs[ibatch][1][ibox].item&lt;int>(),                     w = outputs[ibatch][2][ibox].item&lt;int>(),                     h = outputs[ibatch][3][ibox].item&lt;int>();                 unsigned short                     x1 = cx - w \/ 2,                     y1 = cy - h \/ 2,                     x2 = cx + w \/ 2,                     y2 = cy + h \/ 2;                 candidates.push_back(Box(x1,y1,x2,y2,conf));             }         }     }     sort(candidates.begin(), candidates.end(), [](Box b1, Box b2){return b1.conf > b2.conf;});     vector&lt;Box> boxes = nms(candidates, iouThres);     return boxes; } \/\/ ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>nms<\/code> , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043a\u0441\u044b-\u043a\u0430\u043d\u0434\u0438\u0442\u0430\u0442\u044b \u0438 \u0441\u043e\u043f\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e <code>iou<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043c\u0435\u0442\u0440\u0438\u043a\u0443 IoU (<em>Intersection Over Union<\/em>):<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... float iou(Box &amp;fb, Box &amp;sb) {     float inter = max(min(fb.x2, sb.x2) - min(fb.x1, sb.x1), 0) * max(min(fb.y2, sb.y2) - min(fb.y1, sb.y1), 0);     float union_ = (fb.x2-fb.x1)*(fb.y2-fb.y1) + (sb.x2-sb.x1)*(sb.y2-sb.y1) - inter;     return inter \/ union_; }  vector&lt;Box> nms(vector&lt;Box> &amp;boxes, float iouThres) {     vector&lt;Box> supBoxes;     for (Box box: boxes) {         bool valid = true;         for (Box supBox: supBoxes) {             if (iou(box, supBox) > iouThres) {                 valid = false;                 break;             }         }         if (valid == true) {             supBoxes.push_back(box);         }     }     return supBoxes; } \/\/ getBoxes<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0431\u043e\u043a\u0441\u044b \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   vector&lt;Box> boxes = getBoxes(outputs);   \/\/ ...<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043a\u0441\u043e\u0432 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... void highlightBoxes(cv::Mat &amp;img, vector&lt;Box> &amp;boxes) {   cv::Scalar rectColor(0,192,0);   unsigned short fontScale = 2, confPrecis = 2;    for (Box box: boxes) {     string text = to_string(box.conf);     cv::rectangle(img, {box.x1,box.y1}, {box.x2,box.y2}, rectColor, 2);     cv::rectangle(       img,       {box.x1, box.y1 - fontScale * 12},       {box.x1 + (unsigned short)text.length() * fontScale * 9, box.y1},       rectColor,       -1     );     cv::putText(img, text, {box.x1,box.y1}, cv::FONT_HERSHEY_PLAIN, fontScale, {255,255,255}, 2);   } } \/\/ ...<\/code><\/pre>\n<p>\u0418 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   highlightBoxes(img, boxes);   cv::imshow(\"Result\", img);   \/\/ return<\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b25\/f3d\/b21\/b25f3db21c61f3a1695ec77380c4a37e.png\" alt=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\" title=\"\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\" width=\"384\" height=\"402\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b25\/f3d\/b21\/b25f3db21c61f3a1695ec77380c4a37e.png\"\/><\/p>\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/figcaption><\/div>\n<\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0432\u0435\u0442\u043e\u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044e \u043b\u0438\u0446 <a href=\"https:\/\/github.com\/dsc-sgu\/face-recognition\/tree\/apps-c-cpp\/frui\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0441\u043f\u043b\u0438<\/a>. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9b2\/ff7\/137\/9b2ff71378cf667458346316aba104e4.png\" width=\"64\" height=\"64\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9b2\/ff7\/137\/9b2ff71378cf667458346316aba104e4.png\"\/><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/732670\/\"> https:\/\/habr.com\/ru\/articles\/732670\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><\/figure>\n<p>\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u043e\u0432 \u043f\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0435\u0439 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u043d\u0430 Python. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0440\u0430\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u0437\u0434\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438 \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0439 \u044f\u0437\u044b\u043a \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, C++. \u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043d\u0430 C++ \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435\u0439\u0440\u043e\u0441\u0435\u0442\u0438 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 <em>YOLOv8 \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446<\/em> \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <em>PyTorch<\/em>.<\/p>\n<h2>\u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438<\/h2>\n<p>PyTorch \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 C++-\u0432\u0435\u0440\u0441\u0438\u044e \u0441\u0432\u043e\u0435\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <strong>LibTorch<\/strong>. \u0415\u0451 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/pytorch.org\/get-started\/locally\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0430\u0442\u044c<\/a> (<em>cxx11 ABI<\/em>) \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0440\u044f\u0434\u043e\u043c \u0441 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439:<\/p>\n<pre><code class=\"bash\">wget https:\/\/download.pytorch.org\/libtorch\/cpu\/libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip unzip libtorch-cxx11-abi-shared-with-deps-2.0.0%2Bcpu.zip<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 <strong>OpenCV<\/strong>. \u0415\u0451 \u0441\u0442\u043e\u0438\u0442 <a href=\"https:\/\/docs.opencv.org\/4.x\/df\/d65\/tutorial_table_of_content_introduction.html\" rel=\"noopener noreferrer nofollow\">\u0441\u043a\u0430\u0447\u0430\u0442\u044c<\/a> \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c:<\/p>\n<pre><code class=\"bash\">wget https:\/\/github.com\/opencv\/opencv\/archive\/4.7.0.zip unzip opencv-4.7.0.zip &amp;&amp; cd opencv-4.7.0 mkdir build &amp;&amp; cd build cmake .. cmake --build . sudo make install<\/code><\/pre>\n<h2>\u0412\u0435\u0441\u0430<\/h2>\n<p>\u041e\u0431\u044b\u0447\u043d\u044b\u0439 \u0444\u043e\u0440\u043c\u0430\u0442 \u0432\u0435\u0441\u043e\u0432 \u043c\u043e\u0434\u0435\u043b\u0438 PyTorch \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 <code>TorchScript<\/code>. \u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"python\"># model_export.py import torch  MODEL_PATH = 'model.pt' MODEL_INPUT_SHAPE = (1,3,640,640)  model = torch.load(MODEL_PATH) example = torch.randn(MODEL_INPUT_SHAPE) model(example) # model warmup model_traced = torch.jit.trace(model, example) model_traced.save('model_traced.torchscript')<\/code><\/pre>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c YOLOv8 \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0446 \u043e\u0442 Ultralytics, \u0435\u0451 \u043c\u043e\u0436\u043d\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0433\u043e\u0442\u043e\u0432\u044b\u0445 \u043c\u043e\u0434\u0443\u043b\u0435\u0439:<\/p>\n<pre><code class=\"bash\">pip3 install ultralytics pip3 install gdown gdown 1jG8_C_P0SbnzYROZORe7CiJO6oMgp7eZ # yolov8n-face.pt<\/code><\/pre>\n<pre><code class=\"python\"># model_export_yolov8face.py import torch from ultralytics import YOLO  model = YOLO('yolov8n-face.pt') model.export(format='torchscript')<\/code><\/pre>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435<\/h2>\n<h3>\u0418\u043d\u043a\u043b\u044e\u0434\u044b, \u043b\u0438\u0431\u044b \u0438 \u043b\u0438\u043d\u043a\u043e\u0432\u043a\u0430<\/h3>\n<p>\u0414\u043b\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0443\u0442\u044c \u0434\u043e icnlude- \u0438 lib-\u0444\u0430\u0439\u043b\u043e\u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a LibTorch \u0438 OpenCV, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043b\u0438\u043d\u043a\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 lib-\u0444\u0430\u0439\u043b\u044b (\u043e\u043d\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438; \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u043e\u0438\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c IDE, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Visual Studio Code):<\/p>\n<pre><code class=\"bash\"># ... -I\/usr\/local\/include\/opencv4 -I\/path\/to\/libtorch\/include -L\/path\/to\/libtorch\/lib # ... -o main.o # ... -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio -ltorch -ltorch_cpu -lc10<\/code><\/pre>\n<h3>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043c\u043e\u0434\u0435\u043b\u0438<\/h3>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp #include &lt;torch\/script.h> #include &lt;opencv2\/opencv.hpp>  int main() {    torch::jit::script::Module     model = torch::jit::load(\"yolov8n-face.torchscript\");      return 0; }<\/code><\/pre>\n<h3>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438<\/h3>\n<p>LibTorch \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0435\u0439 \u0432\u0435\u043a\u0442\u043e\u0440 \u0438\u0437 <code>torch::jit::IValue<\/code> \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>torch::jit::IValue<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u044d\u043a\u0441\u043f\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b (\u0442\u0435\u043d\u0437\u043e\u0440 <code>.toTensor()<\/code>, \u043a\u043e\u0440\u0442\u0435\u0436 <code>.toTuple()<\/code> \u0438\u043b\u0438 \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0439).<\/p>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c-\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0442\u0435\u043d\u0437\u043e\u0440 \u0441 \u0444\u043e\u0440\u043c\u043e\u0439 <code>(B, 3, 640, 640)<\/code> \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043d\u0437\u043e\u0440 \u0441 \u0444\u043e\u0440\u043c\u043e\u0439 <code>(1, 5, 8400)<\/code>, \u0433\u0434\u0435 5 \u0442\u0435\u043d\u0437\u043e\u0440\u043e\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 8400 \u0446\u0435\u043b\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <em>cx<\/em>, <em>cy<\/em>, <em>w<\/em>, <em>h<\/em> \u0438 \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 <em>conf<\/em> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043e\u043a\u0441\u0430 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0432\u044b\u0445\u043e\u0434\u043e\u0432:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   cv::Mat img = cv::imread(source);   cv::cvtColor(img, img, cv::COLOR_BGR2RGB);   cv::normalize(img, img, 0.0, 1.0, cv::NORM_MINMAX, CV_32F);   vector&lt;torch::jit::IValue> inputs = {     torch::from_blob(       imgNorm.data,       {640,640,3},       torch::kFloat32     ).permute({2,0,1}).unsqueeze(0)   };   at::Tensor outputs = model.forward(inputs).toTensor();   \/\/ ...<\/code><\/pre>\n<h3>\u0414\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0438\u0446<\/h3>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u0438-\u043f\u0440\u0438\u043c\u0435\u0440\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 <code>Box<\/code> \u0434\u043b\u044f \u0431\u043e\u043a\u0441\u043e\u0432 \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... class Box {     public:         int x1, y1, x2, y2;         float conf;         Box(int x1, int y1, int x2,  int y2, float conf) {             this->x1 = x1;             this->y1 = y1;             this->x2 = x2;             this->y2 = y2;             this->conf = conf;         } }; \/\/ ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>getBoxes<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0431\u043e\u043a\u0441\u044b \u043e\u0442\u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 <em>conf<\/em> \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e <em>Non Maximum Supression<\/em> (NMS):<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... vector&lt;Box> getBoxes(     at::Tensor &amp;outputs,     float confThres = 0.25,     float iouThres = 0.15 ) {     vector&lt;Box> candidates;     for (unsigned short ibatch = 0; ibatch &lt; outputs.sizes()[0]; ibatch++) {         for (unsigned short ibox = 0; ibox &lt; outputs.sizes()[2]; ibox++) {             float conf = outputs[ibatch][4][ibox].item&lt;float>();             if (conf >= confThres) {                 unsigned short                     cx = outputs[ibatch][0][ibox].item&lt;int>(),                     cy = outputs[ibatch][1][ibox].item&lt;int>(),                     w = outputs[ibatch][2][ibox].item&lt;int>(),                     h = outputs[ibatch][3][ibox].item&lt;int>();                 unsigned short                     x1 = cx - w \/ 2,                     y1 = cy - h \/ 2,                     x2 = cx + w \/ 2,                     y2 = cy + h \/ 2;                 candidates.push_back(Box(x1,y1,x2,y2,conf));             }         }     }     sort(candidates.begin(), candidates.end(), [](Box b1, Box b2){return b1.conf > b2.conf;});     vector&lt;Box> boxes = nms(candidates, iouThres);     return boxes; } \/\/ ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>nms<\/code> , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043a\u0441\u044b-\u043a\u0430\u043d\u0434\u0438\u0442\u0430\u0442\u044b \u0438 \u0441\u043e\u043f\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e <code>iou<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043c\u0435\u0442\u0440\u0438\u043a\u0443 IoU (<em>Intersection Over Union<\/em>):<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... float iou(Box &amp;fb, Box &amp;sb) {     float inter = max(min(fb.x2, sb.x2) - min(fb.x1, sb.x1), 0) * max(min(fb.y2, sb.y2) - min(fb.y1, sb.y1), 0);     float union_ = (fb.x2-fb.x1)*(fb.y2-fb.y1) + (sb.x2-sb.x1)*(sb.y2-sb.y1) - inter;     return inter \/ union_; }  vector&lt;Box> nms(vector&lt;Box> &amp;boxes, float iouThres) {     vector&lt;Box> supBoxes;     for (Box box: boxes) {         bool valid = true;         for (Box supBox: supBoxes) {             if (iou(box, supBox) > iouThres) {                 valid = false;                 break;             }         }         if (valid == true) {             supBoxes.push_back(box);         }     }     return supBoxes; } \/\/ getBoxes<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0431\u043e\u043a\u0441\u044b \u0434\u0435\u0442\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   vector&lt;Box> boxes = getBoxes(outputs);   \/\/ ...<\/code><\/pre>\n<p>\u0410 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0434\u043b\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043a\u0441\u043e\u0432 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">\/\/ detect.h \/\/ ... void highlightBoxes(cv::Mat &amp;img, vector&lt;Box> &amp;boxes) {   cv::Scalar rectColor(0,192,0);   unsigned short fontScale = 2, confPrecis = 2;    for (Box box: boxes) {     string text = to_string(box.conf);     cv::rectangle(img, {box.x1,box.y1}, {box.x2,box.y2}, rectColor, 2);     cv::rectangle(       img,       {box.x1, box.y1 - fontScale * 12},       {box.x1 + (unsigned short)text.length() * fontScale * 9, box.y1},       rectColor,       -1     );     cv::putText(img, text, {box.x1,box.y1}, cv::FONT_HERSHEY_PLAIN, fontScale, {255,255,255}, 2);   } } \/\/ ...<\/code><\/pre>\n<p>\u0418 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">\/\/ main.cpp   \/\/ ...   highlightBoxes(img, boxes);   cv::imshow(\"Result\", img);   \/\/ return<\/code><\/pre>\n<figure class=\"\">\n<div><figcaption>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430<\/figcaption><\/div>\n<\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0432\u0435\u0442\u043e\u043a \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043e \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u043d\u0438\u044e \u043b\u0438\u0446 <a href=\"https:\/\/github.com\/dsc-sgu\/face-recognition\/tree\/apps-c-cpp\/frui\" rel=\"noopener noreferrer nofollow\">\u0420\u0430\u0441\u043f\u043b\u0438<\/a>. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430 \u0447\u0442\u043e-\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0435.<\/p>\n<figure class=\"\"><\/figure>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/732670\/\"> https:\/\/habr.com\/ru\/articles\/732670\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-409096","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/409096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=409096"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/409096\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=409096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=409096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=409096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}