Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yolov10_det改為yolov10_det_webcam相關問題 #37

Open
HsiaoCH opened this issue Oct 29, 2024 · 0 comments
Open

yolov10_det改為yolov10_det_webcam相關問題 #37

HsiaoCH opened this issue Oct 29, 2024 · 0 comments

Comments

@HsiaoCH
Copy link

HsiaoCH commented Oct 29, 2024


        public void yolov10_det_webcam(string model_path, string device)
        {
            // Initialize webcam
            VideoCapture capture = new VideoCapture(0);  // 0 is usually the default camera index
            if (!capture.IsOpened())
            {
                Slog.ERROR("Failed to open the webcam.");
                return;
            }

            // -------- Step 1. Initialize OpenVINO Runtime Core --------
            Core core = new Core();
            Slog.INFO("1. Initialized OpenVINO Runtime Core.");

            // -------- Step 2. Read inference model --------
            Model model = core.read_model(model_path);
            Slog.INFO("2. Read inference model.");
            OvExtensions.printf_model_info(model);

            // -------- Step 3. Load the model onto the device --------
            CompiledModel compiled_model = core.compile_model(model, device);
            Slog.INFO("3. Loaded model onto the device.");

            // -------- Step 4. Create an infer request --------
            InferRequest infer_request = compiled_model.create_infer_request();
            Slog.INFO("4. Created infer request.");

            Mat frame = new Mat();
            Mat max_image;
            Mat input_mat;
            Rect roi;
            Tensor input_tensor;
            Shape input_shape;
            Tensor output_tensor;
            Rect box;
            while (true)
            {
                // Capture frame from webcam
                capture.Read(frame);
                if (frame.Empty()) continue;

                // -------- Step 5. Process input images --------
                int max_image_length = frame.Cols > frame.Rows ? frame.Cols : frame.Rows;
                max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
                roi = new Rect(0, 0, frame.Cols, frame.Rows);
                frame.CopyTo(new Mat(max_image, roi));
                float factor = (float)(max_image_length / 640.0);

                // -------- Step 6. Set up input data --------
                input_tensor = infer_request.get_input_tensor();
                input_shape = input_tensor.get_shape();
                input_mat = CvDnn.BlobFromImage(max_image, 1.0 / 255.0, new OpenCvSharp.Size(input_shape[2], input_shape[3]), new Scalar(0, 0, 0), true, false);
                float[] input_data = new float[input_shape[1] * input_shape[2] * input_shape[3]];
                Marshal.Copy(input_mat.Ptr(0), input_data, 0, input_data.Length);

                input_tensor.set_data<float>(input_data);

                // -------- Step 7. Do inference synchronously --------
                infer_request.infer();

                // -------- Step 8. Get infer result data --------
                output_tensor = infer_request.get_output_tensor();
                int output_length = (int)output_tensor.get_size();
                float[] output_data = output_tensor.get_data<float>(output_length);

                // -------- Step 9. Process results --------
                List<Rect> position_boxes = new List<Rect>();
                List<int> class_ids = new List<int>();
                List<float> confidences = new List<float>();

                for (int i = 0; i < output_data.Length / 6; i++)
                {
                    int s = 6 * i;
                    if (output_data[s + 4] > 0.5)
                    {
                        float cx = output_data[s + 0];
                        float cy = output_data[s + 1];
                        float dx = output_data[s + 2];
                        float dy = output_data[s + 3];
                        int x = (int)((cx) * factor);
                        int y = (int)((cy) * factor);
                        int width = (int)((dx - cx) * factor);
                        int height = (int)((dy - cy) * factor);
                        box = new Rect(x, y, width, height);

                        position_boxes.Add(box);
                        class_ids.Add((int)output_data[s + 5]);
                        confidences.Add(output_data[s + 4]);
                    }
                }

                // Draw detection results
                for (int i = 0; i < class_ids.Count; i++)
                {
                    int index = i;
                    Cv2.Rectangle(frame, position_boxes[index], new Scalar(0, 0, 255), 2, LineTypes.Link8);
                    Cv2.Rectangle(frame, new OpenCvSharp.Point(position_boxes[index].TopLeft.X, position_boxes[index].TopLeft.Y + 30),
                            new OpenCvSharp.Point(position_boxes[index].BottomRight.X, position_boxes[index].TopLeft.Y), new Scalar(255, 0, 255), -1);
                    Cv2.PutText(frame, class_ids[index] + "-" + confidences[index].ToString("0.00"),
                            new OpenCvSharp.Point(position_boxes[index].X, position_boxes[index].Y + 25),
                            HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 0), 2);
                }

                // Display the frame with detections
                Cv2.ImShow("YOLOv10 Webcam Detection", frame);
                if (Cv2.WaitKey(1) == 27) // Break on 'ESC'
                {
                    break;
                }

                input_mat?.Dispose();
                max_image?.Dispose();
                input_tensor = null;
                output_tensor = null;
            }

            frame.Dispose(); // Release frame at the end
            capture.Release();
            Cv2.DestroyAllWindows();
        }

你好,
先感謝你釋出這麼好用的資源
但我在使用時遇到了一點問題:
上列code是修改成支援webcam,期望進行連續yolov10偵測
在執行時遇到下列問題
在前幾秒時偵測皆為正常運行(如上圖)
但在幾秒後偵測框不正常出現,class_ids.Count固定是300(如下圖)
我想問題是與資源未釋放相關嗎?還是code用法有誤?

謝謝你

螢幕擷取畫面 2024-10-04 162615
螢幕擷取畫面 2024-10-04 162713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant