Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to complete the image recognition function with 10 lines of code in python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to complete the image recognition function through 10 lines of code in python. Xiaobian thinks it is quite practical, so share it with you as a reference. I hope you can gain something after reading this article.

ImageAI is a python library designed to enable developers to build applications and systems with deep learning and computer vision capabilities using a few simple lines of code.

ImageAI installation jobs

To perform object detection using ImageAI, all you need to do is:

Install Python on your computer system Install ImageAI and its dependencies Download the object detection model file Run the sample code (only 10 lines) So let's start now:

Download and install Python 3 from the official Python language website. Installation via pip: TensorFlow, OpenCV, Keras, ImageAI

pip3 install tensorflowpip3 install opencv-pythonpip3 install keraspip3 install imageai --upgrade

3)Download the RetinaNet model file for object detection via the link in this article:

https://towardsdatascience.com/object-detection-with-10-lines-of-code-d6cb4d86f606

run the program

That's great. Now that we have the dependencies installed, we can write our first object detection code. Create a Python file and give it a name (e.g., FirstDetection.py), then write the following code into it. Copy the RetinaNet model file image to be detected to the folder containing the python files.

from imageai.Detection import ObjectDetectionimport osexecution_path =os.getcwd()detector = ObjectDetection()detector.setModelTypeAsRetinaNet()detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h6"))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"] )

Note that if you encounter this error while running:

ValueError: Unable to import backend : theano python mymodel.py

Then you can try:

import osos.environ['KERAS_BACKEND'] = 'tensorflow'fromimageai.Detection import ObjectDetection

Then run the code and wait for the results to print in the console. Once the results are printed to the console, go to the folder where your FirstDetection.py is located and you'll find a new image saved. Take a look at the two image samples below and the new images saved after detection.

Before Detection: How to Complete Target Detection with 10 Lines of Code After Detection: How to Complete Target Detection with 10 Lines of Code Data Results

We can see that the program prints out some probability data for each object:

person : 55.8402955532074person : 53.21805477142334person : 69.25139427185059person : 76.41745209693909bicycle : 80.30363917350769person : 83.58567953109741person : 89.06581997871399truck : 63.10953497886658person : 69.82483863830566person : 77.11606621742249bus : 98.00949096679688truck : 84.02870297431946car : 71.98476791381836

It can be seen that the program can detect the following objects in the image:

People, bicycles, trucks, cars, buses.

You can directly want to detect their own photos into the program to run inside to see the effect.

Principle explanation

Now let's explain how 10 lines of code work.

from imageai.Detection import ObjectDetectionimport osexecution_path= os.getcwd()

In the above 3 lines of code, we import the ImageAI object detection class in the first line, the python os class in the second line, and define a variable to hold the path to the folder where the python file, RetinaNet model file, and image are located.

detector = ObjectDetection()detector.setModelTypeAsRetinaNet()detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h6"))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 code above, we define the object detection class in the first line, set the model type to RetinaNet in the second line, set the path of the model path in the third line RetinaNet model, which is loaded into the object detection class in the fourth line, then we call the detection function, which parses the path of the input image and the path of the output image in the fifth line.

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

In the code above, we iterated over all the results returned by the detector.detectObjectsFromImage function on the first line, and then printed out the model name and percentage probability of each object detected in the image on the second line.

About "how to complete the image recognition function through 10 lines of code in python" This article is shared here. I hope the above content can be of some help to everyone so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report