예제 #1
0
        public static (Coordinate[], string[]) GetHotelLocations(PostInput data)
        {
            try
            {
                if (data.Keywords == null)
                {
                    data.Keywords = new string[0];
                }

                string[]           popo  = new string[0];
                string[]           popo2 = new string[0];
                GoogleVisionResult res   = new GoogleVisionResult();

                if (data.Image != null && data.Image != "")
                {
                    string desc = ApiInterface.MicrosoftVision(data.Image);
                    //string[] popo = ApiInterface.MicrosoftKeywords(desc);
                    res = ApiInterface.GoogleVision(data.Image);

                    popo2 = res.descriptions.SelectMany(x => ApiInterface.MicrosoftKeywords(x)).ToArray();
                }



                string[] safakeys = data.Keywords.Select(x => x.ToLower().Trim()).ToArray();
                string[] allkeys  = data.Keywords.Concat(popo).Concat(popo2).Select(x => x.ToLower().Trim()).ToArray();
                string[] imgkeys  = popo.Concat(popo2).Select(x => x.ToLower().Trim()).ToArray();


                string[] cities = new string[] { ApiInterface.GetCity(allkeys), ApiInterface.GetCity(safakeys), ApiInterface.GetCity(imgkeys) };

                var locations = new List <Coordinate>();

                if (res.HasCoordinates)
                {
                    locations.Add(new Coordinate()
                    {
                        lat = res.Latitude, lng = res.Longitude
                    });
                }

                locations.AddRange(cities.Where(x => x != "").Select(x => ApiInterface.GetLocation(x)));

                return(locations.ToArray(), cities);
            }
            catch
            {
                return(new Coordinate[0], new string[0]);
            }
        }
예제 #2
0
        public static GoogleVisionResult GoogleVision(string base64image)
        {
            if (base64image == "")
            {
                return(new GoogleVisionResult());
            }
            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://vision.googleapis.com/v1/images:annotate?key=AIzaSyBdN2xmcmCIcp2JC7Zdc_aVBr-XVX83seU");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method      = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = $@"{{
  ""requests"": [
    {{
      ""image"": {{
        ""content"" : ""{base64image}""
      }},
      ""features"": [
        {{
          ""type"": ""LANDMARK_DETECTION"",
          ""maxResults"": 1
        }},
        {{
          ""type"": ""WEB_DETECTION"",
          ""maxResults"": 2
        }}
      ]
    }}
  ]
}}";
                    streamWriter.Write(json);
                }

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var     result = streamReader.ReadToEnd();
                    JObject data   = JObject.Parse(result);

                    GoogleVisionResult gres = new GoogleVisionResult();
                    gres.descriptions   = new string[0];
                    gres.HasCoordinates = false;

                    try
                    {
                        var essek = data["responses"][0]["landmarkAnnotations"][0]["locations"][0]["latLng"];
                        gres.Latitude       = Convert.ToDouble(essek["latitude"]);
                        gres.Longitude      = Convert.ToDouble(essek["longitude"]);
                        gres.HasCoordinates = true;
                    }
                    catch
                    { }

                    try
                    {
                        var essek = data["responses"][0]["webDetection"]["webEntities"];
                        gres.descriptions = essek.Select(x => x["description"].ToString()).ToArray();
                    }
                    catch { }

                    return(gres);
                }
            }
            catch
            {
                return(null);
            }
        }