C# (Windows)
This topic introduces how to use the C# samples of Mech-DLK SDK on Windows. For sample descriptions, refer to Sample Usage Guide.
Installation Package Contents
The files related to C# samples in the Mech-DLK SDK installation package are as follows:
| Contents | Description |
|---|---|
csharp/docs |
C# interface documentation |
csharp/examples |
C# example programs |
csharp/lib |
C# SDK dynamic libraries ( |
|
Prerequisites
To use the C# samples in Mech-DLK SDK, the following software must be installed:
Install Mech-DLK SDK
Obtain the latest version of Mech-DLK SDK from Installation Guide and complete installation.
Install Visual Studio
-
Download the Visual Studio installation package.
-
During installation, select Desktop & Mobile under .NET Desktop development workloads. If the .NET SDK component is displayed in the installation interface, it is recommended to select it together, and then click Install in the lower-right corner.
|
Install OpenCV (Optional)
If you need to use OpenCV, download and install the C# version of OpenCV, i.e. OpenCvSharp.
| This sample uses OpenCV 4.12.0 as an example. If you are using other versions, adjust the configuration according to the actual situation. |
Example Adjustment Instructions
Before you compile and run various C# samples, you can adjust the input images, model packages, and related resource files according to your actual business needs. The following contents provide examples of common adjustment items.
-
Input image: Replace the image to be detected, or modify the image reading path according to actual requirements.
-
Model package: A defect segmentation model package is provided by default in the installation package. You can replace it with the one you need according to your actual business needs.
-
Output: Adjust how detection results are output based on project requirements, including modifying the save path, file name, or output location of result images.
-
Resource files: You can add or replace images, model packages, and related resource files required by the business in the resources directory.
Compile and Run Samples
After completing relevant adjustments, you can follow these steps to compile and run the sample.
Basic Sample
Compile the Sample
You can follow these steps to compile Basic samples:
-
Copy the csharp/lib directory and paste the
mmind_dl_sdk_csharp.dllfile to the directory where thebasic.slnsolution file of the Basic sample is stored (that is, csharp\examples\basic). -
Double-click the
basic.slnfile and open it in Visual Studio. -
On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select .
-
After the compilation, you can see the generated executable file in the csharp\examples\basic\build directory. The
example_csharp_basic.exeis used as an example in this section.
Run the Sample
-
Copy the
hasp_rt.exefile and all.dllfiles in the cpp/bin directory to the directory where the generatedexample_csharp_basic.exefile is stored.
-
Copy the
resourcesfolder in thecppfolder to the directory where thebasic.slnfile is stored.
-
Double-click the generated
example_csharp_basic.exefile in the csharp\examples\basic\build directory, or click Run in Visual Studio to run the sample directly.
|
The cpp/resources directory contains the model and resource files needed for the sample to run. Ensure that they are in a location that is properly accessible to the program. |
Advanced Sample (Multi-Thread Inference)
Compile the Sample
-
Copy the
mmind_dl_sdk_csharp.dllin the csharp/lib folder to the directory where themulti_thread_infer.slnsolution file of the multi-thread inference sample is stored (that is, csharp/examples/advanced/multi_thread_infer). -
Double-click the
multi_thread_infer.slnfile to open it in Visual Studio. -
On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select .
-
After the compilation, you can see the generated executable file in the csharp\examples\advanced\multi_thread_infer\build directory. The
example_csharp_multi_thread.exeis used as an example in this section.
Run the Sample
-
Copy the
hasp_rt.exefile and all.dllfiles in the cpp/bin directory to the directory where the generatedexample_csharp_multi_thread.exefile is stored. -
Copy the
resourcesfolder in thecppfolder to the directory where themulti_thread_infer.slnfile is stored. -
In the csharp\examples\advanced\multi_thread_infer\build directory, double-click the generated
example_csharp_multi_thread.exefile or run the sample directly in Visual Studio.
Advanced Sample (OpenCV)
Install OpenCV
Refer to Install OpenCV to complete the OpenCV installation.
Compile the Sample
-
Copy the
mmind_dl_sdk_csharp.dllfile in the csharp/lib folder to the directory where theinfer_with_opencv.slnsolution file of the OpenCV sample is stored (that is, csharp/examples/advanced/infer_with_opencv). -
Double-click the
infer_with_opencv.slnfile to open it in Visual Studio. -
On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select .
-
After the compilation, you can see the generated executable file in the csharp\examples\advanced\infer_with_opencv\build directory.
Theexample_csharp_opencv.exeis used as an example in this section.
Run the Sample
-
Copy the
hasp_rt.exefile and all.dllfiles in the cpp/bin directory to the directory where the generatedexample_csharp_opencv.exefile is stored. -
Copy the
resourcesfolder in thecppfolder to the directory whereinfer_with_opencv.slnis stored. -
In the csharp\examples\advanced\infer_with_opencv\build directory, double-click the generated
example_csharp_opencv.exefile or run the sample directly in Visual Studio.
Advanced Sample (HALCON)
Install HALCON
Refer to Install HALCON to finish HALCON installation.
Compile the Sample
-
Copy the
mmind_dl_sdk_csharp.dllfile in the csharp/lib folder to the directory whereinfer_with_halcon.sln, the solution file of the HALCON sample program, is stored (that is, csharp/examples/advanced/infer_with_halcon). -
Double-click the
infer_with_halcon.slnfile to open it in Visual Studio. -
On the toolbar of Visual Studio, set Solution Configuration to Release and Solution Platform to x64. In the Visual Studio menu bar, select .
-
After the compilation, you can see the generated executable file in the csharp\examples\advanced\infer_with_halcon\build directory.
Theexample_csharp_halcon.exeis used as an example in this section.
Run the Sample
-
Copy the
hasp_rt.exefile and all.dllfiles in the cpp/bin directory to the directory where the generatedexample_csharp_halcon.exefile is stored. -
Copy the
resourcesfolder in thecppfolder to the directory whereinfer_with_halcon.slnis stored. -
In the csharp\examples\advanced\infer_with_halcon\build directory, double-click the generated
example_csharp_halcon.exefile or run the sample directly in Visual Studio.
FAQs
Compilation Issues
Missing .dll Errors
-
Problem: Compilation failed, indicating missing
.dllfile. -
Solution: Confirm that
mmind_dl_sdk_csharp.dllin the csharp/lib folder has been copied to the folder where the.slnfile is stored.
Could Not Find .NET SDK
-
Problem: The .NET SDK could not be found during compilation.
-
Solution: When installing Visual Studio, ensure that the .NET Desktop Development Workload under the Desktop & Mobile category is selected, and that the .NET SDK component displayed during installation is also selected.