예제 #1
0
def customVisionDetectObjectsLocalDisplay(imageFileName):
    """
        Description: This function detects selected workstation objects, using a custom vision algorithm, in an image retrieved from an Azure Blob Storage, via its URL. It then prints out the results to the console. 
        Source: The Azure Custom Vision SDK Sample was used as guidance and for finding an elegant way to print the results to the console (https://github.com/Azure-Samples/cognitive-services-python-sdk-samples/blob/master/samples/vision/custom_vision_prediction_samples.py)
        Input: (containerName -> string), (blobName -> string)
        Output: No direct output, but it writes the results of the object detection to the console.
    """
    print("\n\n===== Custom Vision Object Detection=====\n")
    #Retriving the probability threshold to recognise an object
    result_probThreshold = dbc.retrieve_probThreshold("customVisionObjects")
    probThreshold = float(result_probThreshold[0])

    # Client Authentication
    predictor = CustomVisionPredictionClient(CCV_endpoint, CCV_credentials)

    # Get URL image with different objects
    #remote_image_url_objects = hbl.getBlobURI(containerName, blobName)
    with open(dm.createTargetDirectory("Images") + imageFileName, "rb") as image_contents:
        # Call API with URL
        custom_vision_prediction = predictor.detect_image_with_no_store(project_id, published_name, image_contents.read())
    
    print("Objects Detected with Custom Vision and a Probability Threshold >= 0.2:")
    if len(custom_vision_prediction.predictions) == 0:
        print("No objects detected.")
    else:
         for prediction in custom_vision_prediction.predictions:
             if (prediction.probability >= probThreshold):
                print("->\t" + prediction.tag_name +
                ": {0:.2f}%".format(prediction.probability * 100))
예제 #2
0
    cv2.imshow("preview", frame)
    rval, frame = vc.read()
    im = Image.fromarray(frame)
    # im.save('test.png') # quick test
    # exit()

    imgByteArr = io.BytesIO()
    im.save(imgByteArr, format='PNG')
    imgByteArr = imgByteArr.getvalue()

    if settings['store_images']:
        results = predictor.detect_image(settings['project_id'],
                                         settings['published_name'],
                                         imgByteArr)
    else:
        results = predictor.detect_image_with_no_store(
            settings['project_id'], settings['published_name'], imgByteArr)

    predictions = [
        p for p in results.predictions
        if p.probability > settings['prediction_threshold']
        and p.tag_name == settings['object_of_interest']
    ]
    for prediction in predictions:
        print(
            "\t" + prediction.tag_name +
            ": {0:.2f}% bbox.left = {1:.2f}, bbox.top = {2:.2f}, bbox.width = {3:.2f}, bbox.height = {4:.2f}"
            .format(prediction.probability * 100, prediction.bounding_box.left,
                    prediction.bounding_box.top, prediction.bounding_box.width,
                    prediction.bounding_box.height))

    key = cv2.waitKey(20)
