AIにクラッピーを見せると何を思うのか（Azureの画像分析APIです）

# はじめに
[#クラッピーチャレンジ Advent Calendar 2020](https://adventar.org/calendars/5187)　8日目の記事です。
クラッピーが手元にないので、過去に撮影したクラッピーの画像をAzure Computer Visionの画像分析APIに投げてみました。画像分析APIを用いると、画像の説明文を生成してくれます。
HoloLens2でもこんな記事「[HoloLens2 × Azure Cognitive Services（画像分析APIで画像説明文生成）](https://qiita.com/SatoshiGachiFujimoto/items/04756ec0760e17439df4)」を書いているので見てみてください。

# 開発環境
- Windows 10
- Python 3.6
- Azure 

# 導入
画像分析APIに投げるPythonプログラムは下記になります。AzureポータルからComputer Vision APIを作成し、エンドポイントとキーをコピペしてください。

~~~analyze.py
import os
import sys
import requests
# If you are using a Jupyter notebook, uncomment the following line.
# %matplotlib inline
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
import os

subscription_key = "<Insert Your API Key>"
endpoint = "https://<Insert Your Resource Name>.cognitiveservices.azure.com/"
analyze_url = endpoint + "vision/v3.1/analyze"

# Set image_path to the local path of an image that you want to analyze.
# Sample images are here, if needed:
# https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/ComputerVision/Images

dirname = "clappy/"
files = os.listdir(dirname)

for file in files:
    # Read the image into a byte array
    image_data = open(dirname + file, "rb").read()
    headers = {'Ocp-Apim-Subscription-Key': subscription_key,
            'Content-Type': 'application/octet-stream'}
    params = {'visualFeatures': 'Categories,Description,Color'}
    response = requests.post(
        analyze_url, headers=headers, params=params, data=image_data)
    response.raise_for_status()

    # The 'analysis' object contains various fields that describe the image. The most
    # relevant caption for the image is obtained from the 'description' property.
    analysis = response.json()
    print(analysis)
    image_caption = analysis["description"]["captions"][0]["text"].capitalize()

    # Display the image and overlay it with the caption.
    image = Image.open(BytesIO(image_data))
    fig = plt.figure()
    plt.imshow(image)
    plt.axis("off")
    _ = plt.title(image_caption, size="x-large", y=-0.1)
    # plt.show()
    fig.savefig("result/"+file)
~~~

# 実行

![19030338_1491492647538744_773272452102692181_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/199c2dc1-cf5c-ef30-34e0-fa1a866ac88c.jpeg)
![19055112_1491486954205980_1297198113259920897_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/665ef565-9693-f81d-7fed-4caa263adb1a.jpeg)
![19264650_1499046780116664_3424860175366470380_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/04f2cd7c-059b-da52-2e4f-d9e3619c67e2.jpeg)
![23318983_1635601143127893_871019205316474673_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/4fd52318-5248-5608-0d8e-3240aa58122b.jpeg)
![23319335_1635695543118453_5621071620397277174_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/b100fecb-8eee-d303-1ed4-6e2f6337face.jpeg)
![23376365_1636484413039566_2707645763496136760_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/857b9612-f2a2-5d9d-96e4-c419fdf2ac77.jpeg)
![23376647_1635572793130728_923685704748732178_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/a2045c83-86c6-1021-20d4-16d20a98ad4e.jpeg)
![23473031_1635696433118364_7171265576451749495_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/03dd52b8-5092-4977-0acc-f5c18451f546.jpeg)
![25446047_1673387339349273_34401910827914340_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/aab0455a-c2ca-548c-b216-a5f40e635069.jpeg)
![30710917_1788011027886903_6576225328295837696_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/f43b9f0e-5591-1a07-5a6e-188ffbf004b4.jpeg)
![37179064_1897943366893668_7397617731798827008_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/2311aaca-ff10-ff81-2c41-82c80390bdfd.jpeg)
![43250600_2016093018412035_3469528603807449088_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/1981ef28-8fb6-c04a-9d99-9223cb753f91.jpeg)
![16665649_1374984332522910_4747950487955808095_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/141570f5-f0c9-9bb3-6636-36908a96499f.jpeg)
![16797175_1386987047989305_7491875210929899417_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/e1ccdc1e-c5f9-7318-6f7c-906d6141d600.jpeg)
![17349923_1413753251979351_164391718457275837_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/dd9d9ecb-2125-a5d2-df99-747fc957f36e.jpeg)
![17358723_1413887895299220_7035955493849861248_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/4e260d20-33c5-c427-7523-b591d9a07249.jpeg)
![17545527_1421129511241725_2620203238592672616_o.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/b70458e3-aca8-50e1-3f8a-690606d086f4.jpeg)
![18952605_1491492500872092_9196702837233785555_n.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/63863/c7805f5f-9ad8-343d-a766-545e1ee73862.jpeg)

# まとめ
割と正確にtoyだった。