本地上传文件 $file = $_FILES['file'];
取的filr_path 是 $file['tmp_name'];
其他的功能 自己看文档调用吧
<?php
/**
* 百度图像识别
*
* @date 2018-05-25 01:57:53
*
* @author huqitao <huqitaoit@gmail.com>
*
*/
namespace app\extra;
require 'aip-php-sdk-2.2.3/AipOcr.php';
class ai {
const APP_ID = ''; //百度参数 自己弄啊
const API_KEY = ''; //百度参数 自己弄啊
const SECRET_KEY = ''; //百度参数 自己弄啊
protected $client;
public function __construct() {
$this->client = new \AipOcr(self::APP_ID, self::API_KEY, self::SECRET_KEY);
}
//文字识别
public function getTextForImage($file_path) {
if (self::_check_file_path_type_is_url($file_path)) {
return $this->client->webImageUrl($file_path);
} else {
$image = file_get_contents($file_path);
return $this->client->webImage($image);
}
}
//银行卡识别
public function getBankCardForImage($file_path) {
if (self::_check_file_path_type_is_url($file_path)) {
return $this->client->bankcardUrl($file_path); //sdk里面没有 自己参考百度的代码补充一个
} else {
$image = file_get_contents($file_path);
return $this->client->bankcard($image);
}
}
static private function _check_file_path_type_is_url($file_path) {
if (preg_match('/http/', $file_path)) {
return true;
}
return false;
}
}
使用事例 --- 别忘了引入 上面的文件/**
* ai 图像识别
* 微软雅黑字体识别效果最好
* @return type
*/
public function recognition() {
$file = $_FILES['file'];
$file_url = input('post.file_url');
$file_path = $file['tmp_name'] ?: $file_url;
$type = input('post.type');
$ai = new ai();
switch ($type) {
case 'text': //微软雅黑字体识别效果最好
$result = $ai->getTextForImage($file_path);
break;
case 'bank_card':
$result = $ai->getBankCardForImage($file_path);
break;
}
return json($result);
}