{"action":"created","issue":{"url":"https://api.github.com/repos/opencv/opencv/issues/11310","repository_url":"https://api.github.com/repos/opencv/opencv","labels_url":"https://api.github.com/repos/opencv/opencv/issues/11310/labels{/name}","comments_url":"https://api.github.com/repos/opencv/opencv/issues/11310/comments","events_url":"https://api.github.com/repos/opencv/opencv/issues/11310/events","html_url":"https://github.com/opencv/opencv/issues/11310","id":314342884,"number":11310,"title":"YOLOv3 in Opencv","user":{"login":"SoullessStone","id":10846282,"avatar_url":"https://avatars0.githubusercontent.com/u/10846282?v=4","gravatar_id":"","url":"https://api.github.com/users/SoullessStone","html_url":"https://github.com/SoullessStone","followers_url":"https://api.github.com/users/SoullessStone/followers","following_url":"https://api.github.com/users/SoullessStone/following{/other_user}","gists_url":"https://api.github.com/users/SoullessStone/gists{/gist_id}","starred_url":"https://api.github.com/users/SoullessStone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SoullessStone/subscriptions","organizations_url":"https://api.github.com/users/SoullessStone/orgs","repos_url":"https://api.github.com/users/SoullessStone/repos","events_url":"https://api.github.com/users/SoullessStone/events{/privacy}","received_events_url":"https://api.github.com/users/SoullessStone/received_events","type":"User","site_admin":false},"labels":[{"id":635087908,"url":"https://api.github.com/repos/opencv/opencv/labels/category:%20dnn","name":"category: dnn","color":"ededed","default":false},{"id":241023000,"url":"https://api.github.com/repos/opencv/opencv/labels/feature","name":"feature","color":"84b6eb","default":false}],"state":"closed","locked":false,"assignee":{"login":"dkurt","id":25801568,"avatar_url":"https://avatars0.githubusercontent.com/u/25801568?v=4","gravatar_id":"","url":"https://api.github.com/users/dkurt","html_url":"https://github.com/dkurt","followers_url":"https://api.github.com/users/dkurt/followers","following_url":"https://api.github.com/users/dkurt/following{/other_user}","gists_url":"https://api.github.com/users/dkurt/gists{/gist_id}","starred_url":"https://api.github.com/users/dkurt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkurt/subscriptions","organizations_url":"https://api.github.com/users/dkurt/orgs","repos_url":"https://api.github.com/users/dkurt/repos","events_url":"https://api.github.com/users/dkurt/events{/privacy}","received_events_url":"https://api.github.com/users/dkurt/received_events","type":"User","site_admin":false},"assignees":[{"login":"dkurt","id":25801568,"avatar_url":"https://avatars0.githubusercontent.com/u/25801568?v=4","gravatar_id":"","url":"https://api.github.com/users/dkurt","html_url":"https://github.com/dkurt","followers_url":"https://api.github.com/users/dkurt/followers","following_url":"https://api.github.com/users/dkurt/following{/other_user}","gists_url":"https://api.github.com/users/dkurt/gists{/gist_id}","starred_url":"https://api.github.com/users/dkurt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkurt/subscriptions","organizations_url":"https://api.github.com/users/dkurt/orgs","repos_url":"https://api.github.com/users/dkurt/repos","events_url":"https://api.github.com/users/dkurt/events{/privacy}","received_events_url":"https://api.github.com/users/dkurt/received_events","type":"User","site_admin":false}],"milestone":null,"comments":13,"created_at":"2018-04-14T16:38:42Z","updated_at":"2018-05-11T10:06:56Z","closed_at":"2018-04-20T17:57:30Z","author_association":"NONE","body":"##### System information (version)\r\n- OpenCV => 3.4.0\r\n- Operating System / Platform => Windows 10 64 Bit\r\n- Compiler => Visual Studio v15.4.3\r\n\r\n##### Detailed description\r\nI tried to do the opencv-tutorial on yolo (https://docs.opencv.org/3.4.1/da/d9d/tutorial_dnn_yolo.html), which works perfectly on yolov2 weights and config. I also tried to use yolov3 weights and configs, which  gave me an error when calling \"readNetFromDarknet(modelConfiguration, modelBinary);\". I used the \"official\" files (more detail in the reproduce section), and first tried them out successfully in a \"not-opencv\"-yolo environment on ubuntu (see tutorial located here: https://pjreddie.com/darknet/yolo/).\r\nI posted a question on SO (https://stackoverflow.com/questions/49777632/opencv-using-yolov3), on which I got an answer telling me yolov3 is currently not supported in opencv and telling me to create an issue here.\r\n\r\nThis is my first github-ticket. If there's something missing, just ask.\r\n\r\n##### Steps to reproduce\r\nThe full code is at the bottom of the issue or here: https://docs.opencv.org/3.4.1/da/d9d/tutorial_dnn_yolo.html (I just added the stdafx.h-include for it to work on my setup).\r\n\r\nRun this with the following files:\r\n- cfg from: https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg\r\n- model from: https://pjreddie.com/media/files/yolov3.weights\r\n- class_names from: https://github.com/pjreddie/darknet/blob/master/data/coco.names\r\nreferenced like this in the params:\r\n-source=some.MP4  -cfg=yolov3.cfg -model=yolov3.weights   -class_names=coco.names\r\nthe source can also be an image.\r\n\r\n```\r\n// Brief Sample of using OpenCV dnn module in real time with device capture, video and image.\r\n// VIDEO DEMO: https://www.youtube.com/watch?v=NHtRlndE2cg\r\n#include \"stdafx.h\";\r\n#include <opencv2/dnn.hpp>\r\n#include <opencv2/dnn/shape_utils.hpp>\r\n#include <opencv2/imgproc.hpp>\r\n#include <opencv2/highgui.hpp>\r\n#include <fstream>\r\n#include <iostream>\r\nusing namespace std;\r\nusing namespace cv;\r\nusing namespace cv::dnn;\r\nstatic const char* about =\r\n\"This sample uses You only look once (YOLO)-Detector (https://arxiv.org/abs/1612.08242) to detect objects on camera/video/image.\\n\"\r\n\"Models can be downloaded here: https://pjreddie.com/darknet/yolo/\\n\"\r\n\"Default network is 416x416.\\n\"\r\n\"Class names can be downloaded here: https://github.com/pjreddie/darknet/tree/master/data\\n\";\r\nstatic const char* params =\r\n\"{ help           | false | print usage         }\"\r\n\"{ cfg            |       | model configuration }\"\r\n\"{ model          |       | model weights       }\"\r\n\"{ camera_device  | 0     | camera device number}\"\r\n\"{ source         |       | video or image for detection}\"\r\n\"{ out            |       | path to output video file}\"\r\n\"{ fps            | 3     | frame per second }\"\r\n\"{ style          | box   | box or line style draw }\"\r\n\"{ min_confidence | 0.24  | min confidence      }\"\r\n\"{ class_names    |       | File with class names, [PATH-TO-DARKNET]/data/coco.names }\";\r\nint main(int argc, char** argv)\r\n{\r\n\tCommandLineParser parser(argc, argv, params);\r\n\tif (parser.get<bool>(\"help\"))\r\n\t{\r\n\t\tcout << about << endl;\r\n\t\tparser.printMessage();\r\n\t\treturn 0;\r\n\t}\r\n\tString modelConfiguration = parser.get<String>(\"cfg\");\r\n\tString modelBinary = parser.get<String>(\"model\");\r\n\tdnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);\r\n\tif (net.empty())\r\n\t{\r\n\t\tcerr << \"Can't load network by using the following files: \" << endl;\r\n\t\tcerr << \"cfg-file:     \" << modelConfiguration << endl;\r\n\t\tcerr << \"weights-file: \" << modelBinary << endl;\r\n\t\tcerr << \"Models can be downloaded here:\" << endl;\r\n\t\tcerr << \"https://pjreddie.com/darknet/yolo/\" << endl;\r\n\t\texit(-1);\r\n\t}\r\n\tVideoCapture cap;\r\n\tVideoWriter writer;\r\n\tint codec = CV_FOURCC('M', 'J', 'P', 'G');\r\n\tdouble fps = parser.get<float>(\"fps\");\r\n\tif (parser.get<String>(\"source\").empty())\r\n\t{\r\n\t\tint cameraDevice = parser.get<int>(\"camera_device\");\r\n\t\tcap = VideoCapture(cameraDevice);\r\n\t\tif (!cap.isOpened())\r\n\t\t{\r\n\t\t\tcout << \"Couldn't find camera: \" << cameraDevice << endl;\r\n\t\t\treturn -1;\r\n\t\t}\r\n\t}\r\n\telse\r\n\t{\r\n\t\tcap.open(parser.get<String>(\"source\"));\r\n\t\tif (!cap.isOpened())\r\n\t\t{\r\n\t\t\tcout << \"Couldn't open image or video: \" << parser.get<String>(\"video\") << endl;\r\n\t\t\treturn -1;\r\n\t\t}\r\n\t}\r\n\tif (!parser.get<String>(\"out\").empty())\r\n\t{\r\n\t\twriter.open(parser.get<String>(\"out\"), codec, fps, Size((int)cap.get(CAP_PROP_FRAME_WIDTH), (int)cap.get(CAP_PROP_FRAME_HEIGHT)), 1);\r\n\t}\r\n\tvector<String> classNamesVec;\r\n\tifstream classNamesFile(parser.get<String>(\"class_names\").c_str());\r\n\tif (classNamesFile.is_open())\r\n\t{\r\n\t\tstring className = \"\";\r\n\t\twhile (std::getline(classNamesFile, className))\r\n\t\t\tclassNamesVec.push_back(className);\r\n\t}\r\n\tString object_roi_style = parser.get<String>(\"style\");\r\n\tfor (;;)\r\n\t{\r\n\t\tMat frame;\r\n\t\tcap >> frame; // get a new frame from camera/video or read image\r\n\t\tif (frame.empty())\r\n\t\t{\r\n\t\t\twaitKey();\r\n\t\t\tbreak;\r\n\t\t}\r\n\t\tif (frame.channels() == 4)\r\n\t\t\tcvtColor(frame, frame, COLOR_BGRA2BGR);\r\n\t\tMat inputBlob = blobFromImage(frame, 1 / 255.F, Size(416, 416), Scalar(), true, false); //Convert Mat to batch of images\r\n\t\tnet.setInput(inputBlob, \"data\");                   //set the network input\r\n\t\tMat detectionMat = net.forward(\"detection_out\");   //compute output\r\n\t\tvector<double> layersTimings;\r\n\t\tdouble tick_freq = getTickFrequency();\r\n\t\tdouble time_ms = net.getPerfProfile(layersTimings) / tick_freq * 1000;\r\n\t\tputText(frame, format(\"FPS: %.2f ; time: %.2f ms\", 1000.f / time_ms, time_ms),\r\n\t\t\tPoint(20, 20), 0, 0.5, Scalar(0, 0, 255));\r\n\t\tfloat confidenceThreshold = parser.get<float>(\"min_confidence\");\r\n\t\tfor (int i = 0; i < detectionMat.rows; i++)\r\n\t\t{\r\n\t\t\tconst int probability_index = 5;\r\n\t\t\tconst int probability_size = detectionMat.cols - probability_index;\r\n\t\t\tfloat *prob_array_ptr = &detectionMat.at<float>(i, probability_index);\r\n\t\t\tsize_t objectClass = max_element(prob_array_ptr, prob_array_ptr + probability_size) - prob_array_ptr;\r\n\t\t\tfloat confidence = detectionMat.at<float>(i, (int)objectClass + probability_index);\r\n\t\t\tif (confidence > confidenceThreshold)\r\n\t\t\t{\r\n\t\t\t\tfloat x_center = detectionMat.at<float>(i, 0) * frame.cols;\r\n\t\t\t\tfloat y_center = detectionMat.at<float>(i, 1) * frame.rows;\r\n\t\t\t\tfloat width = detectionMat.at<float>(i, 2) * frame.cols;\r\n\t\t\t\tfloat height = detectionMat.at<float>(i, 3) * frame.rows;\r\n\t\t\t\tPoint p1(cvRound(x_center - width / 2), cvRound(y_center - height / 2));\r\n\t\t\t\tPoint p2(cvRound(x_center + width / 2), cvRound(y_center + height / 2));\r\n\t\t\t\tRect object(p1, p2);\r\n\t\t\t\tScalar object_roi_color(0, 255, 0);\r\n\t\t\t\tif (object_roi_style == \"box\")\r\n\t\t\t\t{\r\n\t\t\t\t\trectangle(frame, object, object_roi_color);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\tPoint p_center(cvRound(x_center), cvRound(y_center));\r\n\t\t\t\t\tline(frame, object.tl(), p_center, object_roi_color, 1);\r\n\t\t\t\t}\r\n\t\t\t\tString className = objectClass < classNamesVec.size() ? classNamesVec[objectClass] : cv::format(\"unknown(%d)\", objectClass);\r\n\t\t\t\tString label = format(\"%s: %.2f\", className.c_str(), confidence);\r\n\t\t\t\tint baseLine = 0;\r\n\t\t\t\tSize labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);\r\n\t\t\t\trectangle(frame, Rect(p1, Size(labelSize.width, labelSize.height + baseLine)),\r\n\t\t\t\t\tobject_roi_color, FILLED);\r\n\t\t\t\tputText(frame, label, p1 + Point(0, labelSize.height),\r\n\t\t\t\t\tFONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0));\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (writer.isOpened())\r\n\t\t{\r\n\t\t\twriter.write(frame);\r\n\t\t}\r\n\t\timshow(\"YOLO: Detections\", frame);\r\n\t\tif (waitKey(1) >= 0) break;\r\n\t}\r\n\treturn 0;\r\n} // main\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/opencv/opencv/issues/comments/388321134","html_url":"https://github.com/opencv/opencv/issues/11310#issuecomment-388321134","issue_url":"https://api.github.com/repos/opencv/opencv/issues/11310","id":388321134,"user":{"login":"dkurt","id":25801568,"avatar_url":"https://avatars0.githubusercontent.com/u/25801568?v=4","gravatar_id":"","url":"https://api.github.com/users/dkurt","html_url":"https://github.com/dkurt","followers_url":"https://api.github.com/users/dkurt/followers","following_url":"https://api.github.com/users/dkurt/following{/other_user}","gists_url":"https://api.github.com/users/dkurt/gists{/gist_id}","starred_url":"https://api.github.com/users/dkurt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dkurt/subscriptions","organizations_url":"https://api.github.com/users/dkurt/orgs","repos_url":"https://api.github.com/users/dkurt/repos","events_url":"https://api.github.com/users/dkurt/events{/privacy}","received_events_url":"https://api.github.com/users/dkurt/received_events","type":"User","site_admin":false},"created_at":"2018-05-11T10:06:55Z","updated_at":"2018-05-11T10:06:55Z","author_association":"MEMBER","body":"@IgMart, there are three output layers. All of them can be found by [getUnconnectedOutLayers](https://docs.opencv.org/master/db/d30/classcv_1_1dnn_1_1Net.html#ae62a73984f62c49fd3e8e689405b056a)."}}
{"actor":{"display_login":"dkurt"}}