예제 #3
0
class customVisionApi:
    '''Methods to call Custom Vision Prediction & Training API.'''
    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 listImagesInFile(self):
        '''Browse all images in a directory and split them in a dictionnary according if model name is present in file name'''
        model_name = "PL"
        images = {'Model_A': [], 'Model_B': []}
        #List all images in directory & display count
        directory = os.listdir(self.training_images)
        print("Found:", len(directory), "images")
        #Split images in dictionnary according to their name
        for image in directory:
            if model_name in image:
                images['Model_A'].append(image)
            else:
                images['Model_B'].append(image)
        print("Found:", len(images['Model_A']), "Model_A")
        print("Found:", len(images['Model_B']), "Model_B")
        return images

    def storeInSqlDb(self, AllDefaultDictionnary):
        # Connect to the database
        print(AllDefaultDictionnary)

    def predictModelNoStore(self, imageToBeDetected):
        '''Apply Cusom Vision API on image and list defaults'''
        listAllDefaultsInImage = []
        with open(self.training_images + '/' + imageToBeDetected,
                  "rb") as image_contents:
            results = self.predictor.detect_image_with_no_store(
                self.project_id, self.iteration_name, image_contents.read())
            allDefault = {}
            fileName = imageToBeDetected
            #List all defaults in image with probability
            getDefaultList = self.getDefaults(results.predictions)
            #Create default categories with sum of probability
            countDefault = self.defaultCounter(getDefaultList)
            #Return Tag Name with probability > 10%
            getSelectedTag = self.returnDefaultTag(countDefault)
            #Create a list of dict with all defaults found in the file images
            allDefault[fileName] = getSelectedTag
            sql = self.storeInSqlDb(allDefault)
        return allDefault

    def writeResult(self, listToBeWritten):
        '''Create a .txt file with the iteration name & write list content.'''
        fileName = 'result_' + self.iteration_name + '.txt'
        with open(fileName, 'a') as f:
            f.write(str(listToBeWritten))
            f.close()
        return print("Results saved in " + fileName)

    def getDefaults(self, predictionResults):
        '''List all default found in image with its probability.'''
        #Initialise empty list to store defaults
        default_list = []
        for prediction in predictionResults:
            #Initialize a dictionnary with key:Default & value:probability
            default_proba = {}
            default_proba[prediction.tag_name] = prediction.probability * 100
            #Add default to list
            default_list.append(dict(default_proba))
        #Return list with all defaults and their probabilities
        return default_list

    def defaultCounter(self, ListDefaultsWithProb):
        '''Group default in categories and sum probabilities.'''
        #Initialise counter() from Collections to sum probabilities per default
        counter = collections.Counter()
        for probabilityDefault in ListDefaultsWithProb:
            counter.update(probabilityDefault)
        #Return dictionnary with default and sum probability
        defaultWithProbaSum = dict(counter)
        return defaultWithProbaSum

    def returnDefaultTag(self, defaultsDictionnary):
        "Return only default with a certain probability."
        #Return Dictionnary with only default tag where probability > 10%
        accepted_probability = 10
        OnlySelectedDefaultTag = {
            k: v
            for (k, v) in defaultsDictionnary.items()
            if v > accepted_probability
        }
        print(OnlySelectedDefaultTag)
        return OnlySelectedDefaultTag

    def predictQuick(self):
        '''Upload image and display predictions in terminal'''
        #List all images in directory
        directory = os.listdir(self.training_images)
        print("Found:", len(directory), "images")
        #Upload image without storing, get prediction
        for file in directory:
            with open(self.training_images + '/' + file,
                      "rb") as image_contents:
                self.results = self.predictor.detect_image(
                    self.projectId, self.publish_iteration_name,
                    image_contents.read())
                print("Image : " + file)
            # Display the results.
            for prediction in self.results.predictions:
                print("\t" + prediction.tag_name +
                      ": {0:.2f}%".format(prediction.probability * 100))

    def getIdsinPredictions(self):
        """List Ids of images present in prediction"""
        #Retrieve Prediction Token
        query = PredictionQueryToken(order_by='Newest',
                                     iteration_id=self.Iteration_id)
        #Get all predictions results
        response = self.trainer.query_predictions(self.projectId, query)
        #Create a list with all ids & return it
        list_ids = []
        for elt in response.results:
            list_ids.append(elt.id)
        print("Found:", len(list_ids), "images in prediction")
        return (list_ids)

    def deletePredictions(self):
        '''Delete images in prediction'''
        #List all predictions Id
        allIds = self.getIdsinPredictions()
        #Create batches of 64 images (maximum for delete) & delete images
        batch = [allIds[i:i + 64] for i in range(0, len(allIds), 64)]
        for i in batch:
            delete = self.trainer.delete_prediction(self.projectId, ids=i)
        return print("All images deleted")