柔晶美网络工作室

柔晶美网络工作室,倾心于web技术的博客站点

关注我 微信公众号

您现在的位置是: 首页 > 博客日记

laravel 6.0使用百度AI进行图文识别和表格识别

2019-09-29 admin php  laravel  1756

前言

目前,百度AI图文识别,特别是表格识别的准确率相对较高,而且每天100条免费,对于开发者来说,比较经济实惠。

百度AI文档

点击查看

目录结构:

├── AipOcr.php //OCR
     └── lib
       ├── AipHttpClient.php    //内部http请求类 
       ├── AipBCEUtil.php    //内部工具类
       └── AipBase        //Aip基类

Laravel使用百度AI方法

1.官方网站下载php SDK压缩包。

2.将下载的aip-php-sdk-version.zip解压后,复制AipOcr.php以及APP/ocr/lib到工程文件夹中。

3.引入AipOcr.php类,将根目录的composer.json修改,在classmap添加自动加载项: "app/ocr"

4.在命令行运行: composer dumpautoload

5.在控制器中,使用new \AipOcr()初始化类,详细代码如下:

namespace App\Admin\Forms;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\WzsbModel;

class Wzsbupload extends Form{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '文字识别用户页';
    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        //获取上传后的表单和文件数据
        $openid = $request->openid;
        $username = $request->username;
        $pictures = $request->pictures;
        //$picjson = json_encode($pictures,JSON_UNESCAPED_UNICODE);
        //dd($picjson);
        //引入百度图文识别SDK
        $options = array();
        //定位和识别结果数组
        $options["detect_direction"] = "true";
        //检测朝向
        $client = new \AipOcr('APPID ', 'AK ', 'SK');
        //你的百度 APPID AK SK
        // 通用文字识别
        $data = $client->general(file_get_contents($pictures[0]), $options);
        //识别第一张图片,可考虑循环识别多张图
        //$jsondata = json_encode($data,JSON_UNESCAPED_UNICODE);//转为json格式,给app和小程序使用
        $txtsz = $data['words_result'];
        //words_result定位和识别结果数组
        $txt = '';
        //无换行纯文本
        $txtbr = '';
        //包含换行的纯文本
        foreach ($txtsz as $val) {
            $txt = $txt . $val['words'];
            $txtbr = $txt . '
' . $val['words'];
        }
        //dd($txt);
        //写入数据库
        $dbwrite = new WzsbModel();
        $dbwrite->openid = $openid;
        $dbwrite->username = $username;
        $dbwrite->txt = $txt;
        //$dbwrite->pictures = $pictures;
        $dbwrite->save();
        /**                DB::table('wzsb')->insert(
                    ['openid' => $openid, 'username' => $username, 'txt' => $txt]
                        );
        **/
        admin_success($txt);
        return back();
    }
    /**
     * Build a form here.
     */
    public function form()
    {
        //$this->text('openid','openid')->rules('required');
        //$this->text('username','用户昵称')->rules('required');
        $this->multipleImage('pictures', '上传图片');
        //按住ctrl选择多图上传
        //$this->text('txt', '识别结果');
        //$this->text('beizhu', '备注');
        //$this->datetime('created_at');
    }
    /**
     * The data of the form.
     *
     * @return array $data
     * 默认数据
     */
    public function data()
    {
        return ['openid' => 'openid', 'username' => '用户昵称', 'created_at' => now()];
    }}

控制器的代码可根据需要修改。

文章评论


需要 登录 才能发表评论
热门评论
0条评论

暂时没有评论!