×

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

Python Hash Table

An Introduction to Hashing A technique which used to identify a particular object uniquely from a set of similar objects is known as Hashing. Some real-life examples of hashing implementations include: A...

9 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python sort() function

Python provides many built-in functions for solving many problems that arise in different situations in programs. One of such methods is the sort () method. In this article, the syntax...

4 minutes read.

Convert Float to Int in Python using Pandas

Introduction To play with huge amounts of data, in python we require a tool. The tool which is available in Python is pandas. A panda is an open-source library. It is...

4 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

CSV Module In Python

Introduction CSV stands for "comma separated values". It is the most common and simple file structure used for storing and arranging tabular data. for example; a spreadsheet or database. It stores...

5 minutes read.

Python Dictionary keys() method

Python Dictionary keys() method The dictionary.keys() method in Python returns a view object that displays a list of all the keys in the dictionary Syntax dictionary.keys() Parameter NA Return This method returns a view object that displays...

2 minutes read.

Python pynmea2

Define Pynmea2 Python Programming Language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in...

4 minutes read.

How to Configure Python Interpreter in Eclipse

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

Python Array

Python Array In the programming language or computer science, an array is defined as the form of a data structure which consists of or store collection of various types of elements...

10 minutes read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

Python SQLite

SQLite It is an RDBMS (Relational Database Management System). It is an embedded, serverless, transactional SQL database engine. It is an open-source application. It is named SQLite because of its lightweight....

3 minutes read.

Python Parallel Processing

By performing more jobs concurrently, your software may complete more tasks in a shorter amount of time. These aid in solving major issues. The following subjects will be covered in...

2 minutes read.

Python Random Module

The tutorial for the Python random module demonstrates how to produce pseudo-random integers in Python. Random Number Generator (RNG) The RNG (random number generator) generates a series of values with no discernible...

8 minutes read.

Data Distribution in python

Before discussing data distribution, we will discuss each word that is data and Distribution. What is meant by data? A collection of raw facts and figures is called data. The word "raw"...

18 minutes read.

Python Interpreter

In this tutorial, we will go through the basic knowledge about what an interpreter is and how we use it in the python programming language. It is one of the...

3 minutes read.

Multiple Linear Regression using Python

Linear Regression: Linear regression is a method that models the relationship between a dependent variable and one or more independent variables; in other terms, that models the relationship between a target...

6 minutes read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.