문제 설명
Bing Visual Search API 끝점은 bing.com/visualsearch와 다른 결과를 반환합니다. (Bing Visual Search API endpoint returns different results than bing.com/visualsearch)
Bing Visual Search API를 사용하여 이미지의 유사한 이미지를 수집하려고 합니다. 다음 코드 스니펫이 있고 이 이미지를 API 엔드포인트로 전송합니다.
import requests
import json
BASE_URI = "https://api.bing.microsoft.com/v7.0/images/visualsearch"
SUBSCRIPTION_KEY = ''
HEADERS = {'Ocp‑Apim‑Subscription‑Key': SUBSCRIPTION_KEY}
insightsToken = 'ccid_twIfDfmx*cp_BF680FB5127A96880FCA7D76CC402B60*mid_D6663701CA72F63CF255CBF7EB4998ED50CAABC8*simid_608045920540183139*thid_OIP.twIfDfmxxN4umi!_‑sacdNAHaFo'
formData = '{"imageInfo":{"imageInsightsToken":"' + insightsToken + '", }, "knowledgeRequest":{"invokedSkills": ["SimilarImages"]}}'
file = {'knowledgeRequest': (None, formData)}
def main():
try:
response = requests.post(BASE_URI, headers=HEADERS, files=file)
response.raise_for_status()
print_json(response.json())
except Exception as ex:
raise ex
def print_json(obj):
"""Print the object as json"""
print(json.dumps(obj, sort_keys=True, indent=2, separators=(',', ': ')))
bing.com/visualsearch 해당 이미지에 대해 표시됩니다. 이미지 업로드와 이미지 링크 제공을 모두 시도했습니다. 결과는 동일했습니다.
이제 API 엔드포인트를 통해 코드 스니펫으로 수집된 처음 몇 개의 이미지를 살펴보세요.
어떤 이유로 bing.com/visualsearch
결과는 Bing Visual Search API보다 훨씬 더 정확하고 정확합니다. Bing Image Search API에 시장 매개변수(IP, 쿠키 및 검색 기록 기반)가 있다는 것을 알고 있습니다. 하지만, 내 IP를 변경하고 브라우저에서 시크릿 모드를 사용하려고 해도 bing.com/visualsearch
를 사용할 때 여전히 동일한 결과를 얻었습니다. 또한 params = (('mkt', 'en‑us'))
를 API 엔드포인트에 추가했지만 결과는 여전히 거의 동일했습니다. 따라서 이 매개변수는 결과에 큰 영향을 미치지 않는다고 생각합니다. 그래서 저는 아무것도 제공하지 않습니다. 이렇게 하면 결과가 웹 검색에 가깝다고 생각하기 때문입니다.
그런데도 결과가 왜 이렇게 다를까요? Bing은 웹 검색에 하나의 API 버전을 사용하고 클라우드 서비스에 다른 버전을 사용하고 있습니까?
결과가 왜 이렇게 다른가요? Bing은 웹 검색에 하나의 API 버전을 사용하고 클라우드 서비스에 다른 버전을 사용하고 있습니까? 결과가 왜 이렇게 다른가요? Bing은 웹 검색에 하나의 API 버전을 사용하고 클라우드 서비스에 다른 버전을 사용하고 있습니까?
참조 솔루션
방법 1:
Is Bing using one API version for their web search and another version for their cloud services?
I can't speak for the internal workings of microsoft, but it appears that the endpoint API for bing.com/visualsearch
and your code are different.
To determine an endpoint called from the browser, open inspect element in firefox on the image search page after uploading your image, and reload. In the network tab, you will see lots of requests. Search for api and for me a pointer to an ad service comes up, along with the endpoint for the api service thats used to find similar images.
The url used by microsoft visual search is https://www.bing.com/images/api/custom/knowledge
Instead of your https://api.bing.microsoft.com/v7.0/images/visualsearch
In that same inspect element window, you can navigate to the Request tab within the networking tab and view the post request. The form used by the browser, surprisingly, is perfectly compatible with your code. I copied your code onto my machine with my own azure key and replaced the formData
variable with the browsers form. You should try it yourself and judge the results. For me however, the results still weren't quite the same, although they seemed objectively closer, with some images overlapping between the browser vs python api results. The form my browser sent was this:
{
"imageInfo":{
"imageInsightsToken":"bcid_Tq7w6Pw5It0DxQ*ccid_rvDo/Dki",
"source":"Gallery"
},
"knowledgeRequest":{
"invokedSkills":[
"ImageById",
"BestRepresentativeQuery",
"Offline",
"ObjectDetection",
"OCR",
"EntityLinkingFace",
"EntityLinkingDog",
"EntityLinkingAnimal",
"EntityLinkingPlant",
"EntityLinkingLandmark",
"EntityLinkingFood",
"EntityLinkingBook",
"SimilarProducts",
"SimilarImages",
"RelatedSearches",
"ShoppingSources",
"PagesIncluding",
"TextAds",
"ProductAds",
"SponsoredAds",
"Annotation",
"Recipes",
"Travel"
],
"invokedSkillsRequestData":{
"adsRequest":{
"textRequest":{
"mainlineAdsMaxCount":2
}
}
},
"index":1
}
}
So in addition to similar images, the webpage also invoked a number of other skills including Object Detection, and BestRepresentativeQuery.
In summary, it appears that the endpoints used are different, and only a microsoft employee could tell you if they point to the same internal api. However, refining the form submitted in your post request to match what the browser uses will give you similar results to the official image search webpage.
(by lowlypalace、Andrew James)
참조 문서