|
| 1 | +# Hailo-Powered Car Park Management with ThingsBoard |
| 2 | + |
| 3 | +This project delivers a custom model for detecting parking possibilities in a car park. The system identifies three classes: available slots, parked slots, and improper parking. |
| 4 | + |
| 5 | +The model was trained using the YOLOv8n framework and deployed on a Raspberry Pi with a Hailo-8L accelerator. The modified code enables real-time parking slot counting and sends MQTT messages to the ThingsBoard IoT platform. |
| 6 | + |
| 7 | +ThingsBoard provides an intuitive interface for data visualization and analysis, making it easy to monitor parking conditions and optimize management. |
| 8 | + |
| 9 | +## Step 1: Data Collection and Labeling |
| 10 | + |
| 11 | +We created a small prototype of a car park to demonstrate the working phenomenon. Using a webcam connected to a Raspberry Pi, we collected images of the parking area. These images were then uploaded to a Roboflow repository for further processing. |
| 12 | + |
| 13 | +The images were labeled into three distinct classes: |
| 14 | + |
| 15 | +- Available |
| 16 | +- Parked |
| 17 | +- Improper |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +You can view the project here: [Car Park - Roboflow Repository](https://app.roboflow.com/kasun-thushara-fxbng/car-park-cq0uw/1) |
| 24 | + |
| 25 | +## Step 2: Model Training |
| 26 | + |
| 27 | +We trained a YOLOv8n model using a custom dataset. Roboflow provides a downloadable link for the dataset, which can be used to train the model with YOLOv8n. |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + After training, the model must be exported to the ONNX format for deployment. |
| 32 | + |
| 33 | +Detailed instructions on model labeling and training can be found in Chapter 5 |
| 34 | + |
| 35 | +[How to training the model](https://seeed-projects.github.io/Tutorial-of-AI-Kit-with-Raspberry-Pi-From-Zero-to-Hero/docs/Chapter_5-Custom_Model_Development_and_Deployment/Training_Your_Model) |
| 36 | + |
| 37 | +[How to convert ONNX model to HEF](https://seeed-projects.github.io/Tutorial-of-AI-Kit-with-Raspberry-Pi-From-Zero-to-Hero/docs/Chapter_5-Custom_Model_Development_and_Deployment/Convert_Your_Model) |
| 38 | + |
| 39 | +## Step 3: Deploy the Model |
| 40 | + |
| 41 | +On the Raspberry Pi, after installing Hailo tools, you can test if the detection is working correctly. For MQTT connection, ensure you install the required dependencies in the same virtual environment: |
| 42 | + |
| 43 | +```bash |
| 44 | +sudo apt update |
| 45 | +sudo apt install -y mosquitto mosquitto-clients |
| 46 | +pip install paho-mqtt |
| 47 | +``` |
| 48 | +We modified the detection_pipeline.py script and renamed it as park_object_detection.py. Below is the modified code: |
| 49 | + |
| 50 | +```bash |
| 51 | +import gi |
| 52 | +gi.require_version('Gst', '1.0') |
| 53 | +from gi.repository import Gst, GLib |
| 54 | +import os |
| 55 | +import numpy as np |
| 56 | +import cv2 |
| 57 | +import hailo |
| 58 | +import paho.mqtt.client as mqtt |
| 59 | +import json |
| 60 | +from hailo_rpi_common import ( |
| 61 | + get_caps_from_pad, |
| 62 | + get_numpy_from_buffer, |
| 63 | + app_callback_class, |
| 64 | +) |
| 65 | +from detection_pipeline import GStreamerDetectionApp |
| 66 | + |
| 67 | +# ----------------------------------------------------------------------------------------------- |
| 68 | +# User-defined class to be used in the callback function |
| 69 | +# ----------------------------------------------------------------------------------------------- |
| 70 | +class user_app_callback_class(app_callback_class): |
| 71 | + def __init__(self): |
| 72 | + super().__init__() |
| 73 | + self.detection_counts = { |
| 74 | + "available": 0, |
| 75 | + "parked": 0, |
| 76 | + "improper": 0 |
| 77 | + } |
| 78 | + |
| 79 | + def reset_counts(self): |
| 80 | + self.detection_counts = {key: 0 for key in self.detection_counts} |
| 81 | + |
| 82 | +# ----------------------------------------------------------------------------------------------- |
| 83 | +# MQTT Setup |
| 84 | +# ----------------------------------------------------------------------------------------------- |
| 85 | +BROKER = "192.168.8.195" # ThingsBoard Host-Name |
| 86 | +CLIENT_ID = "Car_Park" # The Client ID which you give in the Thingsboard End |
| 87 | +PORT = 1883 |
| 88 | +TOPIC = "v1/devices/me/telemetry" # Topic For Sending data to ThingsBoard |
| 89 | + |
| 90 | +mqtt_client = mqtt.Client(CLIENT_ID) |
| 91 | + |
| 92 | +def setup_mqtt(): |
| 93 | + mqtt_client.connect(BROKER, PORT, 60) |
| 94 | + mqtt_client.loop_start() |
| 95 | + |
| 96 | +def publish_counts(detection_counts): |
| 97 | + payload = json.dumps(detection_counts) # Convert counts to JSON format |
| 98 | + mqtt_client.publish(TOPIC, payload, qos=1) |
| 99 | + print(f"Published: {payload} to topic {TOPIC}") |
| 100 | + |
| 101 | +# ----------------------------------------------------------------------------------------------- |
| 102 | +# User-defined callback function |
| 103 | +# ----------------------------------------------------------------------------------------------- |
| 104 | +def app_callback(pad, info, user_data): |
| 105 | + buffer = info.get_buffer() |
| 106 | + if buffer is None: |
| 107 | + return Gst.PadProbeReturn.OK |
| 108 | + |
| 109 | + user_data.reset_counts() |
| 110 | + roi = hailo.get_roi_from_buffer(buffer) |
| 111 | + detections = roi.get_objects_typed(hailo.HAILO_DETECTION) |
| 112 | + |
| 113 | + for detection in detections: |
| 114 | + label = detection.get_label() |
| 115 | + if label in user_data.detection_counts: |
| 116 | + user_data.detection_counts[label] += 1 |
| 117 | + |
| 118 | + publish_counts(user_data.detection_counts) |
| 119 | + print(f"Detection Counts: {user_data.detection_counts}") |
| 120 | + |
| 121 | + return Gst.PadProbeReturn.OK |
| 122 | + |
| 123 | +# ----------------------------------------------------------------------------------------------- |
| 124 | +# Main |
| 125 | +# ----------------------------------------------------------------------------------------------- |
| 126 | +if __name__ == "__main__": |
| 127 | + setup_mqtt() |
| 128 | + user_data = user_app_callback_class() |
| 129 | + app = GStreamerDetectionApp(app_callback, user_data) |
| 130 | + |
| 131 | + try: |
| 132 | + app.run() |
| 133 | + except KeyboardInterrupt: |
| 134 | + print("Shutting down...") |
| 135 | + finally: |
| 136 | + mqtt_client.loop_stop() |
| 137 | + mqtt_client.disconnect() |
| 138 | + |
| 139 | +``` |
| 140 | +
|
| 141 | +## Step 4: Install ThingsBoard and Add a Device |
| 142 | +
|
| 143 | +[ThingsBoard](https://thingsboard.io/) is an open-source IoT platform designed for device management, data collection, processing, and visualization. It supports various communication protocols such as **MQTT, CoAP, and HTTP**, making it versatile for integrating IoT devices and systems. The platform enables users to **create interactive dashboards, monitor devices in real-time, and analyze data through advanced visualization tools. With its rule engine, ThingsBoard automates workflows and event processing**, simplifying the implementation of IoT use cases across industries. Its scalability and flexibility make it suitable for projects of any size, from small prototypes to large-scale deployments.Additionally, you can use their [cloud solution](https://thingsboard.io/products/paas/) for easier deployment and management. |
| 144 | +
|
| 145 | +
|
| 146 | +
|
| 147 | +We have provided instructions on [installing the ThingsBoard Community Edition](https://wiki.seeedstudio.com/recomputer_r1000_thingsboard_ce/) and the Edge version. You can refer to this wiki lesson for guidance. |
| 148 | +
|
| 149 | +
|
| 150 | +## Step 5: Provision a Device |
| 151 | +
|
| 152 | +1. Navigate to the **Devices** page under the **Entities** section. |
| 153 | +
|
| 154 | + |
| 155 | +
|
| 156 | +2. Click the **'+'** icon in the top right corner of the table and select **'Add new device'** from the drop-down menu. |
| 157 | +
|
| 158 | + |
| 159 | +
|
| 160 | +3. Enter the **device name**, **Client ID**, **Password**, and **Username**. |
| 161 | +Since this is a demo, we have not added a username or password when creating the device. |
| 162 | +
|
| 163 | +4. Click **Add**. |
| 164 | +
|
| 165 | + |
| 166 | +
|
| 167 | +5. As you add more devices, they will appear at the top of the table. The table automatically sorts devices by creation time, with the newest ones listed first. |
| 168 | +
|
| 169 | + |
| 170 | +
|
| 171 | +
|
| 172 | +## Step 6: Connect the Device |
| 173 | +
|
| 174 | +1. Click on your device and then click the **Check connectivity** button in the **Device details** window. |
| 175 | + |
| 176 | +
|
| 177 | +2. In the opened window, select the messaging protocol and your operating system: |
| 178 | + - **Operating System:** Linux |
| 179 | + - **Protocol:** MQTT |
| 180 | +
|
| 181 | + |
| 182 | +
|
| 183 | +3. Make sure to install the required client tools. |
| 184 | +4. Execute the copied command. Once the temperature readings are published successfully, the device state will change from **"Inactive"** to **"Active"**. You will also see the published temperature readings. |
| 185 | +
|
| 186 | + |
| 187 | +
|
| 188 | +## Step 7: Run the Detection Code |
| 189 | +
|
| 190 | +```bash |
| 191 | +cd hailo-rpi5-examples |
| 192 | +
|
| 193 | +source setup_env.sh |
| 194 | +
|
| 195 | +cd basic_pipelines |
| 196 | +
|
| 197 | +python park_object_detection.py --labels-json /home/pi/Desktop/hailo-custom/config.json --hef-path /home/pi/Desktop/hailo-custom/yolov8n_renamed_carpark.hef -i /dev/video0 |
| 198 | +``` |
| 199 | +
|
| 200 | +## Step 8: Create a Dashboard |
| 201 | +### Create an Empty Dashboard |
| 202 | +1. Navigate to the Dashboards page from the main menu on the left side of the screen. |
| 203 | +
|
| 204 | +
|
| 205 | +2. Click the `+` sign in the upper right corner of the screen and select 'Create new dashboard' from the drop-down menu. |
| 206 | + |
| 207 | +3. In the dialog box, enter a title for the dashboard (the description is optional). |
| 208 | +
|
| 209 | +4. Click Add. |
| 210 | + |
| 211 | +5. After creating the dashboard, it will open automatically, and you can start adding widgets. |
| 212 | +6. To save the dashboard, click the Save button in the upper right corner. |
| 213 | + |
| 214 | +
|
| 215 | +7. Your first dashboard is now created. As you add more dashboards, they will appear at the top of the list, sorted by the creation timestamp. |
| 216 | +
|
| 217 | + |
| 218 | +
|
| 219 | +
|
| 220 | +
|
| 221 | +### Add a Chart Widget |
| 222 | +1. Enter edit mode and click the `Add new widget` button at the top of the screen. |
| 223 | + |
| 224 | + |
| 225 | +
|
| 226 | +2. Find the `Charts` widget bundle and click on it. |
| 227 | + |
| 228 | +
|
| 229 | +3. Select the `Time series chart` widget. |
| 230 | + |
| 231 | +
|
| 232 | +4. In the `Device` field, specify the device you created earlier as the data source. |
| 233 | +5. In the `Series` section, enter the data key `Available`,`Park`,`Improper` to monitor the car park related values of a device. |
| 234 | + |
| 235 | +
|
| 236 | +6. Resize the widget and apply the changes. |
| 237 | +7. Click Add. |
| 238 | +
|
| 239 | +You can explore additional [dashboard widgets](https://thingsboard.io/docs/user-guide/dashboards/) in the system. |
| 240 | +
|
| 241 | +
|
| 242 | +
|
| 243 | +## Demo |
| 244 | +
|
| 245 | + |
| 246 | +
|
| 247 | +
|
0 commit comments