关键词:ES图像识别OCR,识别的文字ES检索
一、ES图像识别OCR
1.1 描述
经过几天的调研,选中了百度的图像识别。但它转出来的是对像,我把它改为了字符串,如下:
<?php
$ch = curl_init();
$url = 'http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid';
$header = array(
'Content-Type:application/x-www-form-urlencoded',
'apikey: 265253715aebccc17403bbb6ab72fc11',
);
$file = '5555.jpg';
$pic_content = file_get_contents($file);
$pic_content_base64 = urlencode(base64_encode($pic_content));
$data ="fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=".$pic_content_base64;
//print_r($data);exit;
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
// 添加参数
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);
//json转数组
$arr = json_decode($res,true);
//数组转字符串
$str =null;
if(is_array($arr['retData'])&&count($arr['retData'])){
foreach ($arr['retData'] as $key =>$v) {
if(is_array($v)&&count($v)){
$str = $str.$v['word'];
}
}
}
echo $str;
?>
注:标红部分json转字符串的。效果如下:
图像识别前图片如下:
1.2 放到搜索引擎里
成功了,如下:
1)图像上传如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<meta charset="utf-8">
</head>
<body>
<form method="post"enctype="multipart/form-data"
action="2.2php_photo_base64.php">
<input type="file" name="imgfile" value=''>
<input value='提交' type="submit">
</form>
</body>
</html>
2)百度图像识别返回文字
<?php
//1百度识别图片--------------------------------------------------------------------1
$ch = curl_init();
$url = 'http://apis.baidu.com/idl_baidu/baiduocrpay/idlocrpaid';
$header = array(
'Content-Type:application/x-www-form-urlencoded',
'apikey: 265253715aebccc17403bbb6ab72fc11',
);
$img = $_FILES['imgfile']['tmp_name'];
$pic_content = file_get_contents($img);
$pic_content_base64 = urlencode(base64_encode($pic_content));
$data =
"fromdevice=pc&clientip=10.10.10.0&detecttype=LocateRecognize&languagetype=CHN_EN
G&imagetype=1&image=".$pic_content_base64;
// 添加apikey到header
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
// 添加参数
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// 执行HTTP请求
curl_setopt($ch , CURLOPT_URL , $url);
$res = curl_exec($ch);
//json转数组
$arr = json_decode($res,true);
//数组转字符串
$keyword =null;
if(is_array($arr['retData'])&&count($arr['retData'])){
foreach ($arr['retData'] as $key =>$v) {
if(is_array($v)&&count($v)){
$keyword = $keyword.$v['word'];
}
}
}
//输出百度识别的文字
echo '<fontsize="4" color="red"><b>百度识别结果为:</b>'.$keyword."</font><br><br>";
注:红字部分是获取上传的图像,红字二是把百度返回的json转化的数组循环转化为字符串。
3)调接口返回搜索结果
//2调搜索引擎接口-----------------------------------------------------------------------------------2
$url='http://192.168.1.14:8004/v1/elasticsearch/search/searchs';
$params= array('type'=>1,'keyword'=>$keyword);
print_r(curlRequest($url,$params));
注:这里面用到了一个模似POST请求函数curlRequest如下:
//模似POST请求--------------------------------------------------------------------------函数
functioncurlRequest( $url, $params=array(), $header=array() ){
$ch= curl_init();
$res=curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_TIMEOUT, 60);
if(isset($_SERVER['HTTP_USER_AGENT']) ){
curl_setopt($ch, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
}
if($params ){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($params));
}
if($header ){
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
}
$result= curl_exec ($ch);
curl_close($ch);
if($result == NULL) {
return false;
}
return$result;
}
2017年4月4日星期二