[Serializable]
public class MyAwaitableImage : AwaitableAttachment
{
// Mandatory: you should have this ctor as it is used by the recognizer
public MyAwaitableImage(Attachment source) : base(source) { }
// Mandatory: you should have this serialization ctor as well & call base
protected MyAwaitableImage(SerializationInfo info, StreamingContext context) : base(info, context) { }
// Optional: here you can check for content-type for ex 'image/png' or other..
public override async Task<ValidateResult> ValidateAsync<T>(IField<T> field, T state)
{
var result = await base.ValidateAsync(field, state);
if (result.IsValid)
{
var isValidForMe = this.Attachment.ContentType.ToLowerInvariant().Contains("image/png");
if (!isValidForMe)
{
result.IsValid = false;
result.Feedback = $"Hey, dude! Provide a proper 'image/png' attachment, not any file on your computer like '{this.Attachment.Name}'!";
}
}
return result;
}
// Optional: here you can provide additional or override custom help text completely..
public override string ProvideHelp<T>(IField<T> field)
{
var help = base.ProvideHelp(field);
help += $"{Environment.NewLine}- Only 'image/png' can be attached to this field.";
return help;
}
// Optional: here you can define your custom logic to get the attachment data or add custom logic to check it, etc..
protected override async Task<Stream> ResolveFromSourceAsync(Attachment source)
{
var result = await base.ResolveFromSourceAsync(source);
// You can apply custom logic to result or avoid calling base and resolve it yourself
// For ex. if you plan to use your instance several times you can return a MemoryStream instead
return result;
}
}
// here is the form that the Bot takes to the user
[Serializable]
public class VehicleForm
{
[Prompt("What's your name?")]
public string Name { get; set; }
[Prompt("Upload your image")]
public MyAwaitableImage Image;
我正在bot框架中创建一个表单,我想实现图像识别API并使用它来验证用户上传的图像。如果该图像不是人的图像,则机器人将拒绝并要求该人提供有效的图像。请我需要帮助
答案 0 :(得分:1)
我想实现图像识别API,并使用它来验证用户上传的图像。如果该图像不是人的图像,则机器人将拒绝并要求该人提供有效的图像。
Computer Vision API可以帮助分析图像,您可以使用验证方法执行图像分析。以下示例代码对我有用,您可以参考它。
public override async Task<ValidateResult> ValidateAsync<T>(IField<T> field, T state)
{
var result = await base.ValidateAsync(field, state);
if (result.IsValid)
{
var isValidForMe = this.Attachment.ContentType.ToLowerInvariant().Contains("image/png");
if (!isValidForMe)
{
result.IsValid = false;
result.Feedback = $"Hey, dude! Provide a proper 'image/png' attachment, not any file on your computer like '{this.Attachment.Name}'!";
}
else
{
var url = this.Attachment.ContentUrl;
HttpClient httpClient = new HttpClient();
Stream filestrem = await httpClient.GetStreamAsync(url);
httpClient.Dispose();
byte[] ImageAsByteArray = null;
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
byte[] buf = new byte[1024];
count = filestrem.Read(buf, 0, 1024);
ms.Write(buf, 0, count);
} while (filestrem.CanRead && count > 0);
ImageAsByteArray = ms.ToArray();
}
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", "{Subscription_Key_here}");
// Request parameters. A third optional parameter is "details".
string requestParameters =
"visualFeatures=Categories";
// Assemble the URI for the REST API Call.
string uri = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.0/analyze?" + requestParameters;
HttpResponseMessage response;
using (ByteArrayContent content = new ByteArrayContent(ImageAsByteArray))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json"
// and "multipart/form-data".
content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
// Make the REST API call.
response = await client.PostAsync(uri, content);
}
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
var rs = Newtonsoft.Json.Linq.JToken.Parse(contentString);
if (rs.HasValues)
{
string val = ((Newtonsoft.Json.Linq.JValue)((Newtonsoft.Json.Linq.JProperty)((Newtonsoft.Json.Linq.JContainer)((Newtonsoft.Json.Linq.JContainer)((Newtonsoft.Json.Linq.JContainer)((Newtonsoft.Json.Linq.JContainer)rs).First).First).First).First).Value).Value.ToString();
if (val!= "people_")
{
result.IsValid = false;
result.Feedback = $"The image '{this.Attachment.Name}' is not a person's image!";
}
}
}
}
return result;
}
测试结果:
注意:
您还可以查看以下文档,以了解如何使用Computer Vision API /客户端库分析图像。
更新:
请我需要机器人能够描述图像内容(description),而不仅仅是进行验证。
您可以在querystring参数中包含“描述”以返回图像内容的描述。有关更多信息,您可以阅读"Computer Vision API" documentation。
string requestParameters = "visualFeatures=Categories,Description";