记一次使用百度云图像搜索功能
百度云也提供图像搜索业务,业务介绍地址:
https://ai.baidu.com/tech/imagesearch/product
提供三种服务:
- 相同图像检索:搜索相同的图片;
- 相似图像检索:搜索相似的图片;
- 商品图像检索:搜索相似的图片,但相较于相似图片搜索会进行主体检测;
开发文档
文档地址:
https://ai.baidu.com/docs#/IMAGESEARCH-API/f7537cfc
目前在该页支持的sdk有:
- Java
- Nodejs
- PHP
- Python
- C#
- C++
文档写的很详细,使用起来方便,这点比阿里云图像搜索做的好些。
python sdk使用指南
https://ai.baidu.com/docs#/ImageSearch-Python-SDK/top
按照这个介绍,首先安装python sdk
pip install baidu-aip
图像搜索AipImageSearch已经被封装在baidu-aip中了,调用起来也很简便。
对于每一种应用场景(相同图, 相似图,商品图检索)都提供了四种操作:
- 入库
- 检索
- 更新
- 删除
每种操作至少支持两类输入:本地图像, url;但针对于删除操作,额外多一种,可以使用图片签名
应用例子
几种应用场景操作起来类似,只是封装但函数名称变动一下,这里以商品检索图为例讲解:
入库
from aip import AipImageSearch
# 先建立客户端
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY)
# 入库函数
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 调用商品检索—入库, 图片参数为本地图片 """
client.productAdd(image);
""" 如果有可选参数 """
options = {}
options["brief"] = "{\"name\":\"手机\", \"id\":\"666\"}"
options["class_id1"] = 1
options["class_id2"] = 1
""" 带参数调用商品检索—入库, 图片参数为本地图片 """
client.productAdd(image, options)
url = "http//www.x.com/sample.jpg"
""" 调用商品检索—入库, 图片参数为远程url图片 """
client.productAddUrl(url);
""" 如果有可选参数 """
options = {}
options["brief"] = "{\"name\":\"手机\", \"id\":\"666\"}"
options["class_id1"] = 1
options["class_id2"] = 1
""" 带参数调用商品检索—入库, 图片参数为远程url图片 """
client.productAddUrl(url, options)
检索
from aip import AipImageSearch
# 先建立客户端
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 调用商品检索—检索, 图片参数为本地图片 """
client.productSearch(image);
""" 如果有可选参数 """
options = {}
options["class_id1"] = 1
options["class_id2"] = 1
options["pn"] = "100"
options["rn"] = "250"
""" 带参数调用商品检索—检索, 图片参数为本地图片 """
client.productSearch(image, options)
url = "http//www.x.com/sample.jpg"
""" 调用商品检索—检索, 图片参数为远程url图片 """
client.productSearchUrl(url);
""" 如果有可选参数 """
options = {}
options["class_id1"] = 1
options["class_id2"] = 1
options["pn"] = "100"
options["rn"] = "250"
""" 带参数调用商品检索—检索, 图片参数为远程url图片 """
client.productSearchUrl(url, options)
更新
from aip import AipImageSearch
# 先建立客户端
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 调用商品检索—更新, 图片参数为本地图片 """
client.productUpdate(image);
""" 如果有可选参数 """
options = {}
options["brief"] = "{\"name\":\"周杰伦\", \"id\":\"666\"}"
options["class_id1"] = 1
options["class_id2"] = 1
""" 带参数调用商品检索—更新, 图片参数为本地图片 """
client.productUpdate(image, options)
url = "http//www.x.com/sample.jpg"
""" 调用商品检索—更新, 图片参数为远程url图片 """
client.productUpdateUrl(url);
""" 如果有可选参数 """
options = {}
options["brief"] = "{\"name\":\"周杰伦\", \"id\":\"666\"}"
options["class_id1"] = 1
options["class_id2"] = 1
""" 带参数调用商品检索—更新, 图片参数为远程url图片 """
client.productUpdateUrl(url, options)
contSign = "8cnn32frvrr2cd901"
""" 调用商品检索—更新, 图片参数为图片签名 """
client.productUpdateContSign(contSign);
""" 如果有可选参数 """
options = {}
options["brief"] = "{\"name\":\"周杰伦\", \"id\":\"666\"}"
options["class_id1"] = 1
options["class_id2"] = 1
""" 带参数调用商品检索—更新, 图片参数为图片签名 """
client.productUpdateContSign(contSign, options)
删除
from aip import AipImageSearch
# 先建立客户端
""" 你的 APPID AK SK """
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
SECRET_KEY = '你的 Secret Key'
client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
image = get_file_content('example.jpg')
""" 调用删除商品,传入参数为图片 """
client.productDeleteByImage(image);
url = "http//www.x.com/sample.jpg"
""" 调用删除商品,图片参数为远程url图片 """
client.productDeleteByUrl(url);
contSign = "8cnn32frvrr2cd901"
""" 调用删除商品,传入参数为图片签名 """
client.productDeleteBySign(contSign);
总结
- 百度云上传的时候是针对图片而言,图片是唯一的,即使一张图片对应多个url,上传的时候只上传一张,其它的url会报错,商品已入库;
- 用brief来表示图像信息,可以相同;
- 对于库里的图像而言,图像内容是唯一的,即对应的base64编码;
- 相较于阿里云而言, 百度云更容易部署;