This article was first posted on Jizhi Column

With just 10 lines of Python code, we can implement object 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 the 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"])Copy the code

Yes, with just 10 lines of code, you can implement target detection techniques that are widely used in AI products today.

After looking at the code, let’s talk about the technical background behind object detection and explain the origin and implementation of these 10 lines of Python code.

Introduction to Target Detection

An important field of artificial intelligence is computer vision, the science of computers and software systems recognizing and understanding images and video. Computer vision includes many subdivision directions, such as image recognition, object detection, image generation and image super-resolution. Among them, target detection has the most far-reaching significance in the field of computer vision because of its wide application.

Object detection refers to the ability of computers and software systems to locate and identify objects in images/frames. Target detection technology has been widely used in face detection, vehicle detection, traffic statistics, network images, security systems and unmanned vehicles and other fields. Like other computer vision technologies, object detection will further become an important part of artificial intelligence in the future and has a broad development prospect.

However, it is not straightforward to use modern target detection methods in software applications and systems and to create applications based on these methods. Early target detection implementation is mainly the application of some classical algorithms, such as the algorithm supported in OpenCV. However, the performance of these algorithms is not stable and varies greatly from case to case.

In 2012, the breakthrough of deep learning technology gave rise to a number of highly accurate target detection algorithms such as R-CNN, FAST-RCNN, ftP-RCNN, RetinaNet and Fast and accurate SSD and YOLO. Using these deep learning-based methods and algorithms requires an understanding of a number of mathematical and deep learning frameworks. Millions of developers around the world are using object detection technology to create new products and projects, but there are still many people who don’t get the hang of it because of the complexity of understanding and using it.

To address this problem, computer vision expert Moses Olafenwa led a team that launched the Python library ImageAI, which allows developers to easily apply state-of-the-art computer vision technology to their projects and products with just a few lines of code.

The 10 lines of code we showed at the beginning of the implementation use ImageAI.

How to implement object detection easily with ImageAI

Using ImageAI to perform object detection, you only need to perform 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

Let’s go through it step by step.

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
Copy the code

2.Numpy

pip install numpy
Copy the code

3.SciPy

pip install scipy
Copy the code

4.OpenCV

pip install opencv-python
Copy the code

5.Pillow

pip install pillow
Copy the code

6.Matplotlib

pip install matplotlib
Copy the code

7.H5py

pip install h5py
Copy the code

8.Keras

pip install keras
Copy the code

9.ImageAI

pip install
Copy the code

github.com

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

At this point we have all our dependencies installed and are ready to write our first object detection code. Create a Python file, name it like firstdetection.py, then write the following code to the file and copy the RetinaNet model file with the images you want to detect into 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 the 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"])Copy the code

Then run the code and wait for the console to print the results. Once the console prints out the results, open the folder where firstdetection.py is located and you’ll see the new images saved there. For example, the two sample images below and the two new images saved after target detection.

Before target detection:

We can see that the image shows the names and probabilities of the detected objects.

Read 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()
Copy the code

In the 3 lines above we import the ImageAI target detection class in the first line, the Python OS class in the second line, and a variable in the third line that gets the path to our Python files, RetinaNet model files, and image folders.

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "Resnet50_coco_best_v2. 0.1 the 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"))
Copy the code

In the 5 lines above we define our target detection class in line 1, set the model type of RetinaNet in line 2, set the model path to the path of RetinaNet model in line 3, load the model into the target detection class in line 4, then we call the detection function in line 5, And in the input and output image path for parsing.

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

In two lines of code above, we iterate the first line of the detector. The detectObjectFromImage function returns all of the results, and then print out in the second line model for each object in the image of test results (name and probability).

ImageAI supports a number of powerful object detection customizations, one of which is the ability to extract an image of every object detected on the image. Just by parsing the additional parameter extract_DETECted_objects =True into the detectObjectsFromImage function, as shown below, the object detection class creates a folder for the image objects, extracts each image, and saves them in the newly created folder, And returns an additional 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)
Copy the code

Using the first image above as an example, we can get separate images of each object detected in the image:

ImageAI provides a number of capabilities that can be used for both custom and production deployment of a variety of target detection tasks. Include:

– Adjusted minimum probability: by default, objects with a probability of less than 50% will not be displayed, you can adjust this number if necessary.

– 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 “Fast”, “Faster” and “fastest”.

– Input/output types: You can customize the image path, Numpy array or image file stream as input/output.

Admittedly, each line of these 10 lines of code is not amazing, and many libraries are used, but with only 10 lines of code, we can easily achieve the previously troublesome target detection, and we can still talk about the word “geili”.

For more features and details, check out GitHub

Resources: towardsdatascience.com


0806 artificial Intelligence – From Scratch to Mastery

Limited time discount!

Click here for details

What about online programming? (The first 25 students can also get ¥200 coupon)