def detect_weight_region(image):
    # top_left_corner = (21,120)
    # bottom_right_corner = (217, 210)
    # cv2.imshow("image",image)
    image_shape = image.shape
    print(image_shape)
    credentials = ApiKeyCredentials(in_headers = {"Prediction-Key":"bf595a2cb1854d988a1f9d26834cd4e2"})
    predictor =  CustomVisionPredictionClient("https://pankaj.cognitiveservices.azure.com/", credentials)
    cv2.imwrite('detect_weight_region.png', image)
    digit = ""
    with open("detect_weight_region.png", mode ='rb') as captured_image:
        # print("load digit image... and predict ")
        results = predictor.detect_image("de960dda-1a51-444e-9fe9-84f8fbc4eff1", "Iteration2", captured_image)
        maxm_percentage = 0.0
        ans = []
        for prediction in results.predictions:
            if(prediction.probability > maxm_percentage):
                maxm_percentage = prediction.probability
                ans = [image_shape[0]*prediction.bounding_box.left, image_shape[1]*prediction.bounding_box.top, image_shape[0]*prediction.bounding_box.width, image_shape[1]*prediction.bounding_box.height]
            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))
        # for prediction in results.predictions:
        #     if(prediction.probability> maxm_percentage):
        #         digit = prediction.tag_name
        #         maxm_percentage = prediction.probability
    return ans
Example #2
0
    def localAnalysis(self, location):
        # Initialize Object Predictor
        predictor = CustomVisionPredictionClient(self.prediction_key,
                                                 endpoint=self.ENDPOINT)

        # Open the sample image and get back the prediction results.
        with open(location, mode="rb") as test_data:
            results = predictor.detect_image(self.project_id,
                                             self.publish_iteration_name,
                                             test_data)

        # Display the results.
        notes = []
        for prediction in results.predictions:
            if prediction.probability > self.min_confidence:
                notes.append([
                    prediction.tag_name, prediction.bounding_box.left,
                    prediction.bounding_box.top, prediction.probability
                ])

        # Sort the data.
        self.__noteSort(notes)

        # print("Note, Left, Top, Prob")
        # for note in notes:
        #     print(note)

        output = []
        for note in notes:
            output.append(note[0])

        return output
def azure_detect_object(img_path):
    """
    detect gloves and screwdriver in given image
    :param img_path: Image path
    :return:
    """
    res = {}
    template_prediction_key = "54c71598e9434d5fa7853360c4a9e4ce"
    template_project_id = "54e7f828-d0c8-49d3-8802-9b402612b7c7"
    template_iteration_name = "Iteration2"
    template_prediction_endpoint = "https://southeastasia.api.cognitive.microsoft.com"
    predictor = CustomVisionPredictionClient(
        template_prediction_key, endpoint=template_prediction_endpoint)

    with open(img_path, "rb") as image_contents:
        results = predictor.detect_image(
            template_project_id,
            template_iteration_name,
            image_contents.read(),
            custom_headers={'Content-Type': 'application/octet-stream'})
        res = []
        for prediction in results.predictions:

            res.append(
                (prediction.tag_name, prediction.probability,
                 prediction.bounding_box.left, prediction.bounding_box.top,
                 prediction.bounding_box.width,
                 prediction.bounding_box.height))
    return res
Example #4
0
def success():
    image = request.args.get('name', None)
    ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com/"
    prediction_key = "4c99a663c6984df6b912025e2c7e1dee"
    my_project_id = "c9f9158e-e82d-4541-926a-15699c59a4b4"
    publish_iteration_name = "Iteration3"

    test_data = urlopen(image).read()
    prediction_credentials = ApiKeyCredentials(
        in_headers={"Prediction-key": prediction_key})
    predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)
    results = predictor.detect_image(project_id=my_project_id,
                                     published_name=publish_iteration_name,
                                     image_data=test_data)
    first_value = 0
    maximum = 0
    print(first_value)

    for prediction in results.predictions:
        next_value = int(prediction.probability * 100)
        if maximum <= next_value:
            maximum = next_value
            tag = prediction.tag_name
        print(int(prediction.probability * 100))
        print(maximum)
        print(tag)
        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))
    return tag
Example #5
0
def get_pattern_predictions(image_path):
    predictor = CustomVisionPredictionClient(prediction_key, endpoint=endpoint)

    # Open the image and get back the prediction results.
    with open(image_path, mode="rb") as test_data:
        results = predictor.detect_image(project_id, iteration_name, test_data)

    return {"predictions": results.predictions}
Example #6
0
def process_image():
    file = request.files['file']
    file.save("predict")
    # Read the image via file.stream
    #img = Image.open(file.stream)
    #test_img_file = os.path.join('data', 'object-detection', 'ofr.jpg')
    test_img_file = 'predict'
    test_img = Image.open(test_img_file)
    imgbin = open(test_img_file,mode="rb")
    test_img_h, test_img_w, test_img_ch = np.array(test_img).shape
    print('Ready to predict using model {} in project {}'.format(model_name, project_id))

    # Get a prediction client for the object detection model
    credentials = ApiKeyCredentials(in_headers={"Prediction-key": cv_key})
    predictor = CustomVisionPredictionClient(endpoint=cv_endpoint, credentials=credentials)

    
    print('Detecting objects in {} using model {} in project {}...'.format(test_img_file, model_name, project_id))
    # Detect objects in the test image
    with open(test_img_file, mode="rb") as test_data:
        results = predictor.detect_image(project_id, model_name, test_data)
    # Create a figure to display the results
    fig = plt.figure(figsize=(10, 12))
    #fig = plt.figure(figsize=(test_img_h,test_img_w))
    plt.axis('off')

    # Display the image with boxes around each detected object
    draw = ImageDraw.Draw(test_img)
    lineWidth = int(np.array(test_img).shape[1]/100)
    object_colors = {
        "bebida": "lightgreen",
        "calavera_completa": "yellow",
        "calavera_de_dulce": "yellow",
        "cempasuchil": "orange",
        "comida": "blue",
        "cruz": "gold",
        "fruta": "magenta",
        "pan_de_muerto": "darkcyan",
        "papel_picado": "red",
        "retrato": "cyan"
    }
    found = []
    for prediction in results.predictions:
        color = 'white' # default for 'other' object tags
        if (prediction.probability*100) > 50:
            if prediction.tag_name in object_colors:
                color = object_colors[prediction.tag_name]
                found.append(prediction.tag_name)
            left = prediction.bounding_box.left * test_img_w 
            top = prediction.bounding_box.top * test_img_h 
            height = prediction.bounding_box.height * test_img_h
            width =  prediction.bounding_box.width * test_img_w
            points = ((left,top), (left+width,top), (left+width,top+height), (left,top+height),(left,top))
            draw.line(points, fill=color, width=lineWidth)
            #plt.annotate(prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100),(left,top), backgroundcolor=color)
            plt.annotate(prediction.tag_name,(left,top), backgroundcolor=color)
    test_img.save("./static/Imagenes/out.jpg")
    return jsonify(found)
Example #7
0
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
Example #8
0
def main():
    from dotenv import load_dotenv

    try:
        # Get Configuration Settings
        load_dotenv()
        prediction_endpoint = os.getenv('PredictionEndpoint')
        prediction_key = os.getenv('PredictionKey')
        project_id = os.getenv('ProjectID')
        model_name = os.getenv('ModelName')

        # Authenticate a client for the training API
        credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
        prediction_client = CustomVisionPredictionClient(endpoint=prediction_endpoint, credentials=credentials)

        # Load image and get height, width and channels
        image_file = 'produce.jpg'
        print('Detecting objects in', image_file)
        image = Image.open(image_file)
        h, w, ch = np.array(image).shape

        # Detect objects in the test image
        with open(image_file, mode="rb") as image_data:
            results = prediction_client.detect_image(project_id, model_name, image_data)

        # Create a figure for the results
        fig = plt.figure(figsize=(8, 8))
        plt.axis('off')

        # Display the image with boxes around each detected object
        draw = ImageDraw.Draw(image)
        lineWidth = int(w/100)
        color = 'magenta'
        for prediction in results.predictions:
            # Only show objects with a > 50% probability
            if (prediction.probability*100) > 50:
                # Box coordinates and dimensions are proportional - convert to absolutes
                left = prediction.bounding_box.left * w 
                top = prediction.bounding_box.top * h 
                height = prediction.bounding_box.height * h
                width =  prediction.bounding_box.width * w
                # Draw the box
                points = ((left,top), (left+width,top), (left+width,top+height), (left,top+height),(left,top))
                draw.line(points, fill=color, width=lineWidth)
                # Add the tag name and probability
                plt.annotate(prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100),(left,top), backgroundcolor=color)
        plt.imshow(image)
        outputfile = 'output.jpg'
        fig.savefig(outputfile)
        print('Results saved in ', outputfile)
    except Exception as ex:
        print(ex)
def get_pattern_predictions(image_path):
    predictor = CustomVisionPredictionClient(prediction_key, endpoint=endpoint)

    print 'About to OD the image ' + image_path
    # Open the image and get back the prediction results.
    with open(image_path, mode="rb") as test_data:
        print 'Opened file'
        results = predictor.detect_image(project_id, iteration_name, test_data)

    print results
    # TODO: order the predictions in order to aid readability

    return {"predictions": results.predictions}
Example #10
0
def main():
    """
    Object Detection with Azure Custom Vision
    """
    args = parse_args()
    config = json.load(open(args.config, "r"))

    # Get the predictor
    prediction_credentials = ApiKeyCredentials(
        in_headers={"Prediction-key": config["prediction_key"]})
    predictor = CustomVisionPredictionClient(config["ENDPOINT"],
                                             prediction_credentials)

    # ======================================================================================
    # Open the sample image and get back the prediction results.
    project_id = get_project_id(config)
    with open(args.image, "rb") as test_data:
        results = predictor.detect_image(
            project_id,
            config["publish_iteration_name"],
            test_data,
        )

    # ======================================================================================
    # Draw the bounding boxes on the image
    img = Image.open(args.image)
    draw = ImageDraw.Draw(img)
    font = ImageFont.truetype("../static/TaipeiSansTCBeta-Regular.ttf",
                              size=int(5e-2 * img.size[1]))
    for prediction in results.predictions:
        if prediction.probability > 0.5:
            bbox = prediction.bounding_box.as_dict()
            left = bbox['left'] * img.size[0]
            top = bbox['top'] * img.size[1]
            right = left + bbox['width'] * img.size[0]
            bot = top + bbox['height'] * img.size[1]
            draw.rectangle([left, top, right, bot],
                           outline=(255, 0, 0),
                           width=3)
            draw.text(
                [left, abs(top - 5e-2 * img.size[1])],
                "{0} {1:0.2f}".format(prediction.tag_name,
                                      prediction.probability * 100),
                fill=(255, 0, 0),
                font=font,
            )

    img.save("output.png")
    print("Done!")
    print("Please check ouptut.png")
