123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- using AI.Common.Interface;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- namespace IDCardRecognitionLibs
- {
- public class IDCardRecognition
- {
- #region private
- /// <summary>
- /// 检测算法
- /// </summary>
- private IDCardAnalyser? _idCardAnalyser = null;
- /// <summary>
- /// 主动结束
- /// </summary>
- private volatile bool _disposing = false;
- /// <summary>
- /// 选择的OCR模型,目前只支持PaddleOCR
- /// </summary>
- private EnumOCRModel _modelType = EnumOCRModel.PaddleOCR;
- #endregion
- #region 实现接口
- /// <summary>
- /// 评估单幅图像
- /// </summary>
- /// <param name="image"></param>
- /// <returns></returns>
- public IDCardRecogResult EvaluateOneImage(Bitmap image)
- {
- try
- {
- if (!_disposing)
- {
- // 将bitmap统一转成3通道图像进行接下来的处理
- return _idCardAnalyser.EvaluateOneImage(BitmapToRawImage(image));
- }
- else
- {
- return new IDCardRecogResult();
- }
- }
- catch(Exception excep)
- {
- throw new Exception(excep.Message);
- }
- }
- /// <summary>
- /// 主动销毁
- /// </summary>
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- LogHelper.InfoLog("HepatoRenalEchoContrastDetect.Disposed manually.", string.Empty);
- }
- #endregion
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="dir">模型地址,从该地址下存放的Networks文件夹里面加载模型</param>
- public IDCardRecognition(string netDir)
- {
- //Thread.Sleep(30000);
- // 从该地址下面加载模型,默认使用paddleOCR模型
- _idCardAnalyser = new IDCardAnalyser(netDir, _modelType);
- }
- #region private funcs
- /// <summary>
- /// convert bitmap to bytes
- /// </summary>
- /// <param name="image"></param>
- /// <param name="keepChannel"></param>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- private static RawImage BitmapToRawImage(Bitmap image, bool keepChannel = false)
- {
- EnumColorType enumColorType;
- int width = image.Width;
- int height = image.Height;
- PixelFormat pixelFormat = image.PixelFormat;
- if (pixelFormat != PixelFormat.Format24bppRgb && pixelFormat != PixelFormat.Format32bppArgb &&
- pixelFormat != PixelFormat.Format32bppPArgb && pixelFormat != PixelFormat.Format32bppRgb &&
- pixelFormat != PixelFormat.Format8bppIndexed)
- {
- throw new Exception("Unexpected image pixel format:" + pixelFormat);
- }
- int origChannel = 3;
- if (pixelFormat == PixelFormat.Format24bppRgb)
- {
- origChannel = 3;
- }
- else if (pixelFormat == PixelFormat.Format8bppIndexed)
- {
- origChannel = 1;
- }
- else
- {
- origChannel = 4;
- }
- int dstChannel = 3;
- if (keepChannel)
- {
- dstChannel = origChannel;
- }
- enumColorType = (EnumColorType)dstChannel;
- var bmData = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
- byte[] dstDataArray = new byte[width * height * dstChannel];
- unsafe
- {
- int stride = bmData.Stride;
- int dstStride = width * dstChannel;
- IntPtr ptr = bmData.Scan0;
- IntPtr ptrH, ptrW;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- if (origChannel == dstChannel)
- {
- Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
- }
- else if (origChannel > dstChannel)
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel, dstChannel);
- }
- }
- else
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- for (int nc = 0; nc < dstChannel; nc++)
- {
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel + nc, 1);
- }
- }
- }
- }
- }
- image.UnlockBits(bmData);
- return new RawImage(dstDataArray, width, height, enumColorType);
- }
- /// <summary>
- /// 销毁
- /// </summary>
- private void DoDispose()
- {
- if (!_disposing)
- {
- _disposing = true;
- if (_idCardAnalyser != null)
- {
- _idCardAnalyser.Dispose();
- _idCardAnalyser = null;
- }
- }
- }
- #endregion
- }
- }
|