# Real-Time Object Detection on Raspberry Pi with YOLOv5 and TensorFlow Lite

This project demonstrates how I used a Raspberry Pi to perform real-time object detection using **YOLOv5** with the **TensorFlow Lite** backend. It includes visual feedback via **LED indicators** and a **16x2 LCD** that displays system stats like CPU usage and temperature.

![Raspberry Pi](https://img.shields.io/badge/-RaspberryPi-C51A4A?style=for-the-badge&logo=Raspberry-Pi) ![Python](https://img.shields.io/badge/python-3670A0?style=for-the-badge&logo=python&logoColor=ffdd54) ![Jupyter Notebook](https://img.shields.io/badge/jupyter-%23FA0F00.svg?style=for-the-badge&logo=jupyter&logoColor=white) ![OpenCV](https://img.shields.io/badge/opencv-%23white.svg?style=for-the-badge&logo=opencv&logoColor=white)

---

### 🔍 Features

- Real-time FPS display
- LED output per object class (supports car, person, truck, bus, motorbike)
- CPU and temp stats displayed on LCD

---

## 🎥 Demo

<img src="other/demo_video_yolov5.gif" alt="Demo" width="700">

---

## 📊 Model Performance

| Model              | mAP@50 | mAP@50:95 | FPS  |
|--------------------|--------|-----------|------|
| Yolov5s 640px fp32 | 94.7   | 74.1      | 0.5  |
| Yolov5n 320px int8 | 85.9   | 59.3      | 7.2  |
<!-- table trimmed for brevity -->

---

## 🧰 Hardware Needed

- Raspberry Pi 4 (8 GB version used)
- Raspberry Pi OS 64-bit (Bullseye)
- Pi Camera v1/v2 or USB cam
- 16x2 LCD (I2C)
- LEDs + jumper wires or PCB

---

## 🔌 Wiring

<img src="other/sketch_github_bb.png" alt="Wiring" width="500">

### LED Wiring

| Color  | GPIO |
|--------|------|
| Red    | 17   |
| Green  | 18   |
| Yellow | 23   |
| Cyan   | 27   |
| White  | 22   |
| Black  | GND  |

### I2C LCD Wiring

| Color  | Connection |
|--------|------------|
| Red    | 5V         |
| Black  | GND        |
| Purple | SDA        |
| Brown  | SCL        |

---

## ⚙️ Setup Instructions

git clone https://github.com/Ghostow/RaspberryPi-Project.git
cd RaspberryPi-Project
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt


</details>

## Installation

To run this project, you need [Python 3.5](https://docs.python.org/3/) or higher installed on your system. Follow these steps to get started:

- Clone the repository and navigate to the project directory: :
```bash
  git clone https://github.com/Ghostow/RaspberryPi-Project.git
  cd RaspberryPi-Project
```

- Create a Python virtual environment (optional but recommended):
```bash
  python3 -m venv venv
```

- Activate the virtual environment:
```bash
  source venv/bin/activate
```

- Install the required dependencies using pip3:
```bash
  pip3 install -r requirements.txt
```

Now you have successfully installed the project and its dependencies.
    
## Usage

```bash
$ python detect.py --weights yolov5s.pt --source 0                                   # webcam
                                                     img.jpg                         # image
                                                     vid.mp4                         # video
                                                     path/                           # directory
                                                     'path/*.jpg'                    # glob
                                                     'https://youtu.be/Zgi9g1ksQHc'  # YouTube
                                                     'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream
```


<details>
<summary>example video</summary>

Default (without LED/LCD):
```bash
  python detect.py --img 320 --weights yolov5n_320px-fp32.tflite --source video_test.mp4  
```

With LED/LCD:

```bash
  python detect_rpi_led.py --img 320 --weights yolov5n_320px-fp32.tflite --source video_test.mp4  
```

</details>


<details>
<summary>example webcam</summary>

Default (without LED/LCD):
```bash
  python detect.py --img 320 --weights yolov5n_320px-fp32.tflite --source 0
```

With LED/LCD:

```bash
  python detect_rpi_led.py --img 320 --weights yolov5n_320px-fp32.tflite --source 0
```

</details>

## Training Dataset

If you want to train your own model, you can utilize the resource provided below:

[<a href="https://colab.research.google.com/github/EdjeElectronics/TensorFlow-Lite-Object-Detection-on-Android-and-Raspberry-Pi/blob/master/Train_TFLite2_Object_Detction_Model.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>](https://colab.research.google.com/drive/1XD1UWd6qwKKGVTH23qM9EP5SraieYXRp)

Dataset from Roboflow:

<a href="https://universe.roboflow.com/devan-naratama-2xq45/skripsi-dtmyf"><img src="https://app.roboflow.com/images/download-dataset-badge.svg"></img></a>   <a href="https://universe.roboflow.com/devan-naratama-2xq45/skripsi-dtmyf/model/"><img src="https://app.roboflow.com/images/try-model-badge.svg"></img></a>

Be sure to make use of these resources to train your model and achieve optimal results!

## Tips

You can change your own class, add or modify in [coco128.yaml](/data/coco128.yaml). modify the code below:

```bash
names:
- bus
- mobil
- honda
- orang
- truck
nc: 5
roboflow:
  license: CC BY 4.0
  project: skripsi-dtmyf
  url: https://universe.roboflow.com/devan-naratama-2xq45/skripsi-dtmyf/dataset/2
  version: 2
  workspace: devan-naratama-2xq45
test: ../test/images
train: /devan/datasets/Skripsi-2/train/images
val: /devan/datasets/Skripsi-2/valid/images
```





