Ejemplo n.º 1
0
        public async Task <IActionResult> UploadAsync(IFormFile file)
        {
            IPainter             painter = new RectangleMarker();
            CompareFacesResponse response;

            try
            {
                var sourceImg = Request.Form.Files[0];
                var targetImg = Request.Form.Files[1];
                var srcImg    = ImageHelper.ConvertFormFileToMemoryStream(sourceImg);
                var trgtImg   = ImageHelper.ConvertFormFileToMemoryStream(targetImg);

                if (sourceImg != null) // && targetImg != null)
                {
                    response = await _compareFaces.CompareAsync(srcImg, trgtImg);

                    string markedImg = "";
                    if (response.FaceMatches.Count > 0)
                    {
                        System.Drawing.Image output = System.Drawing.Image.FromStream(trgtImg);

                        foreach (var box in response.FaceMatches)
                        {
                            var boundingBox = box.Face.BoundingBox;
                            output = painter.DrawOnImage(output, targetImg.FileName,
                                                         boundingBox.Height, boundingBox.Width,
                                                         boundingBox.Top, boundingBox.Left, Color.LightGreen);
                        }

                        foreach (var box in response.UnmatchedFaces)
                        {
                            var boundingBox = box.BoundingBox;
                            output = painter.DrawOnImage(output, targetImg.FileName,
                                                         boundingBox.Height, boundingBox.Width,
                                                         boundingBox.Top, boundingBox.Left, Color.Red);
                        }

                        markedImg = ImageHelper.ConvertImageToBase64(output);
                    }


                    return(Ok(new { response, markedImg }));
                }
                else
                {
                    return(BadRequest());
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
        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"));
            }
        }