Object Detection Using Python, AI and Machine Learning

🤖 1. Introduction

Object Detection is a practical application of Artificial Intelligence (AI) and Machine Learning (ML). In this project, we will build a Python application that can examine an image and identify objects present in it.

🔍 What will this project do?

  • 🖼️ Analyze images and look for recognizable objects.
  • 🎯 Detect objects and mark their locations in the image.
  • 🧠 Use a pre-trained AI/ML object detection model to perform the detection.
  • 🐍 Use Python to build and run the application.
  • 📦 Display the detected objects together with their bounding boxes and labels.
🚀 What you will see: After running the program, you can provide an image and watch the application identify objects in it. This makes the project easy to test and gives you a practical introduction to how AI-based computer vision works.

💡 Tip: Try the program with different images. Seeing how the model responds to real images is often the most interesting part of learning object detection.

🛠️ 2. What You Will Build

In this project, you will build a simple Object Detection application using Python. The application will analyze an image, identify objects that it recognizes, and display the results directly on the image.

🎯 What happens when you run the program?
  • 🖼️ The program reads an input image.
  • 🧠 The AI model analyzes the image.
  • 🔎 Recognized objects are detected.
  • 📦 Bounding boxes are drawn around the detected objects.
  • 🏷️ Object names and detection information are displayed.
👀 The final result

Instead of only reading about object detection, you will be able to run the application and see the detected objects yourself.

🚀 Ready to try it?

Once you see the result, you can experiment with your own images and observe which objects the AI model can recognize.

💻 3. Project Requirements

Before running the project, make sure your computer has the following:

🧰 Software Requirements

  • 🪟 Windows
  • 🐍 Python
  • 📝 VS Code or Command Prompt
  • 📦 Required Python packages
  • 🧠 Object detection model/files included with the project
⚠️ Important: Python Version

If multiple Python versions are installed on your computer, specify the Python version when running the program. For this project, the instructions below use Python 3.12, which was used to test the project.

▶️ Run the application

Open Command Prompt in the project folder and run:

py -3.12 object_detection.py
💡 Why use py -3.12?

When more than one Python version is installed, the command py -3.12 explicitly tells Windows to use Python 3.12. This helps prevent the program from accidentally running with another Python installation.

📥 4. Download the Project Files

Before installing the Python package, download the project files provided with this tutorial and place them in a convenient location on your computer.

📦 Step 1: Download the Object Detection Project

Click the button below to download the complete Object Detection project as a ZIP file.

📁 Step 2: Check the Project Folder

After downloading the ZIP file, extract it and open the project folder. You should see the Python program and the sample images provided with this project.

Object_Detection
├── object_detection.py
└── sample_images
🔎 Check before continuing:

Make sure the Python program is present in the project folder and that the sample images are available inside the sample_images folder. Keep the folder structure unchanged unless the instructions specifically ask you to move a file.

➡️ Next: Once the project files are ready, we can install the Python package required to run the application.

🐍 5. Check Python Installation

Before installing the required packages, let’s confirm that Python 3.12 is available on your computer.

▶️ Step 1: Open Command Prompt

Open Command Prompt and run the following command:

py -3.12 –version

✅ Expected Result

Python 3.12.x

The exact last digits may be different. For example, Python 3.12.6 is also a valid result.

💡 If you have multiple Python versions

The command py -3.12 specifically checks Python 3.12. This is useful if Python 3.14 or another version is also installed on your computer.

➡️ Ready to continue? If Python 3.12 is displayed, you can proceed to install the packages required for the Object Detection project.
📦 6. Install the Required Packages

This Object Detection project uses the Ultralytics package to load the YOLO model and perform object detection. Open Command Prompt and install the required package using the command below.

⚠️ Important
Use py -3.12 -m pip instead of simply pip. This ensures that the package is installed for Python 3.12.
🔵 Ultralytics (YOLO)
C:\>   py -3.12 -m pip install ultralytics

