最近博主在做自己的毕业设计时,为了是自己的系统更加为完善,需要添加几个接口,于是找到了百度的识别接口,并把它实现了,此外,还将其与数据库联系在一起,利用PHP语言将检索到的图片下载到本地,将信息添加到数据库中,以下是主要实现截图:

百度识别菜品并将检索到的图片下载到本地并将信息添加到数据库_php


百度识别菜品并将检索到的图片下载到本地并将信息添加到数据库_php_02


百度识别菜品并将检索到的图片下载到本地并将信息添加到数据库_数据库_03


主要实现代码:

<?php
$conn = @mysql_connect('localhost' , 'root' , 'px980305') or die(mysql_error()); //连接数据库
mysql_query('set names utf8' , $conn); //设置连接字符集
@mysql_select_db('dsc' , $conn) or die(mysql_error());
?>
<meta name="referrer" content="never">
<style type="text/css">
input.text{text-align:center;padding:20px 20px;width:160px;}
</style>
<?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"] = 6;

// 带参数调用菜品识别
$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=6 align=center><h1>菜品识别结果</h1></th></tr><tr bgcolor='#8bffff'><th height=50>菜品名称</th><th>热量/KJ</th><th>菜品图片</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 "<form action='add.php' method='post' enctype='multipart/form-data'>";
echo "<tr><th width=100>";
echo "<input type='text' text-align:center style='border-style:none;font-size:22px;width:160px' name='name' value='$e->food_name'>" ."<th width=200 height=180>";
echo "<center><input type='text' style='width:60px;border-style:none;font-size:20px;' name='calorie' value='$e->food_calorie'></center>"."<font color='red'></font><th>";

echo "<img src='$e->food_img' style='border-style:none;border-radius:5px' width=180 height=160><input type='hidden' name='img' value='$e->food_img'>"."<th width=400 align=left>";

echo "    "."<center><textarea style='border-style:none;width:360px;height:200px;font-size:20px;' name='description' >".$e->food_description."</textarea></center>"."</th>";
echo "<th> <center><input type='text' style='width:60px;border-style:none;font-size:24px;' name='price' value='100'>元</center></th>";
echo"<th> <input type='submit' value='添 加' style='width:120px; height:40px;border:none; background-color:#ff7315; background-image:url(images/S81122-14430781.jpg); background-size:100%; border-radius:5px; font-size:24px; font-weight:800; font:Tahoma, Geneva, sans-serif; color:#FFF '></th>";

echo "</form><tr bgcolor='#8bffff'><th colspan=5 ></th>";
}

//echo json_encode($arr_food, JSON_UNESCAPED_UNICODE);
}
//$conn->close();
?>
<?php
require('conn.php');
require('downimage.php');
if(empty($_POST)){
require('login.php');
}else{
date_default_timezone_set("prc");
$stringtime = date("Y-m-d H:i:s",time());

$saveFilename=strtotime($stringtime).'.jpg';
echo $saveFilename;
//$image = mysql_real_escape_string(file_get_contents($_FILES['photo']['tmp_name'])); //获取图片
//$image_type = $_FILES['photo']['type']; //获取图片格式
$name = $_POST['name'];
$calorie = $_POST['calorie'];
$images = $_POST['img'];
$price = $_POST['price'];
$description= $_POST['description'];

$sqlstr = "insert into menu(name,calorie,image,adress,description,price) values('".$name."','".$calorie."','".$saveFilename."','".$images."','".$description."','".$price."')";
downimage($images ,realpath("./images") . '/'.$saveFilename);
@mysql_query($sqlstr) or die(mysql_error()); //执行sql语句,若执行成功,继续下面跳转页面。若执行失败,提示错误信息
header('location:login.php'); //跳转页面
exit();
}
?>