警告
2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅 必应搜索 API 文档。 有关为必应搜索创建新 Azure 资源的说明,请参阅 通过 Azure 市场创建必应搜索资源。
通过视觉搜索客户端库,可以从返回 ImageInsightsToken
的先前搜索中联机查找图像。 此应用获取 ImageInsightsToken
,并在后续搜索中使用该令牌。 然后,它将 ImageInsightsToken
发送到必应,并返回包含必应搜索 URL 和在线找到的类似图像的 URL 的结果。
本教程的完整源代码以及其他错误处理和注释可以在 GitHub上找到。
先决条件
- 任何版本的 Visual Studio 2019。
- 如果使用 Linux/MacOS,可以使用 Mono运行此应用程序。
- NuGet 视觉搜索和图像搜索包。
- 在 Visual Studio 中的解决方案资源管理器中,右键单击项目,然后从菜单中选择 管理 NuGet 包。 安装
Microsoft.Azure.CognitiveServices.Search.CustomSearch
包和Microsoft.Azure.CognitiveServices.Search.ImageSearch
包。 安装 NuGet 包还会安装以下内容:- Microsoft.Rest.ClientRuntime
- Microsoft.Rest.ClientRuntime.Azure
- Newtonsoft.Json
- 在 Visual Studio 中的解决方案资源管理器中,右键单击项目,然后从菜单中选择 管理 NuGet 包。 安装
创建 Azure 资源
通过创建以下 Azure 资源之一开始使用必应视觉搜索 API:
- 可通过 Azure 门户使用,直到删除资源。
- 选择
S9
定价层。
- 可通过 Azure 门户使用,直到删除资源。
- 跨多个 Azure AI 服务对应用程序使用相同的密钥和终结点。
从必应图像搜索客户端库获取“ImageInsightsToken”
此应用程序使用通过 必应图像搜索客户端库获取的 ImageInsightsToken
。 在新的 C# 控制台应用程序中,创建客户端以使用 ImageSearchClient()
调用 API。 然后将 SearchAsync()
用于查询:
var client = new ImageSearchClient(new Microsoft.Azure.CognitiveServices.Search.ImageSearch.ApiKeyServiceClientCredentials(subKey));
var imageResults = client.Images.SearchAsync(query: "canadian rockies").Result;
Console.WriteLine("Search images for query \"canadian rockies\"");
使用 imageResults.Value.First()
存储第一个搜索结果,然后存储图像洞察的 ImageInsightsToken
。
String insightTok = "None";
if (imageResults.Value.Count > 0)
{
var firstImageResult = imageResults.Value.First();
insightTok = firstImageResult.ImageInsightsToken;
}
else
{
insightTok = "None found";
Console.WriteLine("Couldn't find image results!");
}
此 ImageInsightsToken
被在请求中发送到必应视觉搜索。
将 ImageInsightsToken 添加到视觉搜索请求
通过从 Bing 视觉搜索响应中包含的 ImageInsightsToken
创建 ImageInfo
对象来指定视觉搜索请求的 ImageInsightsToken
。
ImageInfo ImageInfo = new ImageInfo(imageInsightsToken: insightsTok);
使用必应视觉搜索从 ImageInsightsToken 中查找图像
VisualSearchRequest
对象包含有关要搜索的 ImageInfo
中的图像的信息。
VisualSearchMethodAsync()
方法获取结果。 无需提供图像二进制文件,因为映像由令牌表示。
VisualSearchRequest VisualSearchRequest = new VisualSearchRequest(ImageInfo);
var visualSearchResults = client.Images.VisualSearchMethodAsync(knowledgeRequest: VisualSearchRequest).Result;
循环访问视觉搜索结果
视觉搜索结果是 ImageTag
对象。 每个标记都包含 ImageAction
对象的列表。 每个 ImageAction
都包含一个 Data
字段,该字段是依赖于作类型的值列表。 例如,可以循环访问 ImageTag
中的 visualSearchResults.Tags
对象,并获取其中的 ImageAction
标记。 以下示例输出 PagesIncluding
操作的详细信息:
if (visualSearchResults.Tags.Count > 0)
{
// List of tags
foreach (ImageTag t in visualSearchResults.Tags)
{
foreach (ImageAction i in t.Actions)
{
Console.WriteLine("\r\n" + "ActionType: " + i.ActionType + " WebSearchURL: " + i.WebSearchUrl);
if (i.ActionType == "PagesIncluding")
{
foreach (ImageObject o in (i as ImageModuleAction).Data.Value)
{
Console.WriteLine("ContentURL: " + o.ContentUrl);
}
}
}
}
}
包含 ActionTypes 的页面
从动作类型获取实际图像 URL 需要将 ActionType
强制转换为读取为 ImageModuleAction
,其中包含一个带有值列表的 Data
元素。 每个值都是图像的 URL。 下面将 PagesIncluding
的操作类型转换为 ImageModuleAction
并读取相应的值:
if (i.ActionType == "PagesIncluding")
{
foreach(ImageObject o in (i as ImageModuleAction).Data.Value)
{
Console.WriteLine("ContentURL: " + o.ContentUrl);
}
}
有关这些数据类型的详细信息,请参阅 图像 - 视觉搜索。
返回的 URL
完整的应用程序返回以下 URL:
动作类型 | 网址 |
---|---|
更多尺寸 -> WebSearchUrl | |
视觉搜索 -> 网页搜索链接 | |
ImageById -> 网页搜索网址 | |
RelatedSearches -> WebSearchUrl: | |
文档级建议 -> 网络搜索网址: | |
主题结果 -> 网络搜索网址 | https://www.bing.com/cr?IG=3E32CC6CA5934FBBA14ABC3B2E4651F9&CID=1BA795A21EAF6A63175699B71FC36B7C&rd=1&h=BcQifmzdKFyyBusjLxxgO42kzq1Geh7RucVVqvH-900&v=1&r=https%3a%2f%2fwww.bing.com%2fdiscover%2fcanadian%2brocky&p=DevEx,5823.1 |
ImageResults -> 网页搜索网址 | https://www.bing.com/cr?IG=3E32CC6CA5934FBBA14ABC3B2E4651F9&CID=1BA795A21EAF6A63175699B71FC36B7C&rd=1&h=PV9GzMFOI0AHZp2gKeWJ8DcveSDRE3fP2jHDKMpJSU8&v=1&r=https%3a%2f%2fwww.bing.com%2fimages%2fsearch%3fq%3doutdoor&p=DevEx,5831.1 |
如上所示,TopicResults
和 ImageResults
类型包含相关图像的查询。 指向必应搜索结果的 URL 链接。