RuVic saballa
Published © MIT

Edge Impulse FOMO tflite(float)-kmodel K210 Maixbit

Running a tflite model generated by EdgeImpulse to Sipeed MAix BiT for RISC-V AI(kmodel)

IntermediateWork in progress5 hours179
Edge Impulse FOMO tflite(float)-kmodel K210 Maixbit

Things used in this project

Hardware components

MAIX-I Series Boards
Sipeed MAIX-I Series Boards
×1
Flash Memory Card, SD Card
Flash Memory Card, SD Card
×1

Software apps and online services

Sipeed MaixPy IDE
Edge Impulse Studio
Edge Impulse Studio

Story

Read more

Code

Sample

MicroPython
import sensor
import image
import time
import gc
import sys
import lcd
import _thread
from Maix import GPIO
import KPU as kpu
from board import board_info
from fpioa_manager import fm

# Initialize the sensor
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
# sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((240, 240))
# sensor.skip_frames(time=2000)
sensor.set_auto_gain(1)
sensor.set_auto_exposure(1)
sensor.set_auto_whitebal(1)
sensor.skip_frames(30)
sensor.set_colorbar(0)
sensor.run(1)

lock = _thread.allocate_lock()

print("Ready to load model")

# Perform memory test
kpu.memtest()

# Initialize clock
clock = time.clock()

# Load model
try:
    task = kpu.load("/sd/riceuno1.kmodel")
    print("Model loaded")
except Exception as e:
    print("Error loading model:", e)
    sys.exit(1)  # Terminate script if model loading fails

# Threshold and size for FOMO
threshold_fomo = 0.96
fomo_size_nn = 12
global fmap, img_cam, img  # Declare global variables

fmap = None  # Initialize fmap
img=None
# Set output for KPU task
kpu.set_outputs(task, 0, fomo_size_nn, fomo_size_nn, 2)

def fomo_list(fomo_size, fomo_fmap, ch, fomo_threshold=0.95):
    global thread1_done

    try:
        # Find indices of elements that are greater than or equal to the threshold
        indices_of_high_values = [index for index, value in enumerate(fomo_fmap[(fomo_size * fomo_size) * ch:(fomo_size * fomo_size) * (ch + 1)]) if value >= fomo_threshold]
        # Use list comprehensions to find values and indices above 90
        values_above_90 = [round(value, 2) for value in fomo_fmap[(fomo_size * fomo_size) * ch:(fomo_size * fomo_size) * (ch + 1)] if value >= fomo_threshold]
        print("Raw Values above:", values_above_90)
        # Print the results
        print("Indices of values above:", indices_of_high_values)
        print("Indices of elements >= ", fomo_threshold, ": ", indices_of_high_values)
        print((fomo_size * fomo_size) * ch)

        # Create a grid
        grid = [[0 for _ in range(fomo_size)] for _ in range(fomo_size)]

        # Update the grid based on the indices of high values
        for index in indices_of_high_values:
            row = index // fomo_size
            col = index % fomo_size
            if ch == 1:
                img.draw_circle((col + 1) * 20 - 4, (row + 1) * 20 - 4, 16, color=(255, 0, 0), fill=False)
            # if ch == 2:
            #     img.draw_circle((col + 1) * 20 - 4, (row + 1) * 20 - 4, 16, color=(0, 255, 0), fill=False)
            grid[row][col] = 1

        print("\nGrid:")
        for row in grid:
            print(" ".join(map(str, row)))

        print("FOMO processing finished")
    except Exception as e:
        print("Error in FOMO processing:", e)
    finally:
        print("Thread End")
        # _thread.exit()

def thread_core_1():
    global lock, img_cam, img, fmap
    while True:
        img = sensor.snapshot()
        img_cam = img.copy()  # consume a lot of process
        img_cam = img_cam.resize(96, 96)  # consume a lot of process
        img_cam = img_cam.to_grayscale(copy=False)  # consume a lot of process

        img_cam.pix_to_ai()
        lock.acquire()
        fmap = kpu.forward(task, img_cam, 0)
        lock.release()
        time.sleep(0.05)

scan = _thread.start_new_thread(thread_core_1, ())

def main():
    global img, img_cam, fmap
    global lock, fmap
    print("Start")

    while True:
        try:

            lock.acquire()
            if fmap is not None:
                clock.tick()
                fomo_list(fomo_size_nn, fmap, 1, threshold_fomo)
                print("FOMO------------------")
                fps = clock.fps()
                print("FPS:", fps)
                img.draw_string(2, 220, ("%2.1ffps" % (fps)), color=(0, 255, 128), scale=2)

            lock.release()

            print("Done")
            time.sleep(0.01)  # Pause before repeating the process
        except Exception as e:
            print("Error in main loop:", e)
            break

try:
    main()
except Exception as e:
    print("Unhandled exception:", e)
finally:
    try:
        kpu.deinit(task)
    except Exception as e:
        print("Error during cleanup:", e)

Credits

RuVic saballa
6 projects • 4 followers
Contact

Comments

Please log in or sign up to comment.