public async Task <IActionResult> TextractTest([FromBody] TextractTestDTO dto)
        {
            List <Block> blocks = 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 Textract API
                blocks = await TextractUtil.GetTextFromStream(this.TextractClient, ms);

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

            return(Ok(blocks));
        }
Exemple #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))
            {
                if (dto.stage_id == 1)
                {
                    // 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");
                    }
                }
                else if (dto.stage_id == 2)
                {
                    // call Textract API
                    List <Block> blocks = await TextractUtil.GetTextFromStream(this.TextractClient, ms);

                    List <string> texts = new List <string>();
                    foreach (Block block in blocks)
                    {
                        texts.Add(block.Text);
                        Console.Write(block.Text + " ");
                    }
                    var matchedObject = _context.StageObject.Where(x => x.game_id == dto.game_id &&
                                                                   x.stage_id == dto.stage_id &&
                                                                   x.found_yn == "N" &&
                                                                   texts.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));
        }