iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 22
0
AI & Machine Learning

利用 MS Bot framework 與 Cognitive Service 建構自用智慧小秘書系列 第 22

22.應用:自用圖片搜尋機器人

前言

Bing 圖片搜尋服務介接並不困難,主要在標頭放入 Ocp-Apim-Subscription-Key 與加上 q 的搜尋內容即可。注意地方為使用 GET 方法有字數限制,不能查詢過長的文字。如下圖所示,images/search API會回傳一串的資訊給你,我們需要解析並加工這些資訊,才得以回傳我們要的資訊。

https://d2mxuefqeaa7sj.cloudfront.net/s_A2B061DB67148C6BC4F01A5EC70E163059D2143B96D3476E114B6B804834A440_1514210523168_image.png


介紹

Step 1. 開啟 Bot Template 新專案,並開啟 RootDialog.cs,先加上於Azure 啟用 Bing Spell Check 成功後,所記錄下來的 key

private string key = "your_key";

Step 2. 將我們在前一篇文章,透過postman 測試成功回傳的 json 檔案,貼上json2csharp,產生對應的物件,貼在 RootDialog.cs 內:
JSON 檔案

    {
        "_type": "Images",
        "instrumentation": {},
        "webSearchUrl": "https://www.bing.com/images/search?q=%E9%9B%AA%E7%B4%8D%E7%91%9E&FORM=OIIARP",
        "totalEstimatedMatches": 838,
        "nextOffset": 41,
        "value": [
            {
                "webSearchUrl": "https://www.bing.com/images/search?view=detailv2&FORM=OIIRPO&q=%e9%9b%aa%e7%b4%8d%e7%91%9e&id=6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05&simid=607993450671246848",
                "name": "Raça Schnauzer - Cachorros Brasil",
                "thumbnailUrl": "https://tse1.mm.bing.net/th?id=OIP.6pRgKSB_8dtdqtswcBayJgHaLL&pid=Api",
                "datePublished": "2013-03-08T19:00:00.0000000Z",
                "contentUrl": "http://www.cachorrosbrasil.com.br/wp-content/uploads/2013/03/schnauzer-07.jpg",
                "hostPageUrl": "http://www.cachorrosbrasil.com.br/blog/racas-de-caes/raca-schnauzer/",
                "contentSize": "488848 B",
                "encodingFormat": "jpeg",
                "hostPageDisplayUrl": "www.cachorrosbrasil.com.br/blog/racas-de-caes/raca-schnauzer",
                "width": 1500,
                "height": 2264,
                "thumbnail": {
                    "width": 474,
                    "height": 715
                },
                "imageInsightsToken": "ccid_6pRgKSB/*mid_6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05*simid_607993450671246848*thid_OIP.6pRgKSB\\_8dtdqtswcBayJgHaLL",
                "insightsMetadata": {
                    "pagesIncludingCount": 72,
                    "availableSizesCount": 20
                },
                "imageId": "6CF952F881BAAFA1BD9323CA5E9416A1AFA00F05",
                "accentColor": "446F16"
            }
        ]
    }

https://d2mxuefqeaa7sj.cloudfront.net/s_A2B061DB67148C6BC4F01A5EC70E163059D2143B96D3476E114B6B804834A440_1514205031325_image.png

public class Instrumentation
{
}

public class Thumbnail
{
    public int width { get; set; }
    public int height { get; set; }
}

public class InsightsMetadata
{
    public int pagesIncludingCount { get; set; }
    public int availableSizesCount { get; set; }
}

public class Value
{
    public string webSearchUrl { get; set; }
    public string name { get; set; }
    public string thumbnailUrl { get; set; }
    public DateTime datePublished { get; set; }
    public string contentUrl { get; set; }
    public string hostPageUrl { get; set; }
    public string contentSize { get; set; }
    public string encodingFormat { get; set; }
    public string hostPageDisplayUrl { get; set; }
    public int width { get; set; }
    public int height { get; set; }
    public Thumbnail thumbnail { get; set; }
    public string imageInsightsToken { get; set; }
    public InsightsMetadata insightsMetadata { get; set; }
    public string imageId { get; set; }
    public string accentColor { get; set; }
}

public class RootObject
{
    public string _type { get; set; }
    public Instrumentation instrumentation { get; set; }
    public string webSearchUrl { get; set; }
    public int totalEstimatedMatches { get; set; }
    public int nextOffset { get; set; }
    public List<Value> value { get; set; }
}

Step 3. 我們增加一個私有方法,用來產生附件

private static Attachment GetInternetAttachment(string name, string contentType, string url)
{
    return new Attachment
    {
        Name = name,
        ContentType = "image/"+contentType,
        ContentUrl = url
    };
}

Step 4. 修改 MessageReceivedAsync 如下:

private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
    var activity = await result as Activity;

    var client = new RestClient("https://api.cognitive.microsoft.com/bing/v7.0/");
    var request = new RestRequest("images/search", Method.GET);
    request.AddHeader("Ocp-Apim-Subscription-Key", key);
    request.AddParameter("q", activity.Text);

    var response = await client.ExecuteTaskAsync<RootObject>(request);
    var replyMessage = context.MakeMessage();
    List<Attachment> list = new List<Attachment>();

    foreach (var item in response.Data.value)
    {
        Attachment attachment = new Attachment();
        attachment = GetInternetAttachment(item.name, item.encodingFormat, item.contentUrl);
        list.Add(attachment);
    }
    replyMessage.Attachments = list;
    await context.PostAsync(replyMessage);
    context.Wait(MessageReceivedAsync);
}

Step 5. 啟動程式並開啟模擬器,輸入雪納瑞:
https://d2mxuefqeaa7sj.cloudfront.net/s_A2B061DB67148C6BC4F01A5EC70E163059D2143B96D3476E114B6B804834A440_1514207505977_image.png


範例程式

https://github.com/matsurigoto/BingImageSearchBotExample.git


如果輸入正妹的話... (被毆 /images/emoticon/emoticon05.gif


上一篇
21.Cognitive Service - Bing 影像搜尋服務
下一篇
23. 資料探勘與推薦系統概述
系列文
利用 MS Bot framework 與 Cognitive Service 建構自用智慧小秘書31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言