Example #11
0
def predict_project(prediction_key, project, iteration):
    predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT)

    # Open the sample image and get back the prediction results.
    with open(os.path.join(IMAGES_FOLDER, "Test", "test_od_image.jpg"),
              mode="rb") as test_data:
        results = predictor.detect_image(project.id, PUBLISH_ITERATION_NAME,
                                         test_data)

    # Display the results.
    for prediction in results.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))
Example #12
0
def get_result(image):

    # Now there is a trained endpoint that can be used to make a prediction

    ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
    predictor = CustomVisionPredictionClient(
        "a9b7310ff55441829878684390c89aae", endpoint=ENDPOINT)

    with open("test.png", "rb") as image_contents:
        results = predictor.detect_image(
            "ca2d87c7-9f53-4606-8db7-2cf0f410a075", "test",
            image_contents.read())

        return set([
            item.tag_name for item in results.predictions
            if item.probability > .5
        ])
Example #13
0
def forecast(meter):
    from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
    from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient
    from msrest.authentication import ApiKeyCredentials
    # Now there is a trained endpoint that can be used to make a prediction
    credentials = ApiKeyCredentials(
        in_headers={"Prediction-key": "5cebf412b5504687bbbe08255e810e88"})
    predictor = CustomVisionPredictionClient(
        endpoint="https://uksouth.api.cognitive.microsoft.com/",
        credentials=credentials)

    with open(meter, mode="rb") as image_contents:
        results = predictor.detect_image(
            "b667b9c2-2604-4f86-be8e-818852441327", "Iteration8",
            image_contents)
# Display the results.

#for prediction in results.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))
    return results.predictions
Example #14
0
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
def get_head_predictions(image_path):
    predictor = CustomVisionPredictionClient(prediction_key, endpoint=endpoint)

    # Open the image and get back the prediction results.
    with open(image_path, mode="rb") as test_data:
        results = predictor.detect_image(project_id, iteration_name, test_data)

    best_prediction = None

    # Find the best prediction
    for prediction in results.predictions:

        if best_prediction == None:
            best_prediction = prediction

        if best_prediction.probability < prediction.probability:
            best_prediction = prediction

    return {
        "predictions": results.predictions,
        "best_prediction": best_prediction
    }
    #Importing the code that detects the arduino Input

    #define frame as the frame of webcam feed
    ret, frame = vidFeed.read()

    #Changes the size of the image such that the aspect ratio is kept the same
    frame = imutils.resize(frame, width=400)

    #Writes the image as a folder to be used with customvision.ai
    cv2.imwrite("Temp_image.jpg", frame)

    iterationName = "HandPrediction-Py5"

    #Gets the predictions based on the image
    with open("Temp_image.jpg", mode="rb") as modelImage:
        output = predict.detect_image(project.id, iterationName, modelImage)

    modelPredictions = []
    #Stores the predictions with a probability higher than 50% in the array modelPredictions
    for prediction in output.predictions:
        if prediction.probability > 0.3:
            modelPredictions.append(prediction)

            print(prediction.tag_name + " detected with prob of " +
                  str(prediction.probability))

    #Used to determine the size of the image
    frame.shape

    #left click functionality
    mouseDown = False
