Beispiel #1
0
        static string RecognizeFromUrl(Configuration conf)
        {
            string      imgUri   = @"https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png";
            OcrApi      api      = new OcrApi(conf);
            var         request  = new PostOcrFromUrlOrContentRequest(null, imgUri, resultType: ResultType.Text);
            OCRResponse response = api.PostOcrFromUrlOrContent(request);

            return(response.Text);
        }
Beispiel #2
0
        static string RecognizeFromUrl(Configuration conf)
        {
            string imgUri = @"http://typecast.com/images/uploads/fluid-type-single-column.png";

            OcrApi      api      = new OcrApi(conf);
            var         request  = new PostOcrFromUrlOrContentRequest(null, imgUri);
            OCRResponse response = api.PostOcrFromUrlOrContent(request);

            return(response.Text);
        }
Beispiel #3
0
        static string RecognizeFromUrlToPdf(Configuration conf)
        {
            string url = @"https://upload.wikimedia.org/wikipedia/commons/2/2f/Book_of_Abraham_FirstPage.png";
            OcrApi api = new OcrApi(conf);

            var         request  = new PostOcrFromUrlOrContentRequest(null, url, LanguageEnum.Spanish, ResultType.Pdf, DsrMode.NoDsrNoFilter);
            OCRResponse response = api.PostOcrFromUrlOrContent(request);

            return(response.Text);
        }
Beispiel #4
0
        static string RecognizeFromContent(Configuration conf)
        {
            string name = "10.png";

            using (FileStream fs = File.OpenRead(name))
            {
                OcrApi      api      = new OcrApi(conf);
                var         request  = new PostOcrFromUrlOrContentRequest(fs);
                OCRResponse response = api.PostOcrFromUrlOrContent(request);

                return(response.Text);
            }
        }
        static string RecognizeFromContentDeFr(Configuration conf)
        {
            string name = "de_1.jpg";

            using (FileStream fs = File.OpenRead(name))
            {
                OcrApi      api      = new OcrApi(conf);
                var         request  = new PostOcrFromUrlOrContentRequest(fs, "", language: LanguageEnum.German);
                OCRResponse response = api.PostOcrFromUrlOrContent(request);

                return(response.Text);
            }
        }
Beispiel #6
0
        static string RecognizeFromContentToPdf(Configuration conf)
        {
            string name = "10.png";

            using (FileStream fs = File.OpenRead(name))
            {
                OcrApi      api      = new OcrApi(conf);
                var         request  = new PostOcrFromUrlOrContentRequest(fs, resultType: ResultType.Pdf, dsrMode: DsrMode.DsrAndFilter);
                OCRResponse response = api.PostOcrFromUrlOrContent(request);

                return(response.Pdf.Substring(0, 30) + " ..................");
            }
        }
Beispiel #7
0
        /// <summary>
        /// Recognize image text from some url if provided or from the request body content
        /// </summary>
        /// <param name="request">Request. <see cref="PostOcrFromUrlOrContentRequest" /></param>
        /// <returns><see cref="OCRResponse"/></returns>
        public OCRResponse PostOcrFromUrlOrContent(PostOcrFromUrlOrContentRequest request)
        {
            // create path and map variables
            var resourcePath = this.mConfiguration.GetApiRootUrl() + "/ocr/recognize";

            resourcePath = Regex
                           .Replace(resourcePath, "\\*", string.Empty)
                           .Replace("&amp;", "&")
                           .Replace("/?", "?");
            var formParams = new Dictionary <string, object>();

            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "url", request.url);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "language", request.language);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "resultType", request.resultType);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "skewCorrect", request.skewCorrect);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "spellCheck", request.spellCheck);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "dsrMode", request.dsrMode);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "dsrConfidence", request.dsrConfidence);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "makeContrastCorrection", request.contrastCorrection);
            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "makeImageUpscaling", request.imageUpscale);


            if (request.File != null)
            {
                formParams.Add("file", this.apiInvoker.ToFileInfo(request.File, "File"));
            }

            try
            {
                return(this.apiInvoker.InvokeApi <OCRResponse>(
                           resourcePath,
                           "POST",
                           null,
                           null,
                           formParams));
            }
            catch (ApiException ex)
            {
                OCRResponse ocrRespose = new OCRResponse();
                ocrRespose.Status        = "1";
                ocrRespose.StatusMessage = "PipelineException";
                return(ocrRespose);
            }
            catch (TimeoutException)
            {
                OCRResponse ocrRespose = new OCRResponse();
                ocrRespose.Status        = "2";
                ocrRespose.StatusMessage = "TimeOutException";
                return(ocrRespose);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Recognize image text from some url if provided or from the request body content
        /// </summary>
        /// <param name="request">Request. <see cref="PostOcrFromUrlOrContentRequest" /></param>
        /// <returns><see cref="OCRResponse"/></returns>
        public OCRResponse PostOcrFromUrlOrContent(PostOcrFromUrlOrContentRequest request)
        {
            // create path and map variables
            var resourcePath = this.configuration.GetApiRootUrl() + "/ocr/recognize";

            resourcePath = Regex
                           .Replace(resourcePath, "\\*", string.Empty)
                           .Replace("&amp;", "&")
                           .Replace("/?", "?");
            var formParams = new Dictionary <string, object>();

            resourcePath = UrlHelper.AddQueryParameterToUrl(resourcePath, "url", request.url);

            if (request.File != null)
            {
                formParams.Add("file", this.apiInvoker.ToFileInfo(request.File, "File"));
            }

            try
            {
                var response = this.apiInvoker.InvokeApi(
                    resourcePath,
                    "POST",
                    null,
                    null,
                    formParams);

                if (response != null)
                {
                    return((OCRResponse)SerializationHelper.Deserialize(response, typeof(OCRResponse)));
                }

                return(null);
            }
            catch (ApiException ex)
            {
                if (ex.ErrorCode == 404)
                {
                    return(null);
                }

                throw;
            }
        }