<jupyter_start><jupyter_text>Import libraries<jupyter_code>import cv2
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>List of categories that can be classified by MobileNet model<jupyter_code>categories = { 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' }<jupyter_output><empty_output><jupyter_text>load the pre-trained model using opencv dnn<jupyter_code>net = cv2.dnn.readNetFromCaffe('MobileNetSSD_deploy.prototxt.txt', 'MobileNetSSD_deploy.caffemodel')<jupyter_output><empty_output><jupyter_text>randomly selecting color for the bounding box<jupyter_code>colors = np.random.uniform(255, 0, size=(len(categories), 3))
# read input video
# cap = cv2.VideoCapture('walking.avi')
"""
use cap = cv2.VideoCapture(0), for webcam
""" 
cap = cv2.VideoCapture(0)
while True:
    # read each frame of the video
    ret, image = cap.read()
    # SSD accepts image size of (300,300). Resize the images
    resized_image = cv2.resize(image,(300,300))
    # create a blob of an image and normalize the image
    blob = cv2.dnn.blobFromImage(resized_image, 0.007843, (300, 300), (127.5, 127.5, 127.5), False)
    # feed blob to the model
    net.setInput(blob)
    # result from the model
    detections = net.forward()
    # to caculate scale factor
    (h, w) = resized_image.shape[:2]
    # iterate over all the detection result
    for i in range(detections.shape[2]):
        confidence = detections[0, 0, i, 2]
        # select if probability of detection is greater than 20%
        if confidence > 0.2: 
            class_id = int(detections[0, 0, i, 1])
            # postion of the detected object in sacled image
            startX = int(detections[0, 0, i, 3] * w) 
            startY = int(detections[0, 0, i, 4] * h)
            endX   = int(detections[0, 0, i, 5] * w)
            endY   = int(detections[0, 0, i, 6] * h)
            heightFactor = image.shape[0]/300.0  
            widthFactor = image.shape[1]/300.0 
            # map position of the detected object into original image
            startX = int(widthFactor * startX) 
            startY = int(heightFactor * startY)
            endX   = int(widthFactor * endX)
            endY   = int(heightFactor * endY)
            # draw rectangular box around each object  
            cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0))
            if class_id in categories:
                label = categories[class_id] + ": " + str(confidence)
                labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
                startY = max(startY, labelSize[1])
                cv2.rectangle(image, (startX, startY - labelSize[1]), (startX + labelSize[0], startY + baseLine), 
                              (0, 255, 0), cv2.FILLED)
                # write label of the object on the image
                cv2.putText(image, label, (startX, startY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255))
    cv2.imshow("object detection result", image)
    # destroy the window by pressing key 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12755997659449972, 'fraction_numerical': 0.044470450555880635, 'mean_word_length': 2.8676470588235294, 'pattern_counts': {'":': 1, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '579297', 'license': 'MIT', 'n_tokens_mistral': 1275, 'n_tokens_neox': 1150, 'n_words': 319, 'path': 'Activity01/.ipynb_checkpoints/Activity01-checkpoint.ipynb', 'repo_name': 'Develop-Packt/Performing-Object-Detection-and-Facial-Recognition', 'size': '3418'}