1.作者介绍
严松,男,西安工程大学电子信息学院,2022级研究生
研究方向:机器人抓取检测
电子邮件:2448052777@qq.com
2.百度API实现logo商标识别接口介绍
该请求用于检测和识别图片中的台标、品牌商标等logo信息。即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片中logo的名称、位置和置信度。
使用时,可直接调用logo识别-检索接口,支持识别超过2万类logo名称;当效果欠佳时,可以建立子库(在控制台创建应用并申请建库)并通过调用logo入口接口完成自定义logo入库,再调用logo识别-检索接口,选择在自定义logo库内检索,提高识别效果。
3.调用百度API实现logo商标识别流程
3.1注册百度智能云并创建应用
百度智能云链接:百度智能云-登录 (baidu.com)
注册好之后点击控制台,先领取免费资源,然后点击创建应用,创建完应用之后可以查看应用,并且能看到API Key和Secret Key。
图1 创建应用
图2 查看应用
3.2Python代码进行调试
完整实验代码:
import requests
API_KEY = ""//查看自己的API_KEY
SECRET_KEY = ""//查看自己的SECRET_KEY
def main():
url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/logo?access_token=" + get_access_token()
payload = {'C:/Users/24480/Desktop/1.bmp'}//自己的logo路径
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
def get_access_token():
"""
使用 AK,SK 生成鉴权签名(Access Token)
:return: access_token,或是None(如果错误)
"""
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
return str(requests.post(url, params=params).json().get("access_token"))
if __name__ == '__main__':
main()
图3 请求参数
图4 返回说明
图5 调试结果
3.3返回示例:
1.比较擦乱的logo调试结果
图6 杂乱logo
返回结果:可以看到一共返回了5组结果,其中有一组"斑马办公"是不符合预期的,证明识别的logo尽量不要有复杂的背景。
{
"result_num": 5,
"result": [
{
"name": "可口可乐",
"type": 1,
"probability": 0.95749333630437,
"location": {
"height": 123,
"left": 44,
"top": 22,
"width": 313
}
},
{
"name": "可口可乐",
"type": 0,
"probability": 0.97189044952393,
"location": {
"height": 123,
"left": 44,
"top": 22,
"width": 313
}
},
{
"name": "斑马办公",
"type": 1,
"probability": 0.33733308532021,
"location": {
"height": 57,
"left": 118,
"top": 127,
"width": 204
}
},
{
"name": "可口可乐",
"type": 0,
"probability": 0.83195910211337,
"location": {
"height": 226,
"left": 36,
"top": 0,
"width": 321
}
},
{
"name": "可口可乐",
"type": 1,
"probability": 0.32056304758245,
"location": {
"height": 226,
"left": 36,
"top": 0,
"width": 321
}
}
],
"log_id": 1657261019508606700
}
2.完整紧致包围盒图像logo识别结果
图7 完整紧致包围盒图像logo
返回结果:可以看到一共返回了4组结果,且都符合预期
{
"result_num": 4,
"result": [
{
"name": "合生元",
"type": 1,
"probability": 0.96611329092496,
"location": {
"height": 34,
"top": 7,
"width": 121,
"left": 36
}
},
{
"name": "合生元",
"type": 0,
"probability": 0.98904836588892,
"location": {
"height": 34,
"top": 7,
"width": 121,
"left": 36
}
},
{
"name": "合生元",
"type": 1,
"probability": 0.5572005059984,
"location": {
"height": 32,
"top": 6,
"width": 64,
"left": 81
}
},
{
"name": "合生元",
"type": 1,
"probability": 0.39284993518483,
"location": {
"height": 36,
"top": 6,
"width": 63,
"left": 61
}
}
],
"log_id": 1657263068743726800
}
参考链接: