[英]How do I convert the following Python script to C#?
I am currently working on TF ML project and its working. 我目前正在TF ML项目及其工作中。 I am writing my client side with C#.
我正在用C#编写客户端。 I already used an updated Python script for testing shown below.
我已经使用了更新的Python脚本进行测试,如下所示。
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#. 我正在用C#实现它。 I have come to a hard stop with converting the image to binary and JSON formatting.
我将图像转换为二进制和JSON格式时遇到了困难。
My C# routine is as follows 我的C#例程如下
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. 我确定我的json格式不正确。 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. 如果我能看到在Python请求起作用时通过嗅探端口来发送到Server的字符串是最好的。 Any software I could check ?
我可以检查任何软件吗?
The problem might be the colon inside the quotes. 问题可能出在引号内。 Try:
尝试:
"""image"":"
instead of """image:"""
"""image"":"
代替"""image:"""
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.