static void IdentifyCelebrityFaces(string filename) { // Using USWest2, not the default region AmazonRekognitionClient rekoClient = new AmazonRekognitionClient(Amazon.RegionEndpoint.USWest2); // Request needs image bytes, so read and add to request byte[] data = File.ReadAllBytes(filename); RecognizeCelebritiesRequest rcr = new RecognizeCelebritiesRequest { Image = new Amazon.Rekognition.Model.Image { Bytes = new MemoryStream(data) } }; RecognizeCelebritiesResponse outcome = rekoClient.RecognizeCelebrities(rcr); if (outcome.CelebrityFaces.Count > 0) { // Load a bitmap to modify with face bounding box rectangles Bitmap facesHighlighted = new Bitmap(filename); Pen pen = new Pen(Color.Black, 3); Font drawFont = new Font("Arial", 12); // Create a graphics context using (var graphics = Graphics.FromImage(facesHighlighted)) { foreach (var fd in outcome.CelebrityFaces) { // Get the bounding box BoundingBox bb = fd.Face.BoundingBox; Console.WriteLine($"Bounding box = ({bb.Left}, {bb.Top}, {bb.Height}, {bb.Width})"); // Draw the rectangle using the bounding box values // They are percentages so scale them to picture graphics.DrawRectangle(pen, x: facesHighlighted.Width * bb.Left, y: facesHighlighted.Height * bb.Top, width: facesHighlighted.Width * bb.Width, height: facesHighlighted.Height * bb.Height); graphics.DrawString(fd.Name, font: drawFont, brush: Brushes.White, x: facesHighlighted.Width * bb.Left, y: facesHighlighted.Height * bb.Top + facesHighlighted.Height * bb.Height); } } // Save the image with highlights as PNG string fileout = filename.Replace(Path.GetExtension(filename), "_celebrityfaces.png"); facesHighlighted.Save(fileout, System.Drawing.Imaging.ImageFormat.Png); Console.WriteLine(">>> " + outcome.CelebrityFaces.Count + " celebrity face(s) highlighted in file " + fileout); } else { Console.WriteLine(">>> No celebrity faces found"); } }
/// <summary> /// Unmarshaller the response from the service to the response class. /// </summary> /// <param name="context"></param> /// <returns></returns> public override AmazonWebServiceResponse Unmarshall(JsonUnmarshallerContext context) { RecognizeCelebritiesResponse response = new RecognizeCelebritiesResponse(); context.Read(); int targetDepth = context.CurrentDepth; while (context.ReadAtDepth(targetDepth)) { if (context.TestExpression("CelebrityFaces", targetDepth)) { var unmarshaller = new ListUnmarshaller <Celebrity, CelebrityUnmarshaller>(CelebrityUnmarshaller.Instance); response.CelebrityFaces = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("OrientationCorrection", targetDepth)) { var unmarshaller = StringUnmarshaller.Instance; response.OrientationCorrection = unmarshaller.Unmarshall(context); continue; } if (context.TestExpression("UnrecognizedFaces", targetDepth)) { var unmarshaller = new ListUnmarshaller <ComparedFace, ComparedFaceUnmarshaller>(ComparedFaceUnmarshaller.Instance); response.UnrecognizedFaces = unmarshaller.Unmarshall(context); continue; } } return(response); }
private static List <Face> MapResponse(ProcessedImage image, RecognizeCelebritiesResponse response) { var faces = new List <Face>(); foreach (var celebrityFace in response.CelebrityFaces) { var face = new Face { MatchConfidence = celebrityFace.MatchConfidence, Name = celebrityFace.Name, FaceInfo = new FaceInfo { Confidence = celebrityFace.Face.Confidence, ImageQuality = new Models.ImageQuality { Brightness = celebrityFace.Face.Quality.Brightness, Sharpness = celebrityFace.Face.Quality.Sharpness }, Pose = new Models.Pose { Pitch = celebrityFace.Face.Pose.Pitch, Roll = celebrityFace.Face.Pose.Roll, Yaw = celebrityFace.Face.Pose.Yaw }, BoundingBox = new Models.BoundingBox { Width = celebrityFace.Face.BoundingBox.Width * image.Width, Height = celebrityFace.Face.BoundingBox.Height * image.Height, Left = celebrityFace.Face.BoundingBox.Left * image.Width, Top = celebrityFace.Face.BoundingBox.Top * image.Height } } }; var landmarks = new List <Landmark>(); foreach (var faceLandmark in celebrityFace.Face.Landmarks) { Enum.TryParse(faceLandmark.Type.Value, true, out LandmarkType type); var landmark = new Landmark { Type = type, X = faceLandmark.X * image.Width, Y = faceLandmark.Y * image.Height }; landmarks.Add(landmark); } face.FaceInfo.Landmarks = landmarks; faces.Add(face); } return(faces); }
public async Task <IActionResult> Login(IFormFile file) { CelebrityModel celeb = new CelebrityModel(); Directory.Delete(_appEnvironment.WebRootPath + "/resources/", true); Directory.CreateDirectory(_appEnvironment.WebRootPath + "/resources/"); if (null != file && file.Length > 0) { string speechFileName = "notjeff.mp3"; string speechText = "Nice try. You're not Jeff, I can't let you in."; AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); RecognizeCelebritiesRequest recognizeCelebritiesRequest = new RecognizeCelebritiesRequest(); Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image(); MemoryStream memStream = new MemoryStream(); file.CopyTo(memStream); img.Bytes = memStream; recognizeCelebritiesRequest.Image = img; RecognizeCelebritiesResponse recognizeCelebritiesResponse = await rekognitionClient.RecognizeCelebritiesAsync(recognizeCelebritiesRequest); if (null != recognizeCelebritiesResponse && recognizeCelebritiesResponse.CelebrityFaces.Count > 0) { celeb.CelebrityName = recognizeCelebritiesResponse.CelebrityFaces[0].Name; celeb.Confidence = recognizeCelebritiesResponse.CelebrityFaces[0].MatchConfidence; if (celeb.CelebrityName == "Jeff Bezos") { speechText = "Hello Boss, Welcome to the Deployment Bot. Please continue to start the deployment."; celeb.IsJeff = true; speechFileName = "jeff.mp3"; } } else { celeb.CelebrityName = "Sure, you're popular among your friends. But that doesn't make you a celebrity."; celeb.Confidence = 0; } AmazonPollyClient pollyclient = new AmazonPollyClient(); Amazon.Polly.Model.SynthesizeSpeechResponse speechResponse = await pollyclient.SynthesizeSpeechAsync(new Amazon.Polly.Model.SynthesizeSpeechRequest() { OutputFormat = OutputFormat.Mp3, Text = speechText, VoiceId = VoiceId.Joanna }); var stream = new FileStream(_appEnvironment.WebRootPath + "/resources/" + speechFileName, FileMode.Create); await speechResponse.AudioStream.CopyToAsync(stream); stream.Close(); } return(View("Login", celeb)); }
public static void Example() { String photo = "moviestars.jpg"; AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(); RecognizeCelebritiesRequest recognizeCelebritiesRequest = new RecognizeCelebritiesRequest(); Amazon.Rekognition.Model.Image img = new Amazon.Rekognition.Model.Image(); byte[] data = null; try { using (FileStream fs = new FileStream(photo, FileMode.Open, FileAccess.Read)) { data = new byte[fs.Length]; fs.Read(data, 0, (int)fs.Length); } } catch (Exception) { Console.WriteLine("Failed to load file " + photo); return; } img.Bytes = new MemoryStream(data); recognizeCelebritiesRequest.Image = img; Console.WriteLine("Looking for celebrities in image " + photo + "\n"); RecognizeCelebritiesResponse recognizeCelebritiesResponse = rekognitionClient.RecognizeCelebrities(recognizeCelebritiesRequest); Console.WriteLine(recognizeCelebritiesResponse.CelebrityFaces.Count + " celebrity(s) were recognized.\n"); foreach (Celebrity celebrity in recognizeCelebritiesResponse.CelebrityFaces) { Console.WriteLine("Celebrity recognized: " + celebrity.Name); Console.WriteLine("Celebrity ID: " + celebrity.Id); BoundingBox boundingBox = celebrity.Face.BoundingBox; Console.WriteLine("position: " + boundingBox.Left + " " + boundingBox.Top); Console.WriteLine("Further information (if available):"); foreach (String url in celebrity.Urls) { Console.WriteLine(url); } } Console.WriteLine(recognizeCelebritiesResponse.UnrecognizedFaces.Count + " face(s) were unrecognized."); }
/// <summary> /// Get Face Matches for Celebrities from Amazon service /// </summary> /// <param name="photo"></param> /// <returns></returns> public async Task <ResponseDTO> GetMatches(IFormFile photo) { try { AmazonRekognitionClient rekognitionClient = new AmazonRekognitionClient(_awsCredentials.Value.Id, _awsCredentials.Value.Key, Amazon.RegionEndpoint.USEast2); RecognizeCelebritiesRequest recognizeCelebritiesRequest = new RecognizeCelebritiesRequest(); Image img = new Image(); byte[] data = null; try { if (photo.Length > 0) { using (var ms = new MemoryStream()) { photo.CopyTo(ms); data = ms.ToArray(); } } } catch (Exception ex) { throw ex; } img.Bytes = new MemoryStream(data); recognizeCelebritiesRequest.Image = img; RecognizeCelebritiesResponse recognizeCelebritiesResponse = await rekognitionClient.RecognizeCelebritiesAsync(recognizeCelebritiesRequest); foreach (Celebrity celebrity in recognizeCelebritiesResponse.CelebrityFaces) { CelebrityDetail.Name = celebrity.Name; foreach (string url in celebrity.Urls) { CelebrityDetail.Url = url; } CelebrityDetails.Add(CelebrityDetail); CelebrityDetail = new CelebrityDetail(); } ResponseDetail.CelebrityDetails = CelebrityDetails; ResponseDetail.UnMatchCount = recognizeCelebritiesResponse.UnrecognizedFaces.Count; return(ResponseDetail); } catch (Exception ex) { throw ex; } }
public async Task <IActionResult> UploadAsync(IFormFile[] imgs) { IPainter painter = new RectangleMarker(); try { var file = Request.Form.Files[0]; var stream = ImageHelper.ConvertFormFileToMemoryStream(file); if (stream != null) { var response = new RecognizeCelebritiesResponse(); response = await _recognizeCelibrities.Recognize(stream); string markedImg = ""; if (response.CelebrityFaces.Count > 0) { System.Drawing.Image output = System.Drawing.Image.FromStream(stream); foreach (var box in response.CelebrityFaces) { var boundingBox = box.Face.BoundingBox; output = painter.DrawOnImage(output, file.FileName, boundingBox.Height, boundingBox.Width, boundingBox.Top, boundingBox.Left, Color.LightGreen); } markedImg = ImageHelper.ConvertImageToBase64(output); } return(Ok(new { response, markedImg })); } else { return(BadRequest()); } } catch (Exception ex) { return(StatusCode(500, "Internal server error")); } }
public string GetImageInfo(ImageData imageData, byte[] imgData = null) { try { var path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot", imageData.fileName); imgData = Convert.FromBase64String(imageData.base64Data); _imageData = new MemoryStream(imgData); RecognizeCelebritiesRequest recognizeCelebrities = new RecognizeCelebritiesRequest() { Image = new Image() { Bytes = _imageData } }; RecognizeCelebritiesResponse celebrity = _rekognitionClient.RecognizeCelebritiesAsync(recognizeCelebrities).Result; List <Celebrity> lstCelebrities = celebrity.CelebrityFaces; StringBuilder sbCelebrities = new StringBuilder(); foreach (var item in lstCelebrities) { sbCelebrities.Append(item.Name); sbCelebrities.Append(','); } string Celebrities = sbCelebrities.ToString().TrimEnd(','); return(Celebrities); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } }
/// <summary> /// Scans the contents of an image file looking for celebrities. If any /// are found, a bounding box will be drawn around the face and the name /// of the celebrity drawn under the box. /// </summary> /// <param name="client">The Rekognition client used to call /// RecognizeCelebritiesAsync.</param> /// <param name="filename">The name of the file that potentially /// contins faces.</param> public static async Task IdentifyCelebrityFaces(AmazonRekognitionClient client, string filename) { // Request needs image bytes, so read and add to request. byte[] data = File.ReadAllBytes(filename); RecognizeCelebritiesRequest request = new RecognizeCelebritiesRequest { Image = new Amazon.Rekognition.Model.Image { Bytes = new MemoryStream(data), }, }; RecognizeCelebritiesResponse response = await client.RecognizeCelebritiesAsync(request); if (response.CelebrityFaces.Count > 0) { // Load a bitmap to modify with face bounding box rectangles. Bitmap facesHighlighted = new Bitmap(filename); Pen pen = new Pen(Color.Black, 3); Font drawFont = new Font("Arial", 12); // Create a graphics context. using (var graphics = Graphics.FromImage(facesHighlighted)) { foreach (var fd in response.CelebrityFaces) { // Get the bounding box. BoundingBox bb = fd.Face.BoundingBox; Console.WriteLine($"Bounding box = ({bb.Left}, {bb.Top}, {bb.Height}, {bb.Width})"); // Draw the rectangle using the bounding box values. // They are percentages so scale them to the picture. graphics.DrawRectangle( pen, x: facesHighlighted.Width * bb.Left, y: facesHighlighted.Height * bb.Top, width: facesHighlighted.Width * bb.Width, height: facesHighlighted.Height * bb.Height); graphics.DrawString( fd.Name, font: drawFont, brush: Brushes.White, x: facesHighlighted.Width * bb.Left, y: (facesHighlighted.Height * bb.Top) + (facesHighlighted.Height * bb.Height)); } } // Save the image with highlights as PNG. string fileout = filename.Replace(Path.GetExtension(filename), "_celebrityfaces.png"); facesHighlighted.Save(fileout, System.Drawing.Imaging.ImageFormat.Png); Console.WriteLine($">>> {response.CelebrityFaces.Count} celebrity face(s) highlighted in file {fileout}."); Console.WriteLine("Found the following celebritie(s):"); foreach (var celeb in response.CelebrityFaces) { Console.WriteLine($"{celeb.Name}"); } } else { Console.WriteLine(">>> No celebrity faces found"); } }