Azure Lei Zhang的博客

weibo: LeiZhang的微博/QQ: 185165016/QQ群:319036205/邮箱:leizhang1984@outlook.com/TeL:139-161-22926

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

  《Windows Azure Platform 系列文章目录

  

  在上一节内容中,笔者介绍了微软认知服务的概览。

  在本节中,笔者将详细介绍微软认知服务中的一种:计算机视觉 (Computer Vision) API

 

  我的一个客户有需求,他们需要消费者与自己的产品合照,然后上传到服务器并转发到朋友圈。

  但是为了防止恶意用户上传不健康的照片,需要对图像进行筛查。

  计算机视觉API的分析图像功能,正好有Adult参数,可以检测图像是否是色情的,正好满足客户的需求。

  

  请注意:

  -  本文使用的是国内由世纪互联运维的Azure China计算机视觉服务,API参考:

    https://dev.cognitive.azure.cn/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa

  -  如果是使用的是海外的Azure China计算机视觉服务,API参考:

    https://westus.dev.cognitive.microsoft.com/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa

 

  计算机视觉解决的问题:

  1.分析图像

  检查图像中发现的视觉内容,分析是否有不健康内容。

  2.生成缩略图

  裁剪和生成缩略图

  3.读取图片中的文字

  4.识别名人

 

  关键步骤主要有:

  1.创建计算机视觉API,并获得API Key

  2.了解API参数

  3.使用API测试控制台调试API,并设置调试API的参数

 

  接下来我们进入Demo时间,在开始之前,请先准备Azure China账户

  

  第一部分:创建计算机视觉API,并获得API Key

  1.我们找到需要分析的图片URL,我这里准备了一张人脸的照片:https://leizhangstorage.blob.core.chinacloudapi.cn/azureblog/analyzeimagesample.jpg

  2.我们登录Azure China管理界面:https://portal.azure.cn

  3.点击下图的认知服务账户

  

  4.点击 创建认知服务账户。如下图:

  

  5.定价层,我们选择免费。因为是Demo环境,我们就用免费,如果是生产环境建议选择标准。

  

  6.创建完认知服务以后,我们点击密钥,获得访问这个计算机视觉API的Access Key。请保留好这个Key,下面还要继续使用。

 

  第二部分:了解API参数

  1.Request URL: https://api.cognitive.azure.cn/vision/v1.0/analyze[?visualFeatures][&details][&language]

  2.Request参数

  (1)visualFeatures参数

    我们可以设置visualFeatures为:

    A.Categories:对图像内容进行分类

    B.Tags:对图像进行标记

    C.Description:用完整的英文句子描述图像内容

    D.Faces:检测脸部是否存在。 如果存在,生成坐标,性别和年龄

    E.ImageType:检测图像是剪贴还是直线绘图

    F.Color:确定重音颜色,主色,以及图像是否为黑白

    G.Adult:检测图像是否是色情的(描绘裸露或性行为)。 还会检测到性暗示内容。

  (2)details:

    如果设置Celebrities,则可以识别名人

  (3)language:

    默认是en,英文

    可以设置为zh,简体中文

  3.Request headers

  (1)Content-Type

  (2)Ocp-Apim-Subscription-Key:上面的API Access Key

  4.Request body

  (1)支持的图像为JPEG, PNG, GIF和BMP

  (2)图像的尺寸必须小于4MB

  (3)图像的分辨率至少为50 X 50

 

 

  第三部分:使用API测试控制台调试API,并设置调试API的参数

  我们拿到上面的API Key,就可以写代码开发了。不过Azure认知服务提供了非常好的控制台,可以方便我们进行API调试

  1.我们点击:https://dev.cognitive.azure.cn/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa,选择打开API测试控制台

  2.在API控制台,修改以下内容:

  Query Parameters

  (1)visualFeatures,我们输入:Categories,Tags,Description,Faces,ImageType,Color,Adult

  这样识别多个元素。

  (2)details,我们不输入信息

  (3)language,使用默认的en

  

  Headers:

  (1)Content-Type,我们使用默认值

  (2)Ocp-Apim-Subscription-Key,我们输入步骤一的API Access Key

  

  Request body:

  (1)我们修改为:{"url":"https://leizhangstorage.blob.core.chinacloudapi.cn/azureblog/analyzeimagesample.jpg"}

 

  所有参数的修改内容如下图:

  

 

  Request URL为:https://api.cognitive.azure.cn/vision/v1.0/analyze?visualFeatures=Categories,Tags,Description,Faces,ImageType,Color,Adult&language=en

 

  然后我们点击API测试控制台的Send。显示识别结果。

  下面的结果我就不一一说明了,主要的显示结果有:

  1.faces,识别出图像中的人脸坐标,性别和年龄

  2.adultScore,识别出检测图像是否是色情的,分数越高,则图像色情的可能性越大

