Make a Pi Trash Classifier With ML!

2,516

22

10

Introduction: Make a Pi Trash Classifier With ML!

About: Resources for makers, by makers

The Trash Classifier project, affectionately known as "Where does it go?!", is designed to make throwing things away faster and more reliable.

This project uses a Machine Learning (ML) model trained in Lobe, a beginner-friendly (no code!) ML model builder, to identify whether an object goes in the garbage, recycling, compost, or hazardous waste. The model is then loaded onto a Raspberry Pi 4 computer to make it usable wherever you might find rubbish bins!

This tutorial walks you how to create your own Trash Classifier project on a Raspberry Pi from a Lobe TensorFlow model in Python3.

Difficulty: Beginner++ (some knowledge w/ circuits and coding is helpful)

Read Time: 5 min

Build Time: 60 - 90 min

Cost: ~$70 (including Pi 4)

Supplies

Software (PC-side)

  • Lobe
  • WinSCP (or other SSH file transfer method, can use CyberDuck for Mac)
  • Terminal
  • Remote Desktop Connection or RealVNC

Hardware

  • Raspberry Pi, SD Card, and USB-C power supply (5V, 2.5A)
  • Pi Camera
  • Pushbutton
  • 5 LEDs (4 indicator LEDs and 1 status LED)
    • Yellow LED: garbage
    • Blue LED: recycle
    • Green LED: compost
    • Red LED: hazardous waste
    • White LED: status
  • 6 220 Ohm resistors
  • 10 M-to-M jumper wires
  • Breadboard, half-size

If you choose to solder:

  • 1 JST connector, female end only
  • 2 M-to-F jumper wires
  • 10 F-to-F jumper wires
  • PCB

Enclosure

  • Project case (e.g. cardboard, wood, or plastic box, approx. 6" x 5" x 4")
  • 0.5" x 0.5" (2cm x 2cm) clear plastic square
    • E.g. from a plastic food container lid
  • Velcro

Tools

  • Wire cutters
  • Precision knife (e.g. exacto knife) and cutting mat
  • Soldering iron (optional)
  • Hot melt tool (or other non-conductive glue -- epoxy works great but is permanent)

Step 1: Before We Start

This project assumes you're starting with a fully set-up Raspberry Pi in a headless configuration. Here's a beginner-friendly guide on how to do this.

It also helps to have some knowledge of the following:

  1. Familiarity with the Raspberry Pi
  2. Reading and editing Python code (you won't need to write a program, just edit)
  3. Reading Fritzing wiring diagrams
  4. Using a breadboard

Find out where your trash goes

Each city across the US (and I would assume the globe) has its own garbage/recycling/compost/etc. collection system. This means that to make an accurate trash classifier, we'll need to 1) build a custom ML model (we'll cover this in the next step -- no code!) and 2) know where each piece of trash goes.

Since I didn't always know the proper bin for each item I used to train my model, I used the Seattle Utilities flyer (Photo 1), and also this handy "Where does it go?" lookup tool for the city of Seattle! Check out what resources are available in your city you by looking up your city's garbage collection utility and perusing its website.

Step 2: Create a Custom ML Model in Lobe

Lobeis an easy-to-use tool that has everything you need to bring your machine learning ideas to life. Show it examples of what you want it to do, and it automatically trains a custom machine learning model that can be exported for edge devices and apps. It doesn’t require any experience to get started. You can train on your own computer for free!

