private void bWorker_DoWork(object sender, DoWorkEventArgs e) { //Loading YOLO config Darknet darknet = new Darknet(YoloCfgFile, YoloWeightsFile); var classNames = File.ReadAllLines(YoloNamesFile); //Capturing frames for (; ;) { Mat originalBackground = this.background.Clone(); Mat frame; frame = cam.RetrieveMat(); if (frame.Empty()) { Cv2.WaitKey(); return; } Mat inputBlob = CvDnn.BlobFromImage(frame, 1 / 255d, new OpenCvSharp.Size(544, 544), new Scalar(), true, false); var results = darknet.Detect(frame, 0.5f); foreach (var result in results) { var p1 = new OpenCvSharp.Point(result.x, result.y); var p2 = new OpenCvSharp.Point(result.x + result.w, result.y + result.h); if (CheckContoursAmount(contours, binary, p1, p2)) { //Formatting labels var label = $"{classNames[result.obj_id]}"; var textSize = Cv2.GetTextSize(label, HersheyFonts.HersheyTriplex, 0.5, 1, out var baseline); Cv2.Rectangle(originalBackground, p1, p2, Colors[result.obj_id], -1); Cv2.PutText( originalBackground, label, new OpenCvSharp.Point((int)(result.x + (result.w / 2.0) - (textSize.Width / 2.0)), (int)(result.y + (result.h / 2.0))), HersheyFonts.HersheyTriplex, 0.5, Scalar.Black); } } bWorker.ReportProgress(0, originalBackground); } }
public static void Main(string[] args) { // Initialise darknet with official yolov3 model using (var darknet = new Darknet("data/yolov3.cfg", "data/yolov3.weights")) { // Load all classnames to make the relationship between yolov3 model id and real names of objects var classNames = File.ReadAllLines("data/coco.names"); // Open dog.jpg and convert it to an OpenCV Mat Object var image = new Mat("data/dog.jpg"); // Try to detect all objects that model know with a thresh of 30% var results = darknet.Detect(image, 0.3f); // Print all results found by yolo foreach (var result in results) { Console.WriteLine($"{classNames[result.ObjId]} - {result.Prob * 100}%"); } Console.WriteLine("Press any key to quit the example :"); Console.ReadLine(); } }
public static List <NetResult> DetectObjects(Mat image) { return(_yoloDnnPicker.Detect(image)); }