1.百度智能云-智能时代基础设施 ,建应用获取apiKey secretKey,用于获取AccessToken
2.开通 logo 商标识别-检索 api
1.百度sdk提供了http请求的依赖,我没用,用了一个项目里通用的工具类 2.百度有api文档和在线调试的网页,两个地方的请求参数有出入,要看https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Ok3bcxc59 3.注意:图片需要base64编码、去掉编码头后再进行urlencode。 data:image/png;base64, 要去掉,然后URLEncoder.encode(str) 4.结果会返回多个结果,没特殊要求就用正确概率最高的第一个result 5.入库 删除 检索 3个接口前2个可以用get请求,检索建议用post 6.默认查的是logo公共库,为了精确度可以用私库,百度api上可以建,这里不展开了
api文档以这个网址为准
百度智能云-ApiExplorerhttps://cloud.baidu.com/apiexplorer/index.html?Product=GWSE-wpnv4rj4dDD&Api=GWAI-radNHSuSg7m
这里请求属性名imgurl不对,应该是url,另外它给的java demo我也用不了,返回nullhttps://ai.baidu.com/ai-doc/IMAGERECOGNITION/Ok3bcxc59https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Ok3bcxc59
pom.xml
net.sf.json-lib
json-lib
2.4
jdk15
HttpUtil.java
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
@Slf4j
public class HttpUtil {
/**
* 处理doget请求
*
* @param url
* @return
*/
public static JSONObject doGetstr(String url) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
JSONObject jsonObject = null;
try {
CloseableHttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.info("doGet result" + result);
jsonObject = JSONObject.fromObject(result);
}
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
/**
* 处理post请求
*
* @param url
* @return
*/
public static JSONObject doPoststr(String url, String outStr) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
JSONObject jsonObject = null;
try {
httpPost.setEntity(new StringEntity(outStr, "utf-8"));
CloseableHttpResponse response = httpclient.execute(httpPost);
String result = EntityUtils.toString(response.getEntity(), "utf-8");
log.info("doPost result" + result);
jsonObject = JSONObject.fromObject(result);
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
}
ApiBaiduController.java
import ApiBaiduService;
import HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* (ProjectPhoto)表控制层
*
* @author EasyCode
* @since 2021-11-26 15:49:49
*/
@Slf4j
@Controller
@RequestMapping("/apiBaidu")
public class ApiBaiduController {
@Autowired
private ApiBaiduService apiBaiduService;
/**
* @Date 10:19 2021/9/10
* @Description 对字节数组字符串进行Base64解码并生成图片
* @param: file
* @param: response
* @Return void
*/
@CrossOrigin //跨域的设置
@RequestMapping("/logo/search")
public void uploadBase64(HttpServletRequest req, HttpServletResponse response) {
//获取前端传来的base64码 去掉base64码的data:image/png;base64,
String base64String = req.getParameter("photo").replaceAll("data:image/png;base64,", "");
String name = apiBaiduService.apiLogoSearchPost(base64String);
if (name == null) {
HttpUtil.renderData(response, 1, "fail");
} else {
HttpUtil.renderData(response, 0, name);
}
}
}
ApiBaiduService.java
public interface ApiBaiduService {
boolean apiLogoAdd(String imgUrl, String briefName);
String apiLogoSearchPost(String img);
boolean apiLogoDelete(String contSign);
}
ApiBaiduServiceImpl.java
import HttpUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import java.net.URLEncoder;
/**
* @Description: java类作用描述
* @Author:
* @CreateDate: 2021/12/17$ 11:45$
* @UpdateDate: 2021/12/17$ 11:45$
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Slf4j
@Service("ApiBaiduService")
public class ApiBaiduServiceImpl implements ApiBaiduService {
//25366048
private static final String API_KEY = "aTwq......";
private static final String SECRET_KEY = "igc......";
private static final String BAIDU_OAUTH_URL = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=CLIENT_ID&client_secret=CLIENT_SECRET";
//ACCESS_TOKEN 有效期30天
private static final String ACCESS_TOKEN = "24.c101......";
//logo识别-入库
private static final String AIP_LOGO_ADD = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/add?access_token=ACCESS_TOKEN&url=IMGURL&scenes=logo_search&brief=BRIEF";
//logo识别-检索
private static final String AIP_LOGO_SEARCH = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?access_token=ACCESS_TOKEN&image=IMAGE&scenes=logo_search";
//logo识别-检索
private static final String AIP_LOGO_SEARCH_POST = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?access_token=ACCESS_TOKEN";
//logo识别-删除
private static final String AIP_LOGO_DELETE = "https://aip.baidubce.com/rest/2.0/realtime_search/v1/logo/delete?access_token=ACCESS_TOKEN&cont_sign=CONT_SIGN&scenes=logo_search";
/**
* @Author
* @Date 10:42 2021/12/20
* @Description 通过imageBase64 进行logo识别-检索
* @param: image imageBase64
* @Return java.lang.String 品牌名称
*/
@Override
public String apiLogoSearchPost(String imageBase64) {
try {
//imageBase64进行urlencode
String url = AIP_LOGO_SEARCH_POST.replace("ACCESS_TOKEN", ACCESS_TOKEN);
log.info("百度Baidu apiLogoSearch url:" + url);
String param = "image=" + URLEncoder.encode(imageBase64, "UTF-8");
JSONObject result = HttpUtil.doPoststr(url, param);
log.info("百度Baidu apiLogoSearch result:" + result);
//检索到logo的数量
String result_num = result.getString("result_num");
if (result_num.isEmpty() || Integer.parseInt(result_num) < 1) {
//没检索到logo
return null;
}
//选择第一个结果,并获取品牌名称
String resultArr = result.getJSONArray("result").getString(1);
String logoName = JSONObject.fromObject(resultArr).getString("name");
log.info("百度Baidu apiLogoSearch logoName:" + logoName);
return logoName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* @Author
* @Date 14:21 2021/12/17
* @Description 方法描述
* @param: imgUrl 入库图片url
* @param: briefName 入库logo品牌名
* @Return void
*/
@Override
public boolean apiLogoAdd(String imgUrl, String briefName) {
String brief = "%7B%22name%22%3A%22" + briefName + "%22%7D";
String url = AIP_LOGO_ADD.replace("ACCESS_TOKEN", ACCESS_TOKEN).replace("IMGURL", imgUrl).replace("BRIEF", brief);
log.info("百度Baidu apiLogoAdd url:" + url);
JSONObject result = HttpUtil.doGetstr(url);
log.info("百度Baidu apiLogoAdd result:" + result);
try {
//图片签名
String cont_sign = result.getString("cont_sign");
log.info("百度Baidu cont_sign " + cont_sign);
if (!cont_sign.isEmpty()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Author
* @Date 14:21 2021/12/17
* @Description logo识别—删除
* @param: contSign 入库图片图片签名
* @Return void
*/
@Override
public boolean apiLogoDelete(String contSign) {
String url = AIP_LOGO_DELETE.replace("ACCESS_TOKEN", ACCESS_TOKEN).replace("CONT_SIGN", contSign);
log.info("百度Baidu apiLogoDelete url:" + url);
JSONObject result = HttpUtil.doGetstr(url);
log.info("百度Baidu apiLogoDelete result:" + result);
try {
//图片签名
String log_id = result.getString("log_id");
log.info("百度Baidu apiLogoDelete " + log_id);
if (!log_id.isEmpty()) {
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* @Author
* @Date 13:20 2021/12/17
* @Description 获取AccessToken
* @param:
* @Return void
*/
public void getAccessToken() {
String url = BAIDU_OAUTH_URL.replace("CLIENT_ID", API_KEY).replace("CLIENT_SECRET", SECRET_KEY);
log.info("百度Baidu getAccessToken url:" + url);
JSONObject result = HttpUtil.doGetstr(url);
log.info("百度Baidu getAccessToken result:" + result);
String accessToken = "";
String expires_in = "";
try {
accessToken = result.getString("access_token");
String expiresIn = result.getString("expires_in");
log.info("百度Baidu accessToken " + accessToken + " expiresIn:" + expiresIn);
} catch (Exception e) {
e.printStackTrace();
}
}
}
前端测试页面
Insert title here
上传照片测试