public async Task <OcrResultDTO> Post()
        {
            StringBuilder sb           = new StringBuilder();
            OcrResultDTO  ocrResultDTO = new OcrResultDTO();

            try
            {
                if (Request.Form.Files.Count > 0)
                {
                    var file = Request.Form.Files[Request.Form.Files.Count - 1];

                    if (file.Length > 0)
                    {
                        var memoryStream = new MemoryStream();
                        file.CopyTo(memoryStream);
                        byte[] imageFileBytes = memoryStream.ToArray();
                        memoryStream.Flush();

                        string JSONResult = await ReadTextFromStreamAzure(imageFileBytes);

                        string JSONResult2 = await ReadTextFromStreamAWS(imageFileBytes);

                        ImageDescription imgDescAzure = JsonConvert.DeserializeObject <ImageDescription>(JSONResult);
                        var           AWSList         = JsonConvert.DeserializeObject <List <string> >(JSONResult2);
                        ImageAnalysis imageAnalysis   = JsonConvert.DeserializeObject <ImageAnalysis>(JSONResult);
                        TagResult     TagResult       = JsonConvert.DeserializeObject <TagResult>(JSONResult);
                        //OcrResult ocrResult = JsonConvert.DeserializeObject<OcrResult>(JSONResult);
                        var AzureList = new List <string>();
                        foreach (var item in imageAnalysis.Description.Tags)
                        {
                            AzureList.Add(item);
                        }
                        var selectedRTRCAzure = GetWinner(AzureList);
                        var selectedRTRCAWS   = GetWinner(AWSList);


                        sb.Append("*********************Azure*********************** ");
                        sb.Append("\n");
                        sb.Append(selectedRTRCAzure);
                        sb.Append("\n");

                        sb.Append("**********************AWS************************ ");
                        sb.Append("\n");
                        sb.Append(selectedRTRCAWS);
                        sb.Append("\n");



                        ocrResultDTO.DetectedText = sb.ToString();
                    }
                }
                return(ocrResultDTO);
            }
            catch
            {
                ocrResultDTO.DetectedText = "Error occurred. Try again";
                ocrResultDTO.Language     = "unk";
                return(ocrResultDTO);
            }
        }
Exemple #2
0
        public async Task <OcrResultDTO> Post([FromBody] myImg image)
        {
            StringBuilder sb           = new StringBuilder();
            OcrResultDTO  ocrResultDTO = new OcrResultDTO();

            //  var baseURLImage = "https://www.fmpilot2.com/Attachment/IFM/AAR-PV08/WEB-1721300/1[20200616_182215543].jpg";
            try
            {
                if (!string.IsNullOrEmpty(image.filename))
                {
                    string JSONResult = await ReadTextFromStreamAzureUrl(image.filename);

                    //string JSONResult2 = await ReadTextFromStreamAWS(ImageUrl);

                    ImageDescription imgDescAzure = JsonConvert.DeserializeObject <ImageDescription>(JSONResult);
                    // var AWSList = JsonConvert.DeserializeObject<List<string>>(JSONResult2);
                    ImageAnalysis imageAnalysis = JsonConvert.DeserializeObject <ImageAnalysis>(JSONResult);
                    TagResult     TagResult     = JsonConvert.DeserializeObject <TagResult>(JSONResult);
                    //OcrResult ocrResult = JsonConvert.DeserializeObject<OcrResult>(JSONResult);
                    var AzureList = new List <string>();
                    if (imageAnalysis.Description != null)
                    {
                        foreach (var item in imageAnalysis.Description.Tags)
                        {
                            AzureList.Add(item);
                        }
                        var selectedRTRCAzure = GetWinner(AzureList);
                        //    var selectedRTRCAWS = GetWinner(AWSList);


                        sb.Append("*********************Azure*********************** ");
                        sb.Append("\n");
                        sb.Append(selectedRTRCAzure);
                        sb.Append("\n");

                        sb.Append("**********************AWS************************ ");
                        sb.Append("\n");
                        //   sb.Append(selectedRTRCAWS);
                        sb.Append("\n");



                        ocrResultDTO.DetectedText = sb.ToString();
                    }
                    else
                    {
                        ocrResultDTO.DetectedText = "Cannot process this image";
                    }
                }

                return(ocrResultDTO);
            }
            catch
            {
                ocrResultDTO.DetectedText = "Error occurred. Try again";
                ocrResultDTO.Language     = "unk";
                return(ocrResultDTO);
            }
        }
