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
| #include <layer.h> #include <net.h>
#include <opencv2/opencv.hpp>
#include <float.h> #include <stdio.h> #include <vector>
#include "NcnnUtil.hpp" #include "cap.h"
int main() { ncnn::Net yolov5; yolov5.opt.use_vulkan_compute = true; if (yolov5.load_param("best-sim-opt.param")) return -1; if (yolov5.load_model("best-sim-opt.bin")) return -1;
const int imgsize = 640; const float probThreshold = 0.25f; const float nms_threshold = 0.5f; int gamex = 2560; int gamey = 1440;
capture cap(gamex, gamey, imgsize, imgsize, "窗口名");
ncnn::Mat anchors1(6); anchors1[0] = 10.f; anchors1[1] = 13.f; anchors1[2] = 16.f; anchors1[3] = 30.f; anchors1[4] = 33.f; anchors1[5] = 23.f; ncnn::Mat anchors2(6); anchors2[0] = 30.f; anchors2[1] = 61.f; anchors2[2] = 62.f; anchors2[3] = 45.f; anchors2[4] = 59.f; anchors2[5] = 119.f; ncnn::Mat anchors3(6); anchors3[0] = 116.f; anchors3[1] = 90.f; anchors3[2] = 156.f; anchors3[3] = 198.f; anchors3[4] = 373.f; anchors3[5] = 326.f;
const float normalVals[3] = { 1 / 255.f, 1 / 255.f, 1 / 255.f };
while (true) { auto img = (BYTE*)cap.cap();
ncnn::Mat inPad = ncnn::Mat::from_pixels(img, ncnn::Mat::PIXEL_BGR2RGB, imgsize, imgsize); inPad.substract_mean_normalize(0, normalVals);
auto t1 = std::chrono::steady_clock::now();
ncnn::Extractor ex = yolov5.create_extractor(); ex.input("images", inPad);
std::vector<BoundingBox> proposals; { ncnn::Mat out; ex.extract("output", out); std::vector<BoundingBox> objects8; generate_proposals(anchors1, 8, inPad, out, probThreshold, objects8); proposals.insert(proposals.end(), objects8.begin(), objects8.end()); } { ncnn::Mat out; ex.extract("output1", out); std::vector<BoundingBox> objects16; generate_proposals(anchors2, 16, inPad, out, probThreshold, objects16); proposals.insert(proposals.end(), objects16.begin(), objects16.end()); } { ncnn::Mat out; ex.extract("output2", out); std::vector<BoundingBox> objects32; generate_proposals(anchors3, 32, inPad, out, probThreshold, objects32); proposals.insert(proposals.end(), objects32.begin(), objects32.end()); }
auto t2 = std::chrono::steady_clock::now(); double dr_ms = std::chrono::duration<double, std::milli>(t2 - t1).count(); cout <<"infer time : "<< dr_ms << "ms" << endl; vector<BoundingBox> newbox = cpu_nms(proposals, nms_threshold);
cv::Mat img = cv::Mat(imgsize, imgsize, CV_8UC3, img); if (!newbox.empty()) { for (const BoundingBox& detection : newbox) { cv::rectangle(img, cv::Point((int)detection.left, (int)detection.top), cv::Point((int)detection.right, (int)detection.bottom), cv::Scalar(0, 255, 0), 1); cv::Point screen_center = cv::Point(img.cols / 2, img.rows / 2); cv::Point center = cv::Point((detection.left + detection.right) / 2, (detection.top + detection.bottom) / 2); double x_distance = center.x - screen_center.x; double y_distance = center.y - screen_center.y; std::cout << "(x=" << x_distance << ", y=" << y_distance << ")" << std::endl; } } cv::imshow("c", img); cv::waitKey(1); } return 0; }
|