コード例 #1
0
        public async Task AddingImageFileWithCropShouldBeSuccessful()
        {
            string inputID = GenerateRandomID();

            try
            {
                /*
                 * Add input with concepts.
                 */
                ClarifaiResponse <List <IClarifaiInput> > addResponse = await Client.AddInputs(
                    new ClarifaiFileImage(
                        ReadResource(BALLOONS_IMAGE_FILE),
                        id : inputID,
                        crop : new Crop(0.1M, 0.2M, 0.3M, 0.4M)))
                                                                        .ExecuteAsync();

                Assert.True(addResponse.IsSuccessful);

                ClarifaiURLImage input = (ClarifaiURLImage)addResponse.Get()[0];
                Assert.AreEqual(new Crop(0.1M, 0.2M, 0.3M, 0.4M), input.Crop);
            }
            finally
            {
                /*
                 * Delete the input.
                 */
                await DeleteInput(inputID);
            }
        }
コード例 #2
0
        /// <inheritdoc />
        protected override List <IClarifaiInput> Unmarshaller(dynamic jsonObject)
        {
            var inputs = new List <IClarifaiInput>();

            foreach (var input in jsonObject.inputs)
            {
                inputs.Add(ClarifaiURLImage.Deserialize(input));
            }
            return(inputs);
        }
コード例 #3
0
        public async Task <TwiMLResult> Index(SmsRequest request)
        {
            // TODO: Define the environment variable or plug your api key in
            var CLARIFAI_API_KEY = System.Environment.GetEnvironmentVariable("CLARIFAI_API_KEY") ?? "YOUR_CLARIFAI_API_KEY_HERE";

            var response = new MessagingResponse();

            var url = Request.Form["MediaUrl0"];

            if (string.IsNullOrEmpty(url))
            {
                response.Message("Picture not found. Please send one.");
                return(TwiML(response));
            }

            try
            {
                var client = new ClarifaiClient(CLARIFAI_API_KEY);

                var image = new ClarifaiURLImage(url);

                var result = await client.PublicModels.GeneralModel.Predict(image, maxConcepts : 5).ExecuteAsync();

                if (result.IsSuccessful)
                {
                    var concepts = result.Get()[0].Data.Select(c => $"{c.Name}:{c.Value}");
                    var body     = string.Join(", ", concepts);

                    response.Message(body);
                }
                else
                {
                    response.Message($"The request was not successful: {result.Status.Description}");
                }
            }
            catch (Exception e)
            {
                response.Message($"Something went wrong: {e.Message}");
            }

            return(TwiML(response));
        }
コード例 #4
0
 public static SearchBy ImageVisually(ClarifaiURLImage image, Crop crop = null)
 {
     return(ImageVisually(image.URL, crop));
 }
コード例 #5
0
 /// <inheritdoc />
 protected override IClarifaiInput Unmarshaller(dynamic jsonObject)
 {
     return(ClarifaiURLImage.Deserialize(jsonObject.input));
 }
コード例 #6
0
        public async Task SearchInputsByNameRequestAndResponseShouldBeCorrect()
        {
            var httpClient = new FkClarifaiHttpClient(
                postResponse: @"
{
  ""status"": {
    ""code"": 10000,
    ""description"": ""Ok""
  },
  ""hits"": [
    {
      ""score"": 0.99,
      ""input"": {
        ""id"": ""@inputID"",
        ""created_at"": ""2016-11-22T17:06:02Z"",
        ""data"": {
          ""image"": {
            ""url"": ""@inputURL""
          }
        },
        ""status"": {
          ""code"": 30000,
          ""description"": ""Download complete""
        }
      }
    }
  ]
}
");

            var client   = new ClarifaiClient(httpClient);
            var response = await client.SearchInputs(SearchBy.ConceptName("@conceptName"))
                           .ExecuteAsync();

            var expectedRequestBody = JObject.Parse(@"
{
  ""query"": {
    ""ands"": [
      {
        ""output"": {
          ""data"": {
            ""concepts"": [
              {
                ""name"": ""@conceptName""
              }
            ]
          }
        }
      }
    ]
  }
}
");

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

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

            Assert.AreEqual(1, searchHits.Count);
            Assert.AreEqual("@inputID", searchHits[0].Input.ID);

            IClarifaiInput input = searchHits[0].Input;

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

            ClarifaiURLImage image = (ClarifaiURLImage)input;

            Assert.AreEqual("@inputURL", image.URL);
        }
コード例 #7
0
        public async Task SearchInputsByGeoLocationRequestAndResponseShouldBeCorrect()
        {
            var httpClient = new FkClarifaiHttpClient(
                postResponse: @"
{
  ""status"": {
    ""code"": 10000,
    ""description"": ""Ok""
  },
  ""hits"": [
    {
      ""score"": 0.99,
      ""input"": {
        ""id"": ""@inputID"",
        ""created_at"": ""2016-11-22T17:06:02Z"",
        ""data"": {
          ""image"": {
            ""url"": ""@inputURL""
          }
        },
        ""status"": {
          ""code"": 30000,
          ""description"": ""Download complete""
        }
      }
    }
  ]
}
");

            var client   = new ClarifaiClient(httpClient);
            var response = await client.SearchInputs(SearchBy.Geo(new GeoPoint(1.5M, -1),
                                                                  new GeoRadius(1, GeoRadius.RadiusUnit.WithinKilometers)))
                           .ExecuteAsync();

            var expectedRequestBody = JObject.Parse(@"
{
  ""query"": {
    ""ands"": [
      {
        ""input"": {
          ""data"": {
            ""geo"": {
              ""geo_point"": {
                ""longitude"": 1.5,
                ""latitude"": -1.0
              },
              ""geo_limit"": {
                ""type"": ""withinKilometers"",
                ""value"": 1.0
              }
            }
          }
        }
      }
    ]
  }
}");

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

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

            Assert.AreEqual(1, searchHits.Count);
            Assert.AreEqual("@inputID", searchHits[0].Input.ID);

            IClarifaiInput input = searchHits[0].Input;

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

            ClarifaiURLImage image = (ClarifaiURLImage)input;

            Assert.AreEqual("@inputURL", image.URL);
        }
コード例 #8
0
        public IEnumerable <IMachineTag> GetTagsForImageUrl(string imageUrl)
        {
            var clarifaiInput = new ClarifaiURLImage(imageUrl);

            return(this.GetTagsForInput(clarifaiInput));
        }
コード例 #9
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);
        }