Esempio n. 1
0
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
Esempio n. 2
0
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")