Skip to content

C# 调用示例

入门例示:快速入门

首先定义 c# 的调用DllLkinferApi类,用于调用dll函数

c#
using System;
using System.Runtime.InteropServices;

namespace Lkinfer_demo
{
    public class DllLkinferApi
    {

        // 定义与 C++ 结构体对应的 C# 结构体
        // 要传递的数据对象
        [ComVisible(true)]
        [StructLayout(LayoutKind.Sequential)]
        public struct OcrParam
        {
            public int padding; // 图像外接白框,用于提升识别率,文字框没有正确框住所有文字时,增加此值。默认50。
            public int max_size; // 按图像长边进行总体缩放,大图增加识别耗时但精度更高,小图减小耗时但精度降低,maxSize为0表示不缩放
            public float sim;// 文字置信度,推理的相似度,默认0.5
            public float box_thresh;// 用于控制检测到的文本框(bounding box)是否被保留的阈值参数,默认 0.3
            public float un_clip_ratio;// 单个文字框大小倍率,越大时单个文字框越大,默认1.6 [1.0-2.0(常见默认值1.5-1.8)]
            public int use_angle_cls; // 是否启用文本方向分类器。关闭:0,启用:1。默认:0(关闭)
            public int most_angle; // 控制方向分类的投票策略,决定是否采用多数角度作为最终结果。1或0。0:保持自己的预测角度。1:选择频率最高的角度作为全局方向  默认:0

        }

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int initOcrv4(int nThreads);
        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int loadOcrv4Model(string dbin, string cbin, string rbin, string kbin, int nThreads);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr ocrv4DetectParam(
            string img_path, float sim = 0.5f, int padding = 50,
            int max_size = 1024, float box_thresh = 0.3f, float un_clip_ratio = 1.6f,
            int use_angle_cls = 0, int most_angle = 0);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int loadYolov8Model(string name, string modelPath);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr yolov8Detect(string name, string imgPath);
        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int setYolov8Sim(string name, float sim);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr version();

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int clearCache();


        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int existsYolov8Name(string name);
        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr getAllYolov8Name();
        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr yolov8DetectDraw(string name, string imgPath, string outPath);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr getLastError();

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr getYolov8Info(string name);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int log_off();

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int reLoadYolov8Model(string name, string modelPath);


        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int setYolov8Iou(string name, float iou);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr ocrv4Detect(string img_path, OcrParam param_);

        [DllImport("Lkinfer.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr utf8ToGBK(string u8);
    }
}

图片例示:

exe

例示源码下载:/doc/download.html