0

我正在尝试构建一个基于ImageResizer 示例的简单 Azure 函数,但使用Microsoft Cognitive Server Computer Vision API来调整大小。

我已经移植到 Azure 函数中的计算机视觉 API 的工作代码。

一切似乎都正常(没有错误),但我的输出 blob 永远不会被保存或显示在存储容器中。不知道我做错了什么,因为没有错误可以使用。

我的CSX(C#功能代码)如下

using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;

public static void Run(Stream original, Stream thumb, TraceWriter log)
{
    //log.Verbose($"C# Blob trigger function processed: {myBlob}. Dimensions");
    string _apiKey = "PutYourComputerVisionApiKeyHere";
    string _apiUrlBase = "https://api.projectoxford.ai/vision/v1.0/generateThumbnail";
    string width = "100";
    string height = "100";
    bool smartcropping = true;

    using (var httpClient = new HttpClient())
    {
        //setup HttpClient
        httpClient.BaseAddress = new Uri(_apiUrlBase);
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);

        //setup data object
        HttpContent content = new StreamContent(original);
        content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");

        // Request parameters
        var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartcropping}";

        //make request
        var response = httpClient.PostAsync(uri, content).Result;

        //log result
        log.Verbose($"Response: IsSucess={response.IsSuccessStatusCode}, Status={response.ReasonPhrase}");

        //read response and write to output stream
        thumb = new MemoryStream(response.Content.ReadAsByteArrayAsync().Result);
    }
}

我的函数json如下

{
  "bindings": [
    {
      "path": "originals/{name}",
      "connection": "thumbnailgenstorage_STORAGE",
      "name": "original",
      "type": "blobTrigger",
      "direction": "in"
    },
    {
      "path": "thumbs/%rand-guid%",
      "connection": "thumbnailgenstorage_STORAGE",
      "type": "blob",
      "name": "thumb",
      "direction": "out"
    }
  ],
  "disabled": false
}

我的 Azure 存储帐户称为 'thumbnailgenstorage',它有两个容器,分别名为'originals''thumbs'。存储帐户密钥是KGdcO+hjvARQvSwd2rfmdc+rrAsK0tA5xpE4RVNmXZgExCE+Cyk4q0nSiulDwvRHrSAkYjyjVezwdaeLCIb53g==.

我很高兴人们使用我的密钥来帮助我解决这个问题!:)

4

1 回答 1

2

我现在开始工作了。我写错了输出流。

此解决方案是一个 Azure 函数,它在名为“Originals”的 Azure Blob 存储容器中的 blob 到达时触发,然后使用计算机视觉 API智能地调整图像大小并存储在另一个名为“Thumbs”的 blob 容器中。

这是工作的 CSX(c# 脚本):

using System;
using System.Text;
using System.Net.Http;
using System.Net.Http.Headers;

public static void Run(Stream original, Stream thumb, TraceWriter log)
{
    int width = 320;
    int height = 320;
    bool smartCropping = true;
    string _apiKey = "PutYourComputerVisionApiKeyHere";
    string _apiUrlBase = "https://api.projectoxford.ai/vision/v1.0/generateThumbnail";

    using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri(_apiUrlBase);
        httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _apiKey);
        using (HttpContent content = new StreamContent(original))
        {
            //get response
            content.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/octet-stream");
            var uri = $"{_apiUrlBase}?width={width}&height={height}&smartCropping={smartCropping.ToString()}";
            var response = httpClient.PostAsync(uri, content).Result;
            var responseBytes = response.Content.ReadAsByteArrayAsync().Result;

            //write to output thumb
            thumb.Write(responseBytes, 0, responseBytes.Length);
        }
    }
}

这是集成 JSON

{
  "bindings": [
    {
      "path": "originals/{name}",
      "connection": "thumbnailgenstorage_STORAGE",
      "name": "original",
      "type": "blobTrigger",
      "direction": "in"
    },
    {
      "path": "thumbs/{name}",
      "connection": "thumbnailgenstorage_STORAGE",
      "name": "thumb",
      "type": "blob",
      "direction": "out"
    }
  ],
  "disabled": false
}
于 2016-05-03T12:20:04.647 回答