# 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:

1. 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.
2. Example Python code used for frame extraction:

```python
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.

```markup
<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>

```

&#x20;**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:

```python
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.

```python
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.

```python
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.
* &#x20;Results are saved as individual CSVs for each video and summarized into a master CSV.

**Counting Rabbits and Exporting to CSV**

**Sample Output :**&#x20;

<figure><img src="https://843131242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8U1yBUBlixXNqT1lQ16c%2Fuploads%2FKoQZ7bkameOcXO9fcH8f%2FScreenshot%202025-01-13%20at%2011.18.30%20PM.png?alt=media&#x26;token=b8165ceb-7f3f-4847-9514-cfa37f929a7b" alt=""><figcaption><p>Console output pre-final</p></figcaption></figure>

<figure><img src="https://843131242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8U1yBUBlixXNqT1lQ16c%2Fuploads%2F3eWamstwGBokKFk7i4jd%2FScreenshot%202025-01-06%20at%202.44.18%20PM.png?alt=media&#x26;token=4adf0f22-b302-44d9-bb71-946046a7f217" alt=""><figcaption><p>Console Output final</p></figcaption></figure>

**CSV Output  :**&#x20;

<figure><img src="https://843131242-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F8U1yBUBlixXNqT1lQ16c%2Fuploads%2FJVu3CPv6M4BRsnOidfgk%2FScreenshot%202025-01-13%20at%2011.20.46%20PM.png?alt=media&#x26;token=f0904698-7512-4268-b317-f5bb9e19c5e5" alt=""><figcaption></figcaption></figure>
