# This script is used to demonstrate MobileNet-SSD network using OpenCV deep learning module.
#
# It works with model taken from https://github.com/chuanqi305/MobileNet-SSD/ that
# was trained in Caffe-SSD framework, https://github.com/weiliu89/caffe/tree/ssd.
# Model detects objects from 20 classes.
#
# Also TensorFlow model from TensorFlow object detection model zoo may be used to
# detect objects from 90 classes:
# http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz
# Text graph definition must be taken from opencv_extra:
# https://github.com/opencv/opencv_extra/tree/master/testdata/dnn/ssd_mobilenet_v1_coco.pbtxt
import numpy as np
import argparse
from collections import Counter

try:
    import cv2 as cv
except ImportError:
    raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '
                      'configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')

inWidth = 300
inHeight = 300
WHRatio = inWidth / float(inHeight)
inScaleFactor = 0.007843
meanVal = 127.5

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Script to run MobileNet-SSD object detection network '
                    'trained either in Caffe or TensorFlow frameworks.')
    parser.add_argument("--video", help="path to video file. If empty, camera's stream will be used")
    parser.add_argument("--image", help="path to image file.")
    parser.add_argument("--prototxt", default="MobileNetSSD_deploy.prototxt.txt",
                                      help='Path to text network file: '
                                           'MobileNetSSD_deploy.prototxt for Caffe model or '
                                           'ssd_mobilenet_v1_coco.pbtxt from opencv_extra for TensorFlow model')
    parser.add_argument("--weights", default="MobileNetSSD_deploy.caffemodel",
                                     help='Path to weights: '
                                          'MobileNetSSD_deploy.caffemodel for Caffe model or '
                                          'frozen_inference_graph.pb from TensorFlow.')
    parser.add_argument("--num_classes", default=20, type=int,
                        help="Number of classes. It's 20 for Caffe model from "
                             "https://github.com/chuanqi305/MobileNet-SSD/ and 90 for "
                             "TensorFlow model from http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz")
    parser.add_argument("--thr", default=0.2, type=float, help="confidence threshold to filter out weak detections")
    args = parser.parse_args()

    if args.num_classes == 20:
        net = cv.dnn.readNetFromCaffe(args.prototxt, args.weights)
        swapRB = False
        classNames = { 0: 'background',
            1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat',
            5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair',
            10: 'cow', 11: 'diningtable', 12: 'dog', 13: 'horse',
            14: 'motorbike', 15: 'person', 16: 'pottedplant',
            17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor' }
    else:
        assert(args.num_classes == 90)
        net = cv.dnn.readNetFromTensorflow(args.weights, args.prototxt)
        swapRB = True
        classNames = { 0: 'background',
            1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane', 6: 'bus',
            7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light', 11: 'fire hydrant',
            13: 'stop sign', 14: 'parking meter', 15: 'bench', 16: 'bird', 17: 'cat',
            18: 'dog', 19: 'horse', 20: 'sheep', 21: 'cow', 22: 'elephant', 23: 'bear',
            24: 'zebra', 25: 'giraffe', 27: 'backpack', 28: 'umbrella', 31: 'handbag',
            32: 'tie', 33: 'suitcase', 34: 'frisbee', 35: 'skis', 36: 'snowboard',
            37: 'sports ball', 38: 'kite', 39: 'baseball bat', 40: 'baseball glove',
            41: 'skateboard', 42: 'surfboard', 43: 'tennis racket', 44: 'bottle',
            46: 'wine glass', 47: 'cup', 48: 'fork', 49: 'knife', 50: 'spoon',
            51: 'bowl', 52: 'banana', 53: 'apple', 54: 'sandwich', 55: 'orange',
            56: 'broccoli', 57: 'carrot', 58: 'hot dog', 59: 'pizza', 60: 'donut',
            61: 'cake', 62: 'chair', 63: 'couch', 64: 'potted plant', 65: 'bed',
            67: 'dining table', 70: 'toilet', 72: 'tv', 73: 'laptop', 74: 'mouse',
            75: 'remote', 76: 'keyboard', 77: 'cell phone', 78: 'microwave', 79: 'oven',
            80: 'toaster', 81: 'sink', 82: 'refrigerator', 84: 'book', 85: 'clock',
            86: 'vase', 87: 'scissors', 88: 'teddy bear', 89: 'hair drier', 90: 'toothbrush' }
    
    is_image = False 
    if args.video:
        cap = cv.VideoCapture(args.video)
    elif args.image:
        is_image = True
        original_img = cv.imread(args.image)  # Preserved to be used later to get final localizations
        frame = cv.imread(args.image)
    else:
        cap = cv.VideoCapture(0)
    
    # output is a dictionary that will store the key:value of labels:list of lists of final running avg bottom left point, height, and width of the rectangular box 
    output = {}  
    best_LeftBottom_pt = (0, 0)
    best_height = 0
    best_width = 0
    best_confidence = {}  # Dictionary of labels mapped to their best confidence scores
    frame_num = 0
    
    while True:
        # Capture frame-by-frame
        if not is_image:
            ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth, inHeight), (meanVal, meanVal, meanVal), swapRB)
        net.setInput(blob)
        detections = net.forward()

        cols = frame.shape[1]
        rows = frame.shape[0]

        if cols / float(rows) > WHRatio:
            cropSize = (int(rows * WHRatio), rows)
        else:
            cropSize = (cols, int(cols / WHRatio))

        y1 = int((rows - cropSize[1]) / 2)
        y2 = y1 + cropSize[1]
        x1 = int((cols - cropSize[0]) / 2)
        x2 = x1 + cropSize[0]
        frame = frame[y1:y2, x1:x2]

        cols = frame.shape[1]
        rows = frame.shape[0]

        if frame_num == 0:
            # Don't need Counter or any of the code in the next block but keeping it for now in case I want to fix the situation of multiple classes later
            labels = []
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > args.thr:
                    class_id = int(detections[0, 0, i, 1])
                    labels.append(classNames[class_id])
                    
            count = Counter(labels)  # dict of labels and their corresponding number of appearances if above a certain confidence threshold
            # print("***--------------------------*** \n%r" % Counter(labels))
            for key in count.keys():
                output[key] = []
                best_confidence[key] = 0
                
            print("INIT OUTPUT: ", output, "INIT BEST CONFIDENCE: ", best_confidence)
                        
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > args.thr:
                class_id = int(detections[0, 0, i, 1])
                l = classNames[class_id]  # the actual label in string form ('book' instead of 84)
                
                xLeftBottom = int(detections[0, 0, i, 3] * cols)
                yLeftBottom = int(detections[0, 0, i, 4] * rows)
                xRightTop   = int(detections[0, 0, i, 5] * cols)
                yRightTop   = int(detections[0, 0, i, 6] * rows)
                height = abs(int(yLeftBottom)-int(yRightTop))
                width = abs(int(xLeftBottom)-int(xRightTop))
                              
                # img_width, img_height = (frame.shape[1], frame.shape[0])  # tuple w shape (width, height)
                # cv.rectangle(frame, (100, 100), (img_width-100, img_height-100), (255, 255, 255))  # Test rectangle just to see where things are
                cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop, yRightTop),
                              (0, 255, 0))
                
                if class_id in classNames:
                    
                    # Compare confidence with the curr best confidence for that class, 0 if none
                    if confidence > best_confidence.get(l, 0):
                        best_LeftBottom_pt = (xLeftBottom, yLeftBottom)
                        best_height = height
                        best_width = width
                        best_confidence[l] = confidence
                        
                         
                        try:
                            output[l].append([best_LeftBottom_pt, best_width, best_height, best_confidence[l]])
                        except KeyError:
                            output[l] = [[best_LeftBottom_pt, best_width, best_height, best_confidence[l]]]
                    
                        print("Label: %s\nBottom left point: (%s, %s) \nTop right point: (%s, %s)\nHeight: %s\nWidth: %s\nBest: %r\n\n\n" % (l, xLeftBottom, yLeftBottom, xRightTop, yRightTop, str(height), str(width), output))
                    
                    label = l + ": " + str(confidence)
                    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 1, 2)

                    yLeftBottom = max(yLeftBottom, labelSize[1])
                    
                    # This forms the filled in small white rectangle that is the label (inside of the outer green rectangle)
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - labelSize[1]*2),
                                         (xLeftBottom + labelSize[0]*3, yLeftBottom + baseLine*2),
                                         (255, 255, 255), cv.FILLED)
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom),
                                cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
        
        frame_num += 1
        image = cv.resize(frame, (720, 540))  
        cv.imshow("detections", image)
        if cv.waitKey(1) >= 0:
            break

# Need to crop the original image so that it matches up with the image from frame-by-frame localization step above
cols = original_img.shape[1]
rows = original_img.shape[0]

if cols / float(rows) > WHRatio:
    cropSize = (int(rows * WHRatio), rows)
else:
    cropSize = (cols, int(cols / WHRatio))

y1 = int((rows - cropSize[1]) / 2)
y2 = y1 + cropSize[1]
x1 = int((cols - cropSize[0]) / 2)
x2 = x1 + cropSize[0]
original_img = original_img[y1:y2, x1:x2]

# Want to reproduce the image except with only the best (highest confidence) rectangles and their corresponding classes remaining.
for label, info in output.items():
    print(label, info)
    
    # Converting everything to integers instead of floats
    for ind, i in enumerate(info[0]):
        if ind == 3:  # Don't want to round confidence score since it is a float between 0 and 1
            break
        if type(i) == tuple:
            output[label][0][ind] = (int(i[0]), int(i[1]))
        else:
            output[label][0][ind] = int(i)
    
    if len(info) > 1:
        best_conf = 0
        best_info_ind = 0
        for ind, a in enumerate(info): 
            if a[3] > best_conf:
                best_info_ind = ind
                best_conf = a[3]
        
        best_info = info[best_info_ind]
        output[label][0] = best_info
        
    
    bottomleftpt = info[0][0]
    width = info[0][1]
    height = info[0][2]
    conf = info[0][3]
    print(bottomleftpt, width, height, conf)
    
    l = label + ": " + str(conf)
    labelSize, baseLine = cv.getTextSize(l, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
    cv.rectangle(original_img, (bottomleftpt[0], bottomleftpt[1] - labelSize[1]*2), (bottomleftpt[0] + labelSize[0]*3, bottomleftpt[1] + baseLine*2), (255, 255, 255), cv.FILLED)
    cv.putText(original_img, l, bottomleftpt, cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
    
    cv.rectangle(original_img, bottomleftpt, (bottomleftpt[0] + width, bottomleftpt[1] + height), (0, 255, 0), 4)

img_width, img_height = (original_img.shape[1], original_img.shape[0])
print("width/height", img_width, img_height)
    
image = cv.resize(original_img, (720, 540))
cv.imshow("Localizations", image)
cv.waitKey(0)
    
print("***--------------------------------***\nFinal rolling average measurements: %r" % output)





02d02b78aca2bd31934dfaa77f37674668dce3f8
<mask token>

<mask token>
try:
    import cv2 as cv
except ImportError:
    raise ImportError(
        'Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)'
        )
<mask token>
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=
        'Script to run MobileNet-SSD object detection network trained either in Caffe or TensorFlow frameworks.'
        )
    parser.add_argument('--video', help=
        "path to video file. If empty, camera's stream will be used")
    parser.add_argument('--image', help='path to image file.')
    parser.add_argument('--prototxt', default=
        'MobileNetSSD_deploy.prototxt.txt', help=
        'Path to text network file: MobileNetSSD_deploy.prototxt for Caffe model or ssd_mobilenet_v1_coco.pbtxt from opencv_extra for TensorFlow model'
        )
    parser.add_argument('--weights', default=
        'MobileNetSSD_deploy.caffemodel', help=
        'Path to weights: MobileNetSSD_deploy.caffemodel for Caffe model or frozen_inference_graph.pb from TensorFlow.'
        )
    parser.add_argument('--num_classes', default=20, type=int, help=
        "Number of classes. It's 20 for Caffe model from https://github.com/chuanqi305/MobileNet-SSD/ and 90 for TensorFlow model from http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz"
        )
    parser.add_argument('--thr', default=0.2, type=float, help=
        'confidence threshold to filter out weak detections')
    args = parser.parse_args()
    if args.num_classes == 20:
        net = cv.dnn.readNetFromCaffe(args.prototxt, args.weights)
        swapRB = False
        classNames = {(0): 'background', (1): 'aeroplane', (2): 'bicycle',
            (3): 'bird', (4): 'boat', (5): 'bottle', (6): 'bus', (7): 'car',
            (8): 'cat', (9): 'chair', (10): 'cow', (11): 'diningtable', (12
            ): 'dog', (13): 'horse', (14): 'motorbike', (15): 'person', (16
            ): 'pottedplant', (17): 'sheep', (18): 'sofa', (19): 'train', (
            20): 'tvmonitor'}
    else:
        assert args.num_classes == 90
        net = cv.dnn.readNetFromTensorflow(args.weights, args.prototxt)
        swapRB = True
        classNames = {(0): 'background', (1): 'person', (2): 'bicycle', (3):
            'car', (4): 'motorcycle', (5): 'airplane', (6): 'bus', (7):
            'train', (8): 'truck', (9): 'boat', (10): 'traffic light', (11):
            'fire hydrant', (13): 'stop sign', (14): 'parking meter', (15):
            'bench', (16): 'bird', (17): 'cat', (18): 'dog', (19): 'horse',
            (20): 'sheep', (21): 'cow', (22): 'elephant', (23): 'bear', (24
            ): 'zebra', (25): 'giraffe', (27): 'backpack', (28): 'umbrella',
            (31): 'handbag', (32): 'tie', (33): 'suitcase', (34): 'frisbee',
            (35): 'skis', (36): 'snowboard', (37): 'sports ball', (38):
            'kite', (39): 'baseball bat', (40): 'baseball glove', (41):
            'skateboard', (42): 'surfboard', (43): 'tennis racket', (44):
            'bottle', (46): 'wine glass', (47): 'cup', (48): 'fork', (49):
            'knife', (50): 'spoon', (51): 'bowl', (52): 'banana', (53):
            'apple', (54): 'sandwich', (55): 'orange', (56): 'broccoli', (
            57): 'carrot', (58): 'hot dog', (59): 'pizza', (60): 'donut', (
            61): 'cake', (62): 'chair', (63): 'couch', (64): 'potted plant',
            (65): 'bed', (67): 'dining table', (70): 'toilet', (72): 'tv',
            (73): 'laptop', (74): 'mouse', (75): 'remote', (76): 'keyboard',
            (77): 'cell phone', (78): 'microwave', (79): 'oven', (80):
            'toaster', (81): 'sink', (82): 'refrigerator', (84): 'book', (
            85): 'clock', (86): 'vase', (87): 'scissors', (88):
            'teddy bear', (89): 'hair drier', (90): 'toothbrush'}
    is_image = False
    if args.video:
        cap = cv.VideoCapture(args.video)
    elif args.image:
        is_image = True
        original_img = cv.imread(args.image)
        frame = cv.imread(args.image)
    else:
        cap = cv.VideoCapture(0)
    output = {}
    best_LeftBottom_pt = 0, 0
    best_height = 0
    best_width = 0
    best_confidence = {}
    frame_num = 0
    while True:
        if not is_image:
            ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth,
            inHeight), (meanVal, meanVal, meanVal), swapRB)
        net.setInput(blob)
        detections = net.forward()
        cols = frame.shape[1]
        rows = frame.shape[0]
        if cols / float(rows) > WHRatio:
            cropSize = int(rows * WHRatio), rows
        else:
            cropSize = cols, int(cols / WHRatio)
        y1 = int((rows - cropSize[1]) / 2)
        y2 = y1 + cropSize[1]
        x1 = int((cols - cropSize[0]) / 2)
        x2 = x1 + cropSize[0]
        frame = frame[y1:y2, x1:x2]
        cols = frame.shape[1]
        rows = frame.shape[0]
        if frame_num == 0:
            labels = []
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > args.thr:
                    class_id = int(detections[0, 0, i, 1])
                    labels.append(classNames[class_id])
            count = Counter(labels)
            for key in count.keys():
                output[key] = []
                best_confidence[key] = 0
            print('INIT OUTPUT: ', output, 'INIT BEST CONFIDENCE: ',
                best_confidence)
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > args.thr:
                class_id = int(detections[0, 0, i, 1])
                l = classNames[class_id]
                xLeftBottom = int(detections[0, 0, i, 3] * cols)
                yLeftBottom = int(detections[0, 0, i, 4] * rows)
                xRightTop = int(detections[0, 0, i, 5] * cols)
                yRightTop = int(detections[0, 0, i, 6] * rows)
                height = abs(int(yLeftBottom) - int(yRightTop))
                width = abs(int(xLeftBottom) - int(xRightTop))
                cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop,
                    yRightTop), (0, 255, 0))
                if class_id in classNames:
                    if confidence > best_confidence.get(l, 0):
                        best_LeftBottom_pt = xLeftBottom, yLeftBottom
                        best_height = height
                        best_width = width
                        best_confidence[l] = confidence
                        try:
                            output[l].append([best_LeftBottom_pt,
                                best_width, best_height, best_confidence[l]])
                        except KeyError:
                            output[l] = [[best_LeftBottom_pt, best_width,
                                best_height, best_confidence[l]]]
                        print(
                            """Label: %s
Bottom left point: (%s, %s) 
Top right point: (%s, %s)
Height: %s
Width: %s
Best: %r


"""
                             % (l, xLeftBottom, yLeftBottom, xRightTop,
                            yRightTop, str(height), str(width), output))
                    label = l + ': ' + str(confidence)
                    labelSize, baseLine = cv.getTextSize(label, cv.
                        FONT_HERSHEY_SIMPLEX, 1, 2)
                    yLeftBottom = max(yLeftBottom, labelSize[1])
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - 
                        labelSize[1] * 2), (xLeftBottom + labelSize[0] * 3,
                        yLeftBottom + baseLine * 2), (255, 255, 255), cv.FILLED
                        )
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom), cv
                        .FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
        frame_num += 1
        image = cv.resize(frame, (720, 540))
        cv.imshow('detections', image)
        if cv.waitKey(1) >= 0:
            break
<mask token>
if cols / float(rows) > WHRatio:
    cropSize = int(rows * WHRatio), rows
else:
    cropSize = cols, int(cols / WHRatio)
<mask token>
for label, info in output.items():
    print(label, info)
    for ind, i in enumerate(info[0]):
        if ind == 3:
            break
        if type(i) == tuple:
            output[label][0][ind] = int(i[0]), int(i[1])
        else:
            output[label][0][ind] = int(i)
    if len(info) > 1:
        best_conf = 0
        best_info_ind = 0
        for ind, a in enumerate(info):
            if a[3] > best_conf:
                best_info_ind = ind
                best_conf = a[3]
        best_info = info[best_info_ind]
        output[label][0] = best_info
    bottomleftpt = info[0][0]
    width = info[0][1]
    height = info[0][2]
    conf = info[0][3]
    print(bottomleftpt, width, height, conf)
    l = label + ': ' + str(conf)
    labelSize, baseLine = cv.getTextSize(l, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
    cv.rectangle(original_img, (bottomleftpt[0], bottomleftpt[1] - 
        labelSize[1] * 2), (bottomleftpt[0] + labelSize[0] * 3, 
        bottomleftpt[1] + baseLine * 2), (255, 255, 255), cv.FILLED)
    cv.putText(original_img, l, bottomleftpt, cv.FONT_HERSHEY_SIMPLEX, 3, (
        0, 0, 0), 3)
    cv.rectangle(original_img, bottomleftpt, (bottomleftpt[0] + width, 
        bottomleftpt[1] + height), (0, 255, 0), 4)
<mask token>
print('width/height', img_width, img_height)
<mask token>
cv.imshow('Localizations', image)
cv.waitKey(0)
print(
    """***--------------------------------***
Final rolling average measurements: %r"""
     % output)

<mask token>
try:
    import cv2 as cv
except ImportError:
    raise ImportError(
        'Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)'
        )
inWidth = 300
inHeight = 300
WHRatio = inWidth / float(inHeight)
inScaleFactor = 0.007843
meanVal = 127.5
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=
        'Script to run MobileNet-SSD object detection network trained either in Caffe or TensorFlow frameworks.'
        )
    parser.add_argument('--video', help=
        "path to video file. If empty, camera's stream will be used")
    parser.add_argument('--image', help='path to image file.')
    parser.add_argument('--prototxt', default=
        'MobileNetSSD_deploy.prototxt.txt', help=
        'Path to text network file: MobileNetSSD_deploy.prototxt for Caffe model or ssd_mobilenet_v1_coco.pbtxt from opencv_extra for TensorFlow model'
        )
    parser.add_argument('--weights', default=
        'MobileNetSSD_deploy.caffemodel', help=
        'Path to weights: MobileNetSSD_deploy.caffemodel for Caffe model or frozen_inference_graph.pb from TensorFlow.'
        )
    parser.add_argument('--num_classes', default=20, type=int, help=
        "Number of classes. It's 20 for Caffe model from https://github.com/chuanqi305/MobileNet-SSD/ and 90 for TensorFlow model from http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz"
        )
    parser.add_argument('--thr', default=0.2, type=float, help=
        'confidence threshold to filter out weak detections')
    args = parser.parse_args()
    if args.num_classes == 20:
        net = cv.dnn.readNetFromCaffe(args.prototxt, args.weights)
        swapRB = False
        classNames = {(0): 'background', (1): 'aeroplane', (2): 'bicycle',
            (3): 'bird', (4): 'boat', (5): 'bottle', (6): 'bus', (7): 'car',
            (8): 'cat', (9): 'chair', (10): 'cow', (11): 'diningtable', (12
            ): 'dog', (13): 'horse', (14): 'motorbike', (15): 'person', (16
            ): 'pottedplant', (17): 'sheep', (18): 'sofa', (19): 'train', (
            20): 'tvmonitor'}
    else:
        assert args.num_classes == 90
        net = cv.dnn.readNetFromTensorflow(args.weights, args.prototxt)
        swapRB = True
        classNames = {(0): 'background', (1): 'person', (2): 'bicycle', (3):
            'car', (4): 'motorcycle', (5): 'airplane', (6): 'bus', (7):
            'train', (8): 'truck', (9): 'boat', (10): 'traffic light', (11):
            'fire hydrant', (13): 'stop sign', (14): 'parking meter', (15):
            'bench', (16): 'bird', (17): 'cat', (18): 'dog', (19): 'horse',
            (20): 'sheep', (21): 'cow', (22): 'elephant', (23): 'bear', (24
            ): 'zebra', (25): 'giraffe', (27): 'backpack', (28): 'umbrella',
            (31): 'handbag', (32): 'tie', (33): 'suitcase', (34): 'frisbee',
            (35): 'skis', (36): 'snowboard', (37): 'sports ball', (38):
            'kite', (39): 'baseball bat', (40): 'baseball glove', (41):
            'skateboard', (42): 'surfboard', (43): 'tennis racket', (44):
            'bottle', (46): 'wine glass', (47): 'cup', (48): 'fork', (49):
            'knife', (50): 'spoon', (51): 'bowl', (52): 'banana', (53):
            'apple', (54): 'sandwich', (55): 'orange', (56): 'broccoli', (
            57): 'carrot', (58): 'hot dog', (59): 'pizza', (60): 'donut', (
            61): 'cake', (62): 'chair', (63): 'couch', (64): 'potted plant',
            (65): 'bed', (67): 'dining table', (70): 'toilet', (72): 'tv',
            (73): 'laptop', (74): 'mouse', (75): 'remote', (76): 'keyboard',
            (77): 'cell phone', (78): 'microwave', (79): 'oven', (80):
            'toaster', (81): 'sink', (82): 'refrigerator', (84): 'book', (
            85): 'clock', (86): 'vase', (87): 'scissors', (88):
            'teddy bear', (89): 'hair drier', (90): 'toothbrush'}
    is_image = False
    if args.video:
        cap = cv.VideoCapture(args.video)
    elif args.image:
        is_image = True
        original_img = cv.imread(args.image)
        frame = cv.imread(args.image)
    else:
        cap = cv.VideoCapture(0)
    output = {}
    best_LeftBottom_pt = 0, 0
    best_height = 0
    best_width = 0
    best_confidence = {}
    frame_num = 0
    while True:
        if not is_image:
            ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth,
            inHeight), (meanVal, meanVal, meanVal), swapRB)
        net.setInput(blob)
        detections = net.forward()
        cols = frame.shape[1]
        rows = frame.shape[0]
        if cols / float(rows) > WHRatio:
            cropSize = int(rows * WHRatio), rows
        else:
            cropSize = cols, int(cols / WHRatio)
        y1 = int((rows - cropSize[1]) / 2)
        y2 = y1 + cropSize[1]
        x1 = int((cols - cropSize[0]) / 2)
        x2 = x1 + cropSize[0]
        frame = frame[y1:y2, x1:x2]
        cols = frame.shape[1]
        rows = frame.shape[0]
        if frame_num == 0:
            labels = []
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > args.thr:
                    class_id = int(detections[0, 0, i, 1])
                    labels.append(classNames[class_id])
            count = Counter(labels)
            for key in count.keys():
                output[key] = []
                best_confidence[key] = 0
            print('INIT OUTPUT: ', output, 'INIT BEST CONFIDENCE: ',
                best_confidence)
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > args.thr:
                class_id = int(detections[0, 0, i, 1])
                l = classNames[class_id]
                xLeftBottom = int(detections[0, 0, i, 3] * cols)
                yLeftBottom = int(detections[0, 0, i, 4] * rows)
                xRightTop = int(detections[0, 0, i, 5] * cols)
                yRightTop = int(detections[0, 0, i, 6] * rows)
                height = abs(int(yLeftBottom) - int(yRightTop))
                width = abs(int(xLeftBottom) - int(xRightTop))
                cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop,
                    yRightTop), (0, 255, 0))
                if class_id in classNames:
                    if confidence > best_confidence.get(l, 0):
                        best_LeftBottom_pt = xLeftBottom, yLeftBottom
                        best_height = height
                        best_width = width
                        best_confidence[l] = confidence
                        try:
                            output[l].append([best_LeftBottom_pt,
                                best_width, best_height, best_confidence[l]])
                        except KeyError:
                            output[l] = [[best_LeftBottom_pt, best_width,
                                best_height, best_confidence[l]]]
                        print(
                            """Label: %s
Bottom left point: (%s, %s) 
Top right point: (%s, %s)
Height: %s
Width: %s
Best: %r


"""
                             % (l, xLeftBottom, yLeftBottom, xRightTop,
                            yRightTop, str(height), str(width), output))
                    label = l + ': ' + str(confidence)
                    labelSize, baseLine = cv.getTextSize(label, cv.
                        FONT_HERSHEY_SIMPLEX, 1, 2)
                    yLeftBottom = max(yLeftBottom, labelSize[1])
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - 
                        labelSize[1] * 2), (xLeftBottom + labelSize[0] * 3,
                        yLeftBottom + baseLine * 2), (255, 255, 255), cv.FILLED
                        )
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom), cv
                        .FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
        frame_num += 1
        image = cv.resize(frame, (720, 540))
        cv.imshow('detections', image)
        if cv.waitKey(1) >= 0:
            break
cols = original_img.shape[1]
rows = original_img.shape[0]
if cols / float(rows) > WHRatio:
    cropSize = int(rows * WHRatio), rows
else:
    cropSize = cols, int(cols / WHRatio)
y1 = int((rows - cropSize[1]) / 2)
y2 = y1 + cropSize[1]
x1 = int((cols - cropSize[0]) / 2)
x2 = x1 + cropSize[0]
original_img = original_img[y1:y2, x1:x2]
for label, info in output.items():
    print(label, info)
    for ind, i in enumerate(info[0]):
        if ind == 3:
            break
        if type(i) == tuple:
            output[label][0][ind] = int(i[0]), int(i[1])
        else:
            output[label][0][ind] = int(i)
    if len(info) > 1:
        best_conf = 0
        best_info_ind = 0
        for ind, a in enumerate(info):
            if a[3] > best_conf:
                best_info_ind = ind
                best_conf = a[3]
        best_info = info[best_info_ind]
        output[label][0] = best_info
    bottomleftpt = info[0][0]
    width = info[0][1]
    height = info[0][2]
    conf = info[0][3]
    print(bottomleftpt, width, height, conf)
    l = label + ': ' + str(conf)
    labelSize, baseLine = cv.getTextSize(l, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
    cv.rectangle(original_img, (bottomleftpt[0], bottomleftpt[1] - 
        labelSize[1] * 2), (bottomleftpt[0] + labelSize[0] * 3, 
        bottomleftpt[1] + baseLine * 2), (255, 255, 255), cv.FILLED)
    cv.putText(original_img, l, bottomleftpt, cv.FONT_HERSHEY_SIMPLEX, 3, (
        0, 0, 0), 3)
    cv.rectangle(original_img, bottomleftpt, (bottomleftpt[0] + width, 
        bottomleftpt[1] + height), (0, 255, 0), 4)
img_width, img_height = original_img.shape[1], original_img.shape[0]
print('width/height', img_width, img_height)
image = cv.resize(original_img, (720, 540))
cv.imshow('Localizations', image)
cv.waitKey(0)
print(
    """***--------------------------------***
Final rolling average measurements: %r"""
     % output)

import numpy as np
import argparse
from collections import Counter
try:
    import cv2 as cv
except ImportError:
    raise ImportError(
        'Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)'
        )
inWidth = 300
inHeight = 300
WHRatio = inWidth / float(inHeight)
inScaleFactor = 0.007843
meanVal = 127.5
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=
        'Script to run MobileNet-SSD object detection network trained either in Caffe or TensorFlow frameworks.'
        )
    parser.add_argument('--video', help=
        "path to video file. If empty, camera's stream will be used")
    parser.add_argument('--image', help='path to image file.')
    parser.add_argument('--prototxt', default=
        'MobileNetSSD_deploy.prototxt.txt', help=
        'Path to text network file: MobileNetSSD_deploy.prototxt for Caffe model or ssd_mobilenet_v1_coco.pbtxt from opencv_extra for TensorFlow model'
        )
    parser.add_argument('--weights', default=
        'MobileNetSSD_deploy.caffemodel', help=
        'Path to weights: MobileNetSSD_deploy.caffemodel for Caffe model or frozen_inference_graph.pb from TensorFlow.'
        )
    parser.add_argument('--num_classes', default=20, type=int, help=
        "Number of classes. It's 20 for Caffe model from https://github.com/chuanqi305/MobileNet-SSD/ and 90 for TensorFlow model from http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz"
        )
    parser.add_argument('--thr', default=0.2, type=float, help=
        'confidence threshold to filter out weak detections')
    args = parser.parse_args()
    if args.num_classes == 20:
        net = cv.dnn.readNetFromCaffe(args.prototxt, args.weights)
        swapRB = False
        classNames = {(0): 'background', (1): 'aeroplane', (2): 'bicycle',
            (3): 'bird', (4): 'boat', (5): 'bottle', (6): 'bus', (7): 'car',
            (8): 'cat', (9): 'chair', (10): 'cow', (11): 'diningtable', (12
            ): 'dog', (13): 'horse', (14): 'motorbike', (15): 'person', (16
            ): 'pottedplant', (17): 'sheep', (18): 'sofa', (19): 'train', (
            20): 'tvmonitor'}
    else:
        assert args.num_classes == 90
        net = cv.dnn.readNetFromTensorflow(args.weights, args.prototxt)
        swapRB = True
        classNames = {(0): 'background', (1): 'person', (2): 'bicycle', (3):
            'car', (4): 'motorcycle', (5): 'airplane', (6): 'bus', (7):
            'train', (8): 'truck', (9): 'boat', (10): 'traffic light', (11):
            'fire hydrant', (13): 'stop sign', (14): 'parking meter', (15):
            'bench', (16): 'bird', (17): 'cat', (18): 'dog', (19): 'horse',
            (20): 'sheep', (21): 'cow', (22): 'elephant', (23): 'bear', (24
            ): 'zebra', (25): 'giraffe', (27): 'backpack', (28): 'umbrella',
            (31): 'handbag', (32): 'tie', (33): 'suitcase', (34): 'frisbee',
            (35): 'skis', (36): 'snowboard', (37): 'sports ball', (38):
            'kite', (39): 'baseball bat', (40): 'baseball glove', (41):
            'skateboard', (42): 'surfboard', (43): 'tennis racket', (44):
            'bottle', (46): 'wine glass', (47): 'cup', (48): 'fork', (49):
            'knife', (50): 'spoon', (51): 'bowl', (52): 'banana', (53):
            'apple', (54): 'sandwich', (55): 'orange', (56): 'broccoli', (
            57): 'carrot', (58): 'hot dog', (59): 'pizza', (60): 'donut', (
            61): 'cake', (62): 'chair', (63): 'couch', (64): 'potted plant',
            (65): 'bed', (67): 'dining table', (70): 'toilet', (72): 'tv',
            (73): 'laptop', (74): 'mouse', (75): 'remote', (76): 'keyboard',
            (77): 'cell phone', (78): 'microwave', (79): 'oven', (80):
            'toaster', (81): 'sink', (82): 'refrigerator', (84): 'book', (
            85): 'clock', (86): 'vase', (87): 'scissors', (88):
            'teddy bear', (89): 'hair drier', (90): 'toothbrush'}
    is_image = False
    if args.video:
        cap = cv.VideoCapture(args.video)
    elif args.image:
        is_image = True
        original_img = cv.imread(args.image)
        frame = cv.imread(args.image)
    else:
        cap = cv.VideoCapture(0)
    output = {}
    best_LeftBottom_pt = 0, 0
    best_height = 0
    best_width = 0
    best_confidence = {}
    frame_num = 0
    while True:
        if not is_image:
            ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth,
            inHeight), (meanVal, meanVal, meanVal), swapRB)
        net.setInput(blob)
        detections = net.forward()
        cols = frame.shape[1]
        rows = frame.shape[0]
        if cols / float(rows) > WHRatio:
            cropSize = int(rows * WHRatio), rows
        else:
            cropSize = cols, int(cols / WHRatio)
        y1 = int((rows - cropSize[1]) / 2)
        y2 = y1 + cropSize[1]
        x1 = int((cols - cropSize[0]) / 2)
        x2 = x1 + cropSize[0]
        frame = frame[y1:y2, x1:x2]
        cols = frame.shape[1]
        rows = frame.shape[0]
        if frame_num == 0:
            labels = []
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > args.thr:
                    class_id = int(detections[0, 0, i, 1])
                    labels.append(classNames[class_id])
            count = Counter(labels)
            for key in count.keys():
                output[key] = []
                best_confidence[key] = 0
            print('INIT OUTPUT: ', output, 'INIT BEST CONFIDENCE: ',
                best_confidence)
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > args.thr:
                class_id = int(detections[0, 0, i, 1])
                l = classNames[class_id]
                xLeftBottom = int(detections[0, 0, i, 3] * cols)
                yLeftBottom = int(detections[0, 0, i, 4] * rows)
                xRightTop = int(detections[0, 0, i, 5] * cols)
                yRightTop = int(detections[0, 0, i, 6] * rows)
                height = abs(int(yLeftBottom) - int(yRightTop))
                width = abs(int(xLeftBottom) - int(xRightTop))
                cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop,
                    yRightTop), (0, 255, 0))
                if class_id in classNames:
                    if confidence > best_confidence.get(l, 0):
                        best_LeftBottom_pt = xLeftBottom, yLeftBottom
                        best_height = height
                        best_width = width
                        best_confidence[l] = confidence
                        try:
                            output[l].append([best_LeftBottom_pt,
                                best_width, best_height, best_confidence[l]])
                        except KeyError:
                            output[l] = [[best_LeftBottom_pt, best_width,
                                best_height, best_confidence[l]]]
                        print(
                            """Label: %s
Bottom left point: (%s, %s) 
Top right point: (%s, %s)
Height: %s
Width: %s
Best: %r


"""
                             % (l, xLeftBottom, yLeftBottom, xRightTop,
                            yRightTop, str(height), str(width), output))
                    label = l + ': ' + str(confidence)
                    labelSize, baseLine = cv.getTextSize(label, cv.
                        FONT_HERSHEY_SIMPLEX, 1, 2)
                    yLeftBottom = max(yLeftBottom, labelSize[1])
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - 
                        labelSize[1] * 2), (xLeftBottom + labelSize[0] * 3,
                        yLeftBottom + baseLine * 2), (255, 255, 255), cv.FILLED
                        )
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom), cv
                        .FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
        frame_num += 1
        image = cv.resize(frame, (720, 540))
        cv.imshow('detections', image)
        if cv.waitKey(1) >= 0:
            break
cols = original_img.shape[1]
rows = original_img.shape[0]
if cols / float(rows) > WHRatio:
    cropSize = int(rows * WHRatio), rows
else:
    cropSize = cols, int(cols / WHRatio)
y1 = int((rows - cropSize[1]) / 2)
y2 = y1 + cropSize[1]
x1 = int((cols - cropSize[0]) / 2)
x2 = x1 + cropSize[0]
original_img = original_img[y1:y2, x1:x2]
for label, info in output.items():
    print(label, info)
    for ind, i in enumerate(info[0]):
        if ind == 3:
            break
        if type(i) == tuple:
            output[label][0][ind] = int(i[0]), int(i[1])
        else:
            output[label][0][ind] = int(i)
    if len(info) > 1:
        best_conf = 0
        best_info_ind = 0
        for ind, a in enumerate(info):
            if a[3] > best_conf:
                best_info_ind = ind
                best_conf = a[3]
        best_info = info[best_info_ind]
        output[label][0] = best_info
    bottomleftpt = info[0][0]
    width = info[0][1]
    height = info[0][2]
    conf = info[0][3]
    print(bottomleftpt, width, height, conf)
    l = label + ': ' + str(conf)
    labelSize, baseLine = cv.getTextSize(l, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
    cv.rectangle(original_img, (bottomleftpt[0], bottomleftpt[1] - 
        labelSize[1] * 2), (bottomleftpt[0] + labelSize[0] * 3, 
        bottomleftpt[1] + baseLine * 2), (255, 255, 255), cv.FILLED)
    cv.putText(original_img, l, bottomleftpt, cv.FONT_HERSHEY_SIMPLEX, 3, (
        0, 0, 0), 3)
    cv.rectangle(original_img, bottomleftpt, (bottomleftpt[0] + width, 
        bottomleftpt[1] + height), (0, 255, 0), 4)
img_width, img_height = original_img.shape[1], original_img.shape[0]
print('width/height', img_width, img_height)
image = cv.resize(original_img, (720, 540))
cv.imshow('Localizations', image)
cv.waitKey(0)
print(
    """***--------------------------------***
Final rolling average measurements: %r"""
     % output)

# This script is used to demonstrate MobileNet-SSD network using OpenCV deep learning module.
#
# It works with model taken from https://github.com/chuanqi305/MobileNet-SSD/ that
# was trained in Caffe-SSD framework, https://github.com/weiliu89/caffe/tree/ssd.
# Model detects objects from 20 classes.
#
# Also TensorFlow model from TensorFlow object detection model zoo may be used to
# detect objects from 90 classes:
# http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz
# Text graph definition must be taken from opencv_extra:
# https://github.com/opencv/opencv_extra/tree/master/testdata/dnn/ssd_mobilenet_v1_coco.pbtxt
import numpy as np
import argparse
from collections import Counter

try:
    import cv2 as cv
except ImportError:
    raise ImportError('Can\'t find OpenCV Python module. If you\'ve built it from sources without installation, '
                      'configure environemnt variable PYTHONPATH to "opencv_build_dir/lib" directory (with "python3" subdirectory if required)')

inWidth = 300
inHeight = 300
WHRatio = inWidth / float(inHeight)
inScaleFactor = 0.007843
meanVal = 127.5

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Script to run MobileNet-SSD object detection network '
                    'trained either in Caffe or TensorFlow frameworks.')
    parser.add_argument("--video", help="path to video file. If empty, camera's stream will be used")
    parser.add_argument("--image", help="path to image file.")
    parser.add_argument("--prototxt", default="MobileNetSSD_deploy.prototxt.txt",
                                      help='Path to text network file: '
                                           'MobileNetSSD_deploy.prototxt for Caffe model or '
                                           'ssd_mobilenet_v1_coco.pbtxt from opencv_extra for TensorFlow model')
    parser.add_argument("--weights", default="MobileNetSSD_deploy.caffemodel",
                                     help='Path to weights: '
                                          'MobileNetSSD_deploy.caffemodel for Caffe model or '
                                          'frozen_inference_graph.pb from TensorFlow.')
    parser.add_argument("--num_classes", default=20, type=int,
                        help="Number of classes. It's 20 for Caffe model from "
                             "https://github.com/chuanqi305/MobileNet-SSD/ and 90 for "
                             "TensorFlow model from http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_coco_11_06_2017.tar.gz")
    parser.add_argument("--thr", default=0.2, type=float, help="confidence threshold to filter out weak detections")
    args = parser.parse_args()

    if args.num_classes == 20:
        net = cv.dnn.readNetFromCaffe(args.prototxt, args.weights)
        swapRB = False
        classNames = { 0: 'background',
            1: 'aeroplane', 2: 'bicycle', 3: 'bird', 4: 'boat',
            5: 'bottle', 6: 'bus', 7: 'car', 8: 'cat', 9: 'chair',
            10: 'cow', 11: 'diningtable', 12: 'dog', 13: 'horse',
            14: 'motorbike', 15: 'person', 16: 'pottedplant',
            17: 'sheep', 18: 'sofa', 19: 'train', 20: 'tvmonitor' }
    else:
        assert(args.num_classes == 90)
        net = cv.dnn.readNetFromTensorflow(args.weights, args.prototxt)
        swapRB = True
        classNames = { 0: 'background',
            1: 'person', 2: 'bicycle', 3: 'car', 4: 'motorcycle', 5: 'airplane', 6: 'bus',
            7: 'train', 8: 'truck', 9: 'boat', 10: 'traffic light', 11: 'fire hydrant',
            13: 'stop sign', 14: 'parking meter', 15: 'bench', 16: 'bird', 17: 'cat',
            18: 'dog', 19: 'horse', 20: 'sheep', 21: 'cow', 22: 'elephant', 23: 'bear',
            24: 'zebra', 25: 'giraffe', 27: 'backpack', 28: 'umbrella', 31: 'handbag',
            32: 'tie', 33: 'suitcase', 34: 'frisbee', 35: 'skis', 36: 'snowboard',
            37: 'sports ball', 38: 'kite', 39: 'baseball bat', 40: 'baseball glove',
            41: 'skateboard', 42: 'surfboard', 43: 'tennis racket', 44: 'bottle',
            46: 'wine glass', 47: 'cup', 48: 'fork', 49: 'knife', 50: 'spoon',
            51: 'bowl', 52: 'banana', 53: 'apple', 54: 'sandwich', 55: 'orange',
            56: 'broccoli', 57: 'carrot', 58: 'hot dog', 59: 'pizza', 60: 'donut',
            61: 'cake', 62: 'chair', 63: 'couch', 64: 'potted plant', 65: 'bed',
            67: 'dining table', 70: 'toilet', 72: 'tv', 73: 'laptop', 74: 'mouse',
            75: 'remote', 76: 'keyboard', 77: 'cell phone', 78: 'microwave', 79: 'oven',
            80: 'toaster', 81: 'sink', 82: 'refrigerator', 84: 'book', 85: 'clock',
            86: 'vase', 87: 'scissors', 88: 'teddy bear', 89: 'hair drier', 90: 'toothbrush' }
    
    is_image = False 
    if args.video:
        cap = cv.VideoCapture(args.video)
    elif args.image:
        is_image = True
        original_img = cv.imread(args.image)  # Preserved to be used later to get final localizations
        frame = cv.imread(args.image)
    else:
        cap = cv.VideoCapture(0)
    
    # output is a dictionary that will store the key:value of labels:list of lists of final running avg bottom left point, height, and width of the rectangular box 
    output = {}  
    best_LeftBottom_pt = (0, 0)
    best_height = 0
    best_width = 0
    best_confidence = {}  # Dictionary of labels mapped to their best confidence scores
    frame_num = 0
    
    while True:
        # Capture frame-by-frame
        if not is_image:
            ret, frame = cap.read()
        blob = cv.dnn.blobFromImage(frame, inScaleFactor, (inWidth, inHeight), (meanVal, meanVal, meanVal), swapRB)
        net.setInput(blob)
        detections = net.forward()

        cols = frame.shape[1]
        rows = frame.shape[0]

        if cols / float(rows) > WHRatio:
            cropSize = (int(rows * WHRatio), rows)
        else:
            cropSize = (cols, int(cols / WHRatio))

        y1 = int((rows - cropSize[1]) / 2)
        y2 = y1 + cropSize[1]
        x1 = int((cols - cropSize[0]) / 2)
        x2 = x1 + cropSize[0]
        frame = frame[y1:y2, x1:x2]

        cols = frame.shape[1]
        rows = frame.shape[0]

        if frame_num == 0:
            # Don't need Counter or any of the code in the next block but keeping it for now in case I want to fix the situation of multiple classes later
            labels = []
            for i in range(detections.shape[2]):
                confidence = detections[0, 0, i, 2]
                if confidence > args.thr:
                    class_id = int(detections[0, 0, i, 1])
                    labels.append(classNames[class_id])
                    
            count = Counter(labels)  # dict of labels and their corresponding number of appearances if above a certain confidence threshold
            # print("***--------------------------*** \n%r" % Counter(labels))
            for key in count.keys():
                output[key] = []
                best_confidence[key] = 0
                
            print("INIT OUTPUT: ", output, "INIT BEST CONFIDENCE: ", best_confidence)
                        
        for i in range(detections.shape[2]):
            confidence = detections[0, 0, i, 2]
            if confidence > args.thr:
                class_id = int(detections[0, 0, i, 1])
                l = classNames[class_id]  # the actual label in string form ('book' instead of 84)
                
                xLeftBottom = int(detections[0, 0, i, 3] * cols)
                yLeftBottom = int(detections[0, 0, i, 4] * rows)
                xRightTop   = int(detections[0, 0, i, 5] * cols)
                yRightTop   = int(detections[0, 0, i, 6] * rows)
                height = abs(int(yLeftBottom)-int(yRightTop))
                width = abs(int(xLeftBottom)-int(xRightTop))
                              
                # img_width, img_height = (frame.shape[1], frame.shape[0])  # tuple w shape (width, height)
                # cv.rectangle(frame, (100, 100), (img_width-100, img_height-100), (255, 255, 255))  # Test rectangle just to see where things are
                cv.rectangle(frame, (xLeftBottom, yLeftBottom), (xRightTop, yRightTop),
                              (0, 255, 0))
                
                if class_id in classNames:
                    
                    # Compare confidence with the curr best confidence for that class, 0 if none
                    if confidence > best_confidence.get(l, 0):
                        best_LeftBottom_pt = (xLeftBottom, yLeftBottom)
                        best_height = height
                        best_width = width
                        best_confidence[l] = confidence
                        
                         
                        try:
                            output[l].append([best_LeftBottom_pt, best_width, best_height, best_confidence[l]])
                        except KeyError:
                            output[l] = [[best_LeftBottom_pt, best_width, best_height, best_confidence[l]]]
                    
                        print("Label: %s\nBottom left point: (%s, %s) \nTop right point: (%s, %s)\nHeight: %s\nWidth: %s\nBest: %r\n\n\n" % (l, xLeftBottom, yLeftBottom, xRightTop, yRightTop, str(height), str(width), output))
                    
                    label = l + ": " + str(confidence)
                    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 1, 2)

                    yLeftBottom = max(yLeftBottom, labelSize[1])
                    
                    # This forms the filled in small white rectangle that is the label (inside of the outer green rectangle)
                    cv.rectangle(frame, (xLeftBottom, yLeftBottom - labelSize[1]*2),
                                         (xLeftBottom + labelSize[0]*3, yLeftBottom + baseLine*2),
                                         (255, 255, 255), cv.FILLED)
                    cv.putText(frame, label, (xLeftBottom, yLeftBottom),
                                cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
        
        frame_num += 1
        image = cv.resize(frame, (720, 540))  
        cv.imshow("detections", image)
        if cv.waitKey(1) >= 0:
            break

# Need to crop the original image so that it matches up with the image from frame-by-frame localization step above
cols = original_img.shape[1]
rows = original_img.shape[0]

if cols / float(rows) > WHRatio:
    cropSize = (int(rows * WHRatio), rows)
else:
    cropSize = (cols, int(cols / WHRatio))

y1 = int((rows - cropSize[1]) / 2)
y2 = y1 + cropSize[1]
x1 = int((cols - cropSize[0]) / 2)
x2 = x1 + cropSize[0]
original_img = original_img[y1:y2, x1:x2]

# Want to reproduce the image except with only the best (highest confidence) rectangles and their corresponding classes remaining.
for label, info in output.items():
    print(label, info)
    
    # Converting everything to integers instead of floats
    for ind, i in enumerate(info[0]):
        if ind == 3:  # Don't want to round confidence score since it is a float between 0 and 1
            break
        if type(i) == tuple:
            output[label][0][ind] = (int(i[0]), int(i[1]))
        else:
            output[label][0][ind] = int(i)
    
    if len(info) > 1:
        best_conf = 0
        best_info_ind = 0
        for ind, a in enumerate(info): 
            if a[3] > best_conf:
                best_info_ind = ind
                best_conf = a[3]
        
        best_info = info[best_info_ind]
        output[label][0] = best_info
        
    
    bottomleftpt = info[0][0]
    width = info[0][1]
    height = info[0][2]
    conf = info[0][3]
    print(bottomleftpt, width, height, conf)
    
    l = label + ": " + str(conf)
    labelSize, baseLine = cv.getTextSize(l, cv.FONT_HERSHEY_SIMPLEX, 1, 2)
    cv.rectangle(original_img, (bottomleftpt[0], bottomleftpt[1] - labelSize[1]*2), (bottomleftpt[0] + labelSize[0]*3, bottomleftpt[1] + baseLine*2), (255, 255, 255), cv.FILLED)
    cv.putText(original_img, l, bottomleftpt, cv.FONT_HERSHEY_SIMPLEX, 3, (0, 0, 0), 3)
    
    cv.rectangle(original_img, bottomleftpt, (bottomleftpt[0] + width, bottomleftpt[1] + height), (0, 255, 0), 4)

img_width, img_height = (original_img.shape[1], original_img.shape[0])
print("width/height", img_width, img_height)
    
image = cv.resize(original_img, (720, 540))
cv.imshow("Localizations", image)
cv.waitKey(0)
    
print("***--------------------------------***\nFinal rolling average measurements: %r" % output)




