首页 > 解决方案 > 使用 Bing API v7.0 搜索相似图像

问题描述

我已经可以使用这个 python 脚本通过 text-requser 搜索图像:

ap = argparse.ArgumentParser()
ap.add_argument("-q", "--query", required=True, help="search query to search Bing Image API for")
ap.add_argument("-o", "--output", required=True, help="path to output directory of images")
args = vars(ap.parse_args())

API_KEY = "my_api_key_here"
MAX_RESULTS = 250
GROUP_SIZE = 50
URL = "https://api.cognitive.microsoft.com/bing/v7.0/images/search"
EXCEPTIONS = set([IOError, FileNotFoundError, exceptions.RequestException, exceptions.HTTPError, exceptions.ConnectionError, exceptions.Timeout])
term = args["query"]
headers = {"Ocp-Apim-Subscription-Key": API_KEY}
params = {"q": term, "offset": 0, "count": GROUP_SIZE}

search = requests.get(URL, headers=headers, params=params)
search.raise_for_status()

results = search.json()
estNumResults = min(results["totalEstimatedMatches"], MAX_RESULTS)
total = 0

for offset in range(0, estNumResults, GROUP_SIZE):
    params["offset"] = offset
    search = requests.get(URL, headers=headers, params=params)
    search.raise_for_status()
    results = search.json()
    for v in results["value"]:
        try:
            r = requests.get(v["contentUrl"], timeout=30)

            ext = v["contentUrl"][v["contentUrl"].rfind("."):]
            p = os.path.sep.join([args["output"], "{}{}".format(
                str(total).zfill(8), ext)])

            f = open(p, "wb")
            f.write(r.content)
            f.close()

            image = cv2.imread(p)

            if image is None:
                print("[INFO] deleting: {}".format(p))
                os.remove(p)
                continue
            total += 1
        except Exception as e:
            if type(e) in EXCEPTIONS:
                continue

但我想使用图像 url 搜索类似图像,就像在 Bing 搜索中一样:https ://www.bing.com/images/search?view=detailv2&iss=sbi&form=SBIHMP&sbisrc=UrlPaste&q=imgurl:https%3A%2F%2Fimg -fotki.yandex.ru%2Fget%2F4807%2Fgelionsk.da%2F0_4ba5a_82bb84ac_orig&idpbck=1&selectedindex=0&id=https%3A%2F%2Fimg-fotki.yandex.ru%2Fget%2F4807%2Fgelionsk.da%2F0_4ba5a_82bbA3%2Fmediaurl=https% %2Fimg-fotki.yandex.ru%2Fget%2F4807%2Fgelionsk.da%2F0_4ba5a_82bb84ac_orig&exph=0&expw=0&vt=2&sim=1

但是我在文档中找不到有关它的内容。可能吗?如果是,我该怎么做?

标签: pythonimagebing-api

解决方案


推荐阅读