The Ultralytics package provides the YOLO model (yolo26n.pt) and the necessary components to perform AI-based object detection. It will automatically install its required dependencies.

✔ No Separate Installation Required

The project also uses the following Python standard library modules, which are included with Python and do not normally require separate installation:

  • tkinter – for the file selection dialog
  • pathlib – for handling image file paths
  • os – for opening the output image automatically
ℹ️ First Run Note

When you run the program for the first time, the YOLO model file (yolo26n.pt) may be downloaded automatically. This can take some time depending on your internet connection. Subsequent runs should be faster.

✔ After Installation

Once the package is installed successfully, you can proceed to the next step: opening the project folder and running the Object Detection application.

🔎 7. Verify the Package

Before running the application, it is a good idea to check that the Ultralytics package was installed correctly.

👁️ Check Ultralytics
py -3.12 -m pip show ultralytics

If the package is installed, its package information and installed version will be displayed in the Command Prompt.

🧪 Quick Import Test

You can also check whether Python can import the Ultralytics package without producing an error:

py -3.12 -c “from ultralytics import YOLO; print(‘Ultralytics imported successfully’)”
✅ Expected Result

If you see Ultralytics imported successfully without an error message, the main package installation is ready.

ℹ️ What About Tkinter, pathlib and os?

The project also uses tkinter, pathlib, and os. These are Python standard-library modules and normally do not require separate installation with pip.

⚠️ If an Error Appears

If the import test displays an error, check the installation command and make sure that Ultralytics was installed for Python 3.12.

📂 8. Open the Project Folder

Now open the folder where you extracted the Object Detection project files. We will open Command Prompt directly in this folder.

🖱️ Follow these steps:

  1. Open the Object Detection project folder in File Explorer.
  2. Click once inside the address bar at the top of File Explorer.
  3. Type:
cmd
  1. Press Enter.
✅ What happens now?

A Command Prompt window will open with the project folder already selected as the current location.

🖥️ Example

Command Prompt opened directly in the Object Detection project folder

Command Prompt opened directly in the project folder.

💡 Beginner Tip

You do not need to manually navigate through folders using Command Prompt. Opening cmd from the File Explorer address bar takes you directly to the correct folder.

🚀 9. Run the Object Detection Program

Everything is now ready. Let’s run the application and see Object Detection in action!

▶️ Step 1: Run the program

In the Command Prompt opened in your project folder, enter the following command:

py -3.12 object_detection.py
⌨️ Step 2: Start the application

Press Enter to start the program. Wait a few moments while the application loads the object detection model and processes the image.

🎯 Step 3: See the result

The detected objects should now appear in the output, with bounding boxes and object labels drawn around the objects identified by the model.

📸 Object Detection Output

Object Detection application output showing detected objects with bounding boxes and labels

Example output from the Object Detection application.

🎉 Your AI project is running!

Try the program with different images and observe which objects the model can detect. Once you have tested the application, we can look at how the detection process works internally.

🧪 10. Test the Project

Now comes the most interesting part — experiment with the application yourself! Instead of immediately studying how the model works internally, try different images and observe the detection results.

🔬 Your experiment

Run the program with different test images and check whether the expected objects are detected. Pay attention to the object name, bounding box, and confidence value shown in the output.

🖼️ Try these test images

  • 🧑 Person
  • 🚗 Car
  • 🚲 Bicycle
  • 🍾 Bottle
  • 🐕 Dog
  • 🐈 Cat
  • 🐦 Bird
  • 🌳 Tree
  • 🌍 Multi-object image
▶️ How to test
  1. Select one of the test images.
  2. Place the image in the location expected by the program.
  3. Run the Object Detection program.
  4. Observe the detected objects and their bounding boxes.
  5. Repeat the experiment with another image.
👀 What should you observe?
  • Which objects were detected?
  • Were the objects correctly identified?
  • Where were the bounding boxes placed?
  • Did the confidence value change between images?
  • How did the model perform when several objects appeared in the same image?
⭐ Try the multi-object image last

