Python OpenCV Project – Pigeon Detection System

Machine Learning courses with 100+ Real-time projects Start Now!!

Pigeon detection is the process of identifying and classifying pigeons in digital images or video footage. It plays a crucial role in various applications, including wildlife conservation, urban pest control and surveillance.

Advanced computer vision techniques and deep learning models are used to automatically distinguish pigeons from other objects or animals. Accurate pigeon detection is valuable for managing pigeon populations and understanding their behaviors in different contexts.

What is ResNet-50

ResNet-50 is a deep convolutional neural network architecture used in computer vision tasks. It comprises 50 layers and is known for its residual connections, which allow for the training of very deep networks. ResNet-50 has achieved remarkable success in image recognition tasks such as image classification and object detection by mitigating the vanishing gradient problem and improving training efficiency.

Dataset

A dataset containing pigeon and not pigeon images is crucial for training a deep learning model to distinguish pigeons from other objects. This labeled dataset provides essential training samples, enabling the development of an accurate pigeon detention system for wildlife monitoring.

Prerequisites For Python OpenCV Pigeon Detection System

Proficiency in Python and a solid understanding of the OpenCV library are prerequisites. Additionally, adherence to specified system requirements is necessary for the successful execution of this project.

  • Python 3.7 (64-bit) and above
  • Any Python editor (VS code, Pycharm)
  • GPU 4.00 GB+

Note:- In this Tutorial Google Colab is used.

Download Python OpenCV Pigeon Detection System Project

Please download the source code of Python OpenCV Pigeon Detection System Project: Python OpenCV Pigeon Detection System Project Code.

Installation

Open windows cmd as administrator

1. Install OpenCV library.

pip install opencv-python

2. Install TensorFlow library.

pip install tensorflow

Let’s Implement

Model Training

1. Import all the packages that are required in implementation.

import tensorflow as tf
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt

2. It sets up a deep learning model based on ResNet-50 with specific architecture and pretrained weights. It then freezes the layers in the pertained model to retain their weights.

batch_size = 32
epochs = 10
image_size = (224, 224)
MODEL = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
x = MODEL.output
x = GlobalAveragePooling2D()(x)
x = Dense(1024, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=MODEL.input, outputs=predictions)
for layer in MODEL.layers:
    layer.trainable = False

3. It configures an image and data generator with various data augmentation techniques. It generates batches from a directory of images for binary classification.

train_datagen = ImageDataGenerator(
    rescale=1.0 / 255.0,
    rotation_range=30,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

train_generator = train_datagen.flow_from_directory(
    '/content/train',
    target_size=image_size,
    batch_size=batch_size,
    class_mode='binary'
)

4. It compiles the model using the Adam optimizer with specified parameters. Then it trains the model and prints the accuracy.

model.compile(optimizer=Adam(lr=0.0001), loss='binary_crossentropy', metrics=['accuracy'])
model.fit(
    train_generator,
    steps_per_epoch=train_generator.samples // batch_size,
    epochs=epochs
)
accuracy = model.evaluate(train_generator, return_dict=True)['accuracy']
print(f'Model Accuracy: {accuracy * 100:.2f}%')

Output of this step:

adam optimizer with specified

5. Once the model is trained, it saves the trained model.

model.save('pigeon_detection_model.h5')

Model Testing

6. It loads the trained model for pigeon detection and displays the image with the prediction result.

model = tf.keras.models.load_model('pigeon_detection_model.h5')
def predict_image_class(image_path, model):
    IMAGE = image.load_img(image_path, target_size=(224, 224))
    IMAGE = image.img_to_array(IMAGE)
    IMAGE = np.expand_dims(IMAGE, axis=0)
    IMAGE = IMAGE / 255.0 
    prediction = model.predict(IMAGE)
    return prediction[0][0]  

image_path = '/content/train/Not Pigeon/142.jpg'
prediction = predict_image_class(image_path, model)
IMAGE = image.load_img(image_path)
plt.imshow(IMAGE)
plt.title(f'Prediction: {"Pigeon" if prediction > 0.5 else "Not Pigeon"}')
plt.show()
print(prediction)
if prediction > 0.5:
    print("This is a pigeon.")
else:
    print("This is not a pigeon.")

Python OpenCV Pigeon Detection System Output

opencv pigeon detection system

opencv pigeon detection system output

pigeon detection system

pigeon detection system opencv output

Conclusion

In conclusion, the development of pigeon detection technology holds great promise in addressing urban challenges and improving public spaces. By leveraging advanced computer vision techniques, deep learning models and real-time monitoring systems, we can effectively manage and mitigate issues associated with pigeons, such as sanitation and safety concerns.

This innovative solution not only enhances the quality of urban life but also exemplifies the potential of technology to harmonize the coexistence of wildlife and humans in our ever-evolving cities.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

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