首页 > 技术文章 > 百度云上传图片转文字识别

Mr-zhangwenqiang 2019-11-26 15:26 原文

//百度云文字识别
public function baidu_aip(){
$data=request()->param();
if ($data['icon'] || $data['icon'] != 0) {
$image = $data['icon'];
$imageName = time() . '.png'; //图片名字
if (strstr($image, ",")) {
$image = explode(',', $image);
$image = $image[1];
}
$path = "./uploads/baiduyun_aip/" . date("Ymd", time());
if (!is_dir($path)) { //判断目录是否存在 不存在就创建
mkdir($path, 0777, true);
}
$imageSrc = $path . "/" . $imageName; //图片名字
$r = file_put_contents($imageSrc, base64_decode($image));//返回的是字节数
if (!$r) {
return json(["status" => 0, "msg" => "图片生成失败", "info" =>[]]);
}
} else {
return json(['status'=>0,'msg'=>'您未添加文件,请上传图片']);
}
$status=1;
$msg='获取成功';
// 你的 APPID AK SK
$app_id='9775656';
$api_key='OgmEXoTufwDKVR3QHF5RuPss';
$secret_Key='nfP34548nplc68XAZu5GvWTsBWWmjrtW';
vendor('Aip.AipOcr');
$client = new \AipOcr($app_id, $api_key, $secret_Key);

// 调用通用文字识别, 图片参数为本地图片
$image = file_get_contents($imageSrc);
// 调用通用文字识别(高精度版)
$info=$client->basicAccurate($image);
if(!$info){
$status=0;
$msg='获取失败';
}
return json(['status'=>$status,'msg'=>$msg,'info'=>$info]);
}

推荐阅读