Skip to content

Instantly share code, notes, and snippets.

@nakagami
Created June 2, 2022 02:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nakagami/12f3296f9246b26451dc168a89d9e684 to your computer and use it in GitHub Desktop.
Save nakagami/12f3296f9246b26451dc168a89d9e684 to your computer and use it in GitHub Desktop.
get adult score by Microsoft Computer Vision API
import sys
import os
import json
import http.client
from urllib.parse import urlencode
# Computer Vision API
# https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/
MICROSOFT_COMPUTER_VISION_API_KEY = os.environ['MICROSOFT_COMPUTER_VISION_API_KEY']
def get_ms_vision_score(jpeg_path):
headers = {
'Content-Type': 'application/octet-stream',
'Ocp-Apim-Subscription-Key': MICROSOFT_COMPUTER_VISION_API_KEY,
}
params = urlencode({
'visualFeatures': 'Adult',
})
conn = http.client.HTTPSConnection('southeastasia.api.cognitive.microsoft.com')
conn.request("POST", "/vision/v1.0/analyze?%s" % params, open(jpeg_path, "br").read(), headers)
return conn.getresponse()
if __name__ == "__main__":
if len(sys.argv) < 2:
print("{} <image_file>".format(sys.argv[0]))
sys.exit()
response = get_ms_vision_score(sys.argv[1])
print(json.dumps(json.loads(response.read().decode('utf-8')), indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment