LoginSignup
0

More than 3 years have passed since last update.

Cloud Vison APIを触ってみた

Posted at

概要

タイトル通り、GCPから提供されているCloud Vision APIで遊んでみたのでまと目てみます。

Cloud Vision APIとは?

GCPが提供している、学習済み機械学習モデルをAPIを通して利用できるもの!

  • ラベルの検出
  • テキストの検出
  • 場所の検出

等が出来ます。

TEXT ANNOTATION

つかった画像はこちら

test.png

text.php
<?php
    $apiKey = "Your API KEY";
    $imagePath = $argv[1] ;

    $json = json_encode([
        "requests" => [
            [
                "image" => [
                    "content" => base64_encode(file_get_contents($imagePath))
                ],
                "features" => [
                    [
                        "type" => "TEXT_DETECTION",// テキスト認識を選択
                    ]
                ]
            ]
        ]
    ]);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $apiKey);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($curl);
    $data = json_decode($response, true);
    curl_close($curl);

    echo $data["responses"][0]["fullTextAnnotation"]["text"];

コマンド上から下記のコマンドでレスポンスを得ることができます。

$ php text.php /path/to/image

画像から
文字起こし

きちんと、テキストを認識して返してくれました。

LABEL ANNTATION

つかった画像はこちら

test.png

label.php
<?php
    $apiKey = "Your API KEY";
    $imagePath = $argv[1] ;

    $json = json_encode([
        "requests" => [
            [
                "image" => [
                    "content" => base64_encode(file_get_contents($imagePath))
                ],
                "features" => [
                    [
                        "type" => "LABEL_DETECTION",// ラベル認識を選択
                        "maxResults" => 10,// 最大結果件数を選択
                    ]
                ]
            ]
        ]
    ]);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $apiKey);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($curl);
    $data = json_decode($response, true);
    curl_close($curl);

    foreach ($data["responses"][0]["labelAnnotations"] as $key => $value) {
        echo "descropiton: " . $value["description"] . "\n" . "score: " . $value["score"] . "\n\n";
    }

$ php label.php /path/to/image

descropiton: Font
score: 0.9693762

descropiton: Text
score: 0.9655209

descropiton: Calligraphy
score: 0.58965546

descropiton: Graphics
score: 0.5272959

ラベルの内容とスコア(信頼度)をセットで表示しています。

LANDMARK ANNTATION

つかった画像はこちら

その1:エッフェル塔
eiffel.jpeg

その2:東京タワー
tokyo-tower.jpg

landmark.php
<?php
    $apiKey = "Your API KEY";
    $imagePath = $argv[1] ;

    $json = json_encode([
        "requests" => [
            [
                "image" => [
                    "content" => base64_encode(file_get_contents($imagePath))
                ],
                "features" => [
                    [
                        "type" => "LANDMARK_DETECTION",// ランドマーク認識を選択
                    ]
                ]
            ]
        ]
    ]);

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=" . $apiKey);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

    $response = curl_exec($curl);
    $data = json_decode($response, true);
    curl_close($curl);

    foreach ($data["responses"][0]["landmarkAnnotations"] as $key => $value) {
        echo "descropiton: " . $value["description"] . "\n" . "score: " . $value["score"] . "\n\n";
    }

$ php landmark.php /path/to/image/エッフェル塔

descropiton: Eiffel Tower
score: 0.8110765

$ php landmark.php /path/to/image/東京タワー


エッフェル塔は81%の信頼度で認識してくれました。
しかし東京タワーは認識できず、空の配列が返って来てしまいました。
また、ここには出していませんが、その場所の座標を取得したりすることもできます。

まとめ

他にも色々な画像を試しましたが、文字起こしは画像の光の当たり具合で変に認識されたりしてしまったりします。
しかし、かなり高い制度でラベルをつけてくれたり、場所を特定してくれたりとGoogleすごいと思いました。
ぜひやってみてください!

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
0