当前位置: 首页 > news >正文

运用AI搭建中间服务层(四)

MiddlewareService文件夹

在这个文件夹中,我们需要添加以下文件:

  • 名人服务.cs

  • 名人服务.cs

  • 名人结果.cs

  • ILandmarkService.cs

  • 地标服务 .cs

  • 地标结果 .cs

ICelebrityService.cs – 包装多个串行的认知服务来实现名人识别的中间服务层的接口定义,需要依赖注入

using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public interface ICelebrityService{Task<CelebrityResult> Do(byte[] imgData);}
}

CelebrityService.cs – 包装多个串行的认知服务来实现名人识别中间服务层的逻辑代码

using CognitiveMiddlewareService.CognitiveServices;
using Newtonsoft.Json;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public class CelebrityService : ICelebrityService{private readonly IVisionService visionService;private readonly IEntitySearchService entityService;public CelebrityService(IVisionService vs, IEntitySearchService ess){this.visionService = vs;this.entityService = ess;}public async Task<CelebrityResult> Do(byte[] imgData){// get original recognized resultvar stream = Helper.GetStream(imgData);Celebrity celebrity = await this.visionService.RecognizeCelebrityAsync(stream);if (celebrity != null){// get entity search resultstring entityName = celebrity.name;string jsonResult = await this.entityService.SearchEntityAsync(entityName);EntityResult er = JsonConvert.DeserializeObject<EntityResult>(jsonResult);if (er?.entities?.value.Length > 0){// isolation layer: decouple data structure then return abstract resultCelebrityResult cr = new CelebrityResult(){Name = er.entities.value[0].name,Description = er.entities.value[0].description,Url = er.entities.value[0].url,ThumbnailUrl = er.entities.value[0].image.thumbnailUrl,Confidence = celebrity.confidence};return cr;}}return null;}}
}

小提示:上面的代码中,用CelebrityResult接管了实体搜索结果和名人识别结果的部分有效字段,以达到解耦/隔离的作用,后面的代码只关心CelebrityResult如何定义的即可。

CelebrityResult.cs – 抽象出来的名人识别服务的返回结果

namespace CognitiveMiddlewareService.MiddlewareService
{public class CelebrityResult{public string Name { get; set; }public double Confidence { get; set; }public string Url { get; set; }public string Description { get; set; }public string ThumbnailUrl { get; set; }}
}

ILandmarkService.cs – 包装多个串行的认知服务来实现地标识别的中间服务层的接口定义,需要依赖注入

using CognitiveMiddlewareService.CognitiveServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public interface ILandmarkService{Task<LandmarkResult> Do(byte[] imgData);}
}

LandmarkService.cs – 包装多个串行的认知服务来实现地标识别的中间服务层的逻辑代码