Here's a quick overview on how to use Lobe:

    1. Open the Lobe program and create a new project.

    2. Take or import photos and label them into appropriate categories. (Photo 1) We'll need these labels later on in the software part of the project.

    There are two ways to import photos:

    1. Take photos of items directly from your computer webcam, or
    2. Import photos from existing folders on your computer.
      • Keep in mind that the photo folder name will be used as the category label name, so make sure it matches existing labels!

    Aside: I ended up using both methods, since the more photos you have, the more accurate your model is.

    3. Use the "Play" feature to test the model accuracy. Change distances, lighting, hand positions, etc. to identify where the model is and is not accurate. Add more photos as necessary. (Photos 3 - 4)

    4. When you're ready, export your Lobe ML model in a TensorFlow (TF) Lite format.

    Tips:

      • Before importing photos, make a list of all the categories you'll need and how you want to label them (e.g. "garbage," "recycle," "compost," etc.)
        • Note: Use the same labels as shown in "Lobe Model Labels" photo above to reduce the amount of code you need to change.
      • Include a category for "not trash" that has photos of whatever else might be in the photo (e.g. your hands and arms, the background, etc.)
      • If possible, take photos from the Pi Camera and import into Lobe. This will greatly improve the accuracy of your model!

      Step 3: Build It: Hardware!

      1. Carefully connect the Pi Camera to Pi (visit the Pi Foundation getting started guide for more information). (Photo 1)

      2. Follow the wiring diagram to connect the pushbutton and LEDs to the Pi GPIO pins.

      • Pushbutton: Connect one leg of the pushbutton to GPIO pin 2. Connect the other, via a resistor, to a GPIO GND pin.
      • Yellow LED:Connect the positive (longer) leg to GPIO pin 17. Connect the other leg, via a resistor, to a GPIO GND pin.
      • Blue LED: Connect the positive leg to GPIO pin 27. Connect the other leg, via a resistor, to a GPIO GND pin.
      • Green LED: Connect the positive leg to GPIO pin 22. Connect the other leg, via a resistor, to a GPIO GND pin.
      • Red LED: Connect the positive leg to GPIO pin 23. Connect the other leg, via a resistor, to a GPIO GND pin.
      • White LED: Connect the positive leg to GPIO pin 24. Connect the other leg, via a resistor, to a GPIO GND pin.

      3. It is recommended to test your circuit on a breadboard and run the program before soldering or making any of the connections permanent. To do this, we'll need to write and upload our software program, so let's go to the next step!

      Step 4: Code It: Software!

      1. On your PC, open WinSCP and connect to your Pi. Create a Lobe folder in your Pi's home directory and create a model folder in that directory.

      2. Drag the resulting Lobe TF folder contents onto the Pi. Make note of the file path: /home/pi/Lobe/model

      3. On the Pi, open a terminal and download the lobe-python library for Python3 by running the following bash commands:

      pip3 install https://dl.google.com/coral/python/tflite_runtime-2.1.0.post1-cp37-cp37m-linux_armv7l.whl

      pip3 install lobe

      4. Download the Trash Classifier code (rpi_trash_classifier.py) from this repo onto the Pi (click the "Code" button as shown in Photo 1).

      • Prefer to copy/paste? Get the raw code here.
      • Prefer to download onto your computer? Download the repo/code onto your computer then transfer the Python code to the Pi via WinSCP (or your preferred remote file transfer program).

      5. Once you've connected the hardware to the Pi's GPIO pins, read through the example code and update any file paths as needed:

      • Line 29: filepath to the Lobe TF model
      • Lines 47 and 83: filepath to captured images via Pi Camera

      6. If necessary, update the model labels in the code to exactly match the labels in your Lobe model (including capitalization, punctuation, etc.):

      • Line 57: "garbage"
      • Line 60: "recycle"
      • Line 63: "compost"
      • Line 66: "hazardous waste facility"
      • Line 69: "not trash!"

      7. Run the program using Python3 in the terminal window:

      python3 rpi_trash_classifier.py

      Step 5: Test It: Run the Program!

      Program Overview

      When you first run the program, it will take some time to load the TensorFlow library and the Lobe ML model. When the program is ready to capture an image, the status light (white LED) will pulse.

      Once you've taken an image, the program will compare the image to the Lobe ML model and output the resulting prediction (line 83). The output determines which light is turned on: yellow (garbage), blue (recycle), green (compost), or red (hazardous waste).

      If none of the indicator LEDs turn on and the status LED returns to pulse mode, it means that the image captured was "not trash", in other words, retake the photo!

      Capturing an Image

      Press the pushbutton to capture an image. Note that you may need to hold the pushbutton for at least 1s for the program to register the press. It is recommended to take some test images, then open them on the Desktop to better understand the camera view and frame.

      To allow the user time to position the object and for camera light levels to adjust, it takes about 5s to fully capture an image. You may change these settings in the code (lines 35 and 41), but keep in mind the Pi Foundation recommends a minimum of 2s for light level adjustment.

      Troubleshooting

      The biggest challenge is ensuring that the captured image is what we expect, so take some time to review the images and compare expected results with indicater LED output. If necessary, you can pass in images to the Lobe ML model for direct inferencing and faster comparison.

      A few things to note:

      • The TensorFlow library will likely throw some warning messages -- this is typical for the version used in this sample code.
      • The prediction labels must be exactly as written in the led_select() function, including capitalization, punctuation, and spacing. Be sure to change these if you have a different Lobe model.
      • The Pi requires a steady power supply. The Pi's power light should be bright, solid red.
      • If one or more LEDs are not turning on when expected, check by forcing them on with the command:
      	red_led.on()

      Step 6: (Optional) Build It: Finalize Your Circuit!

      Now that we've tested and, if necessary, debugged, our project so that it works as expected, we're ready to solder our circuit!

      Note: If you do not have a soldering iron, you may skip this step. One alternative is to coat the wire connections in hot glue (this option will allow you to fix/add/use things later, but is more likely to break), or use epoxy or a similar permanent glue (this option will be much more durable but you will not be able to use the circuit or potentially the Pi after doing this)

      Quick comment about my design choices (Photo 1):

      • I opted for female jumper wires for the LEDs and Pi GPIO because they allow me to remove LEDs and swap colors or move them around if needed. You may skip these if you want to make connections permanent.
      • Similarly, I chose a JST connector for the pushbutton.

      Onward to building!

      1. Cut each of the female jumper wires in half (yes, all of them!). Using wire strippers, remove about 1/4" (1/2cm) of the wire insulation.

      2. For each of the LEDs, solder a 220Ω resistor to the negative (shorter) leg. (Photo 2)

      3. Cut a small piece, about 1" (2cm) of heat shrink tube and push over the LED and resistor junction. Make sure the other resistor leg is accessible, then heat up the shrink tube until it secures the joint. (Photo 3)

      4. Insert each LED into a pair of female jumper wires. (Photo 4)

      5. Label the jumper wires (e.g. with tape), then solder jumper wires onto your printed circuit board (PCB). (Photo 5)

      6. Next, use a (cut) female jumper wire to connect each LED to its respective Pi GPIO pin. Solder and label a jumper wire so that the bare metal connects to the positive LED leg via the PCB. (Photo 5)

      Note: Where you solder this wire will depend on your PCB layout. You can also solder this wire directly to the positive LED jumper wire.

      7. Solder a 220Ω resistor to the negative (black) end of the JST connector. (Photo 6)

      8. Solder the JST connector and resistor to the pushbutton.(Photo 6)

      9. Connect the M-to-F jumper wires between the pushbutton connector and the GPIO pins (reminder: black is GND).

      10. Coat connections PCB in hot glue or epoxy for a more secure connection.

      Note: if you choose to use epoxy, you may not be able to use the Pi's GPIO pins for other projects in the future. If you're concerned about this, add in a GPIO ribbon cable and connect the jumper wires to that instead.

      Step 7: (Optional) Build It: Case!

      Create an enclosure for your Pi that will hold the camera, pushbutton, and LEDs in place while also protecting the Pi.
      Design your own enclosure or follow our build instructions below for quickly prototyping a cardboard enclosure!

      1. On the top of the small cardboard box, trace the locations for the pushbutton, status light, identifier lights, and the pi camera window (Photo 1).
        • Note: Pi camera window should be about 3/4" x 1/2".
      2. Using your precision knife, cut out the traces.
        • Note: you may want to test the sizes as you go (Photo 1)
      3. Optional: Paint the case! I opted for spray paint :)
      4. Cut out a rectangular "window" cover for the Pi Camera (Photo 4) and glue on the inside of the box
      5. Finally, cut out slot for the Pi power cable.
        • Recommended to first install all electronics to locate the best place for the pi power cable slot.

      Step 8: Install and Deploy!

      That's it! You're ready to install and deploy your project! Place the enclosure above your trash bins, plug in the Pi, and run the program to get a faster, more reliable way of reducing our waste. Yay!

      Going Forward

      • Share your projects and ideas with other folks via the Lobe Reddit community!
      • Check out the Lobe Python GitHub repo for a general overview on how to use Python to deploy a wider variety of Lobe projects
      • Questions or project requests? Leave a comment on this project or reach out to us directly: AskAMaker@microsoft.com

      Be the First to Share

        Recommendations

        • Made with Math Contest

          Made with Math Contest
        • Unusual Uses Contest

          Unusual Uses Contest
        • Laser Challenge

          Laser Challenge

        10 Comments

        0
        godls1131
        godls1131

        Question 7 months ago

        Doesn't it work in python3.7? I get a code error

        0
        MakersAtMicrosoft
        MakersAtMicrosoft

        Answer 6 months ago

        What is the code error you're getting?

        0
        socalrobots1111
        socalrobots1111

        Reply 6 months ago

        you might need to install some missing items - numpy etc. copy and past the error in google and you will get some ideas on what to install that you are missing. I did the same

        0
        Hassan_Ahmad
        Hassan_Ahmad

        11 months ago

        Hi there! Love the project!! Can it be done with the Arduino Uno?

        0
        MakersAtMicrosoft
        MakersAtMicrosoft

        Reply 11 months ago

        Thanks very much! I don't think it would work w/ an Uno as the model file + TF library are fairly large, but it miiight work with an Arduino Nano 33 BLE Sense board, which can run simple TF models. Here's a writeup that should be helpful: Arduino Blog » Get started with machine learning on Arduino

        If you end up testing it out, would love to hear how it goes!

        0
        prusteen
        prusteen

        1 year ago

        Is there a way for me to import the images that you already uploaded to the machine learning site? That will ensure accuracy and also save a lot of time. Also, is the detection limited only to the items that you took pictures of? For example, if you show a picture of a candy wrapper but you did not upload any pictures of it, will it still be able to classify it? Thank you for the great project!

        0
        MakersAtMicrosoft
        MakersAtMicrosoft

        Reply 1 year ago

        Thanks for the questions! Glad you enjoyed the project! While I cannot share my personal images, you can use the website Kaggle to get datasets! There are links in Step 2 of this tutorial :)

        For unique items, yes, you will need to take images of each item. I did find that the ML model was able to classify different types of similar items that I had not taken photos of, e.g. milk cartons, lids, cardboard, etc. Hope that helps!

        0
        randofo
        randofo

        1 year ago

        Nice project!

        0
        MakersAtMicrosoft
        MakersAtMicrosoft

        Reply 1 year ago

        Thank you! Glad you enjoyed it -- please reach out if you have any questions!