Build an Image Prediction Script with Python & ImageAI
This kind of ImageAI walkthrough is useful because it turns a model demo into a runnable workflow, but the real engineering value begins once the script is embedded in a broader Python application stack. The main dependency chain is not just the library import: it also includes model files, image inputs, runtime packages, and the local execution path. That means packaging, file placement, and environment consistency matter as much as the classification call itself.
There is also an important architectural trade-off in choosing a pre-trained model. Smaller architectures reduce download size and usually improve startup and inference speed, while larger ones may improve recognition quality at the cost of compute and latency. For teams thinking beyond a notebook or terminal test, that trade-off affects whether the workload fits a laptop, a container, or a more controlled inference service. It also influences how quickly the workflow can be repeated across environments.
From a software delivery perspective, the script highlights a common pattern in ML-enabled tools: model loading is a runtime dependency, not a one-time setup step. That creates operational considerations around version control for weights, reproducibility of outputs, and the handling of warnings or model mismatches during execution. If the script is reused in automation, the surrounding code should make file paths, model choice, and result thresholds explicit rather than implicit.
For developers and technical leads, the practical lesson is that image classification is only partially about AI logic. The surrounding implementation determines whether the solution is maintainable, portable, and testable. Clean separation between preprocessing, model selection, inference, and output formatting makes it easier to swap architectures later, benchmark accuracy versus latency, and integrate the classifier into a larger application or pipeline.
Today, only a few buzzwords have captivated our imagination as profoundly as Artificial Intelligence (AI) and Machine Learning (ML) which have now become integral components of our everyday lives. However, to many, the question remains, โWhat is AI or ML, and how can we use it to create something practical?
In this article, we take a foundational look at AI and ML, itโs basic concepts, and explore a use case of how we can leverage these technologies to build a simple, yet practical image prediction python script with the ImageAI library.
Background
First, letโs briefly demystify some essential concepts that we mention throughout this article.
Artificial Intelligence (AI)
Artificial Intelligence is the simulation of human intelligence in machines that are programmed to think and learn like humans. It encompasses various subfields, including machine learning.
Machine Learning (ML)
Machine Learning is a subset of AI that focuses on the development of algorithms and statistical models that enable computers to improve their performance on a specific task through learning from data.
Deep Learning(DL)
Deep learning is a subset of Machine Learning that revolves around artificial neural networks. A deep learning model typically refers to a neural network with multiple layers used for tasks such as image recognition, natural language processing and speech recognition.
Image Prediction
Image prediction is the process of using AI and ML techniques to identify and classify objects or content within images.
ImageAI
ImageAI is a Python library that simplifies image recognition and prediction tasks by providing pre-trained models and a user-friendly interface.
Prerequisites
- A basic understanding of Python programming with version 3 or greater installed
- Make sure to install the ImageAI library. You can install it by running the following pip command โ
pip install imageai
- Gather some images you want to perform predictions on. You can use your own images or find some online.
- You can also clone my personal GitHub repo for additional resource to follow along with โ
Objectives
- Understand the basic concepts of AI and ML
- Use ImageAIโs pre-trained models for image prediction
- Interpret the predictions and probabilities generated by the model
Step 0โ Install Necessary Libraries and Dependencies
First, letโs set up our environment by installing the needed dependencies from the โrequirements.txtโ and โrequirements_gpu.txtโ docs. Run the following commands โ
pip install -r requirements.txt
pip install -r requirements_gpu.txt
Step 1 โ Import Necessary Libraries and Set Execution Path
Now, we need to import the required libraries into our script, including ImageAI and os, then set the execution path to your current working directory to ensure the script can locate and load the files and models in current directory when executed.
You can accomplish this by the code below โ
from imageai.Classification import ImageClassification
import os
execution_path = os.getcwd()
Step 2 โ Choose Your Algorithm and Load the Model
Now, we have the option to choose from the four different image processing algorithms provided by ImageAI. You can automatically download one of algorithm path files by clicking on one of the links below
- MobileNetV2 โ 4.82 mb, fastest prediction time but moderately accurate
- ResNet50 โ by Microsoft Research, 98 mb, fast prediction time and high accuracy
- InceptionV3 โ by Google Brain team, 91.6 mb, slow prediction time and higher accuracy
- DenseNet121 โ by Facebook AI Research, 31.6 mb, slower prediction time and highest accuracy
Each has its own characteristics in terms of file size, prediction speed and accuracy. You are free to choose the one that suits your needs best.
For example, if you prefer higher accuracy over speed, go with DenseNet121.
In the article we will use MobileNetV2 due to its compact size of 4.82mb โ
prediction = ImageClassification()
prediction.setModelTypeAsDenseNet121() # Choose your desired algorithm here
prediction.setModelPath(os.path.join(execution_path, "model_file.pth")) # Path to the model file
prediction.loadModel()
In the code block above, we first create an instance of the ImageClassification class which will be used for the image classification tasks, then we specify the algorithm method the model type will use โ setModelTypeAsMobileNetV2().
Next, we choose the MobileNetV2 architecture as our model of choice. However, feel free to explore other model options by utilizing the corresponding algorithm method that best aligns with your specific needs and preferences.
Then, set the path to the pre-trained model file which contains the pre-trained weights and architecture for the selected deep learning model, using prediction.setModelPath() as the os.path.join() function constructs the full file path based on the previously set execution path execution_path.
Finally, we call the loadModel() method to load the pre-trained model as it initializes the model from the model file, making it ready for making predictions on the provided images.
Replace model_file.pth with the actual path to the pre-trained model file that you intend to use for your image classification task.
Step 3: Perform Image Prediction
Now, itโs time to perform the image prediction. You can replace house.jpg with the path to the image you want to predict โ
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "house.jpg"), result_count=5)
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction, " : ", eachProbability)
The classifyImage() method takes an image file as input, in this case, house.jpg, which is located in the current working directory. It then, analyzes the image and generates predictions for its content. Specifically, it identifies objects or categories present in the image and assigns a confidence probability to each prediction.
The results of the image classification are stored in two variables, predictions and probabilities. Predictions contain the labels or names of the objects or categories that the model predicts to be in the image, while probabilities contain the corresponding confidence scores for each prediction.
The for loop iterates through these predictions and probabilities using the zip() function, allowing us to pair each prediction with its associated confidence score. For each prediction, it prints both the label and the confidence score, for e.g โ cat : 90. This information provides insights into the modelโs assessment of the imageโs content, indicating which objects it recognizes and how confident it is in its predictions.
Step 4: Run the Python Script
Weโre down to our final step! In your terminal/CLI, navigate to the directory that contains the python script and all other files, then run the following command using the name of your Python file to execute the image prediction scriptโ
python3 ai-image-processsing.py
You will receive some warnings which we will ignore at this time. However, at the tail end of the output, you will the predictions of the image given as seen in the image below โ
The image processing mode used was able to have a 97.1359% confidence score that the image in the house.jpg file is a boathouse.
Remember, this is according to the algorithm model we used which is not as accurate as the others. However, it does recognize that is it a house ๐
Congratulations!
Youโve successfully completed โPythonic Predictionsโ. Weโve utilized the power of AI/ML to create a simple image prediction Python script.
For more information about ImageAI check out the official GitHub repo below โ
Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.