After testing individual objects, try an image containing multiple objects. This gives you a better idea of how an object detection model can locate several objects within the same image.

📸 Test Result

Object detection test result showing detected objects with bounding boxes

Example result obtained while testing the Object Detection project.

🧠 Ready to understand what happened?

Once you have experimented with the application, let’s take a closer look at how the object detection process works.

🧪 Test the Object Detection Application

👤 Test 1 – Detect a Person

Input: Use a suitable image containing a person.

Action: Run the Object Detection program and observe the output.

Observe: Check whether a bounding box and the corresponding object label appear around the person.

🚗 Test 2 – Detect a Car

Input: Use the car test image.

Output: Observe whether the car is detected and marked with a bounding box.

🚲 Test 3 – Detect a Bicycle

Input: Use the bicycle test image.

Output: Check whether the bicycle is correctly identified and highlighted.

🍼 Test 4 – Detect a Bottle

Input: Use the bottle test image.

Output: Observe whether the bottle is detected and enclosed by a bounding box.

🐾 Test 5 – Detect an Animal

Try the dog, cat, or bird images supplied with the project.

Check which animals the model recognizes and observe the bounding boxes and labels.

🌍 Test 6 – Detect Multiple Objects

This is the most interesting test because several objects appear in the same image.

Use your multi-object image containing:

👤 Person  •  🚗 Car  •  🍾 Bottle  •  🐕 Dog  •  🐈 Cat  •  🚲 Bicycle

Run the program and observe how the model detects several objects in a single image.

👀 What should you look for?
  • Is the object identified correctly?
  • Is the bounding box placed around the object?
  • What confidence value is displayed?
  • Can the model detect more than one object at the same time?

📸 Multi-Object Detection Result

Multi-object detection result showing several detected objects with bounding boxes

Example result showing multiple objects detected in one image.

🧠 What did we just observe?

A single image can contain one or many objects, and the model attempts to identify each recognizable object separately. Now that you have seen the results, let’s understand how the Object Detection process works.

🖼️ 11. Try Your Own Images

Now it is time to go beyond the sample images. Try the Object Detection application with images of your own.

🚀 Your next experiment

Replace the sample image with an image of your choice and run the program again. Observe which objects the model recognizes and how they are marked in the output.

🔄 How to try another image

  1. Choose an image from your computer.
  2. Copy the image into the project folder or the image folder used by the program.
  3. If required, change the image filename in the Python program.
  4. Run the application again and examine the result.

🔬 Images you can experiment with

  • 🏫 A classroom
  • 🛣️ A street scene
  • 👨‍👩‍👧 A family or group photo
  • 🚗 A vehicle
  • 🐕 Animals
  • 🌍 An image containing multiple objects
👀 Observe the differences

Different images may produce different results. Look at the objects detected, the position of the bounding boxes, and the confidence values shown by the model.

💡 Remember

The model may not recognize every object in every image. Detection results can vary depending on the objects present, their size, visibility, image quality, and the classes supported by the trained model.

🧠 Mini Challenge

Find an image containing several different objects and see how many of them the model can identify. This simple experiment is a great way to understand the practical side of AI-based object detection.

🧠 12. Understanding the Detection Result

You have already run the program and seen the detection results. Now let’s understand the main terms used to describe what you see on the output image.

📦 Bounding Box

A bounding box is a rectangle drawn around a detected object. It shows approximately where the object is located in the image.

🏷️ Object Label

The object label is the name assigned to an object detected by the model.

For example:

person car dog bottle

📊 Confidence Score

The confidence score is a numerical value that indicates how strongly the model supports a particular detection.

Example: A result such as dog – 92% means the model assigned a confidence value of 92% to that detection.

The confidence score is not a guarantee that the detection is correct. It is simply the model’s confidence value for that particular prediction.

🔗 Put the three together

When you look at an output image, think of it this way:

📦 Bounding Box   +   🏷️ Object Label   +   📊 Confidence Score

Together, these provide the main information about an object’s detection in the image.

➡️ Next: How does the model detect objects?

