这是我的代码来捕捉webcamTexture的图像。
public void SavePhoto()
{
Debug.Log(Application.dataPath);
Texture2D photo = new Texture2D(camTexture.width, camTexture.height);
photo.SetPixels(camTexture.GetPixels());
photo.Apply();
//Encode to a PNG
byte[] bytes = photo.EncodeToJPG();
System.IO.File.WriteAllBytes(Application.dataPath + "/photo.png", bytes);
predictImage(bytes);
}
这是我的代码发送图像到API
public async void predictImage(byte[] bytes)
{
ByteArrayContent content = new ByteArrayContent(bytes);
var response = await client.PostAsync("http://74.235.83.70:80", content);
var responseString = await response.Content.ReadAsStringAsync();
Debug.Log(responseString);
}
这是以字节数组的形式发送图像,但是我想以图像文件的形式发送图像,我该怎么做呢?实际上,那个API是一个用于图像分类的机器学习模型。
1条答案
按热度按时间kkbh8khc1#
可以使用内置工具将纹理编码为字节数组:https://docs.unity3d.com/ScriptReference/ImageConversion.EncodeToJPG.html