class BaiduImageSearch(object): # 获得连接对象 APP_ID = '16842923' API_KEY = 'BXngiNhk6QxRR2kEhbcbrCAI' SECRET_KEY = 'A7NeGhtiNw2WUzbSNfGkRQVzQv3Z57yg' client = None def __init__(self): self.client = AipImageSearch(self.APP_ID, self.API_KEY, self.SECRET_KEY) # 获取图片 def get_file_content(self, file_path): with open(file_path, 'rb') as fp: return fp.read() def upload(self, path, good_id, sku_id): """ 调用相似图检索—入库, 图片参数为本地图片 """ image = self.get_file_content(path) """ 如果有可选参数 """ options = {} params = {} params['key'] = path params['good_id'] = good_id params['sku_id'] = sku_id options["brief"] = json.dumps(params) # print(options) """ 带参数调用相似图检索—入库, 图片参数为本地图片 """ res = self.client.similarAdd(image, options) # client.similarAddUrl(url, options) # 远程图片 return res def upload_remote_url(self, url, good_id, sku_id): """ 如果有可选参数 """ pass # options = {} # params = {} # params['key'] = url # params['good_id'] = good_id # params['sku_id'] = sku_id # options["brief"] = json.dumps(params) # print(options) # # """ 带参数调用相似图片搜索—入库, 图片参数为远程url图片 """ # res = self.client.similarAddUrl(url, options) # return res def delete(self, key): """ 调用删除相似图,图片参数为远程url图片 """ url = 'http://tbcdn.tajansoft.com/' + key self.client.similarDeleteByUrl(url) def search(self, path): image = self.get_file_content(path) res = self.client.similarSearch(image) return res
def pic_indatebase(filePath, Name): """ 你的 APPID AK SK """ APP_ID = '10327828' API_KEY = 'PGmsAVMTBtahp6X7PWGbfgur' SECRET_KEY = 'xLQGw2bscQXDZgBQpIs36x2W3g8qcedz ' aipImageSearch = 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(filePath) """ 如果有可选参数 """ options = {} options["brief"] = "{\"name\":\"%s\",}" % Name """ 带参数调用相似图检索—入库 """ result = aipImageSearch.similarAdd(image, options) # """ 调用相似图检索—检索 """ # result =aipImageSearch.sameHqSearch(image) print result
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)
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")
import os options = {} import random import time import pandas as pd for root, dirs, files in os.walk(r'D:\picture_all_April'): bb = [] for file in files: print(file) a = time.time() options["brief"] = file options["tags"] = "100,11" aa_result = client.similarAdd( get_file_content(os.path.join(root, file)), options) bb.append(aa_result) bb_content_sign = pd.DataFrame(bb) print(bb_content_sign) pd.io.sql.to_sql(bb_content_sign, 'april_cont_sign', engine1, schema='test', if_exists='append') # image = get_file_content('O1CN010vAmBL1ntpVDABFQq_!!791105148.jpg') # """ 调用相似图检索—入库, 图片参数为本地图片 """ # client.similarAdd(image) """ 如果有可选参数 """ """ 带参数调用相似图检索—入库, 图片参数为本地图片 """ # client.similarAdd(image, options)