Example #17
0
class Vision:
    def __init__(self, prediction_key, endpoint, iteration_id, iteration_name):
        """Makes a call to custom vision api and use trained model to detect people
        """
        credentials = ApiKeyCredentials(
            in_headers={"Prediction-key": prediction_key})
        self._predictor = CustomVisionPredictionClient(endpoint, credentials)
        self._iteration_id = iteration_id
        self._iteration_name = iteration_name

    def detect_people(self, filepath, show=False):
        """Makes a call to custom vision api and use trained model to detect people
        """
        if show: print("Making call to vision api...")
        people = []
        with open(filepath, mode="rb") as image:
            results = self._predictor.detect_image(self._iteration_id,
                                                   self._iteration_name, image)
        for prediction in results.predictions:
            if (prediction.tag_name == "person"
                    and prediction.probability > .5):
                people.append(prediction)
        count = len(people)
        if show: draw_objects(filepath, people, wait=True)
        return people

    def get_points_from_box(self, prediction, pixw, pixh):
        """
        prediction.bounding_box.left = x
        prediction.bounding_box.top = y
        prediction.bounding_box.width = w
        prediction.bounding_box.height = h
        """
        center = (prediction.bounding_box.width * pixw / 2 +
                  prediction.bounding_box.left * pixw,
                  prediction.bounding_box.top * pixh +
                  prediction.bounding_box.height * pixh)
        upoid = (prediction.bounding_box.width * pixw / 2 +
                 prediction.bounding_box.left * pixw,
                 prediction.bounding_box.top * pixh)
        return center, upoid

    def get_centroids_and_uppoints(self, predictions, pixw, pixh):
        """
        For every bounding box, compute the centroid and the point located on the bottom center of the box
        @ array_boxes_detected : list containing all our bounding boxes
        """
        array_centroids, array_uppoints = [], [
        ]  # Initialize empty centroid and ground point lists
        for prediction in predictions:
            # Draw the bounding box
            # Get the both important points
            centroid, up_point = self.get_points_from_box(
                prediction, pixw, pixh)
            array_centroids.append(centroid)
            array_uppoints.append(centroid)
        return array_centroids, array_uppoints

    # array_groundpoints is pased in as param list_downoids in compute point transformation

    # corner points = corner points of the portion of the image where you want to detect objects (one set of opposite sides must be ||)
    # get the height and width of the image, read the image like cv2.imread(img_path)
    def compute_perspective_transform(self, corner_points, img):
        pt_A = corner_points[0]
        pt_B = corner_points[1]
        pt_C = corner_points[2]
        pt_D = corner_points[3]
        width_AD = np.sqrt(((pt_A[0] - pt_D[0])**2) + ((pt_A[1] - pt_D[1])**2))
        width_BC = np.sqrt(((pt_B[0] - pt_C[0])**2) + ((pt_B[1] - pt_C[1])**2))
        maxWidth = max(int(width_AD), int(width_BC))

        height_AB = np.sqrt(((pt_A[0] - pt_B[0])**2) +
                            ((pt_A[1] - pt_B[1])**2))
        height_CD = np.sqrt(((pt_C[0] - pt_D[0])**2) +
                            ((pt_C[1] - pt_D[1])**2))
        maxHeight = max(int(height_AB), int(height_CD))

        input_pts = np.float32([pt_A, pt_B, pt_C, pt_D])
        output_pts = np.float32([[0, 0], [0, maxHeight - 1],
                                 [maxWidth - 1, maxHeight - 1],
                                 [maxWidth - 1, 0]])
        # matrix
        M = cv2.getPerspectiveTransform(input_pts, output_pts)
        out = cv2.warpPerspective(img,
                                  M, (maxWidth, maxHeight),
                                  flags=cv2.INTER_LINEAR)
        return M, out

    # matrix is the transformed image, list of downoids are the list of points that represent objects detected in the image(bottom center of image)
    def compute_point_perspective_transformation(self, matrix, list_upoids):
        """ Apply the perspective transformation to every ground point which have been detected on the main frame.
        @ matrix : the 3x3 matrix
        @ list_downoids : list that contains the points to transform
        return : list containing all the new points
        """
        # Compute the new coordinates of our points
        #list_points_to_detect = np.float32(list_upoids).reshape(-1, 1, 2)
        list_points_to_detect = np.float32(list_upoids).reshape(-1, 1, 2)
        transformed_points = cv2.perspectiveTransform(list_points_to_detect,
                                                      matrix)
        # Loop over the points and add them to the list that will be returned
        transformed_points_list = list()
        try:
            for i in range(0, transformed_points.shape[0]):
                transformed_points_list.append(
                    [transformed_points[i][0][0], transformed_points[i][0][1]])
        except Exception as e:
            print("computer point perspective transformation error", e)
        return transformed_points_list

    # TODO: { people, violations, time, location }
    def analyzeFrame(self, image_path, dist_threshold=100):
        # val = [[]]
        # for now if val does not exist just set a default val array and we will deal with it later
        # for now if covid violation threshold does not exist just set a default val array and we will deal with it later
        img = cv2.imread(image_path)
        # method to get the num of poeple in a image frame and
        predictions = self.detect_people(image_path)
        p_count = len(predictions)
        if p_count == 0:
            return predictions, 0, 0
        # val[x][y] ---> verticies of image
        # corner_points = [[val[0][0], val[0][1]], [val[1][0], val[1][1]], [val[2][0], val[2][1]], [val[3][0], val[3][1]]]
        corner_points = [[721, 260], [493, 367], [384, 273], [576, 205]]
        # corner_points = [[ 1883 ,  654 ], [ 743 ,  972], [ 11 ,  467 ], [ 961 ,  225 ]]
        # corner_points = [[ 635 ,  293 ], [1879 ,  228 ], [9 ,  883], [1637 ,  1072]]
        M, new_img = self.compute_perspective_transform(corner_points, img)
        h = img.shape[1]
        w = img.shape[0]
        array_centroids, array_uppoints = self.get_centroids_and_uppoints(
            predictions, h, w)
        transformed_points_list = self.compute_point_perspective_transformation(
            M, array_uppoints)
        # for point in transformed_points_list:
        #     new_img = cv2.circle(new_img, (point[0], point[1]), radius=5, color=(0, 0, 255), thickness=-1)
        # cv2.imshow("analyze stuff", new_img)
        violation_count = 0
        for i, pair in enumerate(
                itertools.combinations(transformed_points_list, r=2)):
            # print(pair) --> testing method
            # get distance from points - print this out see whats going on
            if math.sqrt((pair[0][0] - pair[1][0])**2 +
                         (pair[0][1] - pair[1][1])**2) < int(dist_threshold):
                violation_count = violation_count + 1
        return predictions, p_count, violation_count
