1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Posted at

はじめに

#クラッピーチャレンジ Advent Calendar 2020 8日目の記事です。
クラッピーが手元にないので、過去に撮影したクラッピーの画像をAzure Computer Visionの画像分析APIに投げてみました。画像分析APIを用いると、画像の説明文を生成してくれます。
HoloLens2でもこんな記事「HoloLens2 × Azure Cognitive Services(画像分析APIで画像説明文生成)」を書いているので見てみてください。

開発環境

  • 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
19055112_1491486954205980_1297198113259920897_o.jpg
19264650_1499046780116664_3424860175366470380_o.jpg
23318983_1635601143127893_871019205316474673_n.jpg
23319335_1635695543118453_5621071620397277174_n.jpg
23376365_1636484413039566_2707645763496136760_n.jpg
23376647_1635572793130728_923685704748732178_n.jpg
23473031_1635696433118364_7171265576451749495_n.jpg
25446047_1673387339349273_34401910827914340_n.jpg
30710917_1788011027886903_6576225328295837696_n.jpg
37179064_1897943366893668_7397617731798827008_n.jpg
43250600_2016093018412035_3469528603807449088_n.jpg
16665649_1374984332522910_4747950487955808095_o.jpg
16797175_1386987047989305_7491875210929899417_o.jpg
17349923_1413753251979351_164391718457275837_o.jpg
17358723_1413887895299220_7035955493849861248_o.jpg
17545527_1421129511241725_2620203238592672616_o.jpg
18952605_1491492500872092_9196702837233785555_n.jpg

まとめ

割と正確にtoyだった。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?