Visual C++ darknet opencv cuda yolo4測試即時影像、柴犬影片識別。「Vcpkg安裝darknet cuda並以C++ yolo v3 tiny辨識柴犬白狗動作影片」https://www.youtube.com/watch?v=ZODKCQABcKM
#include <iostream> #include <fstream> #include <opencv2/opencv.hpp> #include <vector> using namespace cv; using namespace std; using namespace cv::dnn; vector<string> coco; void name_list() { fstream fin("coco.names", ios::in); string tmp = ""; while (getline(fin, tmp)) coco.push_back(tmp); fin.close(); cout << coco.size() << endl; } int main() { cout << "參考https://pjreddie.com/darknet/yolo/ \n"; name_list(); cv::setUseOptimized(1); VideoCapture cap("shiba_turns_around.mp4"); cap.set(CAP_PROP_FRAME_WIDTH, 1280); cap.set(CAP_PROP_FRAME_HEIGHT, 720); string model = "yolov4.weights"; string config = "yolov4.cfg"; Net network = readNet(model, config, "Darknet"); network.setPreferableBackend(DNN_BACKEND_CUDA); network.setPreferableTarget(DNN_TARGET_CUDA); string wname = "Darknet yolo4 "; namedWindow(wname); Vec3b colors[] = { Vec3b(255,255,0), Vec3b(0, 255,0), Vec3b(0,255,255), Vec3b(255,0,255) }; int i = 0; while (1) { Mat im; cap >> im; if (!im.data) break; Mat blobFromIm; bool swapRB = true; blobFromImage(im, blobFromIm, 1, Size(416, 416), Scalar(), swapRB, false); //Size(608, 608) or (416, 416) float scale = 1.0 / 255.0; Scalar mean = 0; network.setInput(blobFromIm, "", scale, mean); Mat detection = network.forward(); // rows represent number of detected objects int rowsNoOfDetection = detection.rows; // [x ; y ; w; h; class 0 ; class 1 ; class 2 ;....] // int xywh_scores = detection.cols; // Loop over number of detected object. for (int j = 0; j < rowsNoOfDetection; ++j) { // for each row, the score is from element 5 up to end Mat scores = detection.row(j).colRange(5, xywh_scores); Point PositionOfMax; double confidence; //This function find indexes of min and max confidence and related index. minMaxLoc(scores, 0, &confidence, 0, &PositionOfMax); if (confidence > 0.01) { // [x ; y ; w; h; int x = (int)(detection.at<float>(j, 0) * im.cols); int y = (int)(detection.at<float>(j, 1) * im.rows); int w = (int)(detection.at<float>(j, 2) * im.cols); int h = (int)(detection.at<float>(j, 3) * im.rows); int left = x - w / 2; int up = y - h / 2; stringstream ss; int i = PositionOfMax.x; ss << coco[i] << ",prob=" << 100 * confidence << "%"; string what = ss.str(); cout << '#' << j << ":[" << x << ',' << y << ',' << w << ',' << h << "]:"; cout<< what << endl; Vec3b color = confidence * colors[i % 4]; rectangle(im, Rect(left, up, w, h), color, 2, LINE_8, 0); int baseLine = 0; Size labelSize = getTextSize(what, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); rectangle(im, Rect(Point(left, up - labelSize.height), Size(labelSize.width, labelSize.height + baseLine)), color, -1); putText(im, what, Point(left, up), FONT_HERSHEY_SIMPLEX, 0.5, Scalar()); } } imshow(wname, im); if (waitKey(1) == 27) { imwrite("doggy.png", im); break; } } destroyAllWindows(); cap.release(); return 0; }
沒有留言:
張貼留言