python - 使用 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
但是我在文档中找不到有关它的内容。可能吗?如果是,我该怎么做?
解决方案
推荐阅读
- php - Laravel Dusk - 卷曲错误(代码 3):
畸形 - android - 调用 Fragment onStop 时停止计时器
- asp.net - task 或 new Thread(() 在本地工作但不在主机上工作
- python - Python SQLAlchemy 和 Postgres - 如何使用 json 值中的子字符串进行搜索
- ios - 首选语言和设备语言不匹配
- git - 使用 .gitignore 忽略我的项目中的 .env 文件
- javascript - 如何保护 JAMstack 应用程序中的(第 3 方)API 密钥?
- laravel - nuxt 和 laravel 是如何通信的?
- ios - 折叠时如何更改图像单元格?
- javascript - 无条件的循环 - Javascript