DEV Community

Cover image for Getting Started with Object Detection in Python 3
Leandro Nuñez
Leandro Nuñez

Posted on

Getting Started with Object Detection in Python 3

Object detection is a fascinating field within computer vision that allows us to identify and locate specific objects within images.

Python, with its powerful libraries like OpenCV and TensorFlow, provides a robust platform for building object detection applications.

In this tutorial, we'll explore the fundamental steps to detect objects in an image using Python 3.

1. Installing the Required Libraries

Before we begin, make sure you have Python 3 installed on your system. To install the necessary libraries, you can use pip, the Python package manager. Open your terminal or command prompt and run the following commands:

pip install opencv-python
pip install tensorflow
pip install matplotlib
Enter fullscreen mode Exit fullscreen mode

These libraries are essential for image processing, machine learning-based object detection, and visualization.

2. Loading the Image

First, let's load an image onto which we'll perform object detection. You can use any image of your choice. For this example, we'll use the image named "example.jpg" located in the current working directory.

import cv2

# Load the image
image_path = "example.jpg"
image = cv2.imread(image_path)
Enter fullscreen mode Exit fullscreen mode

3. Pre-trained Models for Object Detection

For object detection, we'll employ a pre-trained deep learning model. These models come with pre-learned weights, making it easy for us to utilize them for object detection. One popular pre-trained model is the Single Shot Multibox Detector (SSD) model.

import tensorflow as tf

# Load the pre-trained SSD model
model = tf.keras.applications.SSDMobileNetV2()
Enter fullscreen mode Exit fullscreen mode

4. Object Detection on the Image

Now that we have our image and the pre-trained model ready, we can perform object detection. The model will provide us with bounding boxes around the detected objects.

# Convert the image to RGB (required by the model)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

# Perform object detection
result = model.predict(tf.expand_dims(image_rgb, axis=0))
Enter fullscreen mode Exit fullscreen mode

5. Visualizing the Results

Lastly, let's visualize the results by drawing bounding boxes around the detected objects on the original image.

import matplotlib.pyplot as plt

# Get the detected objects and their bounding boxes
detections = result[0, 0, :, :]
num_detections = int(result[0, 1, 0, 0])

# Draw bounding boxes on the image
for i in range(num_detections):
    class_id = int(detections[i, 1])
    score = detections[i, 2]
    bbox = detections[i, 3:7] * [image.shape[1], image.shape[0], image.shape[1], image.shape[0]]
    x, y, w, h = bbox.astype(int)

    # Draw the bounding box
    cv2.rectangle(image, (x, y), (w, h), (0, 255, 0), 2)
    cv2.putText(image, f"Object {i+1}", (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)

# Show the image with bounding boxes
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You've successfully learned the basics of object detection using Python 3. With the power of OpenCV and TensorFlow, you can now build more advanced object detection applications. Feel free to explore different pre-trained models and experiment with your custom datasets.

Remember, the field of object detection is continually evolving, and there are many other exciting techniques and models to explore. Happy coding!


Please note that the above code snippets are meant to be integrated into a Python script or Jupyter notebook. Feel free to add more details, explanations, or even code improvements in your blog post as per your preferences.

Happy coding!

Top comments (0)