Exemple #3
0
        public async Task <OcrResultDTO> Post()
        {
            StringBuilder sb           = new StringBuilder();
            OcrResultDTO  ocrResultDTO = new OcrResultDTO();

            try
            {
                if (Request.Form.Files.Count > 0)
                {
                    var file = Request.Form.Files[Request.Form.Files.Count - 1];

                    if (file.Length > 0)
                    {
                        var memoryStream = new MemoryStream();
                        file.CopyTo(memoryStream);
                        byte[] imageFileBytes = memoryStream.ToArray();
                        memoryStream.Flush();

                        string JSONResult = await ReadTextFromStream(imageFileBytes);

                        OcrResult ocrResult = JsonConvert.DeserializeObject <OcrResult>(JSONResult);
                        if (!ocrResult.Language.Equals("unk"))
                        {
                            foreach (OcrLine ocrLine in ocrResult.Regions[0].Lines)
                            {
                                foreach (OcrWord ocrWord in ocrLine.Words)
                                {
                                    sb.Append(ocrWord.Text);
                                    sb.Append(' ');
                                }
                                sb.AppendLine();
                            }
                        }
                        else
                        {
                            sb.Append("This language is not supported.");
                        }
                        ocrResultDTO.DetectedText = sb.ToString();
                        ocrResultDTO.Language     = ocrResult.Language;
                    }
                }
                return(ocrResultDTO);
            }
            catch
            {
                ocrResultDTO.DetectedText = "Error occurred. Try again";
                ocrResultDTO.Language     = "unk";
                return(ocrResultDTO);
            }
        }
Exemple #4
0
        protected private async Task GetText()
        {
            if (imageFileBytes != null)
            {
                loading = true;
                Result  = await computerVisionService.GetTextFromImage(imageFileBytes);

                if (LanguageList.ContainsKey(Result.Language))
                {
                    DetectedTextLanguage = LanguageList[Result.Language].Name;
                }
                else
                {
                    DetectedTextLanguage = "Unknown";
                }
                loading = false;
            }
        }
Exemple #5
0
        public async Task <OcrResultDTO> GetTextFromImage(byte[] imageFileBytes)
        {
            StringBuilder sb           = new StringBuilder();
            OcrResultDTO  ocrResultDTO = new OcrResultDTO();

            try
            {
                string JSONResult = await ReadTextFromStream(imageFileBytes);

                OcrResult ocrResult = JsonConvert.DeserializeObject <OcrResult>(JSONResult);

                if (!ocrResult.Language.Equals("unk"))
                {
                    foreach (OcrLine ocrLine in ocrResult.Regions[0].Lines)
                    {
                        foreach (OcrWord ocrWord in ocrLine.Words)
                        {
                            sb.Append(ocrWord.Text);
                            sb.Append(' ');
                        }
                        sb.AppendLine();
                    }
                }
                else
                {
                    sb.Append("This language is not supported.");
                }
                ocrResultDTO.DetectedText = sb.ToString();
                ocrResultDTO.Language     = ocrResult.Language;
                return(ocrResultDTO);
            }
            catch
            {
                ocrResultDTO.DetectedText = "Error occurred. Try again";
                ocrResultDTO.Language     = "unk";
                return(ocrResultDTO);
            }
        }
