0

I am currently working on TF ML project and its working. I am writing my client side with C#. I already used an updated Python script for testing shown below.

import requests
import json
from keras.preprocessing.image import img_to_array, array_to_img
from keras.preprocessing import image

flowers = ['c:/flower_photos/daisy/107592979_aaa9cdfe78_m.jpg', 'c:/flower_photos/daisy/134409839_71069a95d1_m.jpg', 'c:/flower_photos/daisy/144099102_bf63a41e4f_n.jpg','c:/flower_photos/daisy/154332674_453cea64f4.jpg']
for x in flowers:
    image1 = img_to_array(image.load_img(x, target_size=(128,128))) / 255
    payload = {
      "instances": [{'image': image1.tolist()},
    ]
    }
    print("sending request...")
    r = requests.post('http://localhost:8501/v1/models/squishbumps/versions/1:predict', json=payload)
    print(r.content)

I am implementing this with C#. I have come to a hard stop with converting the image to binary and JSON formatting.

My C# routine is as follows

public string PostImageToServerAndClassify(string imagePath)
        {
            //https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c
            string result = null;
            string ModelName = cmbProjectNames.Text.Replace(" ", "");
            string status_url = String.Format("http://localhost:{0}/v1/models/{1}/versions/{2}:predict", txtPort.Text, ModelName, txtVersion.Text);
            string Base64Image = ImageToBase64String(imagePath);
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(status_url);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = @"{"+ @"""instances""" + @":[{" + @"""image:""" +  Base64Image + @"}]}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
            }
            return result;
        }

Image to binary conversion routine is

public string ImageToBase64String(string imagePath)
{
    //https://arcanecode.com/2007/03/21/encoding-strings-to-base64-in-c/
    System.Drawing.Image img = Image.FromFile(imagePath);
    MemoryStream ms = new MemoryStream();
    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
    string returnValue = System.Convert.ToBase64String(ms.ToArray());
    return returnValue;
}

Currently I am getting following error :

{
    "error": "JSON Parse error: Missing a name for object member. at offset: 1"
}

I am sure that my json formatting is not right. Could someone show me how I could get this fixed ?

If I could see whats the string comming to Server by sniffing the port when Python requests works is the best. Any software I could check ?

PCG
  • 784
  • 13
  • 27
  • Seems like your C# JSON body is formatted differently? In the Python one you put "instances" as the key to the image data, but you don't seem to do that in the C# JSON. – granters Feb 10 '19 at 03:39

1 Answers1

0

The problem might be the colon inside the quotes. Try:

"""image"":" instead of """image:"""

Adrita Sharma
  • 17,967
  • 8
  • 40
  • 61