using CognitiveMiddlewareService.CognitiveServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.MiddlewareService
{public class LandmarkService : ILandmarkService{private readonly IVisionService visionService;private readonly IEntitySearchService entityService;public LandmarkService(IVisionService vs, IEntitySearchService ess){this.visionService = vs;this.entityService = ess;}public async Task<LandmarkResult> Do(byte[] imgData){// get original recognized resultvar streamLandmark = Helper.GetStream(imgData);Landmark landmark = await this.visionService.RecognizeLandmarkAsync(streamLandmark);if (landmark != null){// get entity search resultstring entityName = landmark.name;string jsonResult = await this.entityService.SearchEntityAsync(entityName);EntityResult er = JsonConvert.DeserializeObject<EntityResult>(jsonResult);// isolation layer: decouple data structure then return abstract resultLandmarkResult lr = new LandmarkResult(){Name = er.entities.value[0].name,Description = er.entities.value[0].description,Url = er.entities.value[0].url,ThumbnailUrl = er.entities.value[0].image.thumbnailUrl,Confidence = landmark.confidence};return lr;}return null;}}
}

小提示:上面的代码中,用LandmarkResult接管了实体搜索结果和地标识别结果的部分有效字段,以达到解耦/隔离的作用,后面的代码只关心LandmarkResult如何定义的即可。

LandmarkResult.cs – 抽象出来的地标识别服务的返回结果

namespace CognitiveMiddlewareService.MiddlewareService
{public class LandmarkResult{public string Name { get; set; }public double Confidence { get; set; }public string Url { get; set; }public string Description { get; set; }public string ThumbnailUrl { get; set; }}
}

Processors文件夹

在这个文件夹中,我们需要添加以下文件:

  • IProcessService.cs

  • 进程服务 .cs

  • 聚合结果.cs

IProcessService.cs – 任务调度层服务的接口定义,需要依赖注入

using System.Threading.Tasks;namespace CognitiveMiddlewareService.Processors
{public interface IProcessService{Task<AggregatedResult> Process(byte[] imgData);}
}

ProcessService.cs – 任务调度层服务的逻辑代码

using CognitiveMiddlewareService.MiddlewareService;
using System.Collections.Generic;
using System.Threading.Tasks;namespace CognitiveMiddlewareService.Processors
{public class ProcessService : IProcessService{private readonly ILandmarkService landmarkService;private readonly ICelebrityService celebrityService;public ProcessService(ILandmarkService ls, ICelebrityService cs){this.landmarkService = ls;this.celebrityService = cs;}public async Task<AggregatedResult> Process(byte[] imgData){// preprocess// todo: create screening image classifier to get a rough category, then decide call which service// task dispatcher: parallelized run 'Do'// todo: put this logic into Dispatcher serviceList<Task> listTask = new List<Task>();var taskLandmark = this.landmarkService.Do(imgData);listTask.Add(taskLandmark);var taskCelebrity = this.celebrityService.Do(imgData);listTask.Add(taskCelebrity);await Task.WhenAll(listTask);LandmarkResult lmResult = taskLandmark.Result;CelebrityResult cbResult = taskCelebrity.Result;// aggregator// todo: put this logic into Aggregator serviceAggregatedResult ar = new AggregatedResult(){Landmark = lmResult,Celebrity = cbResult};return ar;// ranker// todo: if there have more than one result in AgregatedResult, need give them a ranking// output generator// todo: generate specified JSON data, such as Adptive Card}}
}

小提示:大家可以看到上面这个文件中有很多绿色的注释,带有todo文字的,对于一个更复杂的系统,可以用这些todo中的描述来设计独立的模块。

AggregatedResult.cs – 任务调度层服务的最终聚合结果定义

using CognitiveMiddlewareService.MiddlewareService;namespace CognitiveMiddlewareService.Processors
{public class AggregatedResult{public LandmarkResult Landmark { get; set; }public CelebrityResult Celebrity { get; set; }}
}

相关文章:

运用AI搭建中间服务层(四)

MiddlewareService文件夹 在这个文件夹中&#xff0c;我们需要添加以下文件&#xff1a; 名人服务.cs 名人服务.cs 名人结果.cs ILandmarkService.cs 地标服务 .cs 地标结果 .cs ICelebrityService.cs – 包装多个串行的认知服务来实现名人识别的中间服务层的接口定义&…...

[C#]winform部署yolov5-onnx模型

【官方框架地址】 https://github.com/ultralytics/yolov5 【算法介绍】 Yolov5&#xff0c;全称为You Only Look Once version 5&#xff0c;是计算机视觉领域目标检测算法的一个里程碑式模型。该模型由ultralytics团队开发&#xff0c;并因其简洁高效的特点而备受关注。Yol…...

基于SpringBoot的洗衣店管理系统

基于SpringBoot的洗衣店管理系统的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootMyBatis工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 登录界面 可视化展示 用户界面 管理员界面 摘要 洗衣店管理系统基于Spring Boot框…...

AMEYA360:广和通RedCap模组FG131FG132系列

2024年1月&#xff0c;广和通RedCap模组FG131&FG132系列已进入工程送样阶段&#xff0c;可为终端客户提供样片。广和通RedCap模组系列满足不同终端对5G速率、功耗、尺寸、成本的需求&#xff0c;全面助力RedCap技术的行业应用。 FG131&FG132系列基于骁龙X35 5G调制解调…...

RGB,RGB-D,单目,双目,sterro相机,实例相机介绍

相机—特点及区别 1.相机种类 RGB&#xff0c;RGB-D&#xff0c;单目&#xff0c;双目&#xff0c;sterro相机&#xff0c;实例相机 2.相机特点 2.1单目 只使用一个摄像头进行SLAM&#xff0c;结构简单&#xff0c;成本低 三维空间的二维投影 必须移动相机&#xff0c;才…...

【linux】history命令显示时间的例子

在Linux中&#xff0c;你可以通过设置HISTTIMEFORMAT环境变量来显示命令的执行时间。这个环境变量定义了history命令中时间的显示格式。以下是设置和说明的步骤&#xff1a; 打开终端&#xff1a; 打开你的终端应用。 编辑配置文件&#xff1a; 使用文本编辑器&#xff08;如n…...

Nginx负载均衡以及常用的7层协议和4层协议的介绍

一、引言 明人不说暗话&#xff0c;下面来解析一下 Nginx 的负载均衡。需要有 Linux 和 Nginx 环境哈。 二、nginx负载均衡的作用 高并发&#xff1a;负载均衡通过算法调整负载&#xff0c;尽力均匀的分配应用集群中各节点的工作量&#xff0c;以此提高应用集群的并发处理能力…...

【机器学习300问】4、机器学习到底在学习什么?

首先我们先了解一个前置问题&#xff0c;再回答机器学习到底在学习什么。 一、求机器学习问题有哪几步&#xff1f; 求解机器学习问题的步骤可以分为“学习”和“推理”两个阶段。首先&#xff0c;在学习阶段进行模型的学习&#xff0c;然后&#xff0c;在推理阶段用学到的模型…...

设计一个简易版的数据库路由

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱吃芝士的土豆倪&#xff0c;24届校招生Java选手&#xff0c;很高兴认识大家&#x1f4d5;系列专栏&#xff1a;Spring原理、JUC原理、Kafka原理、分布式技术原理、数据库技术&#x1f525;如果感觉博主的文章还不错的…...

接口自动化测试面试题

前言 前面总结了一篇关于接口测试的常规面试题&#xff0c;现在接口自动化测试用的比较多&#xff0c;也是被很多公司看好。那么想做接口自动化测试需要具备哪些能力呢&#xff1f; 也就是面试的过程中&#xff0c;面试官会考哪些问题&#xff0c;知道你是不是真的做过接口自动…...

Tampermonkey油猴插件-各大网盘批量分享,解放双手-上

Unity3D特效百例案例项目实战源码Android-Unity实战问题汇总游戏脚本-辅助自动化Android控件全解手册再战Android系列...

【DB2】installSAM执行后会重启这件事

碎碎念 在使用自动化工具安装TSAMP的过程中&#xff0c;机器会自动重启这件事。 TSAMP真的挺折磨的&#xff0c;一个月居然因为这件事情debug两次了。 在测试自动化脚本的时候&#xff0c;第一遍安装都是好好的&#xff0c;从第二遍开始&#xff08;因为要测试脚本的幂等性&…...

RTSP网络视频协议

一.RTSP网络视频协议介绍 RTSP是类似HTTP的应用层协议&#xff0c;一个典型的流媒体框架网络体系可参考下图&#xff0c;其中rtsp主要用于控制命令&#xff0c;rtcp主要用于视频质量的反馈&#xff0c;rtp用于视频、音频流从传输。 1、RTSP&#xff08;Real Time Streaming P…...

Python 网络数据采集(四):Selenium 自动化

Python 网络数据采集&#xff08;四&#xff09;&#xff1a;Selenium 自动化 前言一、背景知识Selenium 4Selenium WebDriver 二、Selenium WebDriver 的安装与配置2.1 下载 Chrome 浏览器的驱动程序2.2 配置环境变量三、Python 安装 Selenium四、页面元素定位4.1 选择浏览器开…...

实现秒杀功能设计

页面 登录页面 登录成功后&#xff0c;跳转商品列表 商品列表页 加载商品信息 商品详情页 根据商品id查出商品信息返回VO&#xff08;包括rmiaoshaStatus、emainSeconds&#xff09;前端根据数据展示秒杀按钮&#xff0c;点击开始秒杀 订单详情页 秒杀页面设置 后端返回秒杀…...

每天刷两道题——第十四天

1.1矩阵置零 给定一个 m x n 的矩阵&#xff0c;如果一个元素为 0 &#xff0c;则将其所在行和列的所有元素都设为 0 。请使用原地算法。 输入&#xff1a;matrix [[0,1,2,0],[3,4,5,2],[1,3,1,5]] 输出&#xff1a;[[0,0,0,0],[0,4,5,0],[0,3,1,0]] 原地算法&#xff08;…...

快速掌握Postman实现接口测试

快速掌握Postman实现接口测试 Postman简介 Postman是谷歌开发的一款网页调试和接口测试工具&#xff0c;能够发送任何类型的http请求&#xff0c;支持GET/PUT/POST/DELETE等方法。Postman非常简单易用&#xff0c;可以直接填写URL&#xff0c;header&#xff0c;body等就可以发…...

jmeter--3.使用提取器进行接口关联

目录 1. 正则表达式提取器 1.1 提取单个数据 1.2 名词解释 1.3 提取多个数据 2. 边界值提取器 2.2 名词解释 3. JSON提取器 3.1 Json语法 3.2 名词解释 3.3 如果有多组数据&#xff0c;同正则方式引用数据 1. 正则表达式提取器 示例数据&#xff1a;{"access_to…...

移动通信系统关键技术多址接入MIMO学习(8)

1.Multiple-antenna Techniques多天线技术MIMO&#xff0c;从SISO到SIMO到MISO到如今的MIMO&#xff1b; 2.SIMO单发多收&#xff0c;分为选择合并、增益合并&#xff1b;SIMO&#xff0c;基站通过两路路径将信号发送到终端&#xff0c;因为终端接收到的两路信号都是来自同一天…...

WorkPlus AI助理为企业提供智能客服的机器人解决方案

在数字化时代&#xff0c;企业面临着客户服务的重要挑战。AI客服机器人成为了提升客户体验和提高工作效率的关键工具。作为一款优秀的AI助理&#xff0c;WorkPlus AI助理以其智能化的特点和卓越的功能&#xff0c;为企业提供了全新的客服机器人解决方案。 为什么选择WorkPlus A…...

树莓派超全系列教程文档--(62)使用rpicam-app通过网络流式传输视频

使用rpicam-app通过网络流式传输视频 使用 rpicam-app 通过网络流式传输视频UDPTCPRTSPlibavGStreamerRTPlibcamerasrc GStreamer 元素 文章来源&#xff1a; http://raspberry.dns8844.cn/documentation 原文网址 使用 rpicam-app 通过网络流式传输视频 本节介绍来自 rpica…...

Xshell远程连接Kali(默认 | 私钥)Note版

前言:xshell远程连接&#xff0c;私钥连接和常规默认连接 任务一 开启ssh服务 service ssh status //查看ssh服务状态 service ssh start //开启ssh服务 update-rc.d ssh enable //开启自启动ssh服务 任务二 修改配置文件 vi /etc/ssh/ssh_config //第一…...

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする

日语学习-日语知识点小记-构建基础-JLPT-N4阶段(33):にする 1、前言(1)情况说明(2)工程师的信仰2、知识点(1) にする1,接续:名词+にする2,接续:疑问词+にする3,(A)は(B)にする。(2)復習:(1)复习句子(2)ために & ように(3)そう(4)にする3、…...

Go 语言接口详解

Go 语言接口详解 核心概念 接口定义 在 Go 语言中&#xff0c;接口是一种抽象类型&#xff0c;它定义了一组方法的集合&#xff1a; // 定义接口 type Shape interface {Area() float64Perimeter() float64 } 接口实现 Go 接口的实现是隐式的&#xff1a; // 矩形结构体…...

Linux简单的操作

ls ls 查看当前目录 ll 查看详细内容 ls -a 查看所有的内容 ls --help 查看方法文档 pwd pwd 查看当前路径 cd cd 转路径 cd .. 转上一级路径 cd 名 转换路径 …...

将对透视变换后的图像使用Otsu进行阈值化,来分离黑色和白色像素。这句话中的Otsu是什么意思?

Otsu 是一种自动阈值化方法&#xff0c;用于将图像分割为前景和背景。它通过最小化图像的类内方差或等价地最大化类间方差来选择最佳阈值。这种方法特别适用于图像的二值化处理&#xff0c;能够自动确定一个阈值&#xff0c;将图像中的像素分为黑色和白色两类。 Otsu 方法的原…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

C++.OpenGL (10/64)基础光照(Basic Lighting)

基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...

鱼香ros docker配置镜像报错:https://registry-1.docker.io/v2/

使用鱼香ros一件安装docker时的https://registry-1.docker.io/v2/问题 一键安装指令 wget http://fishros.com/install -O fishros && . fishros出现问题&#xff1a;docker pull 失败 网络不同&#xff0c;需要使用镜像源 按照如下步骤操作 sudo vi /etc/docker/dae…...

select、poll、epoll 与 Reactor 模式

在高并发网络编程领域&#xff0c;高效处理大量连接和 I/O 事件是系统性能的关键。select、poll、epoll 作为 I/O 多路复用技术的代表&#xff0c;以及基于它们实现的 Reactor 模式&#xff0c;为开发者提供了强大的工具。本文将深入探讨这些技术的底层原理、优缺点。​ 一、I…...