Exemple #6
0
        public async Task <IActionResult> Post()
        {
            StringBuilder sb           = new StringBuilder();
            OcrResultDTO  ocrResultDTO = new OcrResultDTO();

            try
            {
                List <byte[]> byteDataArray = new List <byte[]>();
                if (Request.Form.Files.Count > 0)
                {
                    var file1 = Request.Form.Files[0];
                    if (file1.Length > 0)
                    {
                        MemoryStream memoryStream1 = new MemoryStream();
                        file1.CopyTo(memoryStream1);
                        byte[] imageFileBytes1 = memoryStream1.ToArray();
                        ocrResultDTO.ImageFront = imageFileBytes1;
                        byteDataArray.Add(imageFileBytes1);
                        memoryStream1.Flush();
                    }
                    var file2 = Request.Form.Files[1];
                    if (file2.Length > 0)
                    {
                        MemoryStream memoryStream2 = new MemoryStream();
                        file2.CopyTo(memoryStream2);
                        byte[] imageFileBytes2 = memoryStream2.ToArray();
                        ocrResultDTO.ImageBack = imageFileBytes2;
                        byteDataArray.Add(imageFileBytes2);
                        memoryStream2.Flush();
                    }
                    var JSONResult = await ReadTextFromStream(byteDataArray);

                    for (int i = 0; i < JSONResult.Count; i++)
                    {
                        OcrResult ocrResult = JsonConvert.DeserializeObject <OcrResult>(JSONResult[i]);
                        if (!ocrResult.Language.Equals("unk"))
                        {
                            if (JSONResult.IndexOf(JSONResult[i]) == 0)
                            {
                                string faceIdString = await GetFaceId(byteDataArray[0]);

                                List <FaceModel> jObj = JsonConvert.DeserializeObject <List <FaceModel> >(faceIdString);

                                string surname    = ocrResult.Regions[1].Lines[6].Words[0].Text.ToString();
                                string name       = ocrResult.Regions[1].Lines[10].Words[0].Text.ToString();
                                string dob        = ocrResult.Regions[1].Lines[15].Words[0].Text.ToString();
                                string sid        = ocrResult.Regions[0].Lines[4].Words[0].Text.ToString();
                                string validUntil = ocrResult.Regions[1].Lines[18].Words[0].Text.ToString();



                                ocrResultDTO.Name       = name;
                                ocrResultDTO.Surname    = surname;
                                ocrResultDTO.DOB        = dob;
                                ocrResultDTO.sid        = sid;
                                ocrResultDTO.validUntil = validUntil;


                                ocrResultDTO.faceId1 = jObj[0].faceId;
                            }
                            else
                            {
                                string IdCard     = ocrResult.Regions[1].Lines[1].Words[0].Text;
                                string birthPlace = ocrResult.Regions[0].Lines[1].Words[0].Text.ToString();
                                ocrResultDTO.IDCard     = IdCard;
                                ocrResultDTO.birthPlace = birthPlace;
                            }
                        }
                        else
                        {
                            sb.Append("This language is not supported.");
                        }
                    }
                }

                if (ocrResultDTO.Name != null && ocrResultDTO.IDCard != null)
                {
                    using (SqlConnection con = new SqlConnection(Configuration.GetConnectionString("DefaultConnection")))
                    {
                        try
                        {
                            con.Open();

                            using (SqlCommand cmd = new SqlCommand())
                            {
                                cmd.Connection     = con;
                                cmd.CommandTimeout = 500;


                                cmd.CommandText = "INSERT INTO CustomerOnBoarding (FaceId,Name,Surname,DOB,IDCard,ImageFrontSide,ImageBackSide) VALUES ('" + ocrResultDTO.faceId1 + "','" + ocrResultDTO.Name + "','" + ocrResultDTO.Surname + "','" + ocrResultDTO.DOB + "','" + ocrResultDTO.IDCard + "',@ImageFront,@ImageBack)";

                                cmd.Parameters.Add(new SqlParameter("@ImageFront", System.Data.SqlDbType.Image)
                                {
                                    Value = ocrResultDTO.ImageFront
                                });
                                cmd.Parameters.Add(new SqlParameter("@ImageBack", System.Data.SqlDbType.Image)
                                {
                                    Value = ocrResultDTO.ImageBack
                                });
                                cmd.ExecuteNonQuery();
                            }
                        }
                        catch (Exception ex)
                        {
                            Ok(ex.Message);
                        }
                    }
                }
                return(Ok(ocrResultDTO));
            }
            catch (Exception ex)
            {
                return(Ok(ex.Message));
            }
        }