def tearDownClass(cls): trainer = CustomVisionTrainingClient(api_key=TRAINING_KEY, endpoint=ENDPOINT) projects = trainer.get_projects() for project in projects: if project.name.find(PROJECT_PREFIX) == 0: trainer.delete_project(project_id=project.id)
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 __init__( self, endpoint, training_key, prediction_key, prediction_ressource_id, project_id, iteration_id, iteration_name, training_images, ): self.endpoint = endpoint self.training_key = training_key self.prediction_key = prediction_key self.prediction_ressource_id = prediction_ressource_id self.project_id = project_id self.iteration_id = iteration_id self.iteration_name = iteration_name self.training_images = training_images #Initializing Prediction Client self.predictor = CustomVisionPredictionClient(self.prediction_key, self.endpoint) #Initializing Training Client self.trainer = CustomVisionTrainingClient(self.training_key, self.endpoint)
def __init__(self, endpoint, key, resource_id, project_id=None): """ Class Constructor, takes the id from Azure Custom Vision. Here the key will be used both for training and predicition Args: ---- endpoint: str key: str resource_id: str project_id: str """ training_credentials = ApiKeyCredentials( in_headers={"Training-key": key}) prediction_credentials = ApiKeyCredentials( in_headers={"Prediction-key": key}) self.trainer = CustomVisionTrainingClient(endpoint, training_credentials) self.predictor = CustomVisionPredictionClient(endpoint, prediction_credentials) self.project_id = project_id self.tags = {} if project_id is not None: for t in self.trainer.get_tags(project_id): self.tags[t.name] = t.id return
def check_tags(trainer: CustomVisionTrainingClient) -> None: t = ['none', 'Bit', 'Doug', 'Penny'] etags = {t.name: t for t in trainer.get_tags(projectId)} for tag in t: if tag in etags: tags[tag] = etags[tag] else: tags[tag] = trainer.create_tag(projectId, tag)
def find_project(): try: trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) for proj in trainer.get_projects(): if (proj.name == SAMPLE_PROJECT_NAME): return proj except Exception as e: print(str(e))
def check_tags(trainer: CustomVisionTrainingClient) -> None: t = ['touching-face', 'not-touching-face', 'no-face-present'] etags = {t.name: t for t in trainer.get_tags(projectId)} for tag in t: if tag in etags: tags[tag] = etags[tag] else: tags[tag] = trainer.create_tag(projectId, tag)
def check_tags(trainer: CustomVisionTrainingClient) -> None: t = ['none', 'rock', 'paper', 'scissors', 'lizard', 'spock'] etags = {t.name: t for t in trainer.get_tags(projectId)} for tag in t: if tag in etags: tags[tag] = etags[tag] else: tags[tag] = trainer.create_tag(projectId, tag)
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 __init__(self, training_key, prediction_key, endpoint, project_name,\ publish_iteration_name): #コンストラクタの定義 self.predictor = CustomVisionPredictionClient(prediction_key, endpoint=endpoint) self.trainer = CustomVisionTrainingClient(training_key, endpoint=endpoint) self.project_name = project_name self.publish_iteration_name = publish_iteration_name
def find_project(): # Use the training API to find the SDK sample project created from the training example. trainer = CustomVisionTrainingClient(TRAINING_KEY, endpoint=ENDPOINT) for proj in trainer.get_projects(): if (proj.name == PROJECT_NAME): return proj return False
def tearDown(self, *args, **kwargs): trainer = CustomVisionTrainingClient(api_key=self.training_key, endpoint=self.endpoint) projects = trainer.get_projects() for project in projects: if project.name.find(self.project_prefix) == 0: logger.info("Deleting project %s", project.id) trainer.delete_project(project_id=project.id) super(CustomVisionTestCase, self).tearDown(*args, **kwargs)
def get_project_id(config): """ Get project ID list """ credentials = ApiKeyCredentials( in_headers={"Training-key": config["training_key"]}) trainer = CustomVisionTrainingClient(config["ENDPOINT"], credentials) project_id = next(proj.id for proj in trainer.get_projects() if proj.name == config["project_name"]) return project_id
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 fetch_project(): try: print "Get trainer" trainer = CustomVisionTrainingClient(trainingKey, endpoint=ENDPOINT) print "Get project" for proj in trainer.get_projects(): if (proj.name == PROJECT_NAME): return proj except Exception as e: print str(e)
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 main(req: func.HttpRequest) -> func.HttpResponse: logging.info( 'ML Professoar HTTP trigger function AddLabeledDataClient processed a request.' ) try: # retrieve the parameters from the multi-part form http request image_url = req.form.get('ImageUrl') image_labeling_json = req.form.get('DataLabels') except: return func.HttpResponse( "Please pass JSON containing the labeled regions associated with this image on the query string or in the request body.", status_code=400) # validate both parameters were passed into the function if image_url and image_labeling_json: labels = [] count_of_labels_applied_to_image = 0 endpoint = os.environ['ClientEndpoint'] # Get Cognitive Services Environment Variables project_id = os.environ["ProjectID"] training_key = os.environ['TrainingKey'] # load labeled image regions passed in request into dictionary image_labeling_data = json.loads(image_labeling_json) # instanciate custom vision client trainer = CustomVisionTrainingClient(training_key, endpoint=endpoint) # retrieve tags from project and loop through them to find tag ids for tags that need to be applied to the image tags = trainer.get_tags(project_id) image_label = image_labeling_data["label"] for tag in tags: if tag.name == image_label: labels.append(tag.id) count_of_labels_applied_to_image = count_of_labels_applied_to_image + 1 break # create the image from a url and attach the appropriate tags as labels. upload_result = trainer.create_images_from_urls( project_id, [ImageUrlCreateEntry(url=image_url, tag_ids=labels)]) if upload_result.is_batch_successful: return func.HttpResponse( str(count_of_labels_applied_to_image) + " Tag(s) applied to image at url: " + image_url) else: return func.HttpResponse("Upload of " + image_url + " failed.") else: return func.HttpResponse( "Please pass a URL to a blob file containing the image to be added to training in this request on the query string.", status_code=400)
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') dataBlobUrl = req.params.get('dataBlobUrl') if not dataBlobUrl: try: req_body = req.get_json() except ValueError: pass else: dataBlobUrl = req_body.get('dataBlobUrl') if dataBlobUrl: # Get Cognitive Services Environment Variables projectID = os.environ["projectID"] trainingKey = os.environ['trainingKey'] predictionKey = os.environ['predictionKey'] clientEndpoint = os.environ['clientEndpoint'] trainer = CustomVisionTrainingClient(trainingKey, endpoint=clientEndpoint) iterations = trainer.get_iterations(projectID) if len(iterations) != 0: currentIteration = iterations[0] currentIterationName = currentIteration.publish_name httpEndpoint = clientEndpoint + "customvision/v3.0/Prediction/" + projectID + "/classify/iterations/" + currentIterationName + "/url" headers = {'Prediction-Key': predictionKey, 'Content-Type': 'application/json'} data = {"url": dataBlobUrl} response = requests.post(httpEndpoint, headers = headers, json = data) responseDictionary = response.json() Prediction = responseDictionary['predictions'][0] confidence = Prediction['probability'] responseDictionary['confidence'] = confidence # Display the results. return func.HttpResponse(json.dumps(responseDictionary)) else: return f'Model not trained.' # return func.HttpResponse("Model not trained.", status_code=400) else: return func.HttpResponse( "Please pass a dataBlobUrl on the query string or in the request body", status_code=400 )
def main(): """ Training for object detection with Azure Custom Vision """ args = parse_args() config = json.load(open(args.config, "r")) credentials = ApiKeyCredentials(in_headers={"Training-key": config["training_key"]}) trainer = CustomVisionTrainingClient(config["ENDPOINT"], credentials) print("Creating project...") # Find the object detection domain obj_detection_domain = next( domain for domain in trainer.get_domains() if domain.type == "ObjectDetection" and domain.name == "General" ) project = trainer.create_project( config["project_name"], domain_id=obj_detection_domain.id ) # ====================================================================================== print("Adding images...") image_folder = config["image_folder"] annotations = json.load(open("annotation.json", "r")) tagged_images_with_regions = [] for label in annotations.keys(): tagged_images_with_regions += add_image( trainer, label, project.id, annotations[label], image_folder ) upload_result = trainer.create_images_from_files( 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) # ====================================================================================== print("Training...") publish_iteration_name = config["publish_iteration_name"] prediction_resource_id = config["prediction_resource_id"] 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) # 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("Done!")
def createModel(self, database, id): trainer = CustomVisionTrainingClient(self._trainingKey, endpoint=self._endPoint) project = trainer.create_project(MODEL_ID_PREFIX + "-" + str(time.time_ns())) # reserve neccessory resources from the new model and fill the record into database session = database.cursor() session.execute( "INSERT INTO " + self._datatableName + " (id, remote_id) VALUES (%s, %s)", (id, project.id)) database.commit() return True
def main(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') try: data = req.get_json() farmid, runid = data['farmid'], data['runid'] feedback_list = data['feedback'] #Decide on the correct split for feedback in feedback_list: feedback["split"] = "train" if random.random() < 0.5 else "dev" #Get Project ID and CustomVision Trainer logging.info("Get Project with Project ID : " + CVTrainConstants.PROJECT_ID) credentials = ApiKeyCredentials( in_headers={"Training-key": CVTrainConstants.TRAINING_KEY}) trainer = CustomVisionTrainingClient(CVTrainConstants.ENDPOINT, credentials) #Get list of tags in the current project tag_dict = {} for tag in trainer.get_tags( CVTrainConstants.PROJECT_ID): ##Can modify dictionary later tag_dict[tag.name] = tag.id train_image_list = add_train_dev_images(farmid, runid, feedback_list, tag_dict) #train_image_list = add_train_dev_images("1", "2", [{"grid_id": "3", "label": "Charlock", "split": "dev"}, \ #{"grid_id": "7", "label": "Fat Hen", "split": "train"}], tag_dict) #Upload to Custom Vision with new tags from train if len(train_image_list) != 0: # IF not empty upload_result = trainer.create_images_from_urls( CVTrainConstants.PROJECT_ID, images=train_image_list) if not upload_result.is_batch_successful: logging.error("Image batch upload failed.") else: logging.info("Image batch upload Success") best_iteration(trainer) except Exception as e: logging.error( "Exception while trying to get data from HTTP Request: " + str(e)) return func.HttpResponse(f"Failure !") return func.HttpResponse(f"Trained Sucessfully")
def find_or_train_project(): try: training_key = "e1ceb0b98f0543de9694c3309060c564" # os.environ[TRAINING_KEY_ENV_NAME] except KeyError: raise SubscriptionKeyError("You need to set the {} env variable.".format(TRAINING_KEY_ENV_NAME)) # Use the training API to find the SDK sample project created from the training example. from custom_vision_training_samples import train_project, SAMPLE_PROJECT_NAME trainer = CustomVisionTrainingClient("e1ceb0b98f0543de9694c3309060c564","https://westeurope.api.cognitive.microsoft.com") for proj in trainer.get_projects(): if (proj.name == SAMPLE_PROJECT_NAME): return proj # Or, if not found, we will run the training example to create it. return train_project(training_key)
def __init__(self, *args, **kwargs): super(CustomVisionTestCase, self).__init__(*args, **kwargs) self.project_prefix = "unit_test" self.endpoint = ENDPOINT self.training_key = TRAINING_KEY self.trainer = CustomVisionTrainingClient(api_key=self.training_key, endpoint=self.endpoint)
def __init__(self) -> None: """ Reads configuration file Initializes connection to Azure Custom Vision predictor and training resources. Parameters: blob_service_client: Azure Blob Service interaction client Returns: None """ self.ENDPOINT = Keys.get("CV_ENDPOINT") self.project_id = Keys.get("CV_PROJECT_ID") self.prediction_key = Keys.get("CV_PREDICTION_KEY") self.training_key = Keys.get("CV_TRAINING_KEY") self.base_img_url = Keys.get("BASE_BLOB_URL") self.prediction_resource_id = Keys.get("CV_PREDICTION_RESOURCE_ID") self.prediction_credentials = ApiKeyCredentials( in_headers={"Prediction-key": self.prediction_key}) self.predictor = CustomVisionPredictionClient( self.ENDPOINT, self.prediction_credentials) self.training_credentials = ApiKeyCredentials( in_headers={"Training-key": self.training_key}) self.trainer = CustomVisionTrainingClient(self.ENDPOINT, self.training_credentials) connect_str = Keys.get("BLOB_CONNECTION_STRING") self.blob_service_client = BlobServiceClient.from_connection_string( connect_str) try: # get all project iterations iterations = self.trainer.get_iterations(self.project_id) # find published iterations puplished_iterations = [ iteration for iteration in iterations if iteration.publish_name != None ] # get the latest published iteration puplished_iterations.sort(key=lambda i: i.created) self.iteration_name = puplished_iterations[-1].publish_name with api.app.app_context(): models.update_iteration_name(self.iteration_name) except Exception as e: logging.info(e) self.iteration_name = "iteration1"
def test_create_2(self): """valid setting will not create project on customvision will wait until train for the first time """ trainer = CustomVisionTrainingClient(api_key=TRAINING_KEY, endpoint=ENDPOINT) project_count = len(trainer.get_projects()) project_obj = Project.objects.create( setting=Setting.objects.filter(name='valid_setting').first(), camera=Camera.objects.filter(name='demo_camera_1').first(), location=Location.objects.filter(name='demo_location_1').first(), customvision_project_name=f'{PROJECT_PREFIX}-test_create_2', is_demo=False) project_count_after = len(trainer.get_projects()) self.assertEqual(project_obj.customvision_project_id, '') self.assertEqual(project_count, project_count_after)
def _get_trainer_obj_static(endpoint: str, training_key: str): """ return <CustomVisionTrainingClient>. : Success: return CustomVisionTrainingClient object """ trainer = CustomVisionTrainingClient(api_key=training_key, endpoint=endpoint) return trainer
def get_tags_for_project(angle): trainer = CustomVisionTrainingClient(prediction_key, endpoint=endpoint) project_id = None if angle == 'wet-head-right': project_id = config.HEAD_RIGHT if angle == 'wet-head-left': project_id = config.HEAD_LEFT if angle == 'bottling-left': project_id = config.BOTTLING_LEFT if angle == 'bottling-straight': project_id = config.BOTTLING_STRAIGHT if angle == 'bottling-right': project_id = config.BOTTLING_RIGHT results = trainer.get_tags(project_id) return results
def find_or_train_project(): try: training_key = os.environ[TRAINING_KEY_ENV_NAME] except KeyError: raise SubscriptionKeyError( "You need to set the {} env variable.".format( TRAINING_KEY_ENV_NAME)) # Use the training API to find the SDK sample project created from the training example. from custom_vision_training_samples import train_project, SAMPLE_PROJECT_NAME trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) for proj in trainer.get_projects(): if (proj.name == SAMPLE_PROJECT_NAME): return proj # Or, if not found, we will run the training example to create it. return train_project(training_key)
def get_trainer_obj(self) -> CustomVisionTrainingClient: """get_trainer_obj. Returns: CustomVisionTrainingClient: """ credentials = ApiKeyCredentials(in_headers={"Training-key": self.training_key}) return CustomVisionTrainingClient( credentials=credentials, endpoint=self.endpoint )
def classify(image_path): # Replace with a valid key prediction_key = "c3741dd758584195b3bcd4331c4ba6c0" #prediction_resource_id = "/subscriptions/bad6784e-798e-4bbc-89cf-cb5149375471/resourceGroups/Ecolab(Group)/providers/Microsoft.CognitiveServices/accounts/Ecolab-Prediction" training_key = 'd898f79af8f84520994849cf82d4a6be' ENDPOINT = "https://ecolab-prediction.cognitiveservices.azure.com/" publish_iteration_name = "Ecolab3" test_image = image_path trainer = CustomVisionTrainingClient(training_key, endpoint=ENDPOINT) predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT) project = trainer.get_projects()[0] # Now there is a trained endpoint that can be used to make a prediction with open(test_image, "rb") as image_contents: results = predictor.classify_image(project.id, publish_iteration_name, image_contents.read()) # Display the results. for prediction in results.predictions: return prediction.tag_name