private void btn_detectText_Click(object sender, EventArgs e) { var image = ToBytesStream($"{detectTextImgLocation}"); txtDetectedText.Text = ""; var client = new AmazonRekognitionClient(); var request = new DetectTextRequest { Image = image }; var response = client.DetectText(request); txtDetectedText.Text = $"Found {response.TextDetections.Count} texts\n"; foreach (var text in response.TextDetections) { txtDetectedText.Text += $"- {text.DetectedText}\n"; } }
public DetectTextResponse ImageToText() { Image image = new Image(); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { byte[] data = null; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); image.Bytes = new MemoryStream(data); } AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); DetectTextRequest detectTextRequest = new DetectTextRequest() { Image = image, }; DetectTextResponse detectTextResponse = rekognitionClient.DetectText(detectTextRequest); double width = imgPictureFrame.Width; double height = imgPictureFrame.Height; foreach (TextDetection text in detectTextResponse.TextDetections) { bool isLine = true; if (text.Type != TextTypes.LINE) { isLine = false; } BoundingBox boundingBox = text.Geometry.BoundingBox; BindingBox bindingBox = new BindingBox(width * boundingBox.Width, height * boundingBox.Height, height * boundingBox.Top, width * boundingBox.Left, text.Id.ToString(), isLine); gContainer.Children.Add(bindingBox); bindingBoxes.Add(bindingBox); } return(detectTextResponse); }
public static void Example() { String photo = "input.jpg"; String bucket = "bucket"; AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); DetectTextRequest detectTextRequest = new DetectTextRequest() { Image = new Image() { S3Object = new S3Object() { Name = photo, Bucket = bucket } } }; try { DetectTextResponse detectTextResponse = rekognitionClient.DetectText(detectTextRequest); Console.WriteLine("Detected lines and words for " + photo); foreach (TextDetection text in detectTextResponse.TextDetections) { Console.WriteLine("Detected: " + text.DetectedText); Console.WriteLine("Confidence: " + text.Confidence); Console.WriteLine("Id : " + text.Id); Console.WriteLine("Parent Id: " + text.ParentId); Console.WriteLine("Type: " + text.Type); } } catch (Exception e) { Console.WriteLine(e.Message); } }
private void btnOCR_Click(object sender, EventArgs e) { if (textURL.Text == "") { MessageBox.Show("Vui lòng chọn hình ảnh để phân tích!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } Bitmap bmp = new Bitmap(textURL.Text); pb.Image = bmp; pb.SizeMode = PictureBoxSizeMode.StretchImage; var source = ToByteStream(textURL.Text); var client = new AmazonRekognitionClient(); var request = new DetectTextRequest { Image = source }; var response = client.DetectText(request); var tmp = new StringBuilder(); foreach (var item in response.TextDetections) { //MessageBox.Show(item.DetectedText); if (item.Type == "LINE") // LINE or WORD { tmp.Append(item.DetectedText); tmp.Append("\r\n"); } } txtRes.Text = tmp.ToString(); //MessageBox.Show(txtRes.Text); }
static void DetectText(string filename) { // Using USWest2, not the default region AmazonRekognitionClient rekoClient = new AmazonRekognitionClient("AKIAJ3MXTT4YJ5WCU2JA", "1Qgrgk01sRVzRkEhZ0VuB/9kO8FyZQI2qjSy76Ry", Amazon.RegionEndpoint.USWest2); DetectTextRequest dfr = new DetectTextRequest(); // Request needs image butes, so read and add to request Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image(); byte[] data = null; using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); } img.Bytes = new MemoryStream(data); dfr.Image = img; var outcome = rekoClient.DetectText(dfr); if (outcome.TextDetections.Count > 0) { foreach (var text in outcome.TextDetections) { Console.WriteLine("Description:" + text.DetectedText + " - Confidence:" + text.Confidence); //// Get the bounding box //BoundingBox bb = fd.BoundingBox; //Console.WriteLine("Bounding box = (" + bb.Left + ", " + bb.Top + ", " + // bb.Height + ", " + bb.Width + ")"); } } else { Console.WriteLine(">>> No Text found"); } }
public Solicitudes DetectText(List <Tab_ConfigSys> Tab_ConfigSys, byte[] bytes, string Identificacion, int IdTipoIdentificacion, int?OPC) { string xClase = string.Format("{0}|{1}", MethodBase.GetCurrentMethod().Module.Name, MethodBase.GetCurrentMethod().DeclaringType.Name); string xProceso = MethodBase.GetCurrentMethod().Name; bool resp = false; var param = new { UriFoto = bytes, TipoIdentificacion = IdTipoIdentificacion, cedula = Identificacion, OPCe = OPC }; var dto_excepcion = new UTL_TRA_EXCEPCION { STR_CLASE = xClase, STR_EVENTO = xProceso, FEC_CREACION = DateTime.Now, STR_PARAMETROS = JsonConvert.SerializeObject(param), STR_SERVIDOR = System.Net.Dns.GetHostName(), STR_APLICATIVO = ConfigurationManager.AppSettings["APLICATIVO"].ToString() }; Solicitudes _Solicitudes = new Solicitudes(); _Solicitudes.Identificacion = Identificacion; ManagerSolcitudes managerSolcitudes = new ManagerSolcitudes(); var FecVencCedula = managerSolcitudes.ConsultaFechaVencimientoCedula(_Solicitudes); var options = new CredentialProfileOptions { AccessKey = Tab_ConfigSys[0].llave_Config1, SecretKey = Tab_ConfigSys[0].llave_Config2 }; try { var profile = new Amazon.Runtime.CredentialManagement.CredentialProfile("AWSProfileName", options); profile.Region = RegionEndpoint.USWest1; var netSDKFile = new NetSDKCredentialsFile(); netSDKFile.RegisterProfile(profile); using (AmazonRekognitionClient rekoClient = new AmazonRekognitionClient(Tab_ConfigSys[0].llave_Config1, Tab_ConfigSys[0].llave_Config2, RegionEndpoint.USEast1)) { Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image(); img.Bytes = new MemoryStream(bytes); DetectTextRequest dfr = new DetectTextRequest(); dfr.Image = img; var outcome = rekoClient.DetectText(dfr); bool dia = false; bool mes = false; bool anio = false; foreach (var texto in outcome.TextDetections) { string cedula = ""; cedula = texto.DetectedText; cedula = cedula.Replace(" ", "").Trim(); var cedresp = cedula.Split(':'); var respuesta = cedresp.Where(x => x.ToString().Equals(Identificacion) || cedula.Equals(Identificacion)).Any(); if (respuesta) { resp = respuesta; } if (FecVencCedula != null) { var resDia = cedresp.Where(x => x.ToString().Equals(Convert.ToString(FecVencCedula.Dia)) || cedula.Equals(Convert.ToString(FecVencCedula.Dia))).Any(); if (resDia) { dia = resDia; } var resMes = cedresp.Where(x => x.ToString().Equals(Convert.ToString(FecVencCedula.Mes)) || cedula.Equals(Convert.ToString(FecVencCedula.Mes))).Any(); if (resMes) { mes = resMes; } var resAnio = cedresp.Where(x => x.ToString().Equals(Convert.ToString(FecVencCedula.Anio)) || cedula.Equals(Convert.ToString(FecVencCedula.Anio))).Any(); if (resAnio) { anio = resAnio; } if (respuesta == true && dia == true && mes == true && anio == true) { break; } } else { if (respuesta) { break; } } } var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(outcome.TextDetections.Select(x => x.DetectedText)); _Solicitudes.Result = resp; _Solicitudes.Dia = Convert.ToInt32(dia); _Solicitudes.Mes = Convert.ToInt32(mes); _Solicitudes.Anio = Convert.ToInt32(anio); _Solicitudes.DetectedText = jsonString; } //return outcome.TextDetections.Select(x => x.DetectedText).ToList(); return(_Solicitudes); } catch (Exception ex) { dto_excepcion.STR_MENSAJE = ex.Message; dto_excepcion.IS_TELEGRAM = true; TwoFunTwoMe_DataAccess.Utility.guardaExcepcion(dto_excepcion, ConfigurationManager.ConnectionStrings["TwoFunTwoMeConnection"].ConnectionString); _Solicitudes.Mensaje = "ERR_Detect Text"; throw; } }
public static void DoImageRecognition(string path) { if (File.Exists(path)) { var options = new CredentialProfileOptions() { AccessKey = "", SecretKey = "" }; var profile = new CredentialProfile("test", options) { Region = RegionEndpoint.USEast2 }; var netSdkFile = new NetSDKCredentialsFile(); netSdkFile.RegisterProfile(profile); AmazonRekognitionClient rekoClient = new AmazonRekognitionClient(RegionEndpoint.USEast2); MemoryStream mStream; using (System.Drawing.Image image = System.Drawing.Image.FromFile($"{path}")) { using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); mStream = m; } } DetectTextRequest detectTextRequest = new DetectTextRequest() { Image = new Image() { Bytes = mStream } }; try { _stopwatch.Start(); DetectTextResponse detectTextResponse = rekoClient.DetectText(detectTextRequest); foreach (TextDetection text in detectTextResponse.TextDetections) { CheckFoundSerial(text.DetectedText); } Console.WriteLine($"Query time {_stopwatch.ElapsedMilliseconds}ms"); if (foundSerial) { Console.WriteLine($"Serial number: {serialNumber}"); } else { Console.WriteLine("Could not find any serial number"); } } catch (Exception e) { Console.WriteLine(e.Message); } } _stopwatch.Reset(); Console.WriteLine(); Console.WriteLine("Press enter to show menu"); Console.ReadKey(); Console.Clear(); Console.WriteLine("Press 0 for image processing using camera or 1-9 for stored images"); }
public Solicitudes DetectFace(List <ConfigSys> configSys, string UrlFoto, string Identificacion, string IdTipoIdentificacion, int OPC) { string xClase = string.Format("{0}|{1}", MethodBase.GetCurrentMethod().Module.Name, MethodBase.GetCurrentMethod().DeclaringType.Name); string xProceso = MethodBase.GetCurrentMethod().Name; bool resp = false; var param = new { UriFoto = UrlFoto, TipoIdentificacion = IdTipoIdentificacion, cedula = Identificacion, OPCe = OPC }; var dto_excepcion = new UTL_TRA_EXCEPCION { STR_CLASE = xClase, STR_EVENTO = xProceso, FEC_CREACION = DateTime.Now, STR_PARAMETROS = JsonConvert.SerializeObject(param), STR_SERVIDOR = System.Net.Dns.GetHostName(), STR_APLICATIVO = ConfigurationManager.AppSettings["APLICATIVO"].ToString() }; Solicitudes _Solicitudes = new Solicitudes(); var options = new CredentialProfileOptions { AccessKey = configSys[0].llave_Config1, SecretKey = configSys[0].llave_Config2 }; try { var profile = new Amazon.Runtime.CredentialManagement.CredentialProfile("AWSProfileName", options); profile.Region = RegionEndpoint.USWest1; var netSDKFile = new NetSDKCredentialsFile(); netSDKFile.RegisterProfile(profile); String targetImage = UrlFoto; // Using USWest2, not the default region using (AmazonRekognitionClient rekoClient = new AmazonRekognitionClient(configSys[0].llave_Config1, configSys[0].llave_Config2, RegionEndpoint.USEast1)) { Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image(); byte[] data = null; using (FileStream fs = new FileStream(targetImage, FileMode.Open, FileAccess.Read)) { data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); } img.Bytes = new MemoryStream(data); DetectTextRequest dfr = new DetectTextRequest(); dfr.Image = img; var outcome = rekoClient.DetectText(dfr); //List<string> Detectedtext = new List<string>(); //foreach (var texto in outcome.TextDetections) outcome.TextDetections.ForEach(texto => { string cedula = ""; //Detectedtext.Add(texto.DetectedText); cedula = texto.DetectedText; cedula = cedula.Replace(" ", "").Trim(); //if (OPC == 2) //{ var cedresp = cedula.Split(':'); var respuesta = cedresp.Where(x => x.ToString().Equals(Identificacion) || cedula.Equals(Identificacion)).Any(); if (respuesta) { resp = respuesta; } //if (resp.Equals(true)) texto.s; //foreach (var text in cedresp) //{ // if (text == Identificacion) // { // resp = true; // } //} //} //if (cedula == Identificacion && OPC == 1) //{ // resp = true; //} }); var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(outcome.TextDetections.Select(x => x.DetectedText)); _Solicitudes.result = resp; _Solicitudes.DetectedText = jsonString; } //return outcome.TextDetections.Select(x => x.DetectedText).ToList(); return(_Solicitudes); } catch (Exception ex) { dto_excepcion.STR_MENSAJE = ex.Message; DynamicSqlDAO.guardaExcepcion(dto_excepcion, ConfigurationManager.ConnectionStrings["TwoFunTwoMeConnection"].ConnectionString); _Solicitudes.Mensaje = "ERR_Detect Text"; throw; } }
static void Main(string[] args) { String photo = @"C:\Temp\banner.png"; Image image = new Image(); try { using (FileStream fs = new FileStream(photo, FileMode.Open, FileAccess.Read)) { byte[] data = null; data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); image.Bytes = new MemoryStream(data); } } catch (Exception) { Console.WriteLine("Failed to load file " + photo); return; } try { // Constructs a SharedCredentialsFile object from the default credentials file. SharedCredentialsFile sharedCredentialsFile = new SharedCredentialsFile(); // Get the [default] profile from the credentials file. CredentialProfile defaultProfile = GetDefaultProfile(sharedCredentialsFile); if (defaultProfile != null) { // Get the credentials (access key, secret access key, etc.) AWSCredentials credentials = AWSCredentialsFactory.GetAWSCredentials(defaultProfile, new SharedCredentialsFile()); AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(credentials, RegionEndpoint.USEast1); DetectTextRequest detectTextRequest = new DetectTextRequest() { Image = image, }; DetectTextResponse detectTextResponse = rekognitionClient.DetectText(detectTextRequest); foreach (TextDetection td in detectTextResponse.TextDetections) { Console.WriteLine(td.DetectedText); } } else { Console.WriteLine("AWS [default] profile not found"); } } catch (AmazonRekognitionException ex) { Console.WriteLine("AWS Rekognition ERROR: {0}", ex.Message); } catch (Exception ex) { Console.WriteLine("ERROR: {0}", ex.Message); } Console.ReadLine(); }