gpt4 book ai didi

c# - 带有点网Web api的clarifai api

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:32 25 4
gpt4 key购买 nike

我正在使用C#在.net中创建一个Web API,我想调用clarifai api获取图像标签。

我怎样才能做到这一点?


  提前致谢

最佳答案

这是更新的答案,向您展示了如何从本地计算机向Clarifai的API发送图像。本示例使用API​​的版本2。如果尝试使用版本1,则会收到429状态错误。这是为了鼓励人们使用版本2。有关此问题,请参见https://community.clarifai.com/t/request-was-throttled/402

下面的示例是采用流的方法。稍后,此流将转换为字节数组并以base 64进行编码。此编码的数据以JSON格式发送到Clarifai的API。

public void ClarifaiTagging(Stream imageStream)
{
const string ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const string CLARIFAI_API_URL = "https://api.clarifai.com/v2/models/{model}/outputs";

// Convert the stream to a byte array and convert it to base 64 encoding
MemoryStream ms = new MemoryStream();
imageStream.CopyTo(ms);
string encodedData = Convert.ToBase64String(ms.ToArray());

using (HttpClient client = new HttpClient())
{
// Set the authorization header
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + ACCESS_TOKEN);

// The JSON to send in the request that contains the encoded image data
// Read the docs for more information - https://developer.clarifai.com/guide/predict#predict
HttpContent json = new StringContent(
"{" +
"\"inputs\": [" +
"{" +
"\"data\": {" +
"\"image\": {" +
"\"base64\": \"" + encodedData + "\"" +
"}" +
"}" +
"}" +
"]" +
"}", Encoding.UTF8, "application/json");

// Send the request to Clarifai and get a response
var response = client.PostAsync(CLARIFAI_API_URL, json).Result;

// Check the status code on the response
if (!response.IsSuccessStatusCode)
{
// End here if there was an error
return null;
}

// Get response body
string body = response.Content.ReadAsStringAsync().Result.ToString();

Debug.Write(body);
}
}


希望这会有所帮助。

编辑:

要使用Clarifai API的v2标记来自URL的图像,只需使用以下内容。

HttpContent json = new StringContent(
"{" +
"\"inputs\": [" +
"{" +
"\"data\": {" +
"\"image\": {" +
"\"url\": \"" + yourUrlHere + "\"" +
"}" +
"}" +
"}" +
"]" +
"}", Encoding.UTF8, "application/json");


有关Clarifai API的完整文档,请参见 https://www.clarifai.com/developer/guide/

关于c# - 带有点网Web api的clarifai api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36997713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com