POST photos with Microsoft Bing Image Search API to get Image Insights (Python)

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

POST photos with Microsoft Bing Image Search API to get Image Insights (Python)
Post an article with an image to WordPress with Python
[First API] Try to get Qiita articles with Python
Post to slack with Python 3
Image collection by calling Bing Image Search API v5 from Python
Link to get started with python
Get reviews with python googlemap api
Quine Post with Qiita API (Python)
How to get started with Python
How to use bing search api
Easily post to twitter with Python 3
Image upload & download to Azure Storage. With Python + requests + REST API
HTML email with image to send with python
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
[Python] Get Python package information with PyPI API
Sample to convert image to Wavelet with Python
I tried to get the authentication code of Qiita API with Python.
I tried to get the movie information of TMDb API with Python
Convert PDF to image (JPEG / PNG) with Python
Image characters and post to slack (python slackbot)
Get users belonging to your organization from Garoon REST API with Python + Requests
How to crop an image with Python + OpenCV
Get image URL using Flickr API in Python
Get stock price data with Quandl API [Python]
I tried to get CloudWatch data with Python
Post youtube soaring title to twitter with python3
Get Gmail subject and body with Python and Gmail API
[Python] Mention to multiple people with Slack API
A layman wants to get started with Python
Python script to get note information with REAPER
How to scrape image data from flickr with python
I tried to get started with blender python script_Part 01
[Python] Get the numbers in the graph image with OCR
Get schedule from Garoon SOAP API with Python + Zeep
Convert the image in .zip to PDF with Python
I tried to get started with blender python script_Part 02
Crawling with Python and Twitter API 1-Simple search function
How to get mouse wheel verdict with Python curses
[Python] Get user information and article information with Qiita API
How to operate Discord API with Python (bot registration)
Sequential search with Python
Binary search with Python3
Image processing with Python
Get date with python
I tried to find the entropy of the image with python
Easy to use Nifty Cloud API with botocore and python
Compare HTTP GET / POST with cURL (command) and Python (programming)
The first API to make with python Djnago REST framework
Minimum knowledge to get started with the Python logging module
[Ev3dev] How to display bmp image on LCD with python
Post a message to Google Hangouts Chat with a thread (Python)
Get data from analytics API with Google API Client for python
How to get into the python development environment with Vagrant
Image analysis with Object Detection API to try in 1 hour
Get additional data to LDAP with python (Writer and Reader)
Output product information to csv using Rakuten product search API [Python]
[Introduction to Python] How to get data with the listdir function
Get the source of the page to load infinitely with python.
Call github api in python to get pull request information
Image processing with Python (Part 2)
Connect to BigQuery with Python