With just 10 lines of Python code, we can realize the target detection imageai in computer vision

With just 10 lines of Python code, we can realize the target detection imageai in computer vision

With just 10 lines of Python code, we can achieve target detection in computer vision.

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
 
 

That's right, with these just 10 lines of code, you can realize the target detection technology widely used in AI products.

After reading the code, let us talk about the technical background behind target detection, and interpret the origin and implementation principles of these 10 lines of Python code.

Introduction to object detection

An important field of artificial intelligence is computer vision, which refers to the science of computers and software systems that recognize and understand images and videos. Computer vision includes many subdivision directions, such as image recognition, target detection, image generation, and image super-resolution. Among them, target detection has the most far-reaching significance in the field of computer vision due to its wide range of uses.

Target detection refers to the ability of computers and software systems to locate objects in images/screens and recognize them. Target detection technology has been widely used in fields such as face detection, vehicle detection, human flow statistics, network images, security systems, and unmanned vehicles. Like other computer vision technologies, target detection will further become an important part of artificial intelligence in the future, with broad prospects for development.

However, using modern object detection methods in software applications and systems and creating applications based on these methods is not straightforward. The early realization of target detection was mainly the application of some classic algorithms, such as those supported in OpenCV. However, the performance of these algorithms is not stable and varies greatly in different situations.

The breakthrough of deep learning technology in 2012 gave birth to a large number of highly accurate target detection algorithms, such as R-CNN, Fast-RCNN, Faster-RCNN, RetinaNet, and fast and accurate SSD and YOLO. To use these deep learning-based methods and algorithms, you need to understand a lot of mathematics and deep learning frameworks. There are millions of developers all over the world who are creating new products and new projects with the help of target detection technology. However, due to the complicated and difficult understanding and use, many people still have no idea.

In order to solve this problem that plagues developers, computer vision expert Moses Olafenwa led the team to launch the Python library ImageAI, which allows developers to easily apply the most advanced computer vision technology to their projects with just a few lines of code. And in the product.

The 10-line code implementation shown at the beginning is to use ImageAI.

How to easily achieve target detection with the help of ImageAI

Using ImageAI to perform target detection, you only need the following 4 steps:

1. Install Python on your computer

2. Install ImageAI and its environment dependencies

3. Download the target detection module file

4. Run the sample code, which is the 10 lines we showed

Below we explain step by step in detail.

1) Download and install Python 3 from the Python official website

python.org/

2) Install the following environment dependencies through pip

1.Tensorflow

pip install tensorflow
 
 

2.Numpy

pip install numpy
 
 

3. SciPy

pip install scipy
 
 

4.OpenCV

pip install opencv-python
 
 

5.Pillow

pip install pillow
 
 

6.Matplotlib

pip install matplotlib
 
 

7.H5py

pip install h5py
 
 

8.Keras

pip install keras
 
 

9.ImageAI

pip install
 
 

github.com

3) Download the RetinaNet model file through this link for target detection.

At this point we have installed all the dependencies and are ready to write our first target detection code. Create a Python file, name it (such as FirstDetection.py), then write the following code to the file, and then copy the RetinaNet model file and the image you want to detect to the folder containing the Python file.

FirstDetection.py

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))

for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
 
 

Then run the code and wait for the console to print the results. After the console prints the results, you can open the folder where FirstDetection.py is located, and you will find that there are new images saved in it. For example, the following two sample images and two new images saved after performing target detection.

Before target detection:

 

 

After target detection:

 

 

 

We can see that the name and probability of the detected object are displayed on the image.

Decipher 10 lines of code

Let's explain how these 10 lines of code work.

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()
 
 

In the above 3 lines of code, we import the ImageAI target detection class in the first line, import the Python os class in the second line, and define a variable in the third line to obtain the Python file, RetinaNet model file and image leading to us The path of the folder.

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
 
 

In the above 5 lines of code, we define our target detection class in the first line, set the RetinaNet model type in the second line, set the model path to the path of the RetinaNet model in the third line, and load the model to In the target detection class, then we call the detection function in the fifth line and parse it in the input and output image paths.

for eachObject in detections:
print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
 
 

In the above two lines of code, we iterate over all the results returned by the detector.detectObjectFromImage function in the first line, and then print out the detection results (name and probability) of each object on the image by the model in the second line.

ImageAI supports many powerful custom functions for target detection, one of which is the ability to extract the image of each object detected on the image. Simply parse the additional parameter extract_detected_objects=True into the detectObjectsFromImage function, as shown below, the target detection class will create a folder for image objects, extract each image, save them in the newly created folder, and return a containing Extra array of paths through each image.

detections, extracted_images = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"), extract_detected_objects=True)
 
 

We use the previous first image as an example to get a separate image of each object detected in the image:

 

 

ImageAI provides many functions, which can be used for customization and production deployment of various target detection tasks. include:

-Adjust the minimum probability: objects with a probability of less than 50% will not be displayed by default. If necessary, you can adjust this number yourself.

-Custom target detection: Using the provided CustomObject class, you can detect one or more specific objects.

-Adjust the detection speed: you can adjust the detection speed by setting the detection speed to three different levels: "fast", "faster" and "fastest".

-Input and output type: You can customize the path of the image, Numpy array or image file stream as input and output.

It is true that looking at each line of these 10 lines of code alone is not amazing, and a lot of libraries are also used, but only 10 lines of code can allow us to easily achieve the troublesome target detection before, and it can still be said to be "powerful" Two words.

For more features and details, please check GitHub

Reference materials: towardsdatascience.com


Author: King slightly set of intellectual
link: https://juejin.im/post/5b5445495188251acc230ac9
Source: Nuggets
copyright reserved by the authors. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.