images
folder that comes with the dataset are listed below.¶Before using this notebook, please ensure that OpenCV and Pytorch packages are installed so that the Jupyter notebook can access it.
import cv2
import matplotlib.pyplot as plt
from utils import *
from darknet import Darknet
YOLOv3 is the version three of the YOLO system (YOLOv3 Paper). The neural network model architecture is stored in the yolov3.cfg
file, and the pre-trained weights of the neural network are stored in yolov3.weights
. There is a file called coco.names
that has the list of 80 object class that the model will be able to detect. The model has been trained only on these 80 object classes.
# Copy the neural network architecture
cfg_file = 'yolov3.cfg'
# Copy the pre-trained weights
weight_file = 'yolov3.weights'
# Copy the names of the classes
namesfile = 'coco.names'
# Load the neural network architecture
m = Darknet(cfg_file)
# Load the pre-trained weights
m.load_weights(weight_file)
# Load the names of the classes
class_names = load_class_names(namesfile)
## You can visualize the neural network in YOLOv3 by uncommenting the following command
# m.print_network()
images
folder and modify the name of the image below.¶# Load the image
img = cv2.imread('./images/manbike.jpg')
# Converting the image to RGB
original_image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Resizing the image
resized_image = cv2.resize(original_image, (m.width, m.height))
# Display the images
plt.subplot(121)
plt.title('Input Image')
plt.imshow(original_image)
plt.subplot(122)
plt.title('Resized Input Image')
plt.imshow(resized_image)
plt.show()
# Non-Maximal Suppression Threshold is a parameter to keep only the best bounding box.
# Bounding boxes with detection probability less than 0.6 are not selected.
nms_threshold_probability = 0.6
# Intersection Over Union Threshold is a parameter that eliminates boxes with high intersection over union values.
# Bounding boxes with intersection over union of more than 0.4 are not selected.
iou_threshold_probability = 0.4
# The following command detects the objects in the image
boxes = detect_objects(m, resized_image, iou_threshold_probability, nms_threshold_probability)
# The following command prints the objects identified and the confidence level
print_objects(boxes, class_names)
#Plot the image with bounding boxes and corresponding object class labels
plot_boxes(original_image, boxes, class_names, plot_labels = True)