LoginSignup
2
2

More than 5 years have passed since last update.

google ocr で できること php編

Posted at

google ocr でできること。
いろいろあるが、

・顔画像解析
・テキスト抽出

この2つ以外は使いみち無さそう。
サンプルコードをどうぞ。


<?php

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

use Google\Cloud\Vision\VisionClient;

class HogesController extends AppController
{

    public function face()
    {
        $projectId = "carbide-parser-*****";

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

//        ローカルファイル
//        顔写真では無い
        $image = $vision->image(file_get_contents(WWW_ROOT."img/best1.png"), ['FACE_DETECTION']);

//        顔写真である
//        $image = $vision->image(file_get_contents("https://matching-profile.west.edge.storage-yahoo.jp/e9/99/personals-1522845620-384368/profile/m6d1XU-95167300.jpg?AWSAccessKeyId=AKIQHJ6XDD9Y76EXB37&Expires=1524810790&Signature=61UP%2Bq8jXAKQAQiwY6WUyW%2BVRao%3D"), ['FACE_DETECTION']);



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

        if(!empty($result->info()['faceAnnotations'])){
            echo "顔写真です";
        } else {
            echo "顔写真ではありません";
        }


        $this->autoRender = false;

    }

    public function ocr()
    {

        $projectId = "carbide-parser-******";

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

        ]);



//        テキスト抽出
        $img = 'https://cdn-ak.f.st-hatena.com/images/fotolife/r/ryo_abe/20171209/20171209221150.jpg';
        $image = $vision->image(file_get_contents($img), ['TEXT_DETECTION']);
        $result = $vision->annotate($image);
//
//
//        //テキストを表示
        pd($result->info()['textAnnotations'][0]['description']);

        //ここまで テキスト抽出




        $this->autoRender = false;
    }



}



こんな感じでそれぞれ
顔かどうか判断したり、OCRで画像のテキストのみ引っこ抜いたりできる。

2
2
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
2
2