Jen Fox
Published © MIT

Make a Pi Trash Classifier with ML!

Build a custom trash classifier using the Raspberry Pi and Lobe, a beginner-friendly machine learning program (without coding).

IntermediateFull instructions provided1.5 hours7,189
Make a Pi Trash Classifier with ML!

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Camera Module
Raspberry Pi Camera Module
×1
Adafruit Raspberry Pi 5V 3A USBC power supply
×1
Adafruit SD/microSD Memory Card 8GB
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Adafruit Pushbutton
×1
Adafruit 5 LEDs (4 indicator LEDs and 1 status LED)
×1
Resistor 220 ohm
Resistor 220 ohm
×6
Jumper wires (generic)
Jumper wires (generic)
×1
Adafruit JST connector (female-end only)
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×1
Adafruit heat shrink tubing
×1
Adafruit PCB - Half Perma Proto Board
×1
Project case (e.g. cardboard, wood, or plastic box, approx. 6" x 5" x 4")
×1
Clear plastic square
0.5" x 0.5" (2cm x 2cm) E.g. from a plastic food container lid
×1
Velcro
×1

Software apps and online services

Microsoft Lobe
WinSCP
Alternative: any other SSH file transfer method, e.g. CyberDuck for Mac
Microsoft Remote Desktop Connection
Alternative: RealVNC

Hand tools and fabrication machines

Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Wire Stripper & Cutter, 32-20 AWG / 0.05-0.5mm² Solid & Stranded Wires
Soldering iron (generic)
Soldering iron (generic)
Helping Hand Tool, with Magnifying Glass
Helping Hand Tool, with Magnifying Glass
Precision Knife
Cutting Mat
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

Trash Classifier Schematic

Code

Trash Classifier Python Code

Python
Full Python program for the Lobe trash classifier model
# ------------------------------------------------------------------------
# Trash Classifier ML Project
# Please review ReadMe for instructions on how to build and run the program
# 
# (c) 2020 by Jen Fox, Microsoft
# MIT License
# --------------------------------------------------------------------------


#import Pi GPIO library button class
from gpiozero import Button, LED, PWMLED
from picamera import PiCamera
from time import sleep

from lobe import ImageModel

#Create input, output, and camera objects
button = Button(2)

yellow_led = LED(17) #garbage
blue_led = LED(27) #recycle
green_led = LED(22) #compost
red_led = LED(23) #hazardous waste facility
white_led = PWMLED(24) #Status light and retake photo

camera = PiCamera()

# Load Lobe TF model
# --> Change model file path as needed
model = ImageModel.load('/home/pi/Lobe/model')

# Take Photo
def take_photo():
    # Quickly blink status light
    white_led.blink(0.1,0.1)
    sleep(2)
    print("Pressed")
    white_led.on()
    # Start the camera preview
    camera.start_preview(alpha=200)
    # wait 2s or more for light adjustment
    sleep(3) 
    # Optional image rotation for camera
    # --> Change or comment out as needed
    camera.rotation = 270
    #Input image file path here
    # --> Change image path as needed
    camera.capture('/home/pi/Pictures/image.jpg')
    #Stop camera
    camera.stop_preview()
    white_led.off()
    sleep(1)

# Identify prediction and turn on appropriate LED
def led_select(label):
    print(label)
    if label == "garbage":
        yellow_led.on()
        sleep(5)
    if label == "recycle":
        blue_led.on()
        sleep(5)
    if label == "compost":
        green_led.on()
        sleep(5)
    if label == "hazardous waste facility":
        red_led.on()
        sleep(5)
    if label == "not trash!":
        white_led.on()
        sleep(5)
    else:
        yellow_led.off()
        blue_led.off()
        green_led.off()
        red_led.off()
        white_led.off()

# Main Function
while True:
    if button.is_pressed:
        take_photo()
        # Run photo through Lobe TF model
        result = model.predict_from_file('/home/pi/Pictures/image.jpg')
        # --> Change image path
        led_select(result.prediction)
    else:
        # Pulse status light
        white_led.pulse(2,1)
    sleep(1)

Trash Classifier GitHub Repo

Credits

Jen Fox

Jen Fox

34 projects • 139 followers
Dabbled in dark matter, settled into engineering w/ a blend of inventing and education! Sr.PM @ MicrosoftFounder/CEO of FoxBot Industries

Comments