|
| 1 | +# Car Park Solution 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 ONXX 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" # Replace with your MQTT broker's IP address |
| 86 | +CLIENT_ID = "Car_Park" |
| 87 | +PORT = 1883 |
| 88 | +TOPIC = "v1/devices/me/telemetry" # Adjust to match your topic |
| 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. |
| 144 | +
|
| 145 | +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. |
| 146 | +
|
| 147 | +After installing ThingsBoard, you need to add a device. [This lesson](https://wiki.seeedstudio.com/recomputer_r1000_thingsboard_dashboard/) also explains how to create widgets and build dashboards using MQTT credentials. When adding a device, specify the client name in the device configuration and ensure the client name and broker settings in the above code match your ThingsBoard platform configuration. |
| 148 | +
|
| 149 | +Since this is a demo, **we have not added a username or password when creating the device**. |
| 150 | +
|
| 151 | +```bash |
| 152 | +BROKER = "192.168.8.195" # Replace with your MQTT broker's IP address |
| 153 | +CLIENT_ID = "Car_Park" # Replace with your client ID |
| 154 | +``` |
| 155 | +
|
| 156 | +## Demo |
| 157 | +
|
| 158 | + |
| 159 | +
|
| 160 | +
|
0 commit comments