Beispiel #1
0
        /// <summary>
        /// 识别图片
        /// </summary>
        /// <param name="image"></param>
        /// <param name="languageType"></param>
        /// <returns></returns>
        public static List <OcrWord> OcrImage(string image, LanguageType languageType = LanguageType.CHN_ENG)
        {
            // 文字识别地址
            string ocrHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic";

            if (string.IsNullOrWhiteSpace(TOKEN))
            {
                TOKEN = GetAccessToken();
            }
            ocrHost = string.Format("{0}?access_token={1}", ocrHost, TOKEN);
            // head  Content-Type	application/x-www-form-urlencoded
            NameValueCollection paramList = new NameValueCollection();
            NameValueCollection heads     = new NameValueCollection();

            heads.Add("ContentType", "application/x-www-form-urlencoded");
            paramList.Add("image", image);
            // paramList.Add("language_type", languageType.ToString());
            string type;
            string result = HttpHelper.DoPost(ocrHost, paramList, heads, out type);

            if (!string.IsNullOrWhiteSpace(result))
            {
                OcrResponse ocrResponse = JsonConvert.DeserializeObject <OcrResponse>(result);
                if (ocrResponse != null)
                {
                    return(ocrResponse.WordsResult);
                }
            }

            return(null);
        }
Beispiel #2
0
        public IEnumerable <Rectangle> ExtractText()
        {
            if (_textResponse == null)
            {
                var responseMessage = _client.PostAsync(string.Format(BaseUrl, _endpoint, TextDetection), _content)
                                      .Result;
                var stream = responseMessage.Content.ReadAsStreamAsync().Result;
                if (stream == null)
                {
                    return(null);
                }

                using var reader = new StreamReader(stream, Encoding.UTF8);
                var response = reader.ReadToEnd();
                _textResponse = JsonConvert.DeserializeObject <OcrResponse>(response, OcrSettings);
            }

            return(_textResponse?.Regions
                   .Select(region => region.BoundingBox.AsRectangle()));
        }
Beispiel #3
0
        public async Task <IEnumerable <Rectangle> > ExtractTextAsync()
        {
            if (_textResponse == null)
            {
                var responseMessage =
                    await _client.PostAsync(string.Format(BaseUrl, _endpoint, TextDetection), _content);

                var stream = await responseMessage.Content.ReadAsStreamAsync();

                if (stream == null)
                {
                    return(null);
                }

                using var reader = new StreamReader(stream, Encoding.UTF8);
                var response = await reader.ReadToEndAsync();

                _textResponse = JsonConvert.DeserializeObject <OcrResponse>(response, OcrSettings);
            }

            return(ExtractText());
        }
        // GET api/<controller>/
        // Get a image url, return a list of potential candidates
        public HttpResponseMessage Get(string imageUrl)
        {
            try
            {
                string json = new WebClient().DownloadString(String.Format("{0}img={1}",
                                                                           System.Configuration.ConfigurationManager.AppSettings["OcrPrefix"],
                                                                           imageUrl));
                OcrResponse root = Newtonsoft.Json.JsonConvert.DeserializeObject <OcrResponse>(json);
                // TODO Get rid of Fake College
                String fake_college = "lovett";
                if (root.success)
                {
                    StandardMatcher smm           = new StandardMatcher();
                    string          parsedContent = smm.denoise(root.value.ToLower()); // TO FN LN 2132 ST HOUSTON TX 77005 USA
                    //return Request.CreateResponse(HttpStatusCode.InternalServerError, new GeneralFailureMessage(parsedContent));

                    List <Student> results = smm.generateMatch(parsedContent, fake_college);
                    return(Request.CreateResponse(HttpStatusCode.OK, results));
                }
                else
                {
                    // TODO what if this happens.
                    return(Request.CreateResponse(HttpStatusCode.InternalServerError, new GeneralFailureMessage("Ocr API fail")));
                }


                //// Fake response
                //Result r = new Result();
                //r.name = "testname";
                //r.email = "testemail";
                //List<Result> fake = new List<Result>();
                //fake.Add(r);
                //return Request.CreateResponse(HttpStatusCode.OK, fake);
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new GeneralFailureMessage("Get RicePeopePhoto: " + e.Message)));
            }
        }