C# Binding (.NET P/Invoke)
C# wrapper cho CVEDIX runtime qua P/Invoke — .NET 8.
Yêu cầu
- .NET SDK 8.0+
libcvedix_capi.so+libcvedix_core.sotrongLD_LIBRARY_PATH(có sẵn tạilib/x86_64/của SDK)
Build & chạy sample
bash
cd $CVEDIX_SDK_DIR/bindings/csharp
dotnet build
export LD_LIBRARY_PATH=$CVEDIX_SDK_DIR/lib/x86_64:$LD_LIBRARY_PATH
cd Cvedix.Sdk.Sample
dotnet run -- video.mp4 yolo11n.onnx labels.txtDùng trong project của bạn
csharp
using Cvedix.Sdk;
using var pipeline = new CvedixPipeline();
var src = pipeline.FileSource("src", 0, "video.mp4", 0.5f, loop: true);
var det = pipeline.YoloDetector("det", "model.onnx", NativeMethods.Yolo11, "labels.txt");
var app = pipeline.AppDestination("app", 0, (frame, dets) =>
{
foreach (var d in dets)
Console.WriteLine(d); // Detection: Label, Score, TrackId, bbox
});
det.AttachTo(src);
app.AttachTo(det);
pipeline.Start(src);
Console.ReadLine(); // Enter để dừng — Dispose tự dọn dẹpKiến trúc wrapper
| Lớp | File | Vai trò |
|---|---|---|
NativeMethods | Cvedix.Sdk/NativeMethods.cs | P/Invoke declarations map 1-1 với C API |
CvedixPipeline | Cvedix.Sdk/CvedixPipeline.cs | Facade cấp cao — vòng đời + callback |
Detection | — | Struct kết quả: Label, Score, TrackId, bbox |
CvedixException | — | Bọc lỗi native (cvedix_last_error()) |
Lưu ý quan trọng
Callback thread
Callback chạy trên thread native — marshal về UI thread nếu cập nhật giao diện:
csharp
// WPF
Dispatcher.Invoke(() => UpdateUI(dets));
// WinForms
control.BeginInvoke(() => UpdateUI(dets));Delegate lifetime
CvedixPipeline giữ reference cho delegate — ngăn GC thu hồi khi native còn giữ function pointer. Không tự tạo delegate tạm rồi truyền thẳng vào P/Invoke.
Tham chiếu project
xml
<ItemGroup>
<ProjectReference Include="path/to/Cvedix.Sdk/Cvedix.Sdk.csproj" />
</ItemGroup>