Methodology
This section provides a comprehensive overview of the step-by-step methodology employed to develop an AI-based rabbit detection and counting system. The methodology evolved iteratively, from initial experimental approaches to a robust real-time solution deployed for real-world use.
Introduction to Methodology
The project's methodology aimed to achieve automated rabbit detection and counting from video data captured in a rabbit farm. It involved collecting raw data, preprocessing and annotating the data, experimenting with different models, and deploying a real-time detection system.
The project started with initial experimentation using simple scripts for TensorFlow Lite detection to validate feasibility. Later, the approach transitioned into a fully functional pipeline leveraging TensorFlow Lite with a user-friendly interface for batch processing and real-time detection.
Data Collection
The dataset was generated by capturing video footage from CCTV cameras installed in a rabbit farm. The videos covered various scenarios such as lighting changes, crowded rabbit environments, and varying angles.
Key Steps:
Video footage was converted into individual frames using OpenCV to facilitate annotation. The frames were captured at specific intervals to balance data redundancy and diversity.
Example Python code used for frame extraction:
import cv2
video_path = 'path_to_video.mp4'
output_folder = 'frames_output/'
cap = cv2.VideoCapture(video_path)
frame_interval = 50 # Capture every 50th frame
frame_count = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if frame_count % frame_interval == 0:
frame_name = f"{output_folder}/frame_{frame_count:04d}.jpg"
cv2.imwrite(frame_name, frame)
frame_count += 1
cap.release()
cv2.destroyAllWindows()
Data Annotation
To train the object detection model, the extracted frames were annotated using LabelImg, a graphical annotation tool. Each rabbit in a frame was labeled with a bounding box, and the annotations were saved in the PASCAL VOC XML format.
Key Details:
Tool: LabelImg
Annotation Format: PASCAL VOC XML
Annotation Process: Bounding boxes were drawn manually around the rabbits in each frame to create a labeled dataset.
<object>
<name>Rabbit</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>48</xmin>
<ymin>240</ymin>
<xmax>195</xmax>
<ymax>371</ymax>
</bndbox>
</object>
Iterative Model Experiments
Initially, the project employed basic detection scripts using TensorFlow Lite to validate the feasibility of detecting rabbits. Key experimental steps included:
TFLite_detection_video1.py: An early script used to detect and count rabbits directly from video using a pre-trained TensorFlow Lite model.
Challenges: Hard-coded paths, limited flexibility, and lower accuracy in crowded or complex scenarios.
These experiments provided valuable insights into adapting the model for real-world scenarios and refining the data preprocessing pipeline.
Data Splitting
The annotated dataset was split into training, validation, and test sets to ensure a robust evaluation of the model. The splits were performed as:
Training Set: 70%
Validation Set: 15%
Test Set: 15%
Example code for splitting:
import os
import random
import shutil
data_dir = 'path_to_annotated_frames/'
train_dir = 'train_set/'
val_dir = 'val_set/'
test_dir = 'test_set/'
all_files = os.listdir(data_dir)
random.shuffle(all_files)
train_files = all_files[:int(0.7 * len(all_files))]
val_files = all_files[int(0.7 * len(all_files)):int(0.85 * len(all_files))]
test_files = all_files[int(0.85 * len(all_files)):]
for file in train_files:
shutil.copy(os.path.join(data_dir, file), train_dir)
for file in val_files:
shutil.copy(os.path.join(data_dir, file), val_dir)
for file in test_files:
shutil.copy(os.path.join(data_dir, file), test_dir)
Model Selection and Training
The final object detection model was based on TensorFlow Lite for efficiency and compatibility with real-time systems. The model was trained on the annotated dataset, leveraging transfer learning to adapt a pre-trained model to rabbit detection.
Details:
Framework: TensorFlow Object Detection API
Final Model Format:
detect.tflite
Label Mapping: Defined in
labelmap.txt
Training Parameters:
Image Size: 640x640
Epochs: 50
Batch Size: 16
Deployment and Real Time Detection
The final stage of the project involved deploying the trained model for real-time detection and counting. The system processes video files, detects rabbits in each frame, and logs the results into a CSV file. Two main scripts were developed for deployment:
Batch Processing
The batch_processing.py script enables batch processing of multiple video files from a specified folder. The script integrates multiprocessing to speed up processing and generates a consolidated CSV file with the results.
from multiprocessing import Pool
from single_video_processing import process_single_video
def process_all_videos(video_folder, output_csv):
"""Process all videos in the folder using multiprocessing."""
video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(".mp4")]
# Use multiprocessing to process videos in parallel
with Pool(processes=os.cpu_count()) as pool:
results = pool.map(process_single_video, video_files)
# Write aggregated results to CSV
with open(output_csv, "w") as f:
f.write("Video Name,Total Frames,Total Rabbits Detected\n")
for video_name, frame_count, total_rabbits in results:
f.write(f"{video_name},{frame_count},{total_rabbits}\n")
Highlights:
Parallel Processing: Efficiently handles multiple videos simultaneously.
CSV Logging: Outputs a summary report with total frames and rabbits detected per video.
Single Video Processing
The single_video_processing.py script handles the detection and counting of rabbits for a single video file. It uses TensorFlow Lite to detect objects in each frame, count rabbits, and log the results into a CSV file.
interpreter = Interpreter(model_path=MODEL_PATH)
interpreter.allocate_tensors()
# Get model details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Process video
while video.isOpened():
ret, frame = video.read()
if not ret:
break
# Preprocess frame
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_resized = cv2.resize(frame_rgb, (width, height))
input_data = np.expand_dims(frame_resized / 255.0, axis=0).astype(np.float32)
# Perform detection
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
# Extract detection results
boxes = interpreter.get_tensor(output_details[0]['index'])[0]
classes = interpreter.get_tensor(output_details[1]['index'])[0]
scores = interpreter.get_tensor(output_details[2]['index'])[0]
# Count rabbits
rabbit_count = sum(1 for score in scores if score > 0.5)
writer.writerow([frame_count, timestamp_s, rabbit_count])
Highlights:
TensorFlow Lite Integration: Optimized for real-time performance on CPUs.
Rabbit Counting: Filters detections by confidence threshold (>50%) and counts rabbits in each frame.
Real-Time Example Workflow
The batch_processing.py script calls single_video_processing.py for each video file.
Results are saved as individual CSVs for each video and summarized into a master CSV.
Counting Rabbits and Exporting to CSV
Sample Output :


CSV Output :

Last updated
Was this helpful?