Skip to content

Instantly share code, notes, and snippets.

@fukasawah
Created August 1, 2019 11:28
Show Gist options
  • Save fukasawah/0b59741509821d259b5480b1c731580f to your computer and use it in GitHub Desktop.
Save fukasawah/0b59741509821d259b5480b1c731580f to your computer and use it in GitHub Desktop.
ほぼpython3標準のみでCustomVision APIを叩くためのたたき台(multipart/form-dataは諦めろ)
'''
# Usage:
export TRAINING_KEY=XXXXXXXXXXX
python customvision-projects-image-count.py
# API Docs:
CustomVision Training API: https://southcentralus.dev.cognitive.microsoft.com/docs/services/Custom_Vision_Training_3.0/
CustomVision Prediction API: https://southcentralus.dev.cognitive.microsoft.com/docs/services/Custom_Vision_Prediction_3.0/
'''
import os;
import json
from urllib.request import Request,urlopen
TRAINING_KEY = os.environ.get('TRAINING_KEY')
TRAINING_ENDPOINT='https://japaneast.api.cognitive.microsoft.com/customvision/v3.0/training/'
def request_simple(url, data=None, headers={}, method=None):
if method is None:
method = 'GET' if data is None else 'POST'
req = Request(url, data=data, headers=headers, method=method)
with urlopen(req) as f:
return json.load(f.fp)
DEFAULT_HEADERS={
'Training-Key': TRAINING_KEY,
'Content-Type': 'application/json'
}
projects = request_simple(f'{TRAINING_ENDPOINT}/projects', headers=DEFAULT_HEADERS)
for project in projects:
tagged_count = request_simple(f'{TRAINING_ENDPOINT}/projects/{project["id"]}/images/tagged/count', headers=DEFAULT_HEADERS)
untagged_count = request_simple(f'{TRAINING_ENDPOINT}/projects/{project["id"]}/images/untagged/count', headers=DEFAULT_HEADERS)
print(f'{project["name"]}: {tagged_count + untagged_count} ({tagged_count}+{untagged_count})')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment