예제 #1
0
        /// <summary>
        /// 身份证识别
        /// </summary>
        public void IdcardDemo()
        {
            var ApiKey    = "Sd5OH4BMmAqwKZvG6O56oqed";
            var SecretKey = "UX70FFACkOGb0at4q3ljzkYTaKC87vgG";
            var client    = new Baidu.Aip.Ocr.Ocr(ApiKey, SecretKey);

            var image = ImageToBase64(pictureBox.BackgroundImage, System.Drawing.Imaging.ImageFormat.Jpeg);

            if (image == null)
            {
                MessageBox.Show("请先加载图片");
                return;
            }
            var idCardSide = "front";  //front 正面  back 背面

            // 调用身份证识别,可能会抛出网络等异常,请使用try/catch捕获
            var result = client.Idcard(image, idCardSide);

            Console.WriteLine(result);
            // 如果有可选参数
            var options = new Dictionary <string, object> {
                { "detect_direction", "true" },
                { "detect_risk", "false" }
            };

            // 带参数调用身份证识别
            result = client.Idcard(image, idCardSide, options);
            Console.WriteLine(result);
        }
예제 #2
0
        public string AdvancedGeneralDemo(Image url, int category = 0, string other = "front")
        {
            byte[] image  = Common.ImageHelper.ImageToBytes(url);
            var    client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间
            JObject result = null;

            switch (category)
            {
            case 0:    //通用文字识别
                result = client.AccurateBasic(image);
                break;

            case 1:    //生僻字识别
                result = client.GeneralEnhanced(image);
                break;

            case 2:    //身份证识别
                result = client.Idcard(image, other);
                break;

            case 3:    //银行卡识别
                result = client.Bankcard(image);
                break;

            case 4:    //驾驶证识别
                result = client.DrivingLicense(image);
                break;

            case 5:    //行驶证识别
                result = client.VehicleLicense(image);
                break;

            case 6:    //车牌识别
                result = client.LicensePlate(image);
                break;

            case 7:    //通用票据识别
                result = client.Receipt(image);
                break;

            case 8:    //营业执照识别
                result = client.BusinessLicense(image);
                break;

            default:    //通用文字识别
                result = client.AccurateBasic(image);
                break;
            }
            Console.WriteLine(result);
            if (result == null)
            {
                return("");
            }
            return(JsonConvert.SerializeObject(result));
        }
예제 #3
0
        public void IdcardDemo()
        {
            var client = new Baidu.Aip.Ocr.Ocr(API_Key, SECRE_KEY);

            client.Timeout = 60000;
            var image      = File.ReadAllBytes(ImagePath);
            var idCardSide = "back";

            OutResult = client.Idcard(image, idCardSide);
        }
예제 #4
0
        public static string Idcard(byte[] img, string id_card_side)
        {
            var client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);

            client.Timeout = 60000;  // 修改超时时间
            var options = new Dictionary <string, object> {
                { "detect_direction", "true" },
                { "detect_risk", "false" }
            };
            var result = client.Idcard(img, id_card_side, options);

            return(result.ToString());
        }
예제 #5
0
        public static Newtonsoft.Json.Linq.JObject  Execute(IElement ele, System.Activities.Presentation.Model.ModelItem model)
        {
            var wordlimit     = model.GetValue <string>("WordLimit");
            var casesensitive = model.GetValue <bool>("CaseSensitive");
            var isfrontside   = model.GetValue <bool>("IsFrontSide");
            var lang          = Config.local.ocrlanguage;


            string basepath = Interfaces.Extensions.DataDirectory;


            ImageElement[] result = null;
            var            _ocr   = new Baidu.Aip.Ocr.Ocr(ocr.API_KEY, ocr.SECRET_KEY);

            _ocr.Timeout = 60000;//设置超时时间


            // OpenRPA.Interfaces.Image.Util.SaveImageStamped(ele.element, "OCR");
            Bitmap sourceimg = null;

            if (ele is ImageElement)
            {
                sourceimg = ((ImageElement)ele).element;
            }
            else
            {
                sourceimg = Interfaces.Image.Util.Screenshot(ele.Rectangle.X, ele.Rectangle.Y, ele.Rectangle.Width, ele.Rectangle.Height);
            }

            String idCardSide;

            if (isfrontside)
            {
                idCardSide = "front";
            }
            else
            {
                idCardSide = "back";
            }
            MemoryStream ms = new MemoryStream();

            sourceimg.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = ms.GetBuffer();
            ms.Close();

            var word_results = _ocr.Idcard(imageBytes, idCardSide);

            Log.Debug("adding element cords to results: " + word_results);

            return(word_results);
        }
예제 #6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string info = "";

            try
            {
                string            a        = Request.Form["a"];
                string[]          strs     = a.Split(',');
                string            base64   = strs[1];
                byte[]            img      = Sigformat.Toms(base64).ToArray();
                string            apid     = "iDZMn726zfMz8tTuvA18P6Fu";
                string            secretid = "PUjq7PCLMxme2wFu6st8NNZmQAG8guVZ";
                Baidu.Aip.Ocr.Ocr client   = new Baidu.Aip.Ocr.Ocr(apid, secretid);
                client.Timeout = 6000;
                string idside  = "front";
                var    options = new Dictionary <string, object> {
                    { "detect_direction", "false" },
                    { "detect_risk", "true" }
                };
                var res = client.Idcard(img, idside, options);
                if (res["error_code"] is null)
                {
                    string str = "姓名:" + res["words_result"]["姓名"]["words"].ToString();
                    str += "<br>证件号码:" + res["words_result"]["公民身份号码"]["words"].ToString();
                    str += "<br>证件类型:" + res["risk_type"].ToString();
                    info = str;
                }
                else
                {
                    info = "照片内容错误!";
                }
            }
            catch
            {
                info = "内部处理错误!";
            }
            Response.Write(info);
            Response.End();
            Response.Flush();
        }
예제 #7
0
        protected override void Execute(NativeActivityContext context)
        {
            // var match = Element.Get(context);
            var    wordlimit     = WordLimit.Get(context);
            var    lang          = Config.local.ocrlanguage;
            var    casesensitive = CaseSensitive.Get(context);
            var    isfrontside   = IsFrontSide.Get(context);
            string basepath      = Interfaces.Extensions.DataDirectory;
            string path          = System.IO.Path.Combine(basepath, "tessdata");
            //ocr.TesseractDownloadLangFile(path, Config.local.ocrlanguage);
            //ocr.TesseractDownloadLangFile(path, "osd");
            var ele = Element.Get(context);

            // ele.element.Save(@"c:\temp\dump.png", System.Drawing.Imaging.ImageFormat.Png);

            // var result = ocr.GetIdTextcomponents(path, Config.local.ocrlanguage, ele.element);
            // var result = ocr.GetIdTextcomponents(path, Config.local.ocrlanguage, @"c:\temp\dump.png");

            ImageElement[] result;
            // 百度OCR
            var _ocr = new Baidu.Aip.Ocr.Ocr(ocr.API_KEY, ocr.SECRET_KEY);

            _ocr.Timeout = 60000;//设置超时时间
            //_ocr.Init(path, lang.ToString(), Emgu.CV.OCR.OcrEngineMode.TesseractLstmCombined);
            //_ocr.PageSegMode = Emgu.CV.OCR.PageSegMode.SparseText;

            // OpenRPA.Interfaces.Image.Util.SaveImageStamped(ele.element, "OCR");
            Bitmap sourceimg = null;

            if (ele is ImageElement)
            {
                // 传入的是图片
                sourceimg = ((ImageElement)ele).element;
            }
            else
            {
                // 传入非图片,开始截图
                sourceimg = Interfaces.Image.Util.Screenshot(ele.Rectangle.X, ele.Rectangle.Y, ele.Rectangle.Width, ele.Rectangle.Height);
            }
            String idCardSide;

            if (isfrontside)
            {
                idCardSide = "front";
            }
            else
            {
                idCardSide = "back";
            }
            MemoryStream ms = new MemoryStream();

            sourceimg.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] imageBytes = ms.GetBuffer();
            ms.Close();

            var word_results = _ocr.Idcard(imageBytes, idCardSide);

            context.SetValue(Result, word_results["words_result"]);

            var image_status = word_results["image_status"].ToString();

            context.SetValue(OcrStatus, image_status);

            //IEnumerator<ImageElement> _enum = result.ToList().GetEnumerator();
            //context.SetValue(_elements, _enum);
            //bool more = _enum.MoveNext();
            //if (more)
            //{
            //    context.ScheduleAction(Body, _enum.Current, OnBodyComplete);
            //}
        }