public void SearchVisuallyByImageUrlWithCropShouldThrowWhenConstructed()
 {
     Assert.Throws <ClarifaiException>(() =>
     {
         SearchBy.ImageVisually(CELEB1, new Crop(0.1M, 0.2M, 0.3M, 0.4M));
     });
 }
 public void SearchVisuallyByImageFileWithCropShouldThrowWhenConstructed()
 {
     Assert.Throws <ClarifaiException>(() =>
     {
         SearchBy.ImageVisually(
             new ClarifaiFileImage(ReadResource(BALLOONS_IMAGE_FILE)),
             new Crop(0.1M, 0.2M, 0.3M, 0.4M));
     });
 }
        public async Task SearchVisuallyByImageUrlShouldBeSuccessful()
        {
            ClarifaiResponse <SearchInputsResult> response =
                await Client.SearchInputs(SearchBy.ImageVisually(CELEB1))
                .ExecuteAsync();

            Assert.True(response.IsSuccessful);
            Assert.NotNull(response.Get().SearchHits);
        }
        public async Task SearchVisuallyByImageUrlUsingImageShouldBeSuccessful()
        {
            ClarifaiResponse <SearchInputsResult> response =
                await Client.SearchInputs(SearchBy.ImageVisually(new ClarifaiURLImage(CELEB1)))
                .ExecuteAsync();

            AssertResponseSuccess(response);
            Assert.NotNull(response.Get().SearchHits);
        }
        public async Task SearchVisuallyByImageFileShouldBeSuccessful()
        {
            ClarifaiResponse <SearchInputsResult> response =
                await Client.SearchInputs(SearchBy.ImageVisually(
                                              new ClarifaiFileImage(ReadResource(BALLOONS_IMAGE_FILE))))
                .ExecuteAsync();

            AssertResponseSuccess(response);
            Assert.NotNull(response.Get().SearchHits);
        }
        public async Task SearchVisuallyByImageFileWithCropShouldBeSuccessful()
        {
            ClarifaiResponse <SearchInputsResult> response =
                await Client.SearchInputs(SearchBy.ImageVisually(
                                              new ClarifaiFileImage(ReadResource(BALLOONS_IMAGE_FILE)),
                                              new Crop(0.1M, 0.2M, 0.3M, 0.4M)))
                .ExecuteAsync();

            Assert.True(response.IsSuccessful);
            Assert.NotNull(response.Get().SearchHits);
        }
Beispiel #7
0
        public static void Test()
        {
            Console.WriteLine("Testing Started");
            _testImages = _imageDictionary.Where(x => File.ReadAllLines(_testPath).Contains(x.Key)).ToDictionary(x => x.Key, x => x.Value);
            Console.WriteLine($"--Test" + $" Started {DateTime.Now}");
            foreach (var testImage in _testImages)
            {
                var task = _client.SearchInputs(SearchBy.ImageVisually(testImage.Value)).PerPage(5)
                           .Page(1)
                           .ExecuteAsync();

                task.Wait();
                var jsonResult = JObject.Parse(task.Result.RawBody);
                File.WriteAllText(_resultPath + testImage.Key + ".txt", task.Result.RawBody);
            }

            Console.WriteLine($"--Testing Finished {DateTime.Now}");
        }
Beispiel #8
0
        // GET: /<controller>/
        public ActionResult Index(string productcode)
        {
            init();
            var task = _client.SearchInputs(SearchBy.ImageVisually(new ClarifaiFileImage(System.IO.File.ReadAllBytes(_imagesPath + productcode + ".jpg"))))
                       .PerPage(5)
                       .Page(1)
                       .ExecuteAsync();

            task.Wait();
            var jsonResult = JObject.Parse(task.Result.RawBody);


            var hits = jsonResult["hits"].Take(6);
            //Console.Write($"--Item Price:[{exactPrice}]");

            dynamic data     = new ExpandoObject();
            var     products = new List <System.Dynamic.ExpandoObject>();
            var     prices   = new List <decimal>();

            foreach (var hit in hits)
            {
                dynamic result = new System.Dynamic.ExpandoObject();
                if (productcode != (string)hit["input"]["id"])
                {
                    result.productcode = (string)hit["input"]["id"];
                    result.score       = Math.Truncate(decimal.Parse((string)hit["score"]) * 100);;
                    result.price       = _priceDictionary[result.productcode];
                    prices.Add(_priceDictionary[result.productcode]);
                    products.Add(result);
                }
            }
            data.products       = products;
            data.originalPrice  = _priceDictionary[productcode];;
            data.min            = prices.Min();
            data.max            = prices.Max();
            data.avg            = prices.Average();
            Response.StatusCode = 200;
            return(Json(data));
        }
        public async Task <ActionResult <Artwork> > PostArtFinder(ArtFinder artFinder)
        {
            try
            {
                //Get location Id from Nando's API using supplied coordinates (if supplied and pass to Clarifai)

                double successScore = Convert.ToDouble(_configuration["ClarifaiSuccessScore"]);
                var    client       = new ClarifaiClient(_configuration["ClarifaiAPIKey"]);

                int    restaurantResultLimit  = Convert.ToInt32(_configuration["RestaurantResultLimit"]);
                double restaurantSearchRadius = Convert.ToDouble(_configuration["RestaurantSearchRadius"]);
                string distanceUnit           = _configuration["DistanceUnit"];

                string clarifaiImageInputId = "";
                Double clarifaiMatchScore   = 0;

                byte[] imageBytes = null;

                if (_configuration["ClarifaiTestImage"] != "")
                {
                    //temporary code
                    byte[] imageBytesXX = null;
                    imageBytesXX = System.IO.File.ReadAllBytes(_configuration["ClarifaiTestImage"]);

                    string test = System.Convert.ToBase64String(imageBytesXX);
                    imageBytes = System.Convert.FromBase64String(test);
                }
                else
                {
                    //Convert Base64 Encoded string to Byte Array.
                    imageBytes = System.Convert.FromBase64String(artFinder.artImage);
                }


                //Temp code start - not reqired if the search is performed in Clarifai with Geo Coordinates

                string restaurantLocationId = "";

                //Check if the customer is in a Restaurant
                var allLocations = await _context.RestaurantLocations.Where(a => a.longitude != 0).ToListAsync();

                //for each entry in artists, get artist links
                foreach (RestaurantLocation restaurantLocation in allLocations)
                {
                    if (restaurantLocation.latitude.HasValue)
                    {
                        restaurantLocation.distance = new Coordinates(artFinder.latitude.GetValueOrDefault(), artFinder.longitude.GetValueOrDefault()).DistanceTo(new Coordinates(restaurantLocation.latitude.Value, restaurantLocation.longitude.Value), UnitOfLength.Miles);
                    }
                }

                var distanceInAscOrder = allLocations.OrderBy(s => s.distance).Take(restaurantResultLimit);

                foreach (RestaurantLocation restaurantLocation in distanceInAscOrder)
                {
                    if (restaurantLocation.distance <= restaurantSearchRadius)
                    {
                        restaurantLocationId = restaurantLocation.id;
                        break;
                    }
                }

                //Temp code end - not reqired if the search is performed in Clarifai with Geo Coordinates


                // if location is supplied, pass it to clarifai
                if (restaurantLocationId != "")
                {
                    var geoPoint  = new Clarifai.DTOs.GeoPoint((decimal)artFinder.latitude, (decimal)artFinder.longitude);
                    var geoRadius = new Clarifai.DTOs.GeoRadius((decimal)restaurantSearchRadius, Clarifai.DTOs.GeoRadius.RadiusUnit.WithinMiles);


                    //var clarifaiResponse = await client.SearchInputs(SearchBy.Geo(geoPoint, geoRadius),
                    //                                                 SearchBy.ImageVisually(imageBytes))
                    //    .Page(1)
                    //    .ExecuteAsync();

                    //Temp change, we will revert it once clarify geo points are present

                    var clarifaiResponse = await client.SearchInputs(SearchBy.ImageVisually(imageBytes))
                                           .Page(1)
                                           .PerPage(100)
                                           .ExecuteAsync(); // pagination doesn't work, requires investigation. Email sent to Clarifai.

                    _log.LogInformation("Clarifai Response --" + clarifaiResponse.Status.Type + " " + clarifaiResponse.Status.StatusCode + ", " + clarifaiResponse.Status.Description + ":" + clarifaiResponse.Status.ErrorDetails);

                    if (clarifaiResponse.HttpCode == System.Net.HttpStatusCode.OK)
                    {
                        if (clarifaiResponse.IsSuccessful)
                        {
                            Clarifai.DTOs.ClarifaiStatus clarifaiResponseStatus = (Clarifai.DTOs.ClarifaiStatus)clarifaiResponse.Status;
                            //success code in 10000
                            if (clarifaiResponseStatus.StatusCode == 10000)
                            {
                                SearchResults searchResults = JsonConvert.DeserializeObject <SearchResults>(clarifaiResponse.RawBody);

                                //for each entry in artworks, get location name from restaurant location API
                                foreach (Hit hit in searchResults.hits)
                                {
                                    // check if the match score is heigher than the value specified in config fime
                                    if (hit.score >= successScore)
                                    {
                                        // temporay change - code should be removed after testing
                                        // check if the Artwork exists in location
                                        var locationArtworks = await _context.Artwork.Where(b => b.id == Int32.Parse(hit.input.id) && b.locationId == restaurantLocationId).ToListAsync();

                                        foreach (Artwork locationArtwork in locationArtworks)
                                        {
                                            clarifaiImageInputId = hit.input.id;
                                            clarifaiMatchScore   = hit.score * 100;
                                            break;
                                        }

                                        if (clarifaiImageInputId != "")
                                        {
                                            _log.LogInformation("Retrieved clarifaiImageInputId..");
                                            break;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _log.LogInformation(@"Clarifai response unsuccessful - Ststus Code:" + clarifaiResponseStatus.StatusCode.ToString());
                                return(NotFound(@"Clarifai response unsuccessful - Ststus Code:" + clarifaiResponseStatus.StatusCode.ToString()));
                            }
                        }
                        else
                        {
                            _log.LogInformation(@"Clarifai response unsuccessful");
                            return(NotFound(@"Clarifai response unsuccessful"));
                        }
                    }
                    else
                    {
                        _log.LogInformation(@"Clarifai response HTTPCode:" + clarifaiResponse.HttpCode.ToString());
                        return(NotFound(@"Clarifai response HTTPCode:" + clarifaiResponse.HttpCode.ToString()));
                    }

                    if (clarifaiImageInputId == "")
                    {
                        _log.LogInformation(@"Image not found in Clarifai");
                        return(NotFound(@"Image not found in Clarifai"));
                    }
                }
                else
                {
                    var clarifaiResponse = await client.SearchInputs(SearchBy.ImageVisually(imageBytes))
                                           .Page(1)
                                           .ExecuteAsync();

                    _log.LogInformation("Clarifai Response--" + clarifaiResponse.Status.Type + " " + clarifaiResponse.Status.StatusCode + ", " + clarifaiResponse.Status.Description + ":" + clarifaiResponse.Status.ErrorDetails);

                    if (clarifaiResponse.HttpCode == System.Net.HttpStatusCode.OK)
                    {
                        if (clarifaiResponse.IsSuccessful)
                        {
                            Clarifai.DTOs.ClarifaiStatus clarifaiResponseStatus = (Clarifai.DTOs.ClarifaiStatus)clarifaiResponse.Status;
                            //success code in 10000
                            if (clarifaiResponseStatus.StatusCode == 10000)
                            {
                                SearchResults searchResults = JsonConvert.DeserializeObject <SearchResults>(clarifaiResponse.RawBody);

                                //for each entry in artworks, get location name from restaurant location API
                                foreach (Hit hit in searchResults.hits)
                                {
                                    // check if the match score is heigher than the value specified in config fime
                                    if (hit.score >= successScore)
                                    {
                                        clarifaiImageInputId = hit.input.id;
                                        clarifaiMatchScore   = hit.score * 100;
                                    }
                                    break;
                                }
                            }
                            else
                            {
                                _log.LogInformation(@"Clarifai response unsuccessful - Ststus Code:" + clarifaiResponseStatus.StatusCode.ToString());
                                return(NotFound(@"Clarifai response unsuccessful - Ststus Code:" + clarifaiResponseStatus.StatusCode.ToString()));
                            }
                        }
                        else
                        {
                            _log.LogInformation(@"Clarifai response unsuccessful");
                            return(NotFound(@"Clarifai response unsuccessful"));
                        }
                    }
                    else
                    {
                        _log.LogInformation(@"Clarifai response HTTPCode:" + clarifaiResponse.HttpCode.ToString());
                        return(NotFound(@"Clarifai response HTTPCode:" + clarifaiResponse.HttpCode.ToString()));
                    }

                    if (clarifaiImageInputId == "")
                    {
                        _log.LogInformation(@"Image not found in Clarifai");
                        return(NotFound(@"Image not found in Clarifai"));
                    }
                }


                //UI only requires artwork id and score
                Artwork artwork = null;
                if (clarifaiImageInputId != "")
                {
                    artwork            = new Artwork();
                    artwork.id         = Int32.Parse(clarifaiImageInputId);
                    artwork.matchScore = clarifaiMatchScore;
                }

                ////Find artwok matching on clarifai image input id
                //var artworks = await _context.Artwork.Where(b => b.id == Int32.Parse(clarifaiImageInputId)).ToListAsync();

                //if (artworks.Count == 0)
                //{
                //    _log.LogInformation(@"Artwork not found in Artwork DB");
                //    return NotFound(@"Artwork not found in Artwork DB");
                //}
                //else
                //{
                //    //Get 1st matching item
                //    foreach (Artwork artworkTemp in artworks)
                //    {
                //        artworkTemp.matchScore = clarifaiMatchScore;
                //        artwork = artworkTemp;

                //        //Get artwork location Id
                //        var locations = await _context.RestaurantLocations.Where(b => b.id == artwork.locationId).ToListAsync();

                //        foreach (RestaurantLocation location in locations)
                //        {
                //            artwork.locationName = location.locationName;
                //            break;
                //        }
                //        break;
                //    }

                //}
                return(artwork);
            }

            catch (Exception e)
            {
                _log.LogError(e.Message);
                Console.WriteLine("{0} Second exception caught.", e);
            }

            return(NotFound());
        }
Beispiel #10
0
        public async Task SearchInputWithByImageVisuallyPaginationRequestAndResponseShouldBeCorrect()
        {
            var httpClient = new FkClarifaiHttpClient(
                postResponse: @"
{
  ""status"": {
    ""code"": 10000,
    ""description"": ""Ok"",
    ""req_id"": ""@requestID""
  },
  ""id"": ""@ID"",
  ""hits"": [
      {
        ""score"": 0.60579073,
        ""input"": {
          ""id"": ""@inputID"",
          ""data"": {
            ""image"": {
              ""url"": ""@found-url""
            },
            ""concepts"": [
            {
              ""id"": ""wedding"",
              ""name"": ""wedding"",
              ""value"": 1,
              ""app_id"": ""@appID""
            }
            ]
          },
          ""created_at"": ""2019-05-24T10:45:35.225638Z"",
          ""modified_at"": ""2019-05-24T10:45:35.809871Z"",
          ""status"": {
            ""code"": 30000,
            ""description"": ""Download complete""
          }
        }
      },
    ],
    ""query"": {
      ""ands"": [
      {
        ""output"": {
          ""input"": {
            ""data"": {
              ""image"": {
                ""url"": ""@input-url""
              }
            }
          }
        }
      }
      ]
    }
  }
");

            var client   = new ClarifaiClient(httpClient);
            var response = await client.SearchInputs(SearchBy.ImageVisually(
                                                         new ClarifaiURLImage(url: "@input-url")
                                                         ))
                           .Page(5)
                           .PerPage(2)
                           .ExecuteAsync();

            var expectedRequestBody = JObject.Parse(@"
{
  ""query"": {
    ""ands"": [
      {
        ""output"": {
          ""input"": {
            ""data"": {
              ""image"": {
                ""url"": ""@input-url""
              }
            }
          }
        }
      }
    ]
  },
  ""pagination"": {
    ""page"": 5,
    ""per_page"": 2
  }
}
");

            Assert.True(JToken.DeepEquals(expectedRequestBody, httpClient.PostedBody));
            Assert.True(response.IsSuccessful);

            List <SearchHit> searchHits = response.Get().SearchHits;

            Assert.AreEqual(1, searchHits.Count);

            IClarifaiInput input = searchHits[0].Input;

            Assert.AreEqual(InputType.Image, input.Type);
            Assert.AreEqual(InputForm.URL, input.Form);

            ClarifaiURLImage image = (ClarifaiURLImage)input;

            Assert.AreEqual("@found-url", image.URL);
        }