最近博主接触到了百度平台的一些接口,感觉用处蛮大的,在这里整理了一下给大家分享:
菜品识别:可以延伸为图像识别,开通不同接口即可
效果图如下:
图片上传界面是自己写的
通过百度接口返回菜品的百度词条,百度图片等信息,如果想返回其他结果信息可以参考改图:
程序结构:
以下是上传界面代码
菜品添加.img-container{
width: 230px;
height: 150px;
background:#F2F2F2;
margin-bottom:35px;
margin-left:40px;
overflow: hidden;
border: 1px solid #000;
background-color:#FFC;
background-image:url(images/S81122-11210803.jpg);
background-size:100%;
border-radius:5px;
}
.img-container>img{
width:230px;
height: 150px;
}
.img{
width: 230px;
height: 150px;
background-image:url(images/PG%358%601_RR8N9LX4S95Z5B.png);
}
上传菜样: |
---|
//上传图片并预览
function previewImg(fileInput,imgDiv){
if(window.FileReader){//支持FileReader的时候
var reader=new FileReader();
reader.readAsDataURL(fileInput.files[0]);
reader.οnlοad=function(evt){
imgDiv.innerHTML="\";
}
}else{//兼容ie9-
imgDiv.innerHTML='
}
}
function selectImg(fileInputs,imgDivs){
var checkImg=new RegExp("(.jpg$)|(.png$)|(.bmp$)|(.jpeg$)","i");
var i=0;
for(;i
(function(i){//立即执行函数;保存i
fileInputs[i].οnchange=function(){
if(checkImg.test(fileInputs[i].value)){
previewImg(this,imgDivs[i]);
}else{
alert("只支持上传.jpg .png .bmp .jpeg;你的选择有误");
}
};
})(i);
}
}
/* 为IE6 IE7 IE8增加document.getElementsByClassName函数 */
/MSIE\s*(\d+)/i.test(navigator.userAgent);
var isIE=parseInt(RegExp.$1?RegExp.$1:0);
if(isIE>0&&isIE<9){
document.getElementsByClassName=function(cls){
var els=this.getElementsByTagName('*');
var ell=els.length;
var elements=[];
for(var n=0;n
var oCls=els[n].className||'';
if(oCls.indexOf(cls)<0) continue;
oCls=oCls.split(/\s+/);
var oCll=oCls.length;
for(var j=0;j
if(cls==oCls[j]){
elements.push(els[n]);
break;
}
}
}
return elements;
}
}
var fileInputs=document.getElementsByClassName("img-btn");//文件选择按钮
var imgDivs=document.getElementsByClassName("img-container");//图片容器
selectImg(fileInputs,imgDivs);
图片分析界面代码
header('Access-Control-Allow-Origin:*');
error_reporting(0);
require_once 'AipImageClassify.php';
// 你的 APPID AK SK
const APP_ID = '15878504';
const API_KEY = 'KWw027gSGzI7dZ2aVCPqF5Ef';
const SECRET_KEY = 'hSWLps4hq1fXxrQDDKVNdtRQ4IkolqGx';
//echo dirname(__FILE__);
if($_SERVER['REQUEST_METHOD']=='POST')
{ $drivingLicence=$_FILES['drivingLicence']['tmp_name'];
$client = new AipImageClassify(APP_ID, API_KEY, SECRET_KEY);
$image = file_get_contents($drivingLicence);
// 调用菜品识别
$client->dishDetect($image);
// 如果有可选参数
$options = array();
$options["top_num"] = 3;
$options["filter_threshold"] = "0.7";
$options["baike_num"] = 5;
// 带参数调用菜品识别
$a=$client->dishDetect($image, $options);
//print_r($a['result']);
//var_dump($a['result']);
$arr=$a['result'];
class food_data{
public $food_name="";
public $food_calorie="";
public $food_probability="";
public $food_img="暂无图片";
public $food_description="暂无描述";
}
$arr_food=array();
echo "
菜品识别结果 | |||
---|---|---|---|
菜品名称 | 热量 | 菜品图片 | 菜品描述 |
foreach($arr as $arr1){
$e=new food_data();
$e->food_name=$arr1['name'];
$e->food_calorie=$arr1['calorie'];
$e->food_probability=$arr1['probability'];
$e->food_img=$arr1['baike_info']['image_url'];
//$e->food_img=$_FILES['drivingLicence']['tmp_name'];
$e->food_description=$arr1['baike_info']['description'];
//array_push($arr_food,$e);
if($arr1['baike_info']['description']==null){$e->food_description="暂无描述";}
echo "
";echo $e->food_name."
";echo $e->food_calorie."kj
";//echo $e->food_probability."
";echo ""."
";echo " ".$e->food_description."
";}
//echo json_encode($arr_food, JSON_UNESCAPED_UNICODE);
}
//$conn->close();
?>