From 21db2973c79d027aab4deab584a53aa8fa5b081d Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Thu, 14 Nov 2024 17:49:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=8F=AF=E4=BB=A5=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=BD=86=E6=98=AF=E5=A4=84=E7=90=86=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E6=B2=A1=E6=9C=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Matrox/onnx_running.cpp | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/Matrox/onnx_running.cpp b/Matrox/onnx_running.cpp index 10de9a0..c5e6892 100644 --- a/Matrox/onnx_running.cpp +++ b/Matrox/onnx_running.cpp @@ -9,10 +9,13 @@ //#include // 添加此行 #include #include +#include +#include // Path definitions. -#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\WeChat Files\\wxid_ipl8u0ctajtn22\\FileStorage\\File\\2024-11\\best(2).onnx") -#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.bmp") +#define EXAMPLE_ONNX_MODEL_PATH MIL_TEXT("C:\\Users\\zjc\\source\\repos\\cotton_color\\Matrox\\models\\2024_11_12_imgsz640_batch1.onnx") +#define TARGET_IMAGE_DIR_PATH MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.mim") +#define IMAGE_FILE MIL_TEXT("C:\\Users\\zjc\\Desktop\\dimo2.bmp") int MosMain(void) { @@ -23,12 +26,20 @@ int MosMain(void) MilDetectedImage = M_NULL, // MIL image with detections DetectCtx = M_NULL, // MIL ONNX detection context DetectRes = M_NULL; // MIL detection result - // Allocate MIL objects. MappAlloc(M_NULL, M_DEFAULT, &MilApplication); MsysAlloc(M_DEFAULT, M_SYSTEM_DEFAULT, M_DEFAULT, M_DEFAULT, &MilSystem); MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay); + MIL_UNIQUE_BUF_ID dimo2; + MbufImport(IMAGE_FILE, M_DEFAULT, M_RESTORE+M_NO_GRAB+M_NO_COMPRESS, MilSystem, &dimo2); + //MIL_UNIQUE_BUF_ID MimArithdestination = MbufClone(dimo2, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_UNIQUE_ID); + MIL_UNIQUE_BUF_ID MimArithDestination = MbufAllocColor(MilSystem, 3, 640, 640, 32 + M_FLOAT, M_IMAGE + M_PROC, M_UNIQUE_ID); + // Post-Alloc Block for MimArith's destination + MbufClear(MimArithDestination, M_COLOR_BLACK); + + MimArith(dimo2, 255.0, MimArithDestination, M_DIV_CONST); + // Load the image into memory. if (MbufRestore(TARGET_IMAGE_DIR_PATH, MilSystem, &MilImage) != M_NULL) { @@ -40,6 +51,10 @@ int MosMain(void) return 1; // Exit if the image loading failed } + MdispSelect(MilDisplay, MimArithDestination); + + // MbufInquire(MilImage, , NULL); + // Import the YOLOv5 ONNX model into the detection context. MosPrintf(MIL_TEXT("Importing the YOLOv5 ONNX model into the detection context...\n")); MclassAlloc(MilSystem, M_CLASSIFIER_ONNX, M_DEFAULT, &DetectCtx); @@ -52,16 +67,24 @@ int MosMain(void) // Allocate a detection result buffer. MclassAllocResult(MilSystem, M_PREDICT_ONNX_RESULT, M_DEFAULT, &DetectRes); + + // Perform object detection on the image using MclassPredict. - MclassPredict(DetectCtx, MilImage, DetectRes, M_DEFAULT);////执行到这一步报错 + MclassPredict(DetectCtx, MimArithDestination, DetectRes, M_DEFAULT); + + MosPrintf(MIL_TEXT("Object detection completed.\n")); // Allocate a buffer for displaying the detection results. - MbufAlloc2d(MilSystem, 640, 640, 8 + M_UNSIGNED, M_IMAGE + M_PROC, &MilDetectedImage); + MbufAlloc2d(MilSystem, 640, 640, 32 + M_FLOAT, M_IMAGE + M_PROC+M_DISP, &MilDetectedImage); + + + MosPrintf(MIL_TEXT("Detected object detection completed.\n")); // Retrieve and draw the detection results manually. - MIL_INT NumDetections = 0; - MclassGetResult(DetectRes, M_GENERAL, M_TYPE_MIL_INT, &NumDetections); + MIL_FLOAT NumDetections = 0; + + MclassGetResult(DetectRes, M_GENERAL, M_NUMBER_OF_OUTPUTS, &NumDetections); if (NumDetections > 0) { @@ -96,7 +119,7 @@ int MosMain(void) } // Display the image with detection results. - MdispSelect(MilDisplay, MilDetectedImage); + // MdispSelect(MilDisplay, MilDetectedImage); // Wait for the user to close the window. MosPrintf(MIL_TEXT("Press to exit.\n"));