Now that you understand the information shown in the result, we can take a brief look at what happens behind the scenes.

⚙️ 13. How the Program Works

You have already seen the application detect objects in images. Now let’s take a simple look at what happens inside the program.

🖼️ Input Image
📖 Read Image
🧠 Object Detection Model
🔎 Identify Objects
📊 Calculate Detection Results
📦 Draw Bounding Boxes
🖥️ Display Output Image
1️⃣ Input Image

The program receives an image that contains one or more objects that we want the model to analyze.

2️⃣ Read the Image

Python and OpenCV read the image and prepare it for processing by the detection model.

3️⃣ Apply the Object Detection Model

The pre-trained model analyzes the image and looks for objects belonging to the classes it has learned to recognize.

4️⃣ Identify Objects

For each detected object, the model produces information such as its class and location in the image.

5️⃣ Calculate Detection Results

The program processes the model’s results and determines which detections should be displayed.

6️⃣ Draw Bounding Boxes

OpenCV draws rectangles around the detected objects and adds labels and other detection information.

7️⃣ Display the Output Image

Finally, the processed image is displayed so you can see the detected objects, their labels, and bounding boxes.

💡 In simple terms

The program takes an image, sends it through a trained object detection model, receives the detected objects, and then uses OpenCV to present those results visually.

🐍 14. Important Parts of the Python Program

The complete Python program contains several lines of code, but you do not need to understand every line at this stage. Let’s look at the main parts that perform the detection.

🧠 1. Loading the Object Detection Model

The program first loads the pre-trained object detection model and the files required by that model.

# Load the object detection model net = cv2.dnn.readNetFromDarknet(config_file, weights_file)

This prepares the trained model so that it can analyze the input image and produce object detection results.

🖼️ 2. Reading the Input Image

The input image is loaded using OpenCV so that it can be processed by the detection model.

image = cv2.imread(image_path)

The image is now available to the program for further processing.

🔎 3. Running Object Detection

This is the main stage where the image is passed to the object detection model. The model analyzes the image and produces possible object detections.

blob = cv2.dnn.blobFromImage( image, 1 / 255.0, (416, 416), swapRB=True, crop=False ) net.setInput(blob) outputs = net.forward(output_layers)

The model processes the image and returns detection information that the program can examine.

📊 4. Processing the Detection Results

The program examines the model’s output and extracts useful information such as the detected class, confidence value, and object location.

Only detections that satisfy the program’s selected criteria are used for the final display.

📦 5. Drawing the Detection Results

OpenCV is then used to draw a bounding box around each selected detection and display its label.

cv2.rectangle(image, (x, y), (x + w, y + h), …) cv2.putText(image, label, (x, y – 5), …)

This is the stage that produces the visual result you saw during testing.

🖥️ 6. Displaying the Final Image

Finally, the processed image is displayed using OpenCV, allowing you to see the detected objects, labels, and bounding boxes.

🔗 The important code flow
🧠 Load Model  →  🖼️ Read Image  →  🔎 Detect  →  📊 Process  →  📦 Draw  →  🖥️ Display
💡 Beginner Tip

You do not need to memorize these code sections. First understand the overall flow and then experiment with the program. You can return to the individual code blocks whenever you want to study them in more detail.

🔍 15. What Happens When an Object Is Not Detected?

Object detection does not always produce a detection for every object in an image. This is a useful opportunity to experiment with situations that make detection more difficult.

🧪 Try these difficult images

  • 🔹 A very small object
  • 🌫️ A blurry image
  • 🌑 A dark or poorly illuminated image
  • 📐 An object viewed from an unusual angle
  • 👤 An object that is partially hidden
👀 What should you observe?

Run the program with these images and compare the results. You may find that some objects are detected while others are missed or receive lower confidence values.

🧠 Why can this happen?

The model may fail to detect an object when it is too small, partially hidden, blurred, poorly illuminated, difficult to recognize from the given viewpoint, or outside the object classes supported by the model.

⚠️ No detection does not always mean a program error

