When I tried to get Similer Images of Image Insights by image upload according to the sample script and reference on the API page of MS, it did not work because I was told "There is no query q to use for search", so the correct answer after struggling I arrived at.
upload.py
import requests
headers = {
'Ocp-Apim-Subscription-Key': '<Your API KEY>',
}
uri = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=similarimages"
image = open('image.jpg', 'rb')
files = {'param_name': ('filename.jpg', image, 'image/jpeg')}
r = requests.post(uri, data={'dummy':"test"},files=files,
headers=headers)
The point is not to ** write 'Content-Type':'multipart / form-data'
in headers **.
If you attach a file with files =, it will be converted to multipart / form-data without permission, but if you specify it in headers and overwrite it, the boundary specification will disappear.
Apparently the API is looking at this. Without it, you will get the error "No query q for search". I want insights.
By the time I got here, I tried writing in C #, making a UWP application, receiving it with Node-red and investigating it, suffering from Raspberry Pi security settings, etc.
Postscript In C #, the code in this thread worked, https://stackoverflow.com/questions/41463093/microsoft-cognitive-api-image-search
Recommended Posts