最近博主接触到了百度平台的一些接口,感觉用处蛮大的,在这里整理了一下给大家分享:
菜品识别:可以延伸为图像识别,开通不同接口即可
效果图如下:
图片上传界面是自己写的
通过百度接口返回菜品的百度词条,百度图片等信息,如果想返回其他结果信息可以参考改图:
程序结构:
以下是上传界面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>菜品添加</title>
<style>
.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);
}
</style>
</head><center>
<body style="background-image:url(images/img.jpg); background-size:100%;opacity:0.8">
<table><tr><th height=100></th></tr><tr><th width=300px></th><th>
<form method="POST" action="cai.php" enctype="multipart/form-data" class="card-form">
<div>
<div class="img-container"></div>
<font color="#333333" size="+0" ><strong>上传菜样</strong></font>:<input class="img-btn" type="file" id="drivingLicence" name="drivingLicence" style="height:30px; width:180px; " >
</div>
<input type="submit" value="提 交" style="width:320px; height:40px; background-color:#F00; background-image:url(images/S81122-14430781.jpg); background-size:100%; border-radius:5px; font-size:16px; font-weight:800; font:Tahoma, Geneva, sans-serif; color:#FFF ">
</form></th></tr></table>
<script>
//上传图片并预览
function previewImg(fileInput,imgDiv){
if(window.FileReader){//支持FileReader的时候
var reader=new FileReader();
reader.readAsDataURL(fileInput.files[0]);
reader.οnlοad=function(evt){
imgDiv.innerHTML="\<img src="+evt.target.result+"\>";
}
}else{//兼容ie9-
imgDiv.innerHTML='<div class="img" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src=\'' + fileInput.value + '\)\';"></div>';
}
}
function selectImg(fileInputs,imgDivs){
var checkImg=new RegExp("(.jpg$)|(.png$)|(.bmp$)|(.jpeg$)","i");
var i=0;
for(;i<fileInputs.length&&i<imgDivs.length;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<ell;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<oCll;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);
</script>
</body>
</html>
图片分析界面代码
<?php
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 "<center><table ><tr><th colspan=4 align=center><h1>菜品识别结果</h1></th></tr><tr bgcolor='#8bffff'><th height=50>菜品名称</th><th>热量</th><th>菜品图片</th><th>菜品描述</th>";
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 "<tr><th width=100>";
echo $e->food_name."<th width=200 height=180>";
echo $e->food_calorie."<font color='red'>kj</font><th>";
//echo $e->food_probability."<th>";
echo "<img src='$e->food_img' width=180 height=160>"."<th width=400 align=left>";
echo " ".$e->food_description."</th><tr bgcolor='#8bffff'><th colspan=4 ></th>";
}
//echo json_encode($arr_food, JSON_UNESCAPED_UNICODE);
}
//$conn->close();
?>