If an object is not detected, first check the image and the model’s supported classes. An object can be clearly visible to a person but still be difficult for the trained model to recognize.

🎯 A realistic AI experiment

These experiments show an important practical point: AI-based object detection is not perfect. Its results depend on the model, the image, and the conditions under which objects appear.

📦 16. Packages and Modules Used in This Project

The project uses one main AI package along with a few built-in Python modules for handling the user interface, files, and output.

Package / Module Purpose
Ultralytics Provides the YOLO model and performs the AI-based object detection.
tkinter Provides the graphical file-selection window used to choose an input image.
pathlib Handles file and image paths and helps create the output filename.
os Provides operating-system functions, including opening the generated result image on Windows.
🧠 Where does the AI come from?

The main AI functionality is provided by Ultralytics YOLO. The program loads the YOLO model file and uses model.predict() to perform object detection on the selected image.

💡 In simple terms

Ultralytics performs the AI-based detection, while tkinter, pathlib, and os help the Python program interact with the user and manage files and results.

🧠 17. AI and Machine Learning Behind the Project

The application you have just tested is based on Artificial Intelligence (AI) and Machine Learning (ML). At a high level, the object detection model analyzes an image and looks for objects it has been trained to recognize.

🔍 What is Object Detection?

Object detection is a computer vision task in which an AI model identifies objects in an image and determines approximately where those objects are located.

Unlike simple image classification, which may assign a label to an entire image, object detection can identify multiple objects and locate them using bounding boxes.

⚙️ Simplified Detection Process

🖼️ Image 🧠 AI Model 🏷️ Objects + 📍 Locations + 📊 Confidence
🏷️ Objects

The model identifies object classes such as person, car, dog, or bottle.

📍 Locations

The model provides information used to determine where each detected object appears in the image.

📊 Confidence

A confidence value is associated with each detection produced by the model.

💡 That’s enough for this project

You do not need to study neural networks, tensors, training algorithms, or mathematical details to run this project. The practical experiments above give you the foundation needed to explore those topics later.

📚 Want to go deeper?

A separate article can explore the technology in greater detail, including how object detection models are trained and how they identify objects and their locations.

Suggested future article:
How Object Detection Works in AI and Machine Learning

🎯 18. Conclusion

In this project, we built and tested a simple Object Detection application using Python. We installed the required packages, ran the application, tested it with different images, and observed how the model identifies objects using labels, bounding boxes, and confidence values.

The best way to continue learning is to experiment with your own images. Try different scenes, compare the results, and observe where the model performs well and where it may miss an object.

🏆 What you have accomplished

You have taken a complete AI/ML project from installation → execution → testing → understanding. This practical experience gives you a starting point for exploring more advanced computer vision and object detection projects.

🚀 What’s next?

If you want to go deeper, the next step is to learn how object detection models are trained and how they identify objects and their locations.

🚀 19. Next Steps

You have now built, tested, and experimented with an AI-based Object Detection application. If you enjoyed this project, there are many ways to take it further.

💡 Projects you can try next

📷 Real-Time Object Detection
Detect objects using a webcam.
🎬 Object Detection in Video
Analyze objects frame by frame.
🔢 Counting Detected Objects
Count objects automatically.
🙂 Face Detection
Detect faces in images or video.
🎯 Object Tracking
Track detected objects across video frames.
🧠 Custom Object Detection
Train a model for your own object classes.
🛤️ A simple learning path

Start with images, then move to video, webcam detection, object tracking, and eventually custom object detection.

🔬 Keep experimenting!

The easiest way to learn AI and computer vision is to build small projects, test them with different inputs, observe the results, and gradually make them more capable.

Gopal Krishna

Hey Engineers, welcome to the award-winning blog,Engineers Tutor. I'm Gopal Krishna. a professional engineer & blogger from Andhra Pradesh, India. Notes and Video Materials for Engineering in Electronics, Communications and Computer Science subjects are added. "A blog to support Electronics, Electrical communication and computer students".

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »