×

YOLO Python

YOLO means you only look once.It is a technique to perform object detection is called YOLO. It is the algorithmused by the program to identify items in the image.

Earlier detection frameworks use image classification techniques to find objects while looking at various areas of the image regularly at various scales. This technique is inaccurate and slow.

YOLO uses a completely different technique. It simply takes a single look at the full image and scans the network once to find items. Thus, it moves fast.

YOLO uses faster R-CNN and SSD for object detection frameworks.

OpenCV dnn module:

The DNN (Deep Neural Network) module was first included in the opencvcontrib repository. Users may now perform inference on pre-trained deep learning models inside of OpenCV due to its relocation to the master branch of the opencv.

In beginning only Caffe and Torch models were supported. Over the period support for different frameworks like TensorFlow is being added.

Yolo has recently been added. To identify common objects, we use the OpenCV dnn module with a pre-trained YOLO model.

Python Program

// python program
import cv2
import argparse
import numpy as a
// arguments
b = argparse.ArgumentParser()
b.add_argument('-i', '--image', required=True,
help = 'path to input image'’)
b.add_argument('-c', '--config', required=True,
help = 'pre-trained yolo approach’)
b.add_argument('-w', '--weights', required=True,
help = 'how to get to yolo trained weights’)
b.add_argument('-cl', '--classes', required=True,
help = 'path to a text file where class names are located’)
args = b.parse_args()

We require these things to execute the code:

  1. Python 3: If you are installed python already then run it and it verify whether it installed or not. Run python3 in command prompt.
  2. Numpy: pip install numpy

    numpy should install pip. Make pip linked to python3.
  3. OpenCV-python: For image processing and computer vision tasks, OpenCV is a good tool. This open-source library can be used to carry out operations including face detection, object tracking, and landmark detection.

Command Line Arguments

There are four input arguments.

  • Text file containing class names.
  • YOLO config file.
  • Pre-trained YOLO weights.
  • Input image.

Input image was our choice.

Run the program by typing.

$ python yolo_opencv.py --image dog.jpg --config yolov3.cfg --weights yolov3.weights --classes yolov3.txt

Reading input

image = cv2.imread(args.image)

Width = image.shape[1]

Height = image.shape[0]

scale = 0.00392

//class names were read from a text file

classes = None

with open(args.classes, 'r') as f:

classes = [line.strip() for line in f.readlines()]

// generate different colors for different classes

COLORS = a.random.uniform(0, 255, size=(len(classes), 3))

// read pre-trained model and configuration file

z = cv2.dnn.readz(args.weights, args.configuration)

blob = cv2.dnn.blobFromImage(image, scale, (416,416), (0,0,0), True, crop=False)

z.setInput(blob)

read the input image and get it height and width.

Extract the class names to a list from the text file that provides them in human readable form.

Create different colors for different classes.

z = cv2.dnn.readNet(args.weights, args.config)

above command reads the config file and weights.

blob = cv2.dnn.blobFromImage(image, scale, (Width,Height), (0,0,0), True, crop=False)

z.setInput(blob)

Output layer and bounding box

def get_result_layers(z):

layer_names = z.getLayerNames()

result_layers = [layer_names[i[0] - 1] for i in z.getUnconnectedOutLayers()]

return result_layers

// function to draw bounding box on the detected object with class name

def draw_bounding_box(img, class_id, confidence, i, j, i_plus_w, j_plus_h):

 label = str(classes[class_id])

color = COLORS[class_id]

 cv2.rectangle(img, (i,j), (i_plus_w,j_plus_h), color, 2)

cv2.putText(img, label, (x-10,y-10) cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)

In a sequential CNN network, the final output layer will usually be the only one. We are using the YOLO v3 architecture, which has numerous output layers that provide predictions. The names of the output layers are provided by the get output layers() method. There is no next layer connected to an output layer.

The function draw bounding box() draws a rectangle across the specified expected region and identifies the class over the box. We can also write the confidence value if necessary.

Running interface

class_ids = []

confidences = []

boxes = []

conf_threshold = 0.5

nms_threshold = 0.4

// Get the confidence, class ID, and bounding box parameters for each detection made by each output layer, and reject poor detections.

 (confidence < 0.5)

for out in outs:

for detection in out:

scores = detection[5:]

class_id = a.argmax(scores)

confidence = scores[class_id]

if confidence > 0.5:

center_i = int(detection[0] * Width)

center_j = int(detection[1] * Height)

w = int(detection[2] * Width)

h = int(detection[3] * Height)

x = center_i - w / 2

y = center_j - h / 2

class_ids.append(class_id)

confidences.append(float(confidence))

boxes.append([i, j, w, h])

outs = net.forward(get_output_layers(net))

The network receives the identical feed forward as it happens above line. If we don't specify the names of the output layer, it will only deliver predictions from the last output layer by default. Any additional output layer won't be taken into account.

For each detection from each output layer, the class id, confidence, and bounding box corners must be obtained. More importantly, the weak detections must be disregarded (detections with low confidence value).

Non-max suppression

indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)

//after nms, go through the remaining detections and construct a bounding box.

for n in indices:

n = n[0]

box = boxes[n]

i = box[0]

j = box[1]

w = box[2]

h = box[3]

draw_bounding_box(image, class_ids[i], confidences[i], round(x), round(y), round(x+w), round(y+h))

cv2.imshow("object detection", image)

cv2.waitKey()

cv2.imwrite("object-detection.jpg", image)

cv2.destroyAllWindows()

Even though we rejected weak detections, there will be several duplicate detections with overlapping bounding boxes. Non-max suppression is used to produce high overlapping box deletion.

YOLO Python

Output of the program using few images:

YOLO Python YOLO Python YOLO Python

Related Topics

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

Find Words in String Python

Python : Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in...

5 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

How to create a class in python

In any programming language, a class is a user-defined plan or blueprint using which objects or instances of the class are created. You may wonder why we need classes in programming....

5 minutes read.

How to implement classifiers in Python

What is classification? Classification is a type of supervised machine learning problem with a categorical target (response) variable. Given the known label in the training data, the classifier approximates a mapping...

4 minutes read.

Notepad++ For Python

In this article, you are going to learn what notepad++ is and how it is integrated with python to use the python programming language. You can also learn how to...

4 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Writing to Excel using Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

3 minutes read.

Python 2.7 data structures

The rundown information type has a few additional techniques. Here is every one of the strategies for listing objects: list.append(x): Add a thing to the furthest limit of the rundown; comparable...

9 minutes read.

Python Basics

In this tutorial, we will learn about the basics of Python, a very famous programming language. This tutorial will help you understand the language better if you start with python....

8 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python Dictionary get() method

Python Dictionary get() method The dictionary.get() method returns the value of the item with the specified key. Syntax dictionary.get(keyname, value) Parameter keyname- This parameter represents the key to be searched in the dictionary. value- The parameter...

1 minute read.

Number pattern in Python

Number pattern in Python: This article explains how to print number patterns in Python. The FOR loop, while loop, and range() functions are used in the following Python programs to...

4 minutes read.

Python Trigonometric Functions

Before jumping right into the functions and how to use them we need to know what do trigonometric functions mean and how many functions does python has. What are Trigonometric functions? Trigonometric...

3 minutes read.

Python Namespace

In python, the namespace is a very important concept that should be understood before using any function or variables. When we write code, we often use variables, libraries, functions, modules, etc....

4 minutes read.

Python set()

Python set() Class The set() class in Python returns a new set object, optionally with elements taken from iterable.  Syntax class set([iterable]) Parameter iterable : This parameter represents a sequence, collection or an iterator object Return This function returns a...

1 minute read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Falcon Python

Introduction of Python Python is the fastest and the smartest programming language and it is an object oriented language. Python has libraries that can be importedeasily and perform many operations; to...

3 minutes read.