Pragma: no-cache
apim-request-id: 8a9e6b8c-3a20-42a0-91e0-52d6fbdc5f9e
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Cache-Control: no-cache
Date: Thu, 15 Jun 2017 09:06:16 GMT
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 1595
Content-Type: application/json; charset=utf-8
Expires: -1

{
  "categories": [
    {
      "name": "people_group",
      "score": 0.9765625
    }
  ],
  "adult": {
    "isAdultContent": false,
    "isRacyContent": false,
    "adultScore": 0.01091344840824604,
    "racyScore": 0.055492393672466278
  },
  "tags": [
    {
      "name": "outdoor",
      "confidence": 0.99716836214065552
    },
    {
      "name": "person",
      "confidence": 0.99493598937988281
    },
    {
      "name": "posing",
      "confidence": 0.95204299688339233
    },
    {
      "name": "group",
      "confidence": 0.82954329252243042
    },
    {
      "name": "people",
      "confidence": 0.583439290523529
    },
    {
      "name": "crowd",
      "confidence": 0.019400959834456444
    }
  ],
  "description": {
    "tags": [
      "outdoor",
      "person",
      "posing",
      "photo",
      "grass",
      "group",
      "standing",
      "people",
      "man",
      "woman",
      "young",
      "holding",
      "dress",
      "white",
      "court"
    ],
    "captions": [
      {
        "text": "a group of people posing for a picture",
        "confidence": 0.94583615520612
      }
    ]
  },
  "requestId": "8a9e6b8c-3a20-42a0-91e0-52d6fbdc5f9e",
  "metadata": {
    "width": 600,
    "height": 463,
    "format": "Jpeg"
  },
  "faces": [
    {
      "age": 42,
      "gender": "Male",
      "faceRectangle": {
        "left": 117,
        "top": 159,
        "width": 95,
        "height": 95
      }
    },
    {
      "age": 54,
      "gender": "Male",
      "faceRectangle": {
        "left": 490,
        "top": 111,
        "width": 90,
        "height": 90
      }
    },
    {
      "age": 61,
      "gender": "Female",
      "faceRectangle": {
        "left": 17,
        "top": 153,
        "width": 85,
        "height": 85
      }
    },
    {
      "age": 33,
      "gender": "Female",
      "faceRectangle": {
        "left": 386,
        "top": 166,
        "width": 81,
        "height": 81
      }
    },
    {
      "age": 15,
      "gender": "Female",
      "faceRectangle": {
        "left": 235,
        "top": 159,
        "width": 77,
        "height": 77
      }
    },
    {
      "age": 6,
      "gender": "Female",
      "faceRectangle": {
        "left": 323,
        "top": 163,
        "width": 67,
        "height": 67
      }
    }
  ],
  "color": {
    "dominantColorForeground": "White",
    "dominantColorBackground": "White",
    "dominantColors": [
      "White",
      "Brown"
    ],
    "accentColor": "4E5D1A",
    "isBWImg": false
  },
  "imageType": {
    "clipArtType": 0,
    "lineDrawingType": 0
  }
}

 

  

posted on 2017-06-15 17:10  Lei Zhang的博客  阅读(3393)  评论(1编辑  收藏  举报