CognitiveServices文件夹
在这个文件夹中,我们需要添加以下文件:
-
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<Landmark> RecognizeLandmarkAsync(Stream imgStream);
Task<Celebrity> 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<Landmark> 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<Celebrity> 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<VisionResult> 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<VisionResult>(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<string> 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<string> 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<string> 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;
}
}
}
}
在线教程
- 麻省理工学院人工智能视频教程 – 麻省理工人工智能课程
- 人工智能入门 – 人工智能基础学习。Peter Norvig举办的课程
- EdX 人工智能 – 此课程讲授人工智能计算机系统设计的基本概念和技术。
- 人工智能中的计划 – 计划是人工智能系统的基础部分之一。在这个课程中,你将会学习到让机器人执行一系列动作所需要的基本算法。
- 机器人人工智能 – 这个课程将会教授你实现人工智能的基本方法,包括:概率推算,计划和搜索,本地化,跟踪和控制,全部都是围绕有关机器人设计。
- 机器学习 – 有指导和无指导情况下的基本机器学习算法
- 机器学习中的神经网络 – 智能神经网络上的算法和实践经验
- 斯坦福统计学习
有需要的小伙伴,可以点击下方链接免费领取或者V扫描下方二维码免费领取🆓
人工智能书籍
- OpenCV(中文版).(布拉德斯基等)
- OpenCV+3计算机视觉++Python语言实现+第二版
- OpenCV3编程入门 毛星云编著
- 数字图像处理_第三版
- 人工智能:一种现代的方法
- 深度学习面试宝典
- 深度学习之PyTorch物体检测实战
- 吴恩达DeepLearning.ai中文版笔记
- 计算机视觉中的多视图几何
- PyTorch-官方推荐教程-英文版
- 《神经网络与深度学习》(邱锡鹏-20191121)
- …
第一阶段:零基础入门(3-6个月)
新手应首先通过少而精的学习,看到全景图,建立大局观。 通过完成小实验,建立信心,才能避免“从入门到放弃”的尴尬。因此,第一阶段只推荐4本最必要的书(而且这些书到了第二、三阶段也能继续用),入门以后,在后续学习中再“哪里不会补哪里”即可。
第二阶段:基础进阶(3-6个月)
熟读《机器学习算法的数学解析与Python实现》并动手实践后,你已经对机器学习有了基本的了解,不再是小白了。这时可以开始触类旁通,学习热门技术,加强实践水平。在深入学习的同时,也可以探索自己感兴趣的方向,为求职面试打好基础。
第三阶段:工作应用
这一阶段你已经不再需要引导,只需要一些推荐书目。如果你从入门时就确认了未来的工作方向,可以在第二阶段就提前阅读相关入门书籍(对应“商业落地五大方向”中的前两本),然后再“哪里不会补哪里”。
有需要的小伙伴,可以点击下方链接免费领取或者V扫描下方二维码免费领取🆓