本系列共计7篇文章:

1. Power Apps 拍照并调用on-premises 图像识别算法的效果演示

2.Power Apps 调用相机拍照

3.Power Apps 调用 AzureBlobStorage 连接器上传文件

4.制作一个Azure Functions API 调用 computer vision 检测图像内容

5. Power Apps 通过custom connector 自定义连接器调用云端公开的Azure Functions API并显示图像分析结果

6.将步骤4中的Functions添加本地容器支持并在本地运行,安装 on-premises gateway

7.Power Apps 通过 custom connector 自定义连接器调用 on-premises API并显示图像分析结果

本文介绍第四讲:

开发一个Azure Functions Http Trigger 来接收图片位置,然后调用computer vision 来检测图片。

 

 

 

 

 

视频演示如下:

 

 

图文步骤:


 

创建Computer vision服务:

 

创建本地Functions,通过computer vision SDK调用 computer vision检测图像:

using System;
using System.IO;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;


using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Threading;
using System.Linq;
using System.Collections.Generic;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;

namespace Company.Function
{
    public static class HttpTriggerCSharp1
    {
        [FunctionName("HttpTriggerCSharp1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {

            //see details about blobTrigger, name:
            //https://docs.microsoft.com/zh-cn/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=csharp&WT.mc_id=AZ-MVP-5003757#metadata 

           log.LogInformation("C# HTTP trigger function processed a request.");

            try{

            
            string FilePath = req.Query["filepath"];

            string subscriptionKey = Environment.GetEnvironmentVariable("ComputerVisionSubscriptionKey");
            string endpoint = Environment.GetEnvironmentVariable("ComputerVisionEndpoint");
          

            ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);


            string ANALYZE_URL_IMAGE = FilePath;

             log.LogInformation($"Picture url:{ANALYZE_URL_IMAGE}");
           

              List<VisualFeatureTypes?> features = new List<VisualFeatureTypes?>()
                {
                    VisualFeatureTypes.Categories, VisualFeatureTypes.Description,
                
                };

            ImageAnalysis results = await client.AnalyzeImageAsync(ANALYZE_URL_IMAGE, features,null,"zh");

            // Sunmarizes the image content.
            log.LogInformation("Summary:");
            string Summary="";
            foreach (var caption in results.Description.Captions)
            {
                Summary+=($"识别结果:{caption.Text} \n置信度: {caption.Confidence}");
            }
            
            return new OkObjectResult( JsonConvert.SerializeObject(Summary) );
            }
            catch(Exception ex)
            {
                return new BadRequestObjectResult(JsonConvert.SerializeObject(ex.Message));
            }
                        
        }

            

                /*
                * AUTHENTICATE
                * Creates a Computer Vision client used by each example.
                */
                public static ComputerVisionClient Authenticate(string endpoint, string key)
                {
                    ComputerVisionClient client =
                    new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                    { Endpoint = endpoint };
                    return client;
                }



    }
}

 

local.settings.json文件配置computer vison的key 和 endpoint

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=ppblobsean;AccountKey=lTVsZXIKNnIiFWaX2NOnMi2OMPy7BECLJwdIFvWfDxeCnAixZEx5oUdrwKI+zPlKUinffVkdKcfFkLZrUZdyOQ==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
   
    "ComputerVisionSubscriptionKey":"ddbxxxxxx05323",
    "ComputerVisionEndpoint":"https://xxxxx.cognitiveservices.azure.com/"

  }
}

 

key 和 endpoint 在如下位置找到:

 

 

 

修改 storage account 权限,以使computer vision 可以访问照片:

 

 

 

从本地post man发起请求:

filepath填写云端 照片的blob地址:

 

得到如下信息:

 

将functions 部署到云端:ctrl+shift+p 按照向导部署。注意添加配置文件里的key和endpoint。

部署完成后,从post man 发起向云端的调用:

 

结果显示如下: