def main(): from dotenv import load_dotenv global training_client global custom_vision_project try: # Get Configuration Settings load_dotenv() training_endpoint = os.getenv('TrainingEndpoint') training_key = os.getenv('TrainingKey') project_id = os.getenv('ProjectID') # Authenticate a client for the training API credentials = ApiKeyCredentials( in_headers={"Training-key": training_key}) training_client = CustomVisionTrainingClient(training_endpoint, credentials) # Get the Custom Vision project custom_vision_project = training_client.get_project(project_id) # Upload and tag images Upload_Images('more-training-images') # Train the model Train_Model() except Exception as ex: print(ex)
def predictWithID(self, database, modelID, inputDataIDs, onFinished): # load model record session = database.cursor() session.execute( "SELECT remote_id FROM " + self._datatableName + " WHERE id = %s", (modelID, )) result = session.fetchone() session.close() if result: projectID = result[0] predictOK = True resultMap = {} if len(inputDataIDs) > 0: try: # Now there is a trained endpoint that can be used to make a prediction trainer = CustomVisionTrainingClient( self._trainingKey, endpoint=self._endPoint) predictor = CustomVisionPredictionClient( self._predictionKey, endpoint=self._endPoint) project = trainer.get_project(projectID) # load photos for photoID in inputDataIDs: image, _, err = self._serverAPI.getResource( database, photoID) if err is None: isOK, encodedImage = cv2.imencode('.png', image) predictResponse = predictor.classify_image( project.id, projectID, encodedImage) predictResult = predictResponse.predictions if len(predictResult) > 0: resultMap[photoID] = { "CLASS": predictResult[0].tag_name, "SCORE": predictResult[0].probability } except Exception as err: predictOK = False resultMap = {} print("ERROR (", self._datatableName, ") Model", modelID, "failed to predict: " + str(err)) #raise err onFinished({"ISOK": predictOK, "RESULT": resultMap}) else: print("ERROR (", self._datatableName, ") Model", modelID, "failed to predict, model record not found by plugin.") onFinished({"ISOK": False})
def getPredictionBatch(ENDPOINT, publish_iteration_name, prediction_key, prediction_resource_id, file, training_key, project_name): # Now there is a trained endpoint that can be used to make a prediction prediction_credentials = ApiKeyCredentials( in_headers={"Prediction-key": prediction_key}) training_credentials = ApiKeyCredentials( in_headers={"Training-key": training_key}) predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials) trainer = CustomVisionTrainingClient(ENDPOINT, training_credentials) projects = trainer.get_projects() res_batch = {} js_res = {} #Retrieve the object dection project and its tags #Current assumes one tag for p in projects: if p.name == project_name: project = trainer.get_project(p.id) tags = trainer.get_tags(project.id) print('Project Found') for url in file: info = url.split() name = info[0] url = info[1] try: response = requests.get(url) except: print("error retrieving image: " + url) exit(-1) # Open the sample image and get back the prediction results. results = predictor.detect_image(project.id, publish_iteration_name, response.content) # Display the results. js_res["vehicle"] = [] for prediction in results.predictions: x = { "confidence": "{0:.2f}%".format(prediction.probability * 100), "bbox_left": "{0:.2f}".format(prediction.bounding_box.left), "bbox_right": "{0:.2f}".format(prediction.bounding_box.top), "bbox_width": "{0:.2f}".format(prediction.bounding_box.width), "bbox_height": "{0:.2f}".format(prediction.bounding_box.height) } x = json.dumps(x) js_res[prediction.tag_name].append(x) res_batch[name] = js_res return res_batch
def upload_images(training_key): trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) # Find the object detection domain obj_detection_domain = next(domain for domain in trainer.get_domains() if domain.type == "ObjectDetection") print("Creating project...") try: project = trainer.create_project("LEGO Vision", domain_id=obj_detection_domain.id) except HttpOperationError: print("Project already exists. Using this one.") project = trainer.get_project(project_id="71548120-925d-4e59-ba7e-32f99de50240") classes = os.path.join(BASE_DIRECTORY, "class_map.txt") tags = dict() # Make two tags in the new project for _class in list(map(lambda line: line.split('\t')[0], open(classes).readlines())): try: tags[_class] = trainer.create_tag(project.id, _class) except HttpOperationError: print("Tag already created, continuing...") for tag in trainer.get_tags(project_id="71548120-925d-4e59-ba7e-32f99de50240"): tags[tag.name] = tag # Go through the data table above and create the images print("Adding images...") tagged_images_with_regions = [] for image_path in glob.glob(os.path.join(IMAGES_FOLDER, "*.jpg")): file_id, extension = image_path.split(".", 1) image = cv2.imread(image_path) bboxes = read_bboxes(os.path.join(IMAGES_FOLDER, file_id + ".bboxes.tsv"), scale=1, padding=(0, 0, 0, 0)) labels = read_labels(os.path.join(IMAGES_FOLDER, file_id + ".bboxes.labels.tsv")) regions = [Region(tag_id=tags[_class].id, left=bbox[0] / image.shape[1], top=bbox[1] / image.shape[0], width=abs(bbox[0] - bbox[2]) / image.shape[1], height=abs(bbox[1] - bbox[3]) / image.shape[0]) for _class, bbox in zip(labels, bboxes)] with open(image_path, mode="rb") as image_contents: tagged_images_with_regions.append(ImageFileCreateEntry(name=file_id, contents=image_contents.read(), regions=regions)) print("Azure Custom Vision can only accept images in batches of max 64 per batch. Cutting list up in batches..") for batch in chunks(tagged_images_with_regions, 64): trainer.create_images_from_files(project.id, images=batch, tag_ids=[tag.id for tag in tags.values()]) print("Finished adding images. Visit customvision.ai to start training via the GUI.")
def getPrediction(ENDPOINT, publish_iteration_name, prediction_key, prediction_resource_id, img, training_key, project_name): # Now there is a trained endpoint that can be used to make a prediction prediction_credentials = ApiKeyCredentials( in_headers={"Prediction-key": prediction_key}) training_credentials = ApiKeyCredentials( in_headers={"Training-key": training_key}) predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials) trainer = CustomVisionTrainingClient(ENDPOINT, training_credentials) projects = trainer.get_projects() #Retrieve the object dection project and its tags #Current assumes one tag for p in projects: if p.name == project_name: project = trainer.get_project(p.id) tags = trainer.get_tags(project.id) print('Project Found') # Open the sample image and get back the prediction results. results = predictor.detect_image(project.id, publish_iteration_name, img) drawBounds(img, results.predictions) # Display the results. js_res = defaultdict(list) for prediction in results.predictions: print(prediction) x = { "confidence": "{0:.2f}%".format(prediction.probability * 100), "bbox_left": "{0:.2f}".format(prediction.bounding_box.left), "bbox_right": "{0:.2f}".format(prediction.bounding_box.top), "bbox_width": "{0:.2f}".format(prediction.bounding_box.width), "bbox_height": "{0:.2f}".format(prediction.bounding_box.height) } js_res[prediction.tag_name].append(x) return js_res
class CustomVisionAPI(object): def __init__(self, training_key='', prediction_key='', prediction_resource_id='', project_id=''): self.ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com" self.training_key = training_key #"<Replace with training key>" self.prediction_key = prediction_key #"<Replace with prediction key>" self.prediction_resource_id = prediction_resource_id #"<Replace with prediction ressource id>" self.publish_iteration_name = "Iteration1" self.trainer = CustomVisionTrainingClient(training_key, endpoint=self.ENDPOINT) self.predictor = CustomVisionPredictionClient(prediction_key, endpoint=self.ENDPOINT) self.project = self.trainer.get_project( project_id) #'<Replace with project id>' def classify_photo(self, url): with open(url, "rb") as image_contents: #print(url) results = self.predictor.classify_image( self.project.id, self.publish_iteration_name, image_contents.read()) # Display the results. for prediction in results.predictions: print("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100)) return results.predictions[0].tag_name # Exemple #CustomVisionAPI().classify_photo(r'C:\Projet\PhotoClassifier\data\Photos\animal\IMG_20180510_112719_1.jpg')
def __init__(self): # Load credentials from environment # Authenticate the training client credentials = ApiKeyCredentials( in_headers={"Training-key": TRAINING_KEY}) trainer = CustomVisionTrainingClient(TRAINING_ENDPOINT, credentials) # Authenticate the prediction client prediction_credentials = ApiKeyCredentials( in_headers={"Prediction-key": PREDICTION_KEY}) self.predictor = CustomVisionPredictionClient(PREDICTION_ENDPOINT, prediction_credentials) project_name = "car_seal_train_and_validation" self.publish_iteration_name = "Iteration3" self.max_byte_size = 4000000 projects = trainer.get_projects() project_id = next((p.id for p in projects if p.name == project_name), None) print("Connecting to existing project...") self.project = trainer.get_project(project_id)
training_key = "f0dda4df7d7d4cf7a929e471a8aacbe3" prediction_key = "085fb3c741924d09927420ad56be35d4" prediction_resource_id = "/subscriptions/3d325e01-eb54-447a-b415-75dc9f70c03f/resourceGroups/customVision/providers/Microsoft.CognitiveServices/accounts/customVision_prediction" # Plaats hier de juiste naam van je iteratie publish_iteration_name = "Iteration1" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) # Zoek het object detection domain obj_detection_domain = next( domain for domain in trainer.get_domains() if domain.type == "ObjectDetection" and domain.name == "General") # Zet hier de key van je project project = trainer.get_project("97181c71-c821-4f8f-8571-3d7ff56ff334") #########################Met deze code kan je het model trainen##################################### #iteration = trainer.get_iteration #iteration = trainer.train_project(project.id) #while (iteration.status != "Completed"): # iteration = trainer.get_iteration(project.id, iteration.id) # print ("Training status: " + iteration.status) # time.sleep(1) #trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id) predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) #Initialiseren van variabelen nu = datetime.datetime.now() today = date.today()
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com" training_key = ""customvision.py #training_key prediction_key = "" #prediction_key prediction_resource_id = "" #prediction_resource_id publish_iteration_name = "Iteration1" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) project = trainer.get_project('') #projetcid def classify_photo(url): with open(url, "rb") as image_contents: #print(url) results = predictor.classify_image( project.id, publish_iteration_name, image_contents.read()) # Display the results. return results.predictions[0].tag_name
print("Image status: ", image.status) if __name__ == '__main__': # Set Up ENDPOINT = "<INSERT ENDPOINT>" # Hard-coded for ease, but change later training_key = "<INSERT TRAINING KEY" prediction_key = "<INSERT PREDICTION KEY" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) # Currently hard-coded to reference 'WillTest' project project = trainer.get_project('<INSERT PROJECT ID') # Retrieve data from blob storage connect_str = '<INSERT BLOB STORAGE CONNECTION STRING>' container_name = "mlsamples" # Create the BlobServiceClient object which will be used to create a container client blob_service_client = BlobServiceClient.from_connection_string(connect_str) csv_image_dict = { 'Example1.csv': 'ExampleDataset1/', 'Example2.csv': 'ExampleDataset2/' } csv_filename = 'Example1-Phis.csv'
from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateBatch, ImageFileCreateEntry, Region from msrest.authentication import ApiKeyCredentials import time ENDPOINT = "<your API endpoint>" training_key = "<your training key>" prediction_key = "<your prediction key>" prediction_resource_id = "<your prediction resource id>" CUSTOM_VISION_PROJECT_ID = "<your custom vision project id>" credentials = ApiKeyCredentials(in_headers={"Training-key": training_key}) trainer = CustomVisionTrainingClient(ENDPOINT, credentials) print("Getting project...") project = trainer.get_project(CUSTOM_VISION_PROJECT_ID) mask_tag = None nomask_tag = None for tag in trainer.get_tags(project.id): if tag.name == "Mask": mask_tag = tag elif tag.name == "No mask": nomask_tag = tag if not mask_tag: mask_tag = trainer.create_tag(project.id, "Mask") if not nomask_tag: nomask_tag = trainer.create_tag(project.id, "No mask")
def trainModel(self, database, modelID, parameters, onMessage, onFinished): onMessage("Trainer fetching model settings.") session = database.cursor() session.execute( "SELECT remote_id, training_data, extra_info FROM " + self._datatableName + " WHERE id = %s", (modelID, )) result = session.fetchone() session.close() if result: projectID, trainingData, _ = result onMessage("Training starting...") onMessage("Retrieving model...") trainer = CustomVisionTrainingClient(self._trainingKey, endpoint=self._endPoint) project = trainer.get_project(projectID) onMessage("Downloading/Caching and Analyzing training data...") imageList = [] dataClassList = {} try: start = time.time() # retrieve information of created tags createdTags = trainer.get_tags(projectID) for tag in createdTags: dataClassList[tag.name] = tag imageOK = 0 imageFailed = 0 imageTotal = len(trainingData) def visualizeImageDownload(): return "(" + str(imageOK) + "/" + str( imageFailed) + "/" + str(imageTotal) + ")" for photoID in trainingData: image, _, err = self._serverAPI.getResource( database, photoID) if err: imageFailed += 1 onMessage("Failed to download image " + str(photoID) + ". Error: " + err + " " + visualizeImageDownload()) else: imageOK += 1 classOfData = str(trainingData[photoID]) # create tag if not exists if classOfData not in dataClassList: dataClassList[classOfData] = trainer.create_tag( project.id, classOfData) isOK, encodedImage = cv2.imencode('.png', image) imageList.append( ImageFileCreateEntry( name=str(photoID) + ".png", contents=encodedImage, tag_ids=[dataClassList[classOfData].id])) onMessage(visualizeImageDownload()) end = time.time() onMessage("Image caching done. Used: " + str(end - start)) start = time.time() for i in range(0, len(imageList), 64): batch = imageList[i:i + 64] upload_result = trainer.create_images_from_files( project.id, images=batch) if not upload_result.is_batch_successful: onMessage("Image batch upload failed.") for image in upload_result.images: onMessage("Image status: ", image.status) onFinished(False) return end = time.time() onMessage("Image upload done. Used: " + str(end - start)) onMessage("Training model with " + str(imageOK) + " photos...") iteration = trainer.train_project(project.id) while (iteration.status != "Completed"): iteration = trainer.get_iteration(project.id, iteration.id) onMessage("Training status: " + iteration.status) time.sleep(3) # The iteration is now trained. Publish it to the project endpoint trainer.publish_iteration(project.id, iteration.id, projectID, self._resourceID) onMessage("Training done.") onFinished(True) except Exception as err: onMessage("Failed to train.") onMessage("Error Message: " + str(err)) onFinished(False) else: onMessage("The trainer can't recognize the given model any more.") onFinished(False)
credentials = ApiKeyCredentials(in_headers={"Training-key": data["training_key"]}) trainer = CustomVisionTrainingClient(data["endpoint"], credentials) publish_iteration_name = "detectModel" projects = trainer.get_projects() prediction_resource_id = data['prediction_resource_id'] tagged_images_with_regions = [] vehicle_regions = defaultdict(list) #Retrieve the object dection project and its tags #Current assumes one tag for p in projects: if p.name == data["project_name"]: project = trainer.get_project(p.id) tags = trainer.get_tags(project.id) print('Project Found') print('Loading locations') with open(csv_file, 'r',encoding='utf-8' ) as csv_f: reader = csv.reader(csv_f) line_count = 0 d= []
arg_parser = argparse.ArgumentParser() arg_parser.add_argument("-p", "--project", action="store", type=str, help="Project ID", dest="project_id", default=None) arg_parser.add_argument("-k", "--key", action="store", type=str, help="Training-Key", dest="training_key", default=None) arg_parser.add_argument("-e", "--endpoint", action="store", type=str, help="Endoint", dest="endpoint", default="https://southcentralus.api.cognitive.microsoft.com") arg_parser.add_argument("-d", "--dir", action="store", type=str, help="Target directory", dest="dir", default=".\\") args = arg_parser.parse_args() if (not args.project_id or not args.training_key): arg_parser.print_help() exit(-1) print ("Collecting information for source project:", args.project_id) # Create hte client trainer = CustomVisionTrainingClient(args.training_key, endpoint=args.endpoint) # Get the project project = trainer.get_project(args.project_id) print ("Downloading project:", project.name) print ("\tDescription: ", project.description) print ("\tDomain: ", project.settings.domain_id) # get tags tags = trainer.get_tags(project.id) print ("Found:", len(tags), "tags.") with open("tags.txt", "wt") as tags_file: for tag in tags: print(tag.name, tag.id, tag.type, file = tags_file) download_images(trainer, project.id, args.dir)
class CustomVisionBlobUploader(object): _BATCH_SIZE = 64 _WORKER_CONCURRENCY = multiprocessing.cpu_count() * 8 def __init__( self, storage_acct_name, storage_acct_key, storage_container_name, storage_prefix, cv_endpoint, cv_projectid, cv_training_key, tags ): # Configure Blob Storage Instance self._storage_acct_name = storage_acct_name self._storage_acct_key = storage_acct_key self._storage_container_name = storage_container_name self._block_blob_service = BlockBlobService(account_name=storage_acct_name, account_key=storage_acct_key) # Configure Custom Vision Instance self._cv_endpoint = "https://" + str(cv_endpoint) + ".api.cognitive.microsoft.com" self._cv_projectid = cv_projectid self._cv_training_key = cv_training_key self.trainer = CustomVisionTrainingClient(cv_training_key, endpoint=self._cv_endpoint) self.project = self.trainer.get_project(cv_projectid) # Handle tags that may not exist, get tags from project cv_tag_names = [tag.name for tag in self.trainer.get_tags(cv_projectid)] for tag in tags: if tag not in cv_tag_names: self.trainer.create_tag(cv_projectid, tag, description=tag, type="Regular") self.tags_dict = {tag.name:tag for tag in self.trainer.get_tags(cv_projectid)} def start_timer(self): self.START_TIME = default_timer() def load_blob_batches(self, prefix): generator = self._block_blob_service.list_blobs(self._storage_container_name, prefix=prefix) blob_list = [blob.name for blob in generator] batches = [blob_list[i * self._BATCH_SIZE:(i + 1) * self._BATCH_SIZE] for i in range((len(blob_list) + self._BATCH_SIZE - 1) // self._BATCH_SIZE )] self.batches = batches def get_blob_filename(self,blob_path): return blob_path.split('/')[-1] def get_blob(self, bbs, blob_name): blob = bbs.get_blob_to_bytes(self._storage_container_name, blob_name) # print elapsed time elapsed = default_timer() - self.START_TIME time_completed_at = "{:5.2f}s".format(elapsed) print("{0:<30} {1:>20}".format(blob_name, time_completed_at)) return blob async def get_blob_asynchronous(self,batch_input,batch_output_list, tags): print("{0:<30} {1:>20}".format("File", "Completed at")) with ThreadPoolExecutor(max_workers=self._WORKER_CONCURRENCY) as executor: loop = asyncio.get_event_loop() # self.START_TIME = default_timer() tasks = [ loop.run_in_executor( executor, self.get_blob, *(self._block_blob_service, blob) ) for blob in batch_input ] for response in await asyncio.gather(*tasks): customvision_image = ImageFileCreateEntry(name=self.get_blob_filename(response.name),contents=response.content,tag_ids=[self.tags_dict.get(tag).id for tag in tags]) batch_output_list.append(customvision_image)
class LabelUtility: """ Utility for interacting with the Custom Vision label tool. """ def __init__(self, ws_name, project_id, project_key): endpoint_url = "https://{}.cognitiveservices.azure.com/".format( ws_name) self.project_id = project_id self.client = CustomVisionTrainingClient(project_key, endpoint=endpoint_url) self.project = self.client.get_project(project_id=project_id) self.tags = self.client.get_tags(project_id=project_id) def upload_directory(self, data_dir, img_ext="*.jpg", img_dir="images", lbl_file="labels.csv", default_tag_name="important"): """ upload_directory - Upload images from a given directory into the CV workspace :param str data_dir: Source folder of the files. :param str img_ext: image extension. :param str img_dir: image folder. :param str lbl_file: labels file. :param str default_tag_name: default tag name. :returns: None """ label_fn = os.path.join(data_dir, lbl_file) img_folder = os.path.join(data_dir, img_dir) # Check if required folders exist. if not (os.path.isdir(img_folder) and os.path.exists(label_fn)): print("Input data not found") return # Read labels and image list. labels_df = pd.read_csv(os.path.join(label_fn)) image_list = glob.glob(os.path.join(img_folder, img_ext)) # Upload each image with regions for _, path in enumerate(image_list): tagged_images_with_regions = [] regions = [] file_name = path.split("\\")[-1] img = Image.open(path) img_width, img_height = img.size for _, row in labels_df[labels_df.FileName == file_name].iterrows(): x, y, w, h = row.XMin, row.YMin, row.XMax - row.XMin, row.YMax - row.YMin x = x / img_width w = w / img_width y = y / img_height h = h / img_height if "DefectType" in row: default_tag_name = row.DefectType tag = None for a_tag in self.tags: if a_tag.name == default_tag_name: tag = a_tag if not tag: tag = self.client.create_tag(self.project_id, default_tag_name) self.tags = self.client.get_tags(self.project_id) regions.append( Region(tag_id=tag.id, left=x, top=y, width=w, height=h)) with open(path, mode="rb") as image_contents: tagged_images_with_regions.append( ImageFileCreateEntry(name=file_name, contents=image_contents.read(), regions=regions)) upload_result = self.client.create_images_from_files( self.project.id, images=tagged_images_with_regions) if not upload_result.is_batch_successful: print("Image batch upload failed.") for image in upload_result.images: print("Image status: ", image.status) def export_images(self, data_dir, img_dir="images", lbl_file="labels.csv"): """ export_images - Export any tagged images that may exist and preserve their tags and regions. :param str data_dir: Output folder. :param str img_ext: image extension. :param str img_dir: image folder. :param str lbl_file: labels file. :returns: None """ img_folder = os.path.join(data_dir, img_dir) # Check if required folders exist. if not os.path.isdir(img_folder): print("Output folder not found") return count = self.client.get_tagged_image_count(self.project_id) print("Found: ", count, " tagged images.") exported, idx = 0, 0 data = [] while count > 0: count_to_export = min(count, 256) print("Getting", count_to_export, "images") images = self.client.get_tagged_images(self.project_id, take=count_to_export, skip=exported) for image in images: file_name = f'file_{idx}.jpg' img_fname = os.path.join(img_folder, file_name) data += self.download_image(image, img_fname) idx += 1 exported += count_to_export count -= count_to_export df = pd.DataFrame(data, columns=[ "image_name", "DefectName", "xmin", "xmax", "ymin", "ymax" ]) classes = sorted(list(set(df['DefectName']))) class_ids = {} f = open(os.path.join(data_dir, 'label_map.pbtxt'), "w+") for i, clas in enumerate(classes): class_ids[clas] = i + 1 f.write('item {\n') f.write('\tid: ' + str(i + 1) + '\n') f.write('\tname: \'' + clas + '\'\n') f.write('}\n') f.write('\n') f.close() df['classid'] = [ class_ids[the_defect] for the_defect in df['DefectName'] ] df.to_csv(os.path.join(data_dir, lbl_file), index=False) @staticmethod def download_image(image, img_fname): """ download_image - Export an image. :param pyImg3 image: Image object. :param str img_fname: Filename of the image. :returns: None """ regions = [] if hasattr(image, "regions"): regions = image.regions url = image.original_image_uri width = image.width height = image.height # Download the image responseFromURL = req.get(url).content with open(img_fname, 'wb') as f: f.write(responseFromURL) f.close() # Format the regions data = [] for r in regions: left, top, wide, high = r.left, r.top, r.width, r.height left = left * width top = top * height wide = wide * width high = high * height data.append([ img_fname.split("\\")[-1], r.tag_name, int(left), int(left + wide), int(top), int(top + high) ]) return data
class UploadDataset: def __init__(self, files_to_upload: list, project_name: str) -> None: self.files_to_upload = files_to_upload credentials = ApiKeyCredentials( in_headers={"Training-key": TRAINING_KEY}) self.trainer = CustomVisionTrainingClient(TRAINING_ENDPOINT, credentials) self.project_name = project_name self.max_byte_size = 4000000 self.project: Project = self._connect_to_or_create_project( project_name=self.project_name) # Make two tags in the new project self.green_car_seal_tag = self._get_or_create_tag("green_car_seal") self.red_car_seal_tag = self._get_or_create_tag("red_car_seal") self.label_to_tag_id = { 0: self.red_car_seal_tag.id, 1: self.green_car_seal_tag.id, } def _connect_to_or_create_project(self, project_name: str) -> Project: projects = self.trainer.get_projects() project_id = next((p.id for p in projects if p.name == project_name), None) if project_id is not None: print("Connecting to existing project...") return self.trainer.get_project(project_id) print("Creating new project...") obj_detection_domain = next( domain for domain in self.trainer.get_domains() if domain.type == "ObjectDetection" and domain.name == "General") return self.trainer.create_project(project_name, domain_id=obj_detection_domain.id) def _get_or_create_tag(self, tag_name) -> Tag: tags = self.trainer.get_tags(self.project.id) for tag in tags: if tag.name == tag_name: return self.trainer.get_tag(self.project.id, tag.id) return self.trainer.create_tag(self.project.id, tag_name) def _read_annotation_file(self, annotation_path: str) -> list: annotations = [] with open(annotation_path, "r") as f: for line in f: line = line.strip() parameter_list = line.split(" ") label = int(parameter_list[0]) x, y, w, h = list(map(float, parameter_list[1:])) left = x - w / 2 if left < 0: # Accounting for previous rounding error left = 0 top = y - h / 2 if top < 0: # Accounting for previous rounding error top = 0 if left + w > 1: # Accounting for previous rounding error w = 1 - left if top + h > 1: # Accounting for previous rounding error h = 1 - top try: tag_id = self.label_to_tag_id[label] except: raise ValueError( f"Wrong label {label} at {annotation_path}") annotations.append( Region( tag_id=tag_id, left=left, top=top, width=w, height=h, )) return annotations def main(self) -> None: dataset_path = os.path.join(os.path.dirname(__file__), "../../../dataset") existing_image_count = self.trainer.get_image_count( project_id=self.project.id) file_number = existing_image_count self.files_to_upload = self.files_to_upload[file_number:] for file_name in self.files_to_upload: tagged_images_with_regions = [] annotations: list = self._read_annotation_file( annotation_path=os.path.join(dataset_path, "annotations", file_name + ".txt"), ) image_bytes: bytes = read_and_resize_image( image_path=os.path.join(dataset_path, "images", file_name + ".JPG"), max_byte_size=self.max_byte_size, ) print(f"Image {file_name} is {len(image_bytes)} bytes") tagged_images_with_regions.append( ImageFileCreateEntry(name=file_name, contents=image_bytes, regions=annotations)) print("Upload images...") upload_result = self.trainer.create_images_from_files( self.project.id, ImageFileCreateBatch(images=tagged_images_with_regions)) if not upload_result.is_batch_successful: print("Image batch upload failed.") for image in upload_result.images: print("Image status: ", image.status) exit(-1) print( f"Uploaded file number {file_number+1} of {len(self.files_to_upload)}" ) file_number += 1
myProjects = trainer.get_projects() # In[6]: for project in myProjects: print(project.name) print(project.id) print(project.description) # In[7]: Project = trainer.get_project(project_id=project_id) # ## Create the destination Project # In[8]: dest_Project = dest_trainer.create_project(dest_project_new_name, domain_id=dest_classification_domain.id) # In[9]: for project in dest_trainer.get_projects(): print(project.name)
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from azure.cognitiveservices.vision.customvision.training.models import ImageFileCreateEntry, Region, Image, ImageRegionCreateEntry, ImageRegionCreateBatch ENDPOINT = ENDPOINT # Replace with a valid key training_key = "<<training_key>>" prediction_key = "<<prediction_key>>" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) project = trainer.get_project('<<project_id>>') ## Create label tabs import pandas as pd df = pd.read_csv('test_labels.csv') label_list = df['class'].unique() label_tags = [] for label in label_list: label_tags.append(trainer.create_tag(project.id, label).id) label_dict = dict(zip(label_list, label_tags)) #label_dict['can'].id #trainer.get_tags(project.id) """ label_dict = {'can': '485411e9-feee-4d83-af5b-b933c30a1bbb', 'ms clear container': '075ee6d7-1789-4004-b173-3daf233a765e', 'plastic container': '4d71f5b3-cf13-42d7-817c-3e57d64a2445', 'dairy tub': '3d47b7fa-38cc-4fe0-9cb6-aec45903d09f',
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com" training_key = "22ef4a89c10b4e0d8d6cdaabce6b7221" prediction_key = "467bd9a7c49a41f49a9cbf6846379e32" prediction_resource_id = "/subscriptions/f08d91e4-8ee9-45bb-b518-f36e0aecb653/resourceGroups/Castelnau/providers/Microsoft.CognitiveServices/accounts/Classifier_GroupProject_Prediction" publish_iteration_name = "Iteration1" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) project = trainer.get_project('365bd7c3-f643-47c0-a0d3-91f7fb50a65a') def classify_photo(url): with open(url, "rb") as image_contents: #print(url) results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) # Display the results. return results.predictions[0].tag_name
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient #----------- Statics (Custom Vision Project access)---------------- ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com" training_key = "<Replace with training key>" prediction_key = "<Replace with prediction key>" prediction_resource_id = "<Replace with prediction ressource id>" publish_iteration_name = "Iteration1" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) project = trainer.get_project('<Replace with project id>') # #------------------------------------- #----------Function to use in the app-------------------- def classify_photo(url): with open(url, "rb") as image_contents: #print(url) results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) # Display the results. for prediction in results.predictions: print("\t" + prediction.tag_name +
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') # Set Up endpoint = os.environ["ENDPOINT"] # Hard-coded for ease, but change later training_key = os.environ["TRAINING_KEY"] # prediction_key = os.environ["PREDICTION_KEY"] trainer = CustomVisionTrainingClient(training_key, endpoint=endpoint) # Currently hard-coded to reference 'WillTest' project project = trainer.get_project(os.environ["PROJECT"]) # Retrieve data from blob storage connect_str = os.environ["BLOB_CONNECTION_STRING"] container = os.environ["CONTAINER"] # Create the BlobServiceClient object which will be used to create a container client blob_service_client = BlobServiceClient.from_connection_string(connect_str) csv_image_dict = { 'Example1.csv': 'ExampleDataset1/', 'Example2.csv': 'ExampleDataset2/' } csv_filename = 'Example1.csv' print('Getting Metadata...') df = csv_to_df(blob_service_client, csv_filename, container) # print(df) images_per_batch = 10 list_of_dfs = [ df.loc[i:i + images_per_batch - 1, :] for i in range(0, len(df), images_per_batch) ] print('Beginning tagging and upload of', str(len(list_of_dfs) * 10), 'images') for d in list_of_dfs: # Tag Images print('Tagging Images...') image_list = tag_images(d, blob_service_client, container, trainer, project) # Upload to Custom Vision print('Uploading to Custom Vision Portal...') upload_tagged_images(trainer, project, image_list) return func.HttpResponse("It worked!", status_code=200) # size = 10 # list_of_dfs = [df.loc[i:i+size-1,:] for i in range(0, len(df),size)] # print('Beginning tagging and upload of', str(len(list_of_dfs)*10), 'images') # for d in list_of_dfs: # # Tag Images # print('Tagging Images...') # image_list = tag_images(d, blob_service_client, container_name, trainer, project) # # Upload to Custom Vision # print('Uploading to Custom Vision Portal...') # upload_tagged_images(trainer, project, image_list) # import time # # Train model # print ("Training...") # iteration = trainer.train_project(project.id) # while (iteration.status != "Completed"): # iteration = trainer.get_iteration(project.id, iteration.id) # print ("Training status: " + iteration.status) # time.sleep(3) # publish_iteration_name = "classifyModel" # prediction_resource_id = '<INSERT PREDICTION RESOURCE ID' # # The iteration is now trained. Publish it to the project endpoint # trainer.publish_iteration(project.id, iteration.id, publish_iteration_name, prediction_resource_id) # print ("Your model is ready for prediction.") # from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient # # Now there is a trained endpoint that can be used to make a prediction # predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) # print('') # print('Running prediction on sample image...') # with open("temp.jpg", "rb") as image_contents: # results = predictor.classify_image( # project.id, publish_iteration_name, image_contents.read()) # # Display the results. # for prediction in results.predictions: # print("\t" + prediction.tag_name + # ": {0:.2f}%".format(prediction.probability * 100))
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from azure.cognitiveservices.vision.customvision.training.models import ImageUrlCreateEntry, ImageFileCreateEntry from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient import os ENDPOINT = "https://westus2.api.cognitive.microsoft.com" training_key = "329610eab2ab4d5a8dee59cadbdffe1e" prediction_key = "3ab50f6272c747d7831a72ef493fe46c" trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) project = trainer.get_project("9b50491c-c381-4b9f-9e5e-ed160ec8acd2") def delete_all_iterations(): for iteration in trainer.get_iterations(project.id): trainer.delete_iteration(project.id, iteration.id) def add_training_images(to_add): ''' to_add: list of (file, tagname) tuples ''' for image_file, tagname in to_add: add_training_image(image_file, tagname) def add_training_images_filenames(to_add): ''' to_add: list of (filename, tagname) tuples
src_path = sys.argv[1] if not os.path.exists(src_path): print(f'path not exists: {src_path}') sys.exit(1) with open("settings.yml", 'r') as ymlfile: settings = yaml.load(ymlfile, Loader=yaml.FullLoader) start = datetime.datetime.now() print(f'connecting to custom vision project') trainer = CustomVisionTrainingClient(settings['training_key'], endpoint=settings['endpoint']) project = trainer.get_project(settings['project_id']) print(f'connected to project: {project.name}') print(f'listing images in path: {src_path}') files = os.listdir(src_path) batch_size = int(settings['batch_size']) batches = math.ceil(len(files) / batch_size) print( f'found {len(files)} files to upload in {batches} batches, batch size set to {batch_size}' ) for batch in range(0, batches): timestamp = datetime.datetime.now() print(f'loading batch #{batch}')