class baidu(object): """ 你的 APPID AK SK """ APP_ID = '11183322' API_KEY = '8UF9PssmdeEM1CikKrnI78Tj' SECRET_KEY = 'ourP7NkBa1Lbn84RCeSu4e39OC9Ttbm4' def __init__(self): self.client = AipImageSearch(self.APP_ID, self.API_KEY, self.SECRET_KEY) def image_add(self, filename): # 添加 image = get_file_content(filename) options = { "tags": "100,1", "brief": "{\"name\":\"test4\"}" } # tags是进行二维分组 result = self.client.similarAdd(image, options) # 调用参数入库 return False if result.get('error_code') else True def image_search(self, filename): # 搜索 image = get_file_content(filename) options = { "tags": "100,1", "pn": "0", "rn": "1" } # tages在二维分组里查找,pn是分页,rn是截取条数 result = self.client.similarSearch(image, options) for line in result.get('result'): # 检查第一个返回的 pass if int(line.get('score')) > 0.8: # 相似度大于0.8返回相同 return True else: return False def image_delete(self, contSign): # 删除 # contSign = "385018571,3552852085" self.client.similarDeleteBySign(contSign)
from aip import AipImageSearch """ 你的 APPID AK SK """ APP_ID = '11183322' API_KEY = '8UF9PssmdeEM1CikKrnI78Tj' SECRET_KEY = 'ourP7NkBa1Lbn84RCeSu4e39OC9Ttbm4' client = AipImageSearch(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """ contSign = "4043096407,707976548" """ 调用删除相同图,传入参数为图片签名 """ print(client.similarDeleteBySign(contSign))
class SimilarityPic(Pic): """ 你的APPID AK SK """ APP_ID = '15900910' API_KEY = 'w9GDvddBuCn86NwxSTfFiocG' SECRET_KEY = 'W8GgfxwpMpFjDlrFV4mLT1i1Y6pu4Lx1' def __init__(self): self.image = None self.client = AipImageSearch(SimilarityPic.APP_ID, SimilarityPic.API_KEY, SimilarityPic.SECRET_KEY) self.options = {} self.url = None self.contSign = None # 相似图入库 def putIn(self, sign): # 调用相似图检索—入库, 图片参数为本地图片 if sign == 0: return self.client.similarAdd(self.image) # 带参数调用相似图检索—入库, 图片参数为本地图片 elif sign == 1: return self.client.similarAdd(self.image, self.options) # 调用相似图检索—入库, 图片参数为远程url图片 elif sign == 2: return self.client.similarAddUrl(self.url) # 带参数调用相似图检索—入库, 图片参数为远程url图片 elif sign == 3: return self.client.similarAddUrl(self.url, self.options) else: print("请输入正确的sign") # 相似图检索 def check(self, sign): # 调用相似图检索—检索, 图片参数为本地图片 if sign == 0: return self.client.similarSearch(self.image) # 带参数调用相似图检索—检索, 图片参数为本地图片 elif sign == 1: return self.client.similarSearch(self.image, self.options) # 调用相似图检索—检索, 图片参数为远程url图片 elif sign == 2: return self.client.similarSearchUrl(self.url) # 带参数调用相似图检索—检索, 图片参数为远程url图片 elif sign == 3: return self.client.similarSearchUrl(self.url, self.options) else: print("请输入正确的sign") # 相似图更新 def update(self, sign): # 调用相似图检索—更新, 图片参数为本地图片 if sign == 0: return self.client.similarUpdate(self.image) # 带参数调用相似图检索—更新, 图片参数为本地图片 elif sign == 1: return self.client.similarUpdate(self.image, self.options) # 调用相似图检索—更新, 图片参数为远程url图片 elif sign == 2: return self.client.similarUpdateUrl(self.url) # 带参数调用相似图检索—更新, 图片参数为远程url图片 elif sign == 3: return self.client.similarUpdateUrl(self.url, self.options) else: print("请输入正确的sign") # 相似图删除 def delete(self, sign): # 调用删除相似图,传入参数为图片 if sign == 0: return self.client.similarDeleteByImage(self.image) # 调用删除相似图,图片参数为远程url图片 elif sign == 1: return self.client.similarDeleteByUrl(self.url) # 调用删除相似图,传入参数为图片签名 elif sign == 2: return self.client.similarDeleteBySign(self.contSign) else: print("请输入正确的sign")