LoginSignup
1
0

More than 1 year has passed since last update.

Google CLOUD VISION で顔認識

Last updated at Posted at 2018-04-27

追記

関連記事もどうぞ
https://qiita.com/ma7ma7pipipi/items/a931c99cf5983a8283de

何の画像か、label を取得したい


        //解析したい画像を指定
        $image = $vision->image(file_get_contents("https://your.jpg"), ['LABEL_DETECTION']);
        $result = $vision->annotate($image);
        $label = $result->labels();

        foreach ($result->labels() as $v) {
            echo $v->info()['description']." : 判定 ".$v->info()['score']."<br>";
        }

#結果


Nose : 判定 0.98358303
Lip : 判定 0.969561
Eyelash : 判定 0.9153476
Plant : 判定 0.9092044
Ear : 判定 0.8893857
Jaw : 判定 0.87869924
Lipstick : 判定 0.8725287
Gesture : 判定 0.85260487
Happy : 判定 0.8361726
Citrus : 判定 0.8327572

webから検索した結果何なのか取得したい


        //解析したい画像を指定
        $image = $vision->image(file_get_contents("https://your.jpg"), ['WEB_DETECTION']);
        $result = $vision->annotate($image);

        $info = $result->info();
        
        echo "webから解析した結果、以下のものだと思われる<br>";
        foreach ($info['webDetection']['webEntities'] as $v) {
            echo $v['description'] . " : 判定 " . $v['score'] . "<br>";
        }

        echo "webから解析した結果、似たような画像を紹介<br>";
        foreach ($info['webDetection']['visuallySimilarImages'] as $v) {
            echo $v['url']."<br><br>";
        }


#結果


webから解析した結果、以下のものだと思われる
Brown hair : 判定 0.5311102
Hair : 判定 0.5171
Hair coloring : 判定 0.51677877
Forehead : 判定 0.5162524
Long hair : 判定 0.5068101
Bangs : 判定 0.48599726
Brown : 判定 0.34513468
Color : 判定 0.3282
02PD - Circolo del Partito Democratico di Milano : 判定 0.2587
Fashion accessory : 判定 0.2218
webから解析した結果、似たような画像を紹介
https://app.subsocial.network/ipfs/ipfs/QmQXf7mjTmefNeg6YcUxJx9EHp2pE5sjgXmPAcYb1ovyC2

https://images-na.ssl-images-amazon.com/images/I/81+bVPxaLSL.png

・・・以下略

何色があるか知りたい


        $image = $vision->image(file_get_contents("https://your.jpg"), ['IMAGE_PROPERTIES']);
        $result = $vision->annotate($image);

        $info = $result->info();


#結果


Array
(
    [imagePropertiesAnnotation] => Array
        (
            [dominantColors] => Array
                (
                    [colors] => Array
                        (
                            [0] => Array
                                (
                                    [color] => Array
                                        (
                                            [red] => 247
                                            [green] => 244
                                            [blue] => 242
                                        )

                                    [score] => 0.25901115
                                    [pixelFraction] => 0.34657776
                                )

                            [1] => Array
                                (
                                    [color] => Array
                                        (
                                            [red] => 99
                                            [green] => 79
                                            [blue] => 66
                                        )

                                    [score] => 0.062984936
                                    [pixelFraction] => 0.03297778
                                )


#ライブラリをインストール


composer require google/cloud-vision

#APIをオンに
https://cloud.google.com/vision/docs/quickstart?hl=ja

screenshot.1.png

#鍵ファイルを取得
以下を参考に。
https://qiita.com/ma7ma7pipipi/items/ad33e001b6d08c3368e6

コード


<?php

namespace App\Controller;
use App\Controller\AppController;

//ライブラリを読み込み
use Google\Cloud\Vision\VisionClient;

class HogesController extends AppController
{


    public function test()
    {

        $projectId = "carbide-parser-99999";//プロジェクトID

        $vision = new VisionClient([
            'projectId' => $projectId,
            //鍵ファイルを指定
            'keyFile' => json_decode(file_get_contents(CONFIG.'warai.json'), true)

        ]);
        
        //解析したい画像を指定
        $image = $vision->image(file_get_contents(WWW_ROOT."img/girl.jpg"), ['FACE_DETECTION']);
        //$image = $vision->image(file_get_contents($img), ['FACE_DETECTION','SAFE_SEARCH_DETECTION','LABEL_DETECTION','OBJECT_LOCALIZATION']);

        $result = $vision->annotate($image);

        print("Faces:\n");
        foreach ((array) $result->faces() as $face) {
            printf("Anger: %s\n", $face->isAngry() ? 'yes' : 'no');
            printf("Joy: %s\n", $face->isJoyful() ? 'yes' : 'no');
            printf("Surprise: %s\n\n", $face->isSurprised() ? 'yes' : 'no');
        }

        $this->autoRender = false;
    }



}


#結果


Faces: Anger: no Joy: no Surprise: no

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