LoginSignup
0
0

Azure Custom Visionにアップロードした教師データをダウンロードしなおす

Posted at

Custom Visionにアップロード済みの教師データをタグごとにフォルダ分けしてダウンロードするサンプルです。

import os
import requests
import json
import collections
from tqdm.notebook import tqdm as tqdm

headers = {
    "Training-Key": ""
}

params = {
    "take": "256",
    "skip": "256"
}

project_id = ""
endpoint = ""
url = f"https://{endpoint}/customvision/v3.0/training/projects/{project_id}/images/tagged"

r = requests.get(url, params=params, headers=headers)
data = json.loads(r.text)

# タグ名の一覧取得
tag_name_list = []
for item in data:
    tag_name_list.append(item["tags"][0]["tagName"])
tag_name_list = [k for k, v in collections.Counter(tag_name_list).items() if v > 1]

# タグ名に応じたフォルダを作成
for tag_name in tag_name_list:
    folder = "output/" + tag_name
    if not os.path.exists(folder):
        os.makedirs(folder)

# ダウンロードしてタグ名に一致するフォルダに格納
for item in tqdm(data):
    original_image_uri = item["originalImageUri"]
    tag_name = item["tags"][0]["tagName"]
    id = item["id"]
    folder_path = "output/" + tag_name
    file_name = os.path.join(folder_path, f"{id}") + ".png"
    if os.path.exists(file_name):
        print(f"{file_name} is exists.")
        continue
    image = requests.get(original_image_uri).content
    with open(file_name, "wb") as file:
        file.write(image)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0