TOY CAR COUNTER

by Melisa Atish in Circuits > Raspberry Pi

60 Views, 1 Favorites, 0 Comments

TOY CAR COUNTER

IMG_8166.JPG
IMG_8150.JPG

Hey there!

For my school project at the end of my second semester, I created a smart traffic system prototype in just three weeks. This project aims to provide a foundational understanding of how simple AI can be used in traffic management. By counting the number of cars at intersections, the prototype demonstrates the practical application of AI in urban infrastructure.

In this instructable, I will guide you through the process of creating this project step by step, so you can easily recreate it if you're interested. This project is designed for AI enthusiasts and students interested in learning about traffic management, serving as an educational tool that offers hands-on experience with basic AI algorithms.

Future extended versions of this project could benefit a wide range of stakeholders, including commuters, local residents, public transportation users, and city authorities.

Supplies

IMG_8087.JPG
IMG_8027.JPG
IMG_8054.JPG
IMG_8022.JPG
IMG_8023.JPG
IMG_8029.JPG
IMG_8050.JPG

Materials:

  • Project board
  • Raspberry Pi 5
  • 16 GB SD Card
  • LCD Display
  • Wires
  • Camera
  • Tripod (to be hidden inside the cone)
  • LED traffic lights (2)

For visualization:

  • Play mat (90x200 cm)
  • Toy cars
  • Miniature traffic cone (13x13x17 cm)
  • White masking tape
  • Paint (to resemble a traffic cone)
  • Precision knife
  • Wooden plate
  • Wood glue
  • Sandpaper


Freenove Project Board

For this project, I used the Freenove Project Board as the base, connecting both my Raspberry Pi and LCD display to it.

You can find it on amazon.


Raspberry Pi 5 & 16 GB SD Card

I used a Raspberry Pi 5 and a 16 GB SD card to run the system and connect to the LCD display. If you have a Raspberry Pi 4, it will work just fine as well.

You can find it on: https://www.raspberrystore.nl/PrestaShop/en/32-raspberry-pi-5


LCD Display

I utilized the LCD display that came with my project board kit. If you don't have a project board, you can use a basic LCD display and handle the wiring yourself. The LCD is used to display the car count in the left and right directions.


LED Traffic Lights

I used a total of two LED traffic lights, one for each side (right and left). You can connect each light to three GPIO pins and one GND pin on the mother board.

You can find it on amazon.


Camera & Tripod

I used a webcam for image detection, connecting it to my computer. The webcam captures the necessary images for counting cars.

I purchased it on bol.com.


You can find tutorials for various components in the file "Tutorials.pdf." For the LCD display, don't forget to check pages 254 to 265.


Let's get started!

Downloads

Creating and Preparing the Dataset for Car Recognition

IMG_7364.JPG
IMG_7498.JPG

Creating the data

Goal: Help the AI learn what a toy car looks like.

You can see the sample photos I took for object detection above.

Gather Your Images:

  • Take Photos: Snap about 500 pictures of toy cars. Use a play mat and try different backgrounds.
  • Mix It Up: Make sure you have a variety of backgrounds, lighting conditions, and angles. This helps the AI learn better.

Check Your Photos:

  • Quality Matters: Use high-resolution images so the details of the toy cars are clear.
  • Variety is Key: Include different types of toy cars (various colors, sizes, shapes) to make the AI more versatile.

Preparing the Data

Screenshot 2024-06-11 at 10.01.43.png

Goal: Label the images so the AI knows where the toy cars are.

Labeling Your Images:

  • Use Roboflow: I used Roboflow because it makes labeling super easy. You can find the website here.


  • How to Label: Drawing boxes around each toy car in your photos and labeling them (e.g., "toy car") is essential for teaching your AI what to look for. But here’s a neat trick: you can also use an auto labeller! This way, you'll need to label fewer images or just fix any mistakes the auto labeller makes. It’s super speedy and efficient, so I highly recommend giving it a try.


  • Be Consistent: Make sure you label all the images the same way to keep things neat and tidy.


Optional but Helpful: Data Augmentation:

  • Why?: It's all about maximizing your dataset's potential! Data augmentation helps create more data from your existing images, boosting your AI's learning power. For instance, I started with nearly 500 images, and through data augmentation, my dataset expanded to 1000 images for training. That's double the data without extra effort, making your AI even smarter!


  • How?: It's simple! Utilize techniques such as rotating, flipping, scaling, and adjusting colors. These methods can be applied easily within Roboflow or through other tools like OpenCV or TensorFlow. Plus, for added variety, I even incorporated a bit of blurriness into some images!


Split Your Dataset:

  • Training Set: Dedicate 70-80% of your images to teach the AI. In my case, I allocated 85% of my images.


  • Validation Set: Allocate 10-15% of your images to check how well the AI is learning. I set aside 10% for validation.


  • Test Set: Reserve the remaining 10-15% to test the AI on new images it hasn't seen before. I designated 5% for testing.


  • Why?: This division allows you to gauge how well the AI performs and ensures it's not merely memorizing the images.


Do you need more information for training? Visit this link.

Training Your Model With Yolov8

Screenshot 2024-06-10 at 14.36.09.png
Screenshot 2024-06-10 at 14.36.21.png

In this section, I'll walk you through the process of training your model using a Python Jupyter Notebook. Make sure you have Python version 3.11.3 installed before proceeding.


Imports: First, let's import the necessary libraries:

from roboflow import Roboflow
from ultralytics import YOLO
import cv2


Installation: Next, you'll need to install the required packages. Run the following command in your terminal:

python -m pip install roboflow ultralytics


Downloading the Dataset: Now, let's download the dataset using Roboflow;

from roboflow import Roboflow
rf = Roboflow(api_key="XXXXXXXXXXXXXXXX")
project = rf.workspace("project-one-zrgj7").project("toy-cars-435u8")
version = project.version(3)
path = "./Path/to/your/file"
dataset = version.download("yolov8", path)


Training the Model: This step can take a lot of time, depending on your computer's resources. Adjust the number of epochs according to your preference.

Note: Training the model can be time-consuming, often requiring several hours. For example, on my computer, it took almost 8 hours to complete. If you need to expedite the training process, consider reducing the number of epochs, though it's important to note that higher epochs typically result in improved accuracy.


model = YOLO(model="yolov8s.pt")
def main():
model = YOLO(model="yolov8s.pt")
model.train(data = "/Path/to/your/file", epochs=40, imgsz=(480, 640), verbose=True, batch=8)
model.val()
model.export()

if __name__ == '__main__':
main()


Training your model is essential for achieving higher accuracy, but it requires patience. Feel free to experiment with different settings and parameters to optimize your results.

Hardware Connection

IMG_8055.JPG
IMG_8197.JPG

In this step, I'll guide you through connecting your Raspberry Pi to a camera and an LCD display. Since the camera needs to be connected to your computer, we'll use a socket connection between the Raspberry Pi and the computer to facilitate communication.


Hardware Setup:

Camera Connection: Connect the camera module to your computer. Ensure it is properly recognized and configured for use.

Raspberry Pi: Ensure your Raspberry Pi is powered on, connected to your network, and has an operating system installed.

LCD Display: Connect the LCD display to the Raspberry Pi. Follow the specific connection instructions provided by the LCD display manufacturer.

LED Traffic Lights:

Connect the LED traffic lights to the GPIO pins on the project board.Refer to the image above for detailed connection ports.

Here are the specific connections for the traffic lights that I used:

Left Side:

  • Red LED: Connect to GPIO pin 16.
  • Yellow LED: Connect to GPIO pin 20.
  • Green LED: Connect to GPIO pin 21.

Right Side:

  • Red LED: Connect to GPIO pin 13.
  • Yellow LED: Connect to GPIO pin 6.
  • Green LED: Connect to GPIO pin 5.

Software Connection

Set Up Socket Connection

Since the camera needs to be connected to the computer, establish a socket connection between the Raspberry Pi and the computer to facilitate communication.


Server on Computer:

  • Create a server application on your computer using Python. This server will handle capturing images from the camera and sending them to the Raspberry Pi over the network.
  • The server will listen for incoming connections from the Raspberry Pi, capture the images, and transmit the data through a socket connection.


Client on Raspberry Pi:

  • Create a client application on the Raspberry Pi using Python. This client will connect to the server running on your computer, receive the image data, and process it as needed.
  • Ensure the Raspberry Pi and the computer are on the same network to facilitate communication.


Car Detection:

  • Using YOLOv8 Model: Utilize your trained YOLOv8 model to detect cars in the images. After training your model, you will have a file named best.pt which represents your best-performing model.
  • Loading the Model: Load this best.pt model and use it to process incoming images. The model will output bounding boxes around detected cars.


Counting Cars:

  • Image Division: Divide the received image into left and right sections.
  • Analyzing Bounding Boxes: For each detected car, determine whether its bounding box falls in the left or right section of the image. Count the number of cars in each section accordingly.


Displaying on LCD:

  • Initialize Display: Set up the LCD display using the appropriate libraries and initialize it to be ready to receive and display text or graphics.
  • Update Display: After processing each image, format the count of cars on the left and right into a string. Send this string to the LCD display to update what is shown on the screen.


Adjusting the traffic lights:

  • Set the traffic lights according to the crowdedness of the traffic. Light up green if one side is more crowded than the other side, and light up red for the less crowded side. Adjust GPIO pins from low to high to control the LED traffic lights accordingly.


Example Steps:

  1. Load the Model:
  2. Load your best.pt model on the Raspberry Pi.
  3. Process Images:
  4. Pass each received image through the YOLOv8 model.
  5. Extract bounding boxes for detected cars.
  6. Count Cars:
  7. Divide the image into two equal sections: left and right.
  8. Count the bounding boxes in each section.
  9. Display Results:
  10. Format the counts as text (e.g., "Left: 5, Right: 3").
  11. Update the LCD display with the new counts.
  12. Adjust the LED traffic lights based on the traffic density.


By following these steps, you can successfully set up a system where your Raspberry Pi receives images from a computer-connected camera, processes these images to count cars using a pre-trained YOLOv8 model, displays the results on an LCD, and adjusts the traffic lights based on traffic density.


Additionally, I provide my code to help you replicate and further customize this traffic monitoring system for your own projects.

Bringing It All Together - Visualization!

IMG_8036.JPG

Now, for the fun part—visualizing your setup in action!

First, let’s make our camera setup look cool. Here’s a step-by-step guide to turning your camera into a traffic cone that blends seamlessly into the intersection scene.


Preparing the Cone


  • If your cone isn't already orange, start by sanding it lightly to remove any previous paint. Then, apply an orange coat of paint.


  • Next, apply white masking tape around the cone to create evenly spaced stripes. Place the tape at intervals that are visually appealing and realistic.


  • Ensure the tape is applied smoothly, without wrinkles or bubbles.


  • Once the masking tape is securely in place, carefully apply white paint over the tape to create the stripes. Ensure the paint is applied evenly across the taped areas. Multiple coats may be necessary to achieve a solid color. Allow each coat to dry thoroughly before applying the next. Let the paint dry completely before proceeding to the next step.

Cutting the Hole for the Camera

IMG_8038.JPG
IMG_8039.JPG

Mark the Spot:

  • Decide where you want the camera to sit. Typically, you’d place it at the very top of the cone so it can capture the entire play mat.
  • Use a marker to draw a small circle at the top of the cone. The diameter of this circle should be just large enough to fit the tripod of your camera.

Cut the Hole:

  • Using a precision knife, carefully cut along the circle you’ve drawn. Take your time to ensure the hole is clean and smooth.
  • Be cautious while cutting to avoid any accidents.


Installing the Camera

IMG_8046.JPG
IMG_8047.JPG
IMG_8048.JPG

Position the Camera:

  • Insert the tripod of the camera through the hole you’ve just cut. The tripod screw should peek out from the top of the cone.
  • Adjust the camera’s position so it’s stable and the lens is properly aligned.

Hide the Tripod:

  • Place the tripod inside the cone. This will keep it hidden and maintain the realistic look of your setup.
  • Secure the tripod inside the cone to ensure it is stable.
  • Attach the camera to the tripod screw protruding from the top of the cone. Make sure the camera is firmly attached and the lens is properly aligned.


Wooden Box

IMG_8106.JPG
IMG_8200.jpeg

For my project, I chose to use a wooden box to house the components. Here's how I did it:

  1. Measure and Design: First, I measured the dimensions of my motherboard and carefully planned out where I needed holes for cables, LED traffic lights, and the LCD display.
  2. Design Drawing: Using these measurements, I created a detailed drawing of the box with all the necessary cutouts and holes.
  3. Laser Cutting: I then used a laser cutter to precisely cut the wooden plate according to my design.
  4. Assemble the Box: After cutting, I applied wood glue to the edges of each plate and carefully assembled them according to my design.
  5. Secure the Assembly: I used clamps or weights to ensure a tight bond while the glue set
  6. Check Alignment: Double-checked that all components fit properly into the assembled box structure.
  7. Allow Glue to Dry: I let the glue dry completely before proceeding to install the components.
  8. Sandpaper: I used sandpaper to smooth out any rough surfaces.

Downloads

Final Touches

IMG_8056.JPG
IMG_8152.JPG
IMG_8179.JPG
IMG_8149.JPG

Secure Everything:

  • Double-check that the camera is firmly positioned and can capture all the cars.
  • Make sure the cone is stable and won’t tip over easily. If needed, add some weight inside the cone’s base to keep it secure.

Blend It In:

  • Place the cone with the camera and wooden box on your play mat. Position the wooden box where you need to use the lights. You can place the camera wherever you like to detect the cars and change the traffic lights.


This was the final step! If you try this project, please leave a comment and let me know how it went. And if you have any questions, don't hesitate to ask. Happy building!