在这个文件夹中,我们需要添加以下文件:
IVisionService.cs
视觉服务 .cs
视觉结果.cs
IEntitySearchService.cs
实体搜索服务.cs
实体结果.cs
帮助程序.cs
IVisionService.cs - 访问影像服务的接口定义,需要依赖注入
using System.IO;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public interface IVisionService
{
Task RecognizeLandmarkAsync(Stream imgStream);
Task RecognizeCelebrityAsync(Stream imgStream);
}
}
VisionService.cs - 访问影像服务的逻辑代码
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public class VisionService : IVisionService
{
const string LandmarkEndpoint = "https://eastasia.api.cognitive.microsoft.com/vision/v2.0/models/landmarks/analyze";
const string CelebrityEndpoint = "https://eastasia.api.cognitive.microsoft.com/vision/v2.0/models/celebrities/analyze";
const string Key1 = "0e290876aed45d69f6fb97bb621f71";
const string Key2 = "9799f09b87e4be6b2be132309b8e57";
private readonly IHttpClientFactory httpClientFactory;
public VisionService(IHttpClientFactory cf)
{
this.httpClientFactory = cf;
}
public async Task RecognizeLandmarkAsync(Stream imgStream)
{
VisionResult result = await this.MakePostRequest(LandmarkEndpoint, imgStream);
if (result?.result?.landmarks?.Length > 0)
{
return result?.result?.landmarks[0];
}
return null;
}
public async Task RecognizeCelebrityAsync(Stream imgStream)
{
VisionResult result = await this.MakePostRequest(CelebrityEndpoint, imgStream);
if (result?.result?.celebrities?.Length > 0)
{
return result?.result?.celebrities[0];
}
return null;
}
private async Task MakePostRequest(string uri, Stream imageStream)
{
try
{
using (HttpClient httpClient = httpClientFactory.CreateClient())
{
using (StreamContent streamContent = new StreamContent(imageStream))
{
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
using (var request = new HttpRequestMessage(HttpMethod.Post, uri))
{
request.Content = streamContent;
request.Headers.Add("Ocp-Apim-Subscription-Key", Key1);
using (HttpResponseMessage response = await httpClient.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
string resultString = await response.Content.ReadAsStringAsync();
VisionResult result = JsonConvert.DeserializeObject(resultString);
return result;
}
else
{
}
}
}
return null;
}
}
}
catch (Exception ex)
{
return null;
}
}
}
}
小提示:上面的代码中的Key1/Key2是不可用的,请用自己申请的Key和对应的Endpoint来代替。
VisionResult.cs – 认知服务返回的结果类,用于反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public class VisionResult
{
public Result result { get; set; }
public string requestId { get; set; }
}
public class Result
{
public Landmark[] landmarks { get; set; }
public Celebrity[] celebrities { get; set; }
}
public class Landmark
{
public string name { get; set; }
public double confidence { get; set; }
}
public class Celebrity
{
public virtual string name { get; set; }
public virtual double confidence { get; set; }
}
}
IEntitySearchService.cs – 访问实体搜索服务的接口定义,需要依赖注入
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public interface IEntitySearchService
{
Task SearchEntityAsync(string query);
}
}
EntitySearchService.cs – 访问实体搜索服务的逻辑代码
using System.Diagnostics;
using System.Net.Http;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public class EntitySearchService : IEntitySearchService
{
const string SearchEntityEndpoint = "https://api.cognitive.microsoft.com/bing/v7.0/entities?mkt=en-US&q=";
const string Key1 = "a0be81df8ad449481492a11107645b";
const string Key2 = "0803e4673824f9abb7487d8c3db6dd";
private readonly IHttpClientFactory httpClientFactory;
public EntitySearchService(IHttpClientFactory cf)
{
this.httpClientFactory = cf;
}
public async Task SearchEntityAsync(string query)
{
using (HttpClient hc = this.httpClientFactory.CreateClient())
{
string uri = SearchEntityEndpoint + query;
string jsonResult = await Helper.MakeGetRequest(hc, uri, Key1);
Debug.Write(jsonResult);
return jsonResult;
}
}
}
}
小提示:上面的代码中的Key1/Key2是不可用的,请用自己申请的Key和对应的Endpoint来代替。
EntityResult.cs – 认知服务返回的结果类,用于反序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public class EntityResult
{
public string _type { get; set; }
public Querycontext queryContext { get; set; }
public Entities entities { get; set; }
public Rankingresponse rankingResponse { get; set; }
}
public class Querycontext
{
public string originalQuery { get; set; }
}
public class Entities
{
public Value[] value { get; set; }
}
public class Value
{
public string id { get; set; }
public Contractualrule[] contractualRules { get; set; }
public string webSearchUrl { get; set; }
public string name { get; set; }
public string url { get; set; }
public Image image { get; set; }
public string description { get; set; }
public Entitypresentationinfo entityPresentationInfo { get; set; }
public string bingId { get; set; }
}
public class Image
{
public string name { get; set; }
public string thumbnailUrl { get; set; }
public Provider[] provider { get; set; }
public string hostPageUrl { get; set; }
public int width { get; set; }
public int height { get; set; }
public int sourceWidth { get; set; }
public int sourceHeight { get; set; }
}
public class Provider
{
public string _type { get; set; }
public string url { get; set; }
}
public class Entitypresentationinfo
{
public string entityScenario { get; set; }
public string[] entityTypeHints { get; set; }
}
public class Contractualrule
{
public string _type { get; set; }
public string targetPropertyName { get; set; }
public bool mustBeCloseToContent { get; set; }
public License license { get; set; }
public string licenseNotice { get; set; }
public string text { get; set; }
public string url { get; set; }
}
public class License
{
public string name { get; set; }
public string url { get; set; }
}
public class Rankingresponse
{
public Sidebar sidebar { get; set; }
}
public class Sidebar
{
public Item[] items { get; set; }
}
public class Item
{
public string answerType { get; set; }
public int resultIndex { get; set; }
public Value1 value { get; set; }
}
public class Value1
{
public string id { get; set; }
}
}
Helper.cs – 帮助函数
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace CognitiveMiddlewareService.CognitiveServices
{
public class Helper
{
public static byte[] GetBuffer(IFormFile formFile)
{
Stream stream = formFile.OpenReadStream();
MemoryStream memoryStream = new MemoryStream();
formFile.CopyTo(memoryStream);
var buffer = memoryStream.GetBuffer();
return buffer;
}
public static MemoryStream GetStream(byte[] buffer)
{
if (buffer == null)
{
return null;
}
return new MemoryStream(buffer, false);
}
public static async Task MakeGetRequest(HttpClient httpClient, string uri, string key)
{
try
{
using (var request = new HttpRequestMessage(HttpMethod.Get, uri))
{
request.Headers.Add("Ocp-Apim-Subscription-Key", key);
using (HttpResponseMessage response = await httpClient.SendAsync(request))
{
if (response.IsSuccessStatusCode)
{
string jsonResult = await response.Content.ReadAsStringAsync();
return jsonResult;
}
}
}
return null;
}
catch (Exception ex)
{
return null;
}
}
}
}