Help us understand the problem. What is going on with this article?

google ocr で できること php編

More than 1 year has passed since last update.

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で画像のテキストのみ引っこ抜いたりできる。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした