LedIdentify.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace PaddleOCRSharp.Led
  8. {
  9. public class LedIdentify
  10. {
  11. public LedIdentify()
  12. {
  13. oCRParameter.use_gpu = false;
  14. oCRParameter.cpu_math_library_num_threads = 10;//预测并发线程数
  15. oCRParameter.enable_mkldnn = true;//web部署该值建议设置为0,否则出错,内存如果使用很大,建议该值也设置为0.
  16. oCRParameter.cls = false; //是否执行文字方向分类;默认false
  17. oCRParameter.det = false;//是否开启方向检测,用于检测识别180旋转
  18. oCRParameter.use_angle_cls = false;//是否开启方向检测,用于检测识别180旋转
  19. oCRParameter.det_db_score_mode = false;//是否使用多段线,即文字区域是用多段线还是用矩形,
  20. string root = System.IO.Path.GetDirectoryName(typeof(LedIdentify).Assembly.Location);
  21. string modelPathroot = root + @"\inference";
  22. config.det_infer = modelPathroot + @"\ch_PP-OCRv3_det_infer";
  23. config.cls_infer = modelPathroot + @"\ch_ppocr_mobile_v2.0_cls_infer";
  24. config.rec_infer = modelPathroot + @"\ch_PP-OCRv3_rec_infer";
  25. config.keys = modelPathroot + @"\ppocr_keys.txt";
  26. }
  27. public string DetInfer { get => config.det_infer; set => config.det_infer = value; }
  28. public string ClsInfer { get => config.cls_infer; set => config.cls_infer = value; }
  29. public string RecInfer { get => config.rec_infer; set => config.rec_infer = value; }
  30. public string Keys { get => config.keys; set => config.keys = value; }
  31. private PaddleOCREngine engine;
  32. private OCRModelConfig config = new OCRModelConfig();
  33. private OCRParameter oCRParameter = new OCRParameter();
  34. public bool Cls { get=> oCRParameter.cls; set => oCRParameter.cls = value; }
  35. public bool Det { get => oCRParameter.det; set => oCRParameter.det = value; }
  36. public bool UseAngleCls { get => oCRParameter.use_angle_cls; set => oCRParameter.use_angle_cls = value; }
  37. public bool DetDbScoreMode { get => oCRParameter.det_db_score_mode; set => oCRParameter.det_db_score_mode = value; }
  38. public bool UseGpu { get => oCRParameter.use_gpu; set => oCRParameter.use_gpu = true; }
  39. public int CpuMathLibraryNumThreads { get => oCRParameter.cpu_math_library_num_threads; set => oCRParameter.cpu_math_library_num_threads = value; }
  40. public bool IsInit { get; private set; } = false;
  41. public int UserId { get; private set; } = -1;
  42. public bool IsLogin { get; private set; } = false;
  43. public string IP { get;private set; }
  44. public int Port { get; private set; }
  45. public string Username { get; private set; }
  46. public string Password { get; private set; }
  47. public string SerialNumber => System.Text.Encoding.Default.GetString(info.sSerialNumber).Trim('\0');
  48. public byte AlarmInPortNum=> info.byAlarmInPortNum;
  49. public byte AlarmOutPortNum=> info.byAlarmOutPortNum;
  50. public byte DiskNum=> info.byDiskNum;
  51. public byte DVRType=> info.byDVRType;
  52. public byte ChanNum=> info.byChanNum;
  53. public byte StartChan=> info.byStartChan;
  54. public byte AudioChanNum=> info.byAudioChanNum;
  55. public byte IPChanNum=> info.byIPChanNum;
  56. public byte ZeroChanNum=> info.byZeroChanNum;
  57. public byte MainProto=> info.byMainProto;
  58. public byte SubProto=> info.bySubProto;
  59. public byte Support=> info.bySupport;
  60. public byte Support1=> info.bySupport1;
  61. public byte Support2=> info.bySupport2;
  62. public ushort DevType=> info.wDevType;
  63. public byte Support3=> info.bySupport3;
  64. public byte MultiStreamProto=> info.byMultiStreamProto;
  65. public byte StartDChan=> info.byStartDChan;
  66. public byte StartDTalkChan=> info.byStartDTalkChan;
  67. public byte HighDChanNum=> info.byHighDChanNum;
  68. public byte LanguageType=> info.byLanguageType;
  69. public byte Support4=> info.bySupport4;
  70. public ushort PicQuality { get => jpg.wPicQuality; set => jpg.wPicQuality = value; }
  71. public ushort PicSize { get=> jpg.wPicSize; set => jpg.wPicSize = value; }
  72. public bool IsOCRInit { get; private set; } = false;
  73. private QuickNV.HikvisionNetSDK.Defines.NET_DVR_JPEGPARA jpg = new QuickNV.HikvisionNetSDK.Defines.NET_DVR_JPEGPARA();
  74. private byte[] jpgdata = new byte[1024 * 1024 * 10];
  75. private QuickNV.HikvisionNetSDK.Defines.NET_DVR_DEVICEINFO_V30 info = new QuickNV.HikvisionNetSDK.Defines.NET_DVR_DEVICEINFO_V30();
  76. public void InitHikvision()
  77. {
  78. if (IsInit) return;
  79. IsInit = QuickNV.HikvisionNetSDK.Methods.NET_DVR_Init();
  80. }
  81. public void InitOCR()
  82. {
  83. if (IsOCRInit) return;
  84. engine = new PaddleOCREngine(config, oCRParameter);
  85. IsOCRInit = true;
  86. }
  87. public void Login(string ip="192.168.2.65",int port = 8000,string username="admin",string password="l147258369")
  88. {
  89. if (IsLogin) return;
  90. IP = ip;
  91. Port = port;
  92. Username = username;
  93. Password = password;
  94. info = new QuickNV.HikvisionNetSDK.Defines.NET_DVR_DEVICEINFO_V30();
  95. UserId = QuickNV.HikvisionNetSDK.Methods.NET_DVR_Login_V30(IP, Port, Username, Password, ref info);
  96. IsLogin = UserId == 0;
  97. }
  98. public Mat NET_DVR_CaptureJPEGPicture_NEW(int channel=1)
  99. {
  100. if (!IsLogin) return new Mat();
  101. uint picsize = 0;
  102. QuickNV.HikvisionNetSDK.Methods.NET_DVR_CaptureJPEGPicture_NEW(UserId, channel, ref jpg, jpgdata, (uint)jpgdata.Length, ref picsize);
  103. if (picsize == 0) return new Mat();
  104. byte[] result = new byte[picsize];
  105. Array.Copy(jpgdata, result, (int)picsize);
  106. return Mat.FromImageData(result);
  107. }
  108. public void Logout()
  109. {
  110. if (!IsLogin) return;
  111. QuickNV.HikvisionNetSDK.Methods.NET_DVR_Logout(UserId);
  112. IsLogin = false;
  113. }
  114. public OCRResult DetectText(byte[] datas)
  115. {
  116. if (!IsOCRInit || engine == null) return new OCRResult();
  117. return engine.DetectText(datas);
  118. }
  119. public unsafe byte[] GetColos(Mat mat)
  120. {
  121. Mat rgb = mat.CvtColor(ColorConversionCodes.BGR2RGB);
  122. var result = new Span<byte>(rgb.Data.ToPointer(), rgb.Width * rgb.Height * rgb.Channels()).ToArray();
  123. rgb.Dispose();
  124. return result;
  125. }
  126. public OCRResult DetectText(Mat mat)
  127. {
  128. if (!IsOCRInit || engine == null) return new OCRResult();
  129. return engine.DetectText(mat.ToBytes());
  130. }
  131. public void DisposeOCR()
  132. {
  133. if (!IsOCRInit) return;
  134. engine?.Dispose();
  135. IsOCRInit = false;
  136. }
  137. public void Cleanup()
  138. {
  139. if (!IsInit) return;
  140. QuickNV.HikvisionNetSDK.Methods.NET_DVR_Cleanup();
  141. IsInit = false;
  142. }
  143. }
  144. }