{"id":21802451,"url":"https://github.com/qengineering/traffic-counter-rpi_64-bit","last_synced_at":"2025-04-13T18:42:07.105Z","repository":{"id":112948416,"uuid":"530996203","full_name":"Qengineering/Traffic-Counter-RPi_64-bit","owner":"Qengineering","description":"A fully functional traffic counter on a bare Raspberry Pi 4 with MQTT and remote live viewer","archived":false,"fork":false,"pushed_at":"2024-11-12T19:12:17.000Z","size":20729,"stargazers_count":6,"open_issues_count":3,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T09:23:19.340Z","etag":null,"topics":["aarch64","bytetracker","cpp","deep-learning","ncnn","raspberry-pi-4","raspberry-pi-5","traffic-counter","traffic-counting"],"latest_commit_sha":null,"homepage":"https://qengineering.eu/deep-learning-examples-on-raspberry-32-64-os.html","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Qengineering.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-31T08:26:21.000Z","updated_at":"2024-12-08T09:10:31.000Z","dependencies_parsed_at":"2023-10-12T19:02:12.851Z","dependency_job_id":"d9b1d84e-007e-4104-a26d-448133749e11","html_url":"https://github.com/Qengineering/Traffic-Counter-RPi_64-bit","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FTraffic-Counter-RPi_64-bit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FTraffic-Counter-RPi_64-bit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FTraffic-Counter-RPi_64-bit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Qengineering%2FTraffic-Counter-RPi_64-bit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Qengineering","download_url":"https://codeload.github.com/Qengineering/Traffic-Counter-RPi_64-bit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248764549,"owners_count":21158107,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aarch64","bytetracker","cpp","deep-learning","ncnn","raspberry-pi-4","raspberry-pi-5","traffic-counter","traffic-counting"],"created_at":"2024-11-27T11:28:43.249Z","updated_at":"2025-04-13T18:42:07.078Z","avatar_url":"https://github.com/Qengineering.png","language":"C++","funding_links":["https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL"],"categories":[],"sub_categories":[],"readme":"# Traffic counter Raspberry Pi 4\n![Screenshot from 2024-11-12 12-45-17](https://github.com/user-attachments/assets/6a88bdd7-6e89-46fd-9f07-ff63798783dc)\n## Traffic counter with a camera on a bare Raspberry Pi 4. \u003cbr/\u003e\n[![License](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)\u003cbr/\u003e\u003cbr/\u003e\nSpecially made for a Raspberry Pi 4, see [Q-engineering deep learning examples](https://qengineering.eu/deep-learning-examples-on-raspberry-32-64-os.html)\u003cbr\u003e\n\n------------\n\n## Introduction.\nA fully functional traffic counter with a camera working on a bare Raspberry Pi 4.\nHighlights:\n- Stand alone.\n- Lane selection.\n- MQTT messages.\n- JSON messages.\n- Live web viewer.\n- JSON settings.\n- RTSP CCTV streaming.\n- Debug screens.\n\n------------\n\nhttps://github.com/user-attachments/assets/a64cc957-4965-4afb-9077-b37b1071c08a\n\n------------\n\n## Dependencies.\nTo run the application, you have to:\n- A Raspberry Pi 4 with a 64-bit _**Bullseye**_ operating system. \u003cbr/\u003e\n- The Tencent ncnn framework installed. [Install ncnn](https://qengineering.eu/install-ncnn-on-raspberry-pi-4.html) \u003cbr/\u003e\n- Optional: Code::Blocks installed. (```$ sudo apt-get install codeblocks```)\n\n### Installing the dependencies.\nStart with the usual \n```shell\n$ sudo apt-get update \n$ sudo apt-get upgrade\n$ sudo apt-get install curl libcurl4\n$ sudo apt-get install cmake wget\n```\n#### Libcamera\n```shell\n$ sudo apt-get install libcamera-dev\n```\n#### OpenCV\nFollow the Raspberry Pi 4 [guide](https://qengineering.eu/install-opencv-on-raspberry-64-os.html). Or:\n```shell\n$ sudo apt-get install libopencv-dev\n```\n#### Eigen3\n```shell\n$ sudo apt-get install libeigen3-dev\n```\n#### gflags\n```shell\n$ sudo apt-get install libgflags-dev\n```\n#### JSON for C++\nwritten by [Niels Lohmann](https://github.com/nlohmann).\n```shell\n$ cd ~\n$ git clone --depth=1 https://github.com/nlohmann/json.git\n$ cd json\n$ mkdir build\n$ cd build\n$ cmake ..\n$ make -j4\n$ sudo make install\n$ sudo ldconfig\n```\n#### paho.mqtt (MQTT client)\n```shell\n$ cd ~\n$ git clone --depth=1 https://github.com/eclipse/paho.mqtt.c.git\n$ cd paho.mqtt.c\n$ mkdir build\n$ cd build\n$ cmake ..\n$ make -j4\n$ sudo make install\n$ sudo ldconfig\n```\n#### Mosquitto (MQTT broker)\n```shell\n$ sudo apt-get install mosquitto\n```\n\n------------\n\n## Installing the app.\nDownload the software.\u003cbr/\u003e\n```\n$ git clone https://github.com/Qengineering/Traffic-Counter-RPi_64-bit.git\n```\nYour folder must now look like this: \u003cbr/\u003e \n```\n.\n├── CMakeLists.txt\n├── config.json\n├── include\n│   ├── BYTETracker.h\n│   ├── dataType.h\n│   ├── General.h\n│   ├── kalmanFilter.h\n│   ├── lapjv.h\n│   ├── lccv.hpp\n│   ├── libcamera_app.hpp\n│   ├── libcamera_app_options.hpp\n│   ├── MJPG_sender.h\n│   ├── MJPGthread.h\n│   ├── MQTT.h\n│   ├── Numbers.h\n│   ├── STrack.h\n│   ├── Tjson.h\n│   ├── TChannel.h\n│   └── yolo-fastestv2.h\n├── LICENSE\n├── models\n│   ├── yolo-fastestv2-opt.bin\n│   └── yolo-fastestv2-opt.param\n├── README.md\n├── src\n│   ├── BYTETracker.cpp\n│   ├── kalmanFilter.cpp\n│   ├── lapjv.cpp\n│   ├── lccv.cpp\n│   ├── libcamera_app.cpp\n│   ├── libcamera_app_options.cpp\n│   ├── main.cpp\n│   ├── MJPG_sender.cpp\n│   ├── MJPGthread.cpp\n│   ├── MQTT.cpp\n│   ├── STrack.cpp\n│   ├── Tjson.cpp\n│   ├── TChannel.cpp\n│   ├── utils.cpp\n│   └── yolo-fastestv2.cpp\n├── Traffic.cbp\n└── Traffic.mp4\n\n3 directories, 39 files\n```\n------------\n\n## Running the app.\nYou can use **Code::Blocks**.\n- Load the project file *.cbp in Code::Blocks.\n- Select _Release_, not Debug.\n- Compile and run with F9.\n- You can alter command line arguments with _Project -\u003e Set programs arguments..._ \n\nOr use **Cmake**.\n```shell\n$ cd *MyDir*\n$ mkdir build\n$ cd build\n$ cmake ..\n$ make -j4\n```\n------------\n\n## Settings.\nAll important settings are stored in the `config.json`\u003cbr\u003e\nYou can alter these to your liking. Please note the use of commas after each line, except the last one.\u003cbr\u003e\n```json\n{\n    \"VERSION\": \"1.0.0.0\",\n\n    \"MQTT_ON\": true,\n    \"MQTT_SERVER_example\": \"broker.hivemq.com:1883\",\n    \"MQTT_SERVER\": \"localhost:1883\",\n    \"MQTT_CLIENT_ID\": \"Arrow\",\n    \"MQTT_TOPIC\": \"traffic\",\n    \"DEVICE_NAME\": \"highway 12\",\n    \"ANNOTATE\": true,\n\n    \"STREAM_example1\": \"rtsp://admin:L231C865@192.168.178.41:554/stream1\",\n    \"STREAM_example2\": \"RaspiCam\",\n    \"STREAM\": \"Traffic.mp4\",\n\n    \"BORDER_X1\": 10,\n    \"BORDER_Y1\": 300,\n    \"BORDER_X2\": 450,\n    \"BORDER_Y2\": 300,\n\n    \"JSON_PORT\": 8070,\n    \"MJPEG_PORT\": 8090,\n    \"MJPEG_WIDTH\": 640,\n    \"MJPEG_HEIGHT\": 480,\n\n    \"MESSAGE_TIME\": 2,\n\n    \"PARAM_MODEL\": \"./models/yolo-fastestv2-opt.param\",\n    \"BIN_MODEL\": \"./models/yolo-fastestv2-opt.bin\"\n}\n```\n\n| Global parameter | Comment |\n| ----      | ---- |\n| VERSION   | Current version. |\n| MQTT_ON | Enable MQTT messages. 'true-false'. |\n| MQTT_SERVER | MQTT server. Default `localhost:1883` |\n| MQTT_CLIENT_ID | MQTT client ID. Default `Arrow` |\n| MQTT_TOPIC | MQTT topic. Default `traffic` |\n| DEVICE_NAME | Name of the camera, used in the MQTT messages. |\n| ANNOTATE | Show lines, boxes and numbers in live view. Default `true` |\n| STREAM | The used input source.\u003cbr\u003eIt can be a video or a `RaspiCam`, or an RTSP stream, like CCTV cameras. |\n| BORDER_X1 | Left X position of the imaginary borderline. |\n| BORDER_Y1 | Left Y position of the imaginary borderline. |\n| BORDER_X2 | Right X position of the imaginary borderline. |\n| BORDER_Y2 | Right Y position of the imaginary borderline. |\n| JSON_PORT | The JSON message port number.|\n| MJPEG_PORT | The thumbnail browser overview. |\n| MJPEG_WIDTH | Thumbnail width |\n| MJPEG_HEIGHT | Thumbnail height |\n| MESSAGE_TIME | Define the interval between (MQTT) messages in seconds. Default 2. |\n| PARAM_MODEL | Used nccn DNN model (parameters). |\n| BIN_MODEL | Used nccn DNN model (weights). |\n\n------------\n\n![Screenshot from 2024-11-11 14-07-00](https://github.com/user-attachments/assets/492d8264-47e9-4437-abf0-f9c0a14778e2)\n\n------------\n\n## Debug.\nYou can use debug mode to find the optimal position for the borderline.\u003cbr\u003e \nTo enable debug mode, start the app with the --debug flag set to true:\u003cbr\u003e\n```shell\n./Traffic --debug=true\n```\nAlternatively, you can modify the command line argument in Code::Blocks by navigating to Project -\u003e Set programs arguments...\u003cbr\u003e \nIn debug mode, you’ll see the tail of each vehicle. When a vehicle’s tail crosses the imaginary borderline, it is added to the count.\u003cbr\u003e \nAt this point, the bounding box is highlighted, which helps in identifying any missed vehicles.\u003cbr\u003e\u003cbr\u003e\n\nhttps://github.com/user-attachments/assets/4b67bebb-af1f-4edd-9ba6-c8217ecdcf56\n\n------------\n\n## MQTT messages.\nYou can receive MQTT messages locally at localhost:1883, the default setting. Messages are printed to the terminal.\u003cbr\u003e \nWhen connected to the internet, you can send MQTT messages to any broker you choose, such as broker.hivemq.com:1883.\u003cbr\u003e \nThe app only sends messages when the MQTT_ON setting is set to true. The refresh rate, in seconds, is determined by MESSAGE_TIME.\u003cbr\u003e \nAt midnight, all cumulative counts are reset.\u003cbr\u003e\u003cbr\u003e \nYou can also follow the messages in a web browser. To do so, enter the port number after the IP address of your Raspberry Pi.\u003cbr\u003e\u003cbr\u003e\n![2024-11-11 15_22_51-192 168 178 87_8070 - Brave](https://github.com/user-attachments/assets/36d9ffcc-c66d-4da2-adaa-cbd45d19b6d6)\n\n------------\n\n## Preview.\nIf your Raspberry Pi is connected to the internet, you can view live footage in a browser.\u003cbr\u003e\nSimply combine the Raspberry Pi’s IP address with the MJPEG_PORT number specified in the settings to access the camera feed.\u003cbr\u003e\u003cbr\u003e\n![2024-11-11 15_21_32-192 168 178 87_8090 - Brave](https://github.com/user-attachments/assets/567fed46-b240-4450-b3d2-f5a07471f88d)\n\n------------\n\n## LCCV.\nWe use the Bullseye [LCCV camera](https://github.com/Qengineering/LCCV) code, which is a lightweight camera solution that leaves most computing resources available for deep learning tasks.\u003cbr\u003e If you prefer to use GStreamer, you can build the application with the CAMERA flag set to OFF:\n```shell\n$ cd *MyDir*\n$ mkdir build\n$ cd build\n$ cmake -DCAMERA=OFF ..\n$ make -j4\n```\n\n------------\n\n## Final remark.\nThere is plenty of room for improvement. The most obvious upgrade would be adding more computing power. The network used is very lightweight. We’ve also published this application for the Rock 5C, an affordable ($60) device with an NPU. With this setup, you’ll immediately see the impact of using a more powerful DNN model.\n\n------------\n\n[![paypal](https://qengineering.eu/images/TipJarSmall4.png)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick\u0026hosted_button_id=CPZTM5BB3FCYL) \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Ftraffic-counter-rpi_64-bit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqengineering%2Ftraffic-counter-rpi_64-bit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqengineering%2Ftraffic-counter-rpi_64-bit/lists"}