success,image = vidcap.read()
 
count = 0
credentials = ApiKeyCredentials(in_headers={"Prediction-key":cv_key})
predictor = CustomVisionPredictionClient(endpoint=cv_endpoint, credentials=credentials)
 
while success:
    cv2.imwrite("frame%d.jpg" % count, image)
    success,image = vidcap.read()
    test_img_file = os.path.join('frame'+str(count)+'.jpg')
    test_img = Image.open(test_img_file)
    test_img_h, test_img_w, test_img_ch = np.array(test_img).shape
    print('Read a new frame: ', success)
count += 1
with open(test_img_file, mode="rb") as test_data:
    results = predictor.detect_image(project_id, model_name, test_data)

fig = plt.figure(figsize=(8, 8))
plt.axis('off')
draw = ImageDraw.Draw(test_img)
lineWidth = int(np.array(test_img).shape[1]/100)
object_colors = {
    "mask": "lightgreen",
    "no_mask": "yellow"
}

for prediction in results.predictions:
    color = 'white'# default for 'other' object tags
    if (prediction.probability*100) > 50:
        if prediction.tag_name in object_colors:
            color = object_colors[prediction.tag_name]
Example #19
0
    rval = False

while rval:
    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,
Example #20
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")
Example #21
0
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient

import cv2

camera = cv2.VideoCapture(0)
for i in range(10):
    return_value, image = camera.read()
    cv2.imwrite('opencv' + str(i) + '.png', image)
del (camera)

# Now there is a trained endpoint that can be used to make a prediction

ENDPOINT = "https://southcentralus.api.cognitive.microsoft.com"
predictor = CustomVisionPredictionClient("a9b7310ff55441829878684390c89aae",
                                         endpoint=ENDPOINT)

with open("test.jpg", "rb") as image_contents:
    results = predictor.detect_image("ca2d87c7-9f53-4606-8db7-2cf0f410a075",
                                     "test", image_contents.read())

    # Display the results.
    for prediction in results.predictions:
        print("\t" + prediction.tag_name +
              ": {0:.2f}%".format(prediction.probability * 100))
Example #22
0
import cv2
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials

camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

credentials = ApiKeyCredentials(
    in_headers={"Prediction-key": "<PREDICTION_KEY>"})
predictor = CustomVisionPredictionClient("<ENDPOINT_URL>", credentials)

ret, image = camera.read()
cv2.imwrite('capture.png', image)

with open("capture.png", mode="rb") as captured_image:
    results = predictor.detect_image("<PROJECT_ID>", "<ITERATION_NAME>",
                                     captured_image)

for prediction in results.predictions:
    if prediction.probability > 0.9:
        bbox = prediction.bounding_box
        result_image = cv2.rectangle(
            image, (int(bbox.left * 640), int(bbox.top * 480)),
            (int((bbox.left + bbox.width) * 640),
             int((bbox.top + bbox.height) * 480)), (0, 255, 0), 3)
        cv2.imwrite('result.png', result_image)

camera.release()
class PredictImages:
    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)

    @staticmethod
    def show_prediction(image_path, predictions):
        GREEN = (0, 255, 0)
        BBOX_LINE_SIZE = 5
        image = cv2.imread(image_path)
        img_height = image.shape[0]
        img_width = image.shape[1]

        for prediction in predictions:
            if prediction.probability < PREDICTION_THRESHOLD:
                continue
            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,
                ),
            )

            left = prediction.bounding_box.left * img_width
            top = prediction.bounding_box.top * img_height
            width = prediction.bounding_box.width * img_width
            height = prediction.bounding_box.height * img_height

            x0 = int(left)
            y0 = int(top)
            x1 = int(left + width)
            y1 = int(top + height)

            cv2.rectangle(image, (x0, y0), (x1, y1), GREEN, BBOX_LINE_SIZE)

        cv2.imshow("image", image)
        while True:
            key = cv2.waitKey(0)
            if key:
                break

    def predict_image(self, image_path: str):
        image_bytes: bytes = read_and_resize_image(
            image_path=image_path,
            max_byte_size=self.max_byte_size,
        )
        print(f"Predicting on image {image_path}")

        # Send image and get back the prediction results
        results = self.predictor.detect_image(self.project.id,
                                              self.publish_iteration_name,
                                              image_bytes)
        return results.predictions
Example #24
0
# Querying custom vision to extract only the barcodes

# Now there is a trained endpoint that can be used to make a prediction

prediction_key = "<YOUR PREDICTION KEY>"
ENDPOINT = 'https://westeurope.api.cognitive.microsoft.com/'

base_image_url = "./"
image_relative_path = "barcode-on-food-item-c0bf0m.jpg"

