Bob Hammell
Published © CC BY

Flat Tire Detection Using Machine Vision

Warn drivers about low tire pressure with an edge device that is trained to see if car tires are either full or flat.

IntermediateFull instructions provided7 hours3,487

Things used in this project

Hardware components

OpenMV Cam M7
OpenMV Cam M7
OpenMV Cam H7 Plus
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
RGB Diffused Common Cathode
RGB Diffused Common Cathode
×1
Battery, 3.7 V
Battery, 3.7 V
×1
Resistor 330 ohm
Resistor 330 ohm
×3

Software apps and online services

Edge Impulse Studio
Edge Impulse Studio
OpenMV IDE

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)
Soldering iron (generic)
Soldering iron (generic)
Heat gun
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires

Story

Read more

Custom parts and enclosures

Camera Housing

3D printed housing that holds OpenMV Cam, RGB LED, and battery.

Camera Stand

3D printed stand that allows for positioning and tiliting of the camera housing

Schematics

Circuit Diagram - Image

Diagram showing the pin connections between the RGB LED and the OpenMV Cam

Circuit Diagram - Fritzing File

Fritzing schematic file (.fzz) for the project circuit

Code

ei_image_classification.py

Python
Python script loaded onto the OpenMV Cam to classify images and control LED color
# Edge Impulse - OpenMV Image Classification Example

import sensor, image, time, os, tf, pyb
from pyb import Pin

sensor.reset()                         # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)
sensor.set_windowing((240, 240))       # Set 240x240 window.
sensor.skip_frames(time=2000)          # Let the camera adjust.

net = "trained.tflite"
labels = [line.rstrip('\n') for line in open("labels.txt")]

# Define output pins for RGB LED
pin_r = Pin("P7", Pin.OUT_PP)
pin_g = Pin("P8", Pin.OUT_PP)
pin_b = Pin("P9", Pin.OUT_PP)

# Function to set RGB LED color based on input label
def color_by_label(label):
    if label == 'flat':
        # Red
        pin_r.high()
        pin_g.low()
        pin_b.low()
    elif label == 'full':
        # Green
        pin_r.low()
        pin_g.high()
        pin_b.low()
    elif label == 'no-tire':
        # Yellow
        pin_r.high()
        pin_g.high()
        pin_b.low()

clock = time.clock()
while(True):
    clock.tick()

    img = sensor.snapshot()

    # default settings just do one detection... change them to search the image...
    for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):
        print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())
        img.draw_rectangle(obj.rect())
        # This combines the labels and confidence values into a list of tuples
        predictions_list = list(zip(labels, obj.output()))

        # Update RGB LED color based on the highest predicted class
        max_prediction = max(predictions_list, key=lambda item:item[1])
        color_by_label(max_prediction[0])

        for i in range(len(predictions_list)):
            print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))
            img.draw_string(5, 10*i+5,"%s = %f" % (predictions_list[i][0].upper(), predictions_list[i][1]))

    print(clock.fps(), "fps")

Code Repository

GitHub repository containing trained model, label, and Python script files for running tire image classification on an OpenMV Cam

Credits

Bob Hammell

Bob Hammell

8 projects • 21 followers
Developer interested in Python, Arduino, & Matlab. Background in Image Processing and Remote Sensing.

Comments