コード例 #1
0
        public IEnumerable <IMachineTag> GetTagsForImageBytes(byte[] bytes)
        {
            var image = Image.FromBytes(bytes);

            var labels   = this.client.DetectLabels(image);
            var webInfos = this.client.DetectWebInformation(image);

            foreach (var machineTag in ToMTags(labels, webInfos))
            {
                yield return(machineTag);
            }
        }
コード例 #2
0
        public async Task <IHttpActionResult> describeImageWithVoice([FromBody] ImageToVoice imageToVoice)
        {
            try
            {
                var user = await UserManager.FindByIdAsync(imageToVoice.userId);

                var faceDescription = new FaceDescription();
                var googleClient    = await ImageAnnotatorClient.CreateAsync();

                var byteImage = Convert.FromBase64String(imageToVoice.base64Image);
                var image     = Image.FromBytes(byteImage);

                var responseForFacesGoogle = await googleClient.DetectFacesAsync(image);

                var responseForLabels = await googleClient.DetectLabelsAsync(image);

                var responseForLandmark = await googleClient.DetectLandmarksAsync(image);

                var responseForLogo = await googleClient.DetectLogosAsync(image);

                var analyzeImage = new AnalyzeImage();
                analyzeImage.responseFaceGoogle     = responseForFacesGoogle;
                analyzeImage.responseForLabels      = responseForLabels;
                analyzeImage.responseForLogoGoogle  = responseForLogo;
                analyzeImage.responseLandMarkGoogle = responseForLandmark;

                var responseFormMicrosoftFace = new List <CognitiveMicrosoft>();
                if (responseForFacesGoogle.Count > 0)
                {
                    responseFormMicrosoftFace = await faceDescription.MakeAnalysisRequestAsync(byteImage);

                    analyzeImage.responseForFacesMicrosft = responseFormMicrosoftFace;
                }

                string base64Voice = analyzeImage.describeImageWithVoice();

                rela.ImagesProceseds.Add(new ImagesProcesed {
                    UserId = user.Id, date = DateTime.Now, image = imageToVoice.base64Image
                });
                await rela.SaveChangesAsync();

                int imageId = rela.ImagesProceseds.OrderByDescending(img => img.date).ToList()[0].imageId;

                if (responseForFacesGoogle.Count > 0)
                {
                    rela.GoogleFaces.Add(new GoogleFace {
                        GoogleFace1 = JsonConvert.SerializeObject(responseForFacesGoogle), imageId = imageId
                    });
                }

                if (responseForLabels.Count > 0)
                {
                    rela.GoogleLabels.Add(new GoogleLabel {
                        GoogleLabel1 = JsonConvert.SerializeObject(responseForLabels), imageId = imageId
                    });
                }

                if (responseForLandmark.Count > 0)
                {
                    rela.GoogleLandmarks.Add(new GoogleLandmark {
                        GoogleLandamark = JsonConvert.SerializeObject(responseForLandmark), imageId = imageId
                    });
                }

                if (responseForLogo.Count > 0)
                {
                    rela.GoogleLogoes.Add(new GoogleLogo {
                        GoogleLogo1 = JsonConvert.SerializeObject(responseForLogo), imageId = imageId
                    });
                }

                if (responseFormMicrosoftFace.Count > 0)
                {
                    rela.MicrosoftFaces.Add(new MicrosoftFace {
                        imageId = imageId, MicrosoftFace1 = JsonConvert.SerializeObject(responseFormMicrosoftFace)
                    });
                }

                rela.Voices.Add(new Voice {
                    imageId = imageId, GoogleVoice = base64Voice
                });

                await rela.SaveChangesAsync();

                DescribeImage describeImage = new DescribeImage()
                {
                    googleFace    = responseForFacesGoogle,
                    label         = responseForLabels,
                    landmark      = responseForLandmark,
                    logo          = responseForLogo,
                    voiceBase64   = base64Voice,
                    microsoftFace = responseFormMicrosoftFace
                };

                return(Ok(describeImage));
            }
            catch (Exception ex) {
                return(BadRequest("Error"));
            }
        }