predictor = CustomVisionPredictionClient(prediction_key, endpoint=ENDPOINT)

# Open the sample image and get back the prediction results.
with open(base_image_url + "images/" + image_relative_path,
          mode="rb") as test_data:
    results = predictor.detect_image('<CUSTOM_VISION_AI_PROJECT_ID>',
                                     'Iteration1', test_data)

#------------------------------------------------------------
# Selecting the best candidates and storing the temporal
# image


class DetectedObject(object):
    def __init__(self):
        self.probability = 0.0
        self.bounding_box_left = 0.0
        self.bounding_box_top = 0.0
        self.bounding_box_width = 0.0
        self.bounding_box_height = 0.0

def predict_image(img, role):
    start_time = time.time()
    result = []
    img = Image.open(img)
    img.save("frame.jpg")

    with open("frame.jpg", mode="rb") as frame:

        if role == "person":
            # Connectiong to the Face API service
            subscription_key = face_api_key
            base_url = face_api_url
            person_group_id = face_group_id
            CF.BaseUrl.set(base_url)
            CF.Key.set(subscription_key)

            detected_people = CF.face.detect(frame, attributes=face_features)

            if len(detected_people) > 0:

                face_ids = [d['faceId'] for d in detected_people]
                faces = CF.face.identify(face_ids, person_group_id)

                biggest_width, index = get_width(detected_people)

                if (recognize_face(faces)):
                    result.append("member")
                else:
                    processor = FeatureProcessor(
                        "feature_temp.csv", "get",
                        detected_people[index]["faceAttributes"])
                    processor.start()
                    result.append("stranger")

                result.append(biggest_width)
            else:
                result.append("nothing")
                result.append(0)

            end_time = time.time()
            elasped_time = str(int((end_time - start_time) * 1000)) + " ms"

        elif role == "package":
            # Connectiong to the Custom Vision service
            prediction_key = package_api_key
            project_id = package_api_project_id
            iteration_name = package_api_iteration_name
            endpoint = package_api_endpoint
            predictor = CustomVisionPredictionClient(prediction_key,
                                                     endpoint=endpoint)

            results = predictor.detect_image(project_id, iteration_name, frame)
            max_prob = 0.77
            tag = ""

            package_counter = 0

            for prediction in results.predictions:
                if prediction.probability > max_prob:
                    package_counter += 1
                    tag = prediction.tag_name

            if tag != "" and tag.lower() == "package":
                result.append("package")
                result.append(package_counter)
            else:
                result.append("nothing")
                result.append(0)
            end_time = time.time()
            elasped_time = str(int((end_time - start_time) * 1000)) + " ms"
        return result
Example #26
0
from azure.cognitiveservices.vision.customvision.prediction import CustomVisionPredictionClient
from msrest.authentication import ApiKeyCredentials

camera = cv2.VideoCapture(0)
camera.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

credentials = ApiKeyCredentials(
    in_headers={"Prediction-key": "<Prediction-key>"})
predictor = CustomVisionPredictionClient("<Endpoint-URL>", credentials)

ret, image = camera.read()
cv2.imwrite('capture_detect.png', image)

with open("capture_detect.png", mode="rb") as captured_image:
    results = predictor.detect_image("<Project-id>", "<Name-of-Iteration>",
                                     captured_image)

for prediction in results.predictions:
    if prediction.probability > 0.5:

        bbox = prediction.bounding_box
        cv2.putText(image, prediction.tag_name,
                    (int(bbox.left * 640), int(bbox.top * 480) - 4),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
        result_image = cv2.rectangle(
            image, (int(bbox.left * 640), int(bbox.top * 480)),
            (int((bbox.left + bbox.width) * 640),
             int((bbox.top + bbox.height) * 480)), (0, 255, 0), 1)
        cv2.imwrite('result_detect.png', result_image)
        print(prediction.tag_name)
predictor = CustomVisionPredictionClient(ENDPOINT, prediction_credentials)

wideo = cv2.VideoCapture(1)

if (wideo.isOpened()):
    while (True):
        udany_odczyt, ramka = wideo.read()
        if udany_odczyt:
            k = cv2.waitKey(25) & 0xFF
            if k == 27:
                break

            img = cv2.imwrite("temp.jpg", ramka)
            with open("temp.jpg", "rb") as image_contents:
                results = predictor.detect_image(project_id,
                                                 publish_iteration_name,
                                                 image_contents.read())

                valid_predictions = [
                    pred for pred in results.predictions
                    if (pred.tag_name == "with_mask"
                        and pred.probability > with_mask_threshold) or (
                            pred.tag_name == "no_mask"
                            and pred.probability > no_mask_threshold)
                ]
                # Display the results.
                for prediction in valid_predictions:
                    print("\t" + prediction.tag_name +
                          ": {0:.2f}%".format(prediction.probability * 100))
                    lx = int(prediction.bounding_box.left * ramka.shape[1])
                    rx = int((prediction.bounding_box.left +
Example #28
0
    def fileDialog(self):

        self.filename = filedialog.askopenfilename(initialdir =  "/", title = "Select A File", filetype =
        (("jpeg files","*.jpg"),("all files","*.*")) )
        self.label = ttk.Label(self.labelFrame, text = "")
        self.label.grid(column = 1, row = 2)
        self.label.configure(text = self.filename)

        #following details are available in Azure Portal
        
        ENDPOINT = "<insert Resource EndPoint>"
        PROJECT_ID = "<insert Project ID>"

        items=["cinthol_soap",
            "nima",
            "comfort_liquid",
            "himalaya_neem_soap",
            "himalaya_baby_shampoo",
            "genteel",
            "honey_apis",
            "crompton_15w_bulb"]


        index={"cinthol_soap":0 ,
            "nima":1 ,
            "comfort_liquid":2,
            "himalaya_neem_soap":3,
            "himalaya_baby_shampoo":4,
            "genteel":5,
            "honey_apis":6,
            "crompton_15w_bulb":7}
                
        name={"cinthol_soap":"Cinthol Soap" ,
            "nima":"Nima Soap" ,
            "comfort_liquid":"Comfort Fabric Conditioner",
            "himalaya_neem_soap":"Himalaya Neem Soap",
            "himalaya_baby_shampoo":"Himalaya Baby Shampoo",
            "genteel":"Genteel Liquid Detergent",
            "honey_apis":"Apis Himalayan Honey",
            "crompton_15w_bulb":"Crompton Light Bulb 15W"}

        cost=[0 for i in range(8)]

        price=[49.0,14.0,218.0,45.0,160.0,320.0,440.0,160.0]

        #following details are available in custom vision project 

        prediction_key = "<enter Prediction Key >"
        prediction_resource_id = "<enter Resource ID>"
        publish_iteration_name = "<enter Iteration name>"

        credentials = ApiKeyCredentials(in_headers={"Prediction-key": prediction_key})
        predictor = CustomVisionPredictionClient(endpoint=ENDPOINT, credentials=credentials)

        location = self.filename

        location=list(location)
        destination=[]
        for itr in range(len(location)):
            if(location[itr]=="/"):
                destination.append("\\")
            else:
                destination.append(location[itr])
        pic = ''.join(destination)        
        with open(pic, mode="rb") as test_data:
            results = predictor.detect_image(PROJECT_ID, published_name="Iteration1",image_data =test_data.read(),iteration_id= ' e32a6cd8-9a11-42a6-852c-0ec836455726')

        for prediction in results.predictions:
            if(prediction.tag_name in items):
                print ("\t" + prediction.tag_name + ": {0:.2f}%".format(prediction.probability * 100))
                cost[index[prediction.tag_name]]+=1

        dash=['-' for i in range(60)]
        tot=0
        qty=0

        #location of test file to store the result
        sys.stdout=open("<enter the location of destination text file>","w")

        print("".join(dash))
        print("Photo Bill Mart")
        print("".join(dash))
        print('{:30s} {:3s}  {:10s} {:3s}'.format("Item Name","Qty","Cost","MRP"))
        print("".join(dash))
        for i in range(8):
            if(cost[i]>0):
                qty+=cost[i]
                print('{:30s} {:3d}  {:7.2f} {:7.2f}'.format(name[items[i]], cost[i], price[i]*cost[i],price[i]))
                tot+=(price[i]*cost[i])
        print("".join(dash))
        print('{:30s} {:3d}  {:7.2f}'.format("Total",qty,tot))
        print("".join(dash))
        sys.stdout.close()
    
        #print("break\n")  

        img = Image.open(self.filename)
        img = img.resize((450, 350), Image.ANTIALIAS)
        photo = ImageTk.PhotoImage(img)


        self.label2 = Label(image=photo,text="Captured Image",compound='bottom')
        self.label2.image = photo 
        self.label2.grid(column=1, row=4)

        self.configfile = Text(self.labelFrame_res, wrap=WORD, width=60, height= 20)
        with open("<enter the location of destination text file>", 'r') as f:
            self.configfile.insert(INSERT, f.read())
        self.configfile.grid(column=3,row=4)
Example #29
0
    exit(-1)

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(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!")

# Now there is a trained endpoint that can be used to make a prediction

# Open the sample image and get back the prediction results.
with open(base_image_location + "images/Test/test_od_image.jpg",
          mode="rb") as test_data:
    results = predictor.detect_image(project.id, publish_iteration_name,
                                     test_data)

# Display the results.
for prediction in results.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))
Example #30
0
class analyze:
    def __init__(self, path_to_image_to_predict):
        self.model = CustomVisionPredictionClient(prediction_key,
                                                  endpoint=ENDPOINT)
        self.fires = []
        self.smoke = []
        self.image_path = path_to_image_to_predict

    # updates using the self.image_path
    def predictUpdate(self):
        fires = []
        smoke = []

        with open(self.image_path, mode="rb") as test_data:
            results = self.model.detect_image(project_id, publishedName,
                                              test_data)

        for pred in results.predictions:
            if (pred.tag_name == fire_classifier
                    and pred.probability >= fireThreshold):
                fires.append(pred)

        for pred in results.predictions:
            if (pred.tag_name == smoke_classifier
                    and pred.probability >= smokeThreshold):
                smoke.append(pred)

        self.fires = fires
        self.smoke = smoke

    def __areBothEmpty(self):
        return (len(self.fires) == 0 and len(self.smoke) == 0)

    # returns original list after prediction
    def getFireSmoke(self):
        if self.__areBothEmpty():
            self.predictUpdate()
        return self.fires, self.smoke

    # returns two lists of distances from the center of frame
    def returnDistances(self):
        if self.__areBothEmpty():
            self.predictUpdate()

        fireDist = []
        smokeDist = []

        for inst in self.fires:
            x, y = inst.bounding_box.left, inst.bounding_box.top
            delta_x, delta_y = 0.5 - x, 0.5 - y
            dist = math.sqrt(delta_x**2 + delta_y**2)

            fireDist.append(dist)

        for inst in self.smoke:
            x, y = inst.bounding_box.left, inst.bounding_box.top
            delta_x, delta_y = 0.5 - x, 0.5 - y
            dist = math.sqrt(delta_x**2 + delta_y**2)

            fireDist.append(dist)

        return fireDist, smokeDist

    # boxes the object and labels with tag name and probability for matplotlib. for direct_run
    def __boxObject(self, obj_list, droneInstance, ax, im_width, im_height):
        scoring_array = [0] * 250000

        for inst in obj_list:
            x, y = inst.bounding_box.left * im_width, inst.bounding_box.top * im_height
            label = inst.tag_name
            probability = inst.probability

            # scale to frame dimensions
            pic_coord = (x, y)

            # dimensions of recognized region
            obj_width = inst.bounding_box.width * im_width
            obj_height = inst.bounding_box.height * im_height

            # display
            rect = patches.Rectangle(pic_coord,
                                     obj_width,
                                     obj_height,
                                     edgecolor='r',
                                     fill=False)
            plt.text(pic_coord[0], pic_coord[1],
                     f'{label}: {round(probability, 3)}')

            ax.add_patch(rect)

            # scoring
            scoring_array = self.__score(scoring_array, x, y, obj_width,
                                         obj_height)

        # export to FireDrone
        droneInstance.score(scoring_array)

    # changes scoring array for one instance of fire. for direct_run
    def __score(self, scoring_array, top_left_x, top_left_y, width, height):
        width = int(round(width))
        height = int(round(height))
        top_left_x = int(round(top_left_x))
        top_left_y = int(round(top_left_y))
        arr_index = top_left_x + top_left_y * 500
        for _ in range(height):
            for i in range(arr_index, arr_index + width):
                scoring_array[i] = 1
            arr_index += 500

        return scoring_array

    # display on matplotlib. Use only with direct run. for direct_run
    def dispPredict(self, droneInstance, ax):
        if self.__areBothEmpty():
            self.predictUpdate()
            if self.__areBothEmpty():
                return

        im_width, im_height = Image.open(self.image_path).size

        img = np.array(Image.open(self.image_path), dtype=np.uint8)

        self.__boxObject(self.fires, droneInstance, ax, im_width, im_height)

        ax.imshow(img)
        plt.pause(1)

    # returns nearest drone coordinates using image coordinates
    def __img_to_drone_coords(self, im_x, im_y, im_width, im_height):
        im_x, im_y = int(round(im_x)), int(round(im_y))
        drone_x = int(round((im_x - 250) / 100))
        drone_y = int(round((im_height - im_y - 250) / 100))

        max_drone_x = int(round((im_width - 500) / 100))
        max_drone_y = int(round((im_height - 500) / 100))

        # bounding
        if drone_x < 0:
            drone_x = 0
        elif drone_x > max_drone_x:
            drone_x = max_drone_x

        if drone_y < 0:
            drone_y = 0
        elif drone_y > max_drone_y:
            drone_y = max_drone_y

        return drone_x, drone_y

    # boxes objects and returns a Draw PIL image and list of object center coordinates. For reverse_run
    def __imgBox(self, obj_list, image, im_width, im_height):
        im = image.convert('RGBA')
        drawn = Draw(im)
        fontsize = im_height * 0.01

        font = ImageFont.truetype('arial.ttf', int(round(fontsize)))

        coords = []

        for inst in obj_list:
            label = inst.tag_name
            probability = inst.probability

            x0, y0 = inst.bounding_box.left * im_width, inst.bounding_box.top * im_height
            x1, y1 = x0 + inst.bounding_box.width * im_width, y0 + inst.bounding_box.height * im_height
            xC, yC = abs(x0 + x1) / 2, abs(y0 + y1) / 2

            xC, yC = self.__img_to_drone_coords(xC, yC, im_width, im_height)

            coords.append((xC, yC))

            drawn.rectangle([(x0, y0), (x1, y1)], outline=(255, 0, 0, 255))
            drawn.text((x0, y0),
                       f'{label}: {round(probability, 3)}; ({xC}, {yC})',
                       fill=(0, 0, 0, 255),
                       font=font)

        return im, coords

    # for reverse_run
    def imgPredict(self):
        if self.__areBothEmpty():
            self.predictUpdate()
            if self.__areBothEmpty():
                return

        img = Image.open(self.image_path)
        im_width, im_height = img.size

        draw, coordList = self.__imgBox(self.fires, img, im_width, im_height)

        return draw, coordList