Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- """Download training photos from Azure Custom Vision workspace
- """
- # reference: ajakupov@expertime.com
- import http.client
- import urllib.request
- import urllib.parse
- import urllib.error
- import base64
- import json
- import os
- import cv2
- import numpy as np
- # create tag folders if they do not exist
- tag_folders = ['desiccation']
- for folder in tag_folders:
- if not os.path.exists(folder):
- os.makedirs(folder)
- try:
- # base url
- conn = http.client.HTTPSConnection(
- 'australiaeast.api.cognitive.microsoft.com')
- headers = {"Training-key": "{training key}"}
- conn.request(
- "GET", "/customvision/v3.3/Training/projects/{Project id}/images/tagged?take=256&skip=0",headers = headers)
- response = conn.getresponse()
- encoding = 'utf-8'
- # get response as a raw string
- data = response.read().decode(encoding)
- # convert the string to a json object
- data_json = json.loads(data)
- conn.close()
- # write json to file
- with open('data.json', 'a') as f:
- json.dump(data, f, ensure_ascii=False, indent=4)
- for item in data_json:
- # uri for image download
- originalImageUri = item['originalImageUri']
- # image tag
- tag_name = item["tags"][0]["tagName"]
- #create the object, assign it to a variable
- proxy = urllib.request.ProxyHandler({'http': '127.0.0.1'})
- # construct a new opener using your proxy settings
- opener = urllib.request.build_opener(proxy)
- # install the openen on the module-level
- urllib.request.install_opener(opener)
- # download image from uri
- output_file = os.path.join(tag_name, str(item["id"]) + '.jpg')
- urllib.request.urlretrieve(originalImageUri, output_file)
- image_cv = cv2.imread(output_file)
- HEIGHT, WIDTH, channels = image_cv.shape
- for region in item["regions"]:
- x1, y1 = region.get('left'), region.get('top')
- x2, y2 = x1 + region.get('width'), y1 + region.get('height'),
- x1, y1, x2, y2 = round(x1 * WIDTH), round(y1 * HEIGHT), round(x2 * WIDTH), round(y2 * HEIGHT)
- image_cv = cv2.rectangle(image_cv, (x1, y1), (x2, y2), (0, 0, 0), 3)
- cv2.imwrite(output_file, image_cv)
- except Exception as e:
- print(e)
Add Comment
Please, Sign In to add comment