Skip to content
Merged
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
56 changes: 56 additions & 0 deletions modules/tflite/examples/tflite_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2026 Brad Barnett
#
# SPDX-License-Identifier: MIT
"""Run a TensorFlow Lite model: predict a sine wave.

Loads a tiny model that learned sin(x) for x from 0 to 2π, asks it for a
few values, and prints each prediction beside math.sin. The model works in
8-bit integers, so each x is scaled into the model's units on the way in
and back out again on the way out.

Needs: a MicroPython firmware built with tflite (it's opt-in: name it in
the build), and the model on the board. It is esp-vision's
models/tflite/sine/sine.tflite, from https://github.com/espressif/esp-vision.

Run it:
mpremote cp sine.tflite :
mpremote run tflite_simpletest.py

Tested on: a Waveshare ESP32-P4-WIFI6-Touch-LCD-4B on MicroPython 1.30
preview.
"""

import math

import tflite

# Change these to match your setup.
MODEL = "sine.tflite"
POINTS = 8

# Load the model, and read how it scales numbers into 8-bit integers.
model = tflite.Model(MODEL)
in_scale, in_zero = model.input_scale[0], model.input_zero_point[0]
out_scale, out_zero = model.output_scale[0], model.output_zero_point[0]
print("Loaded %s: %d bytes, %d bytes of working memory" % (MODEL, model.len, model.ram))


def predict(x):
"""The model's guess at sin(x)."""

# The model asks for its input by handing over a buffer to fill.
def fill(buf, shape, dtype):
buf[0] = max(-128, min(127, round(x / in_scale) + in_zero)) & 0xFF

# The output is one row of one number, in the model's units.
output = model.predict([fill])[0]
return (output[0][0] - out_zero) * out_scale


# Ask for a few points across one cycle, and compare with math.sin.
for i in range(POINTS):
x = 2 * math.pi * i / (POINTS - 1)
print("sin(%.2f) is %+.3f; the model says %+.3f" % (x, math.sin(x), predict(x)))

# Free the model's memory.
model.deinit()
47 changes: 47 additions & 0 deletions modules/vision/examples/vision_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# SPDX-FileCopyrightText: 2026 Brad Barnett
#
# SPDX-License-Identifier: MIT
"""Find a coloured shape in a picture.

Draws a red rectangle and a blue circle on a blank picture with the image
module, then asks find_blobs for everything red and prints where it is.
It saves the picture as a JPEG, so you can look at it yourself. With a
camera, the same find_blobs call finds red things in its pictures.

Needs: a MicroPython firmware built with vision (it's opt-in: name it in
the build), on an ESP32-S3 or ESP32-P4. No camera or display.

Run it:
mpremote run vision_simpletest.py
mpremote cp :shapes.jpg .

Tested on: a Waveshare ESP32-P4-WIFI6-Touch-LCD-4B on MicroPython 1.30
preview.
"""

import image

# Change these to match your setup.
WIDTH = 320
HEIGHT = 240
FILENAME = "shapes.jpg"

# Red, as find_blobs sees colour: lightness from 30 to 100, and well towards
# red on both of the LAB colour space's colour axes (15 to 127).
RED = (30, 100, 15, 127, 15, 127)

# Draw a red rectangle and a blue circle on a black picture.
picture = image.Image(WIDTH, HEIGHT, image.RGB565)
picture.clear()
picture.draw_rectangle(100, 60, 80, 50, color=(255, 0, 0), fill=True)
picture.draw_circle(40, 40, 20, color=(0, 0, 255), fill=True)

# Find every red patch of at least 100 pixels.
for blob in picture.find_blobs([RED], pixels_threshold=100, area_threshold=100):
print("Found red at x=%d, y=%d, %d wide and %d high" % blob.rect())

# Save the picture as a JPEG.
jpeg = picture.to_jpeg(quality=85, copy=True)
with open(FILENAME, "wb") as f:
f.write(jpeg.bytearray())
print("Saved", FILENAME)
Loading