Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ This repository contains the following files:
| File/folder | Description |
|---------------------------|--------------------------------------------|
| `images` | Folder containing images for this project |
| `tests` | Folder containing tests for connections |
| `rpi_trash_classifier.py` | Trash Classifier Sample Code for the Pi |
| `README.md` | This README file! |
| `LICENSE` | The license for the sample. |
Expand Down Expand Up @@ -114,7 +115,7 @@ Press the pushbutton to capture an image. Note that you may need to hold the pus
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.

### Testing and Troubleshooting
Before you solder or make any of the connections permanent, be sure to test the code with the hardware.
Before you solder or make any of the connections permanent, be sure to test the code with the hardware. For example, in the tests/ directory there are some examples that can be run to verify that each component used in the main program (LEDs, the TF model, the button and the camera) is working as it was intended to.

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.

Expand Down
20 changes: 20 additions & 0 deletions tests/test_button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ------------------------------------------------------------------------
# Trash Classifier ML Project - Test script for the usage of Buttons
# Expected behaviour: the white LED should turn on for a little while as long as the button is pressed.
#
# (c) 2020 by Jen Fox, Microsoft
# MIT License
# --------------------------------------------------------------------------

from gpiozero import Button, PWMLED
from time import sleep
button = Button(2)
white_led = PWMLED(24)

while True:
if button.is_pressed:
white_led.on()
sleep(0.3)
else:
white_led.off()
sleep(0.3)
40 changes: 40 additions & 0 deletions tests/test_leds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ------------------------------------------------------------------------
# Trash Classifier ML Project - Test script for the usage of LEDs
# Expected behaviour: switches all available LEDs on and off for 1 second.
#
# (c) 2020 by Jen Fox, Microsoft
# MIT License
# --------------------------------------------------------------------------

from gpiozero import LED
from time import sleep

# Declare leds
yellow_led = LED(17)
blue_led = LED(27)
green_led = LED(22)
red_led = LED(23)
white_led = LED(24)

# Create a dict with "LED ID" -> LED object
available_leds = {
"yellow" : yellow_led,
"blue" : blue_led,
"green" : green_led,
"red" : red_led,
"white" : white_led
}

# Keeps the led {@code led} on for 1s before turning off again
# @param led Object LED
def blink_led(led):
led.on()
sleep(1)
led.off()
sleep(1)

# Main function
while 1:
for led_color, led_object in available_leds.items():
print("Blinking %s LED" % led_color)
blink_led(led_object)
19 changes: 19 additions & 0 deletions tests/test_model_from_static_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# ------------------------------------------------------------------------
# Trash Classifier ML Project - Test script for the usage of TF Model: file access and prediction
# Expected behaviour: successfully reads the model and makes a prediction from a static image file
#
# (c) 2020 by Jen Fox, Microsoft
# MIT License
# --------------------------------------------------------------------------

from lobe import ImageModel

# Static locations for model and image for testing.
# Please make sure those files are in those static locations before running the script
STATIC_MODEL_PATH = "/home/pi/Lobe/model" # realpath for model
STATIC_JPG_PATH = "/home/pi/Pictures/image.jpg" # realpath for image

# Load model and create prediction from image
model = ImageModel.load(STATIC_MODEL_PATH)
result = model.predict_from_file(STATIC_JPG_PATH)
print("The result of the prediction was {0}.".format(result.prediction))
25 changes: 25 additions & 0 deletions tests/test_picamera.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# ------------------------------------------------------------------------
# Trash Classifier ML Project - Test script for the usage of PiCamera: file access and camera capture
# Expected behaviour: successfully turns on the camera and captures 6 images during one minute in real time.
#
# (c) 2020 by Jen Fox, Microsoft
# MIT License
# --------------------------------------------------------------------------

from picamera import PiCamera
from time import sleep

camera = PiCamera()

# Take photo and save as a jpg file (using photoPath as file realpath)
def take_photo(photoPath):
camera.start_preview(alpha=200)
sleep(3)
camera.rotation = 270
camera.capture(photoPath)
camera.stop_preview()

# take a picture and save as a file in /home/pi/Pictures/
for id in range(1,7):
take_photo("/home/pi/Pictures/image_testing_picamera_id{0}.jpg".format(id))
sleep(10);