src/Classificationbox.Net/ServiceWrapper.cs
/// <summary>
/// Requests the prediction of the given image file by the specified model.
/// </summary>
/// <param name="model_id">id of the model to use</param>
/// <param name="filename">filename of the image</param>
/// <returns></returns>
Requests the prediction of the given image file by the specified model.
Requests the prediction of the given image file by the specified model .
public PredictionResponse Predict(string model_id, string filename)
        {
            Byte[] bytes = File.ReadAllBytes(filename);
            String base64 = Convert.ToBase64String(bytes);
            using (HttpClient client = new HttpClient())
            {
                var payload = string.Format("{{ \"limit\": 10, \"inputs\": [ {{ \"key\": \"image\", \"type\": \"image_base64\", \"value\": \"{0}\"}} ] }}", base64);
                HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json");
                var request = new HttpRequestMessage
                {
                    Method = HttpMethod.Post,
                    RequestUri = new Uri(string.Format("{0}/classificationbox/models/{1}/predict", _baseUrl, model_id)),
                    Headers = {
                        { HttpRequestHeader.Accept.ToString(), "application/json; charset=utf-8" }
                    },
                    Content = content
                };
                HttpResponseMessage rsp;
                try
                {
                    rsp = client.SendAsync(request).GetAwaiter().GetResult();
                }
                catch (Exception ex)
                {
                    return new PredictionResponse() { error = ex.Message };
                }
                var s = rsp.Content.ReadAsStringAsync().Result;
                var o = JsonConvert.DeserializeObject<PredictionResponse>(s);
                return o;
            }
        }
public PredictionResponse Predict ( string model_id , string filename ) { Byte [ ] bytes = File . ReadAllBytes ( filename ) ; String base64 = Convert . ToBase64String ( bytes ) ; using ( HttpClient client = new HttpClient ( ) ) { var payload = string . Format ( " \" \" \" \" \" \" \" \" \" \" \" \" \" \" \" \" " , base64 ) ; HttpContent content = new StringContent ( payload , Encoding . UTF8 , " " ) ; var request = new HttpRequestMessage { Method = HttpMethod . Post , RequestUri = new Uri ( string . Format ( " " , _baseUrl , model_id ) ) , Headers = { { HttpRequestHeader . Accept . ToString ( ) , " " } } , Content = content } ; HttpResponseMessage rsp ; try { rsp = client . SendAsync ( request ) . GetAwaiter ( ) . GetResult ( ) ; } catch ( Exception ex ) { return new PredictionResponse ( ) { error = ex . Message } ; } var s = rsp . Content . ReadAsStringAsync ( ) . Result ; var o = JsonConvert . DeserializeObject < PredictionResponse > ( s ) ; return o ; } }
Requests the prediction of the given image file by the specified model.
Requests the prediction of the given image file by the specified model .
None
model_id
string
id of the model to use
id of the model to use
filename
string
filename of the image
filename of the image