public async Task <IActionResult> RekognitionTest([FromBody] RekognitionTestDTO dto)
        {
            List <Label> labels = null;

            Guid   g          = Guid.NewGuid();
            string guidString = Convert.ToBase64String(g.ToByteArray());

            guidString = guidString.Replace("=", "");
            guidString = guidString.Replace("+", "");
            guidString = guidString.Replace("/", "");

            // Retrieving image data
            string keyName = string.Format("test/{0}.jpg", guidString);

            byte[] imageByteArray = Convert.FromBase64String(dto.base64Image);
            if (imageByteArray.Length == 0)
            {
                return(BadRequest("Image length is 0."));
            }

            using (MemoryStream ms = new MemoryStream(imageByteArray))
            {
                // call Rekonition API
                labels = await RekognitionUtil.GetObjectDetailFromStream(this.RekognitionClient, ms);

                // Upload image to S3 bucket
                // await Task.Run(() => S3Util.UploadToS3(this.S3Client, "S3_BUCKET_NAME_HERE", "KEY_NAME_HERE", ms));
            }

            return(Ok(labels));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Post([FromBody] StagePostImageDTO dto)
        {
            //Console.WriteLine("PostImage entered.");

            StageScoreDTO stageScore = new StageScoreDTO();

            stageScore.game_id  = dto.game_id;
            stageScore.stage_id = dto.stage_id;

            Guid   g          = Guid.NewGuid();
            string guidString = Convert.ToBase64String(g.ToByteArray());

            guidString = guidString.Replace("=", "");
            guidString = guidString.Replace("+", "");
            guidString = guidString.Replace("/", "");

            // Retrieving image data
            byte[] imageByteArray = Convert.FromBase64String(dto.base64Image);
            if (imageByteArray.Length == 0)
            {
                return(BadRequest("Image length is 0."));
            }

            using (MemoryStream ms = new MemoryStream(imageByteArray))
            {
                // call Rekonition API
                List <Label> labels = await RekognitionUtil.GetObjectDetailFromStream(this.RekognitionClient, ms);

                List <string> labelNames = new List <string>();
                foreach (Label label in labels)
                {
                    labelNames.Add(label.Name);
                    Console.Write(label.Name + " ");
                }
                var matchedObject = _context.StageObject.Where(x => x.game_id == dto.game_id &&
                                                               x.stage_id == dto.stage_id &&
                                                               x.found_yn == "N" &&
                                                               labelNames.Contains(x.object_name)).FirstOrDefault();
                if (matchedObject != null)
                {
                    //Console.WriteLine("Matched object: " + matchedObject.object_name);
                    stageScore.object_name  = matchedObject.object_name;
                    stageScore.object_score = matchedObject.object_score;
                    matchedObject.found_yn  = "Y";

                    _context.StageObject.Update(matchedObject);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    Console.WriteLine("no matched object");
                }
            }

            return(Ok(stageScore));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Post([FromBody] TestPicturePostImageDTO dto)
        {
            List <Label> labels = null;

            Guid   g          = Guid.NewGuid();
            string guidString = Convert.ToBase64String(g.ToByteArray());

            guidString = guidString.Replace("=", "");
            guidString = guidString.Replace("+", "");
            guidString = guidString.Replace("/", "");

            // Retrieving image data
            string keyName = string.Format("test/{0}.jpg", guidString);

            byte[] imageByteArray = Convert.FromBase64String(dto.base64Image);
            if (imageByteArray.Length == 0)
            {
                return(BadRequest("Image length is 0."));
            }

            TestPicture newTestPicture = null;

            using (MemoryStream ms = new MemoryStream(imageByteArray))
            {
                // call Rekonition API
                labels = await RekognitionUtil.GetObjectDetailFromStream(this.RekognitionClient, ms);

                // Database update
                newTestPicture = new TestPicture {
                    use_yn   = "Y",
                    file_loc = keyName
                };

                _context.TestPicture.Add(newTestPicture);
                await _context.SaveChangesAsync();

                foreach (Label item in labels)
                {
                    TestPictureLabel newLabel = new TestPictureLabel {
                        picture_id = newTestPicture.picture_id,
                        label_name = item.Name,
                        confidence = item.Confidence
                    };
                    _context.TestPictureLabel.Add(newLabel);
                    await _context.SaveChangesAsync();
                }

                // Upload image to S3 bucket
                // await Task.Run(() => S3Util.UploadToS3(this.S3Client, "S3_BUCKET_NAME_HERE", "KEY_NAME_HERE", ms));
            }

            return(Ok(labels));
        }
        public async Task <IActionResult> PostNewTest([FromBody] GameStagePostImageDTO dto)
        {
            Console.WriteLine("PostNewTest entered.");

            string bucketName = "reinvent-gottalent";

            // Retrieving image data
            // ex: game/10/Happiness.jpg
            // string keyName = string.Format("game/{0}/{1}.jpg", dto.gameId, dto.actionType);
            string keyName        = string.Format("game/0/drawing.jpg");
            string croppedKeyName = string.Format("game/{0}/{1}_cropped.jpg", dto.gameId, dto.actionType);

            byte[] imageByteArray = Convert.FromBase64String(dto.base64Image);
            if (imageByteArray.Length == 0)
            {
                return(BadRequest("Image length is 0."));
            }

            StageLog newStageLog = null;

            using (MemoryStream ms = new MemoryStream(imageByteArray))
            {
                // call Rekonition API
                FaceDetail faceDetail = await RekognitionUtil.GetObjectDetailFromStream(this.RekognitionClient, ms);

                // Crop image to get face only
                // System.Drawing.Image originalImage = System.Drawing.Image.FromStream(ms);
                // System.Drawing.Image croppedImage = GetCroppedFaceImage(originalImage, faceDetail.BoundingBox);
                // MemoryStream croppedms = new MemoryStream();
                // croppedImage.Save(croppedms, ImageFormat.Jpeg);

                // Upload image to S3 bucket
                //     await Task.Run(() => S3Util.UploadToS3(this.S3Client, bucketName, keyName, croppedms));

                //     // Get a specific emotion score
                //     double emotionScore = 0.0f;
                //     if (dto.actionType != "Profile")
                //     {
                //         emotionScore = RekognitionUtil.GetEmotionScore(faceDetail.Emotions, dto.actionType);
                //     }

                //     int evaluatedAge = (faceDetail.AgeRange.High + faceDetail.AgeRange.Low) / 2;
                //     string evaluatedGender = faceDetail.Gender.Value;

                //     // Database update
                //     newStageLog = new StageLog{
                //         game_id = dto.gameId,
                //         action_type = dto.actionType,
                //         score = emotionScore,
                //         file_loc = keyName,
                //         age = evaluatedAge,
                //         gender = evaluatedGender,
                //         log_date = DateTime.Now
                //     };

                //     var value = _context.StageLog.Add(newStageLog);
                //     await _context.SaveChangesAsync();
            }

            // // Send response
            // string signedURL = S3Util.GetPresignedURL(this.S3Client, bucketName, keyName);
            // newStageLog.file_loc = signedURL;

            return(Ok(newStageLog));
        }