Skip to content
Snippets Groups Projects
main.cpp 14.87 KiB
#include "pico/stdlib.h"
#include <stdio.h>

#include "tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_log.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

#include "sign_model.h"

// TEST LETTERS
unsigned char letter_n[] = {178, 179, 180, 180, 180, 180, 180, 181, 180, 180, 180, 182, 182, 182, 183, 184, 181, 181, 182, 182, 181, 181, 181, 181, 180, 178, 177, 178, 179, 180, 180, 180, 180, 181, 182, 181, 180, 182, 183, 182, 180, 180, 178, 178, 187, 186, 184, 183, 180, 181, 182, 182, 181, 180, 178, 179, 180, 181, 181, 181, 182, 182, 181, 181, 181, 182, 181, 183, 194, 197, 178, 144, 138, 169, 167, 182, 183, 183, 184, 184, 182, 182, 180, 179, 180, 181, 181, 181, 182, 182, 181, 181, 182, 182, 181, 202, 213, 211, 197, 151, 107, 84, 115, 119, 185, 183, 183, 182, 182, 182, 180, 181, 181, 181, 181, 181, 181, 182, 183, 183, 181, 183, 183, 172, 175, 201, 203, 162, 115, 71, 98, 75, 153, 191, 181, 184, 182, 183, 183, 183, 181, 181, 181, 182, 183, 183, 182, 184, 184, 152, 170, 169, 147, 167, 193, 175, 127, 76, 111, 118, 133, 195, 183, 185, 184, 184, 183, 183, 180, 181, 183, 183, 183, 184, 180, 199, 200, 143, 145, 172, 176, 182, 176, 180, 154, 77, 111, 110, 125, 195, 185, 185, 185, 185, 183, 183, 182, 182, 183, 184, 184, 183, 182, 215, 201, 167, 143, 113, 152, 177, 170, 141, 106, 67, 59, 114, 174, 188, 187, 186, 185, 186, 185, 185, 181, 182, 182, 184, 182, 193, 154, 136, 183, 191, 169, 108, 53, 129, 174, 147, 123, 103, 79, 99, 196, 186, 187, 186, 186, 186, 186, 185, 180, 182, 182, 176, 196, 203, 137, 89, 113, 202, 182, 141, 53, 45, 100, 130, 156, 168, 143, 85, 131, 200, 185, 187, 187, 185, 185, 184, 181, 181, 180, 196, 206, 180, 131, 111, 71, 175, 194, 144, 103, 44, 44, 66, 180, 191, 177, 141, 83, 158, 195, 184, 185, 185, 185, 183, 182, 180, 191, 207, 172, 169, 144, 133, 67, 126, 203, 155, 137, 68, 54, 47, 131, 205, 191, 157, 113, 90, 184, 187, 184, 186, 185, 184, 182, 183, 198, 174, 137, 162, 157, 140, 89, 56, 182, 184, 157, 109, 42, 78, 148, 193, 193, 158, 134, 89, 145, 196, 184, 186, 186, 186, 181, 188, 203, 185, 148, 149, 170, 138, 127, 66, 77, 195, 173, 146, 90, 111, 206, 186, 174, 148, 134, 91, 140, 198, 186, 187, 187, 187, 180, 188, 197, 182, 163, 135, 152, 160, 157, 119, 36, 99, 187, 176, 164, 129, 194, 180, 158, 124, 107, 79, 158, 195, 187, 186, 187, 186, 183, 187, 200, 197, 179, 156, 127, 147, 164, 142, 121, 46, 151, 177, 163, 178, 184, 166, 148, 118, 86, 77, 179, 190, 187, 187, 187, 185, 174, 175, 201, 207, 193, 170, 155, 142, 146, 145, 160, 98, 177, 197, 190, 193, 177, 158, 138, 112, 80, 99, 197, 192, 192, 191, 189, 188, 102, 99, 191, 210, 200, 181, 165, 161, 162, 144, 117, 138, 197, 207, 207, 184, 171, 156, 131, 102, 76, 117, 179, 173, 177, 177, 179, 181, 87, 69, 159, 214, 201, 190, 175, 165, 170, 169, 140, 134, 185, 206, 202, 185, 158, 142, 123, 93, 82, 87, 93, 96, 96, 96, 96, 97, 92, 81, 117, 207, 196, 195, 190, 177, 170, 168, 158, 124, 151, 201, 193, 178, 156, 128, 108, 88, 81, 89, 82, 76, 77, 87, 116, 142, 90, 90, 85, 180, 190, 190, 193, 190, 177, 168, 156, 140, 125, 180, 181, 164, 146, 119, 98, 86, 77, 90, 108, 128, 156, 178, 190, 182, 89, 93, 82, 122, 194, 184, 193, 196, 182, 169, 153, 140, 127, 146, 168, 149, 125, 112, 97, 78, 83, 150, 187, 194, 190, 184, 187, 181, 90, 89, 92, 80, 157, 192, 189, 189, 173, 161, 148, 135, 125, 125, 143, 142, 127, 108, 92, 65, 138, 218, 195, 182, 175, 161, 163, 172, 90, 90, 91, 84, 95, 189, 185, 170, 158, 150, 142, 132, 113, 108, 129, 146, 134, 100, 80, 69, 191, 203, 189, 177, 153, 134, 130, 145, 90, 89, 88, 91, 77, 160, 195, 161, 142, 144, 139, 129, 112, 95, 132, 157, 123, 89, 64, 116, 219, 194, 194, 177, 133, 106, 126, 136, 90, 90, 87, 93, 80, 128, 198, 167, 157, 157, 143, 125, 112, 95, 136, 155, 102, 84, 61, 177, 209, 190, 205, 188, 138, 92, 111, 136, 90, 91, 87, 92, 81, 112, 200, 178, 170, 170, 150, 131, 122, 97, 134, 144, 97, 74, 81, 207, 188, 184, 206, 206, 157, 98, 94, 127, 91, 92, 87, 91, 81, 111, 192, 186, 181, 175, 153, 141, 131, 92, 127, 138, 98, 63, 109, 210, 186, 180, 196, 213, 175, 107, 84, 107};
unsigned char letter_b[] = {194, 196, 198, 200, 200, 202, 203, 202, 202, 203, 204, 204, 206, 206, 206, 206, 206, 206, 204, 205, 205, 206, 205, 204, 203, 205, 203, 203, 197, 201, 204, 203, 205, 205, 205, 206, 207, 209, 208, 208, 208, 210, 208, 209, 208, 210, 211, 208, 207, 207, 208, 208, 207, 205, 204, 204, 203, 206, 206, 207, 209, 210, 210, 211, 211, 212, 213, 214, 213, 212, 215, 213, 215, 203, 202, 215, 212, 212, 211, 210, 209, 207, 207, 206, 208, 211, 210, 213, 213, 214, 214, 215, 216, 218, 218, 217, 219, 219, 180, 206, 235, 197, 145, 224, 216, 215, 215, 214, 212, 212, 211, 211, 211, 215, 215, 218, 218, 217, 218, 219, 218, 221, 223, 220, 230, 233, 139, 199, 241, 182, 138, 222, 212, 222, 218, 217, 215, 215, 214, 213, 215, 218, 219, 222, 222, 222, 222, 225, 223, 224, 221, 211, 243, 218, 139, 212, 236, 158, 160, 221, 155, 204, 227, 220, 220, 219, 219, 218, 218, 220, 221, 223, 227, 227, 226, 228, 227, 232, 214, 165, 221, 196, 143, 231, 230, 143, 181, 233, 139, 183, 236, 223, 223, 222, 221, 221, 222, 223, 224, 228, 230, 229, 230, 230, 228, 243, 211, 147, 199, 192, 153, 242, 234, 152, 203, 235, 139, 184, 240, 226, 227, 225, 224, 224, 225, 227, 229, 231, 234, 234, 232, 235, 232, 245, 189, 148, 196, 182, 164, 233, 217, 150, 222, 230, 132, 190, 240, 230, 231, 229, 229, 228, 228, 229, 232, 234, 236, 237, 236, 237, 235, 252, 185, 159, 182, 165, 145, 225, 192, 146, 239, 231, 139, 204, 242, 236, 235, 233, 232, 231, 230, 232, 236, 236, 238, 237, 239, 240, 238, 252, 174, 153, 182, 175, 135, 243, 186, 163, 249, 214, 142, 218, 243, 238, 237, 237, 236, 235, 233, 236, 237, 239, 239, 240, 240, 241, 244, 255, 186, 139, 183, 177, 139, 247, 181, 179, 249, 201, 139, 232, 245, 240, 239, 238, 237, 236, 235, 239, 241, 242, 243, 244, 244, 242, 246, 255, 191, 124, 162, 144, 127, 219, 150, 209, 254, 202, 149, 246, 246, 244, 242, 241, 240, 238, 237, 239, 243, 245, 245, 246, 246, 244, 251, 255, 195, 152, 196, 212, 191, 219, 196, 182, 235, 189, 163, 255, 245, 246, 245, 245, 244, 241, 241, 243, 243, 245, 246, 248, 248, 246, 255, 254, 206, 174, 155, 215, 245, 237, 236, 170, 171, 171, 178, 255, 248, 249, 247, 246, 245, 244, 242, 245, 245, 246, 249, 250, 250, 250, 255, 240, 190, 159, 128, 137, 174, 200, 252, 228, 147, 111, 203, 255, 249, 251, 249, 248, 247, 245, 245, 246, 249, 250, 251, 253, 253, 253, 255, 243, 190, 147, 137, 142, 146, 192, 255, 255, 191, 104, 226, 255, 252, 253, 252, 252, 251, 248, 245, 248, 251, 253, 253, 255, 251, 255, 255, 237, 183, 153, 152, 159, 206, 232, 229, 254, 212, 131, 234, 255, 254, 253, 252, 252, 252, 252, 247, 249, 251, 254, 255, 255, 252, 255, 255, 233, 189, 153, 166, 170, 239, 255, 232, 208, 187, 130, 195, 255, 254, 255, 255, 254, 253, 253, 248, 249, 252, 255, 255, 255, 254, 255, 255, 236, 194, 169, 169, 200, 255, 255, 223, 195, 152, 114, 203, 255, 255, 255, 255, 255, 255, 253, 251, 252, 254, 255, 255, 255, 255, 255, 255, 236, 195, 183, 167, 220, 255, 255, 208, 177, 139, 110, 236, 255, 255, 255, 255, 255, 255, 254, 252, 254, 255, 255, 255, 255, 255, 255, 255, 236, 207, 193, 169, 217, 255, 246, 195, 163, 123, 135, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 238, 213, 206, 164, 221, 255, 224, 181, 148, 111, 202, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 242, 215, 205, 164, 231, 247, 212, 175, 131, 126, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 243, 222, 192, 166, 223, 212, 193, 161, 113, 172, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 233, 188, 179, 211, 190, 180, 149, 112, 233, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 254, 255, 255, 255, 233, 196, 194, 198, 183, 169, 123, 167, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 239, 241, 223, 193, 190, 175, 170, 135, 129, 250, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
unsigned char letter_y[] = {161, 166, 171, 176, 178, 181, 183, 186, 186, 187, 188, 189, 189, 189, 190, 190, 189, 188, 188, 188, 187, 186, 185, 185, 184, 184, 181, 180, 162, 167, 173, 177, 178, 181, 184, 186, 187, 189, 191, 191, 191, 191, 192, 190, 190, 190, 190, 189, 188, 187, 185, 186, 186, 184, 182, 181, 164, 169, 173, 176, 183, 183, 185, 187, 188, 190, 191, 192, 192, 191, 192, 192, 190, 191, 192, 190, 189, 189, 187, 187, 186, 183, 183, 182, 165, 168, 185, 198, 165, 186, 187, 189, 190, 192, 192, 193, 193, 192, 194, 193, 192, 192, 192, 190, 190, 190, 189, 189, 187, 184, 185, 183, 168, 165, 204, 219, 138, 189, 189, 190, 191, 193, 194, 195, 195, 194, 195, 195, 194, 192, 192, 191, 191, 191, 190, 189, 188, 185, 186, 185, 169, 167, 207, 212, 132, 188, 191, 191, 193, 194, 196, 198, 198, 198, 197, 196, 195, 195, 194, 193, 192, 192, 191, 190, 190, 188, 186, 185, 171, 168, 206, 219, 130, 180, 194, 193, 195, 196, 197, 198, 199, 199, 198, 197, 197, 197, 195, 194, 194, 193, 193, 191, 190, 189, 188, 187, 172, 170, 201, 226, 140, 176, 196, 193, 196, 198, 198, 198, 199, 199, 199, 199, 200, 199, 197, 197, 195, 195, 194, 193, 192, 191, 190, 189, 173, 173, 196, 230, 144, 166, 200, 195, 198, 198, 200, 201, 199, 201, 203, 201, 200, 199, 198, 197, 196, 196, 195, 194, 193, 193, 191, 189, 173, 175, 193, 237, 165, 148, 198, 198, 198, 206, 202, 196, 214, 209, 190, 203, 200, 200, 201, 199, 198, 196, 195, 194, 194, 194, 193, 191, 174, 179, 190, 246, 193, 148, 230, 209, 183, 239, 205, 154, 227, 225, 155, 174, 211, 203, 202, 201, 200, 201, 199, 199, 195, 194, 193, 192, 175, 180, 188, 236, 200, 180, 255, 208, 190, 243, 208, 156, 220, 212, 168, 136, 202, 207, 204, 203, 200, 194, 188, 183, 197, 194, 194, 192, 176, 183, 185, 232, 220, 214, 255, 208, 189, 251, 200, 168, 237, 199, 157, 117, 176, 213, 203, 204, 180, 157, 138, 117, 176, 201, 195, 194, 178, 186, 185, 236, 242, 228, 255, 216, 173, 253, 189, 172, 236, 193, 135, 103, 167, 209, 203, 198, 158, 127, 131, 156, 189, 200, 197, 196, 179, 184, 193, 233, 235, 224, 254, 221, 164, 237, 191, 176, 226, 182, 110, 80, 161, 212, 202, 175, 136, 154, 201, 207, 202, 199, 198, 197, 180, 182, 204, 230, 204, 209, 235, 218, 159, 227, 197, 176, 222, 159, 92, 122, 177, 205, 166, 138, 146, 209, 207, 203, 203, 201, 199, 198, 182, 183, 212, 253, 190, 183, 218, 207, 152, 231, 189, 178, 217, 147, 179, 206, 193, 168, 129, 134, 201, 210, 206, 206, 206, 205, 202, 200, 183, 181, 218, 255, 206, 171, 208, 202, 165, 238, 167, 165, 221, 211, 223, 198, 171, 153, 123, 195, 216, 209, 209, 208, 207, 206, 204, 201, 182, 180, 220, 255, 219, 162, 201, 212, 197, 230, 153, 193, 246, 219, 201, 190, 155, 133, 175, 219, 209, 209, 208, 208, 207, 205, 203, 201, 183, 182, 220, 255, 222, 176, 192, 205, 191, 210, 221, 250, 234, 210, 196, 175, 137, 147, 217, 213, 212, 210, 210, 208, 206, 205, 204, 201, 183, 182, 219, 255, 229, 202, 186, 211, 250, 255, 255, 236, 219, 207, 188, 150, 128, 195, 218, 213, 213, 212, 212, 210, 208, 206, 206, 203, 183, 184, 209, 255, 241, 210, 199, 223, 255, 255, 255, 229, 207, 187, 169, 131, 174, 221, 214, 215, 214, 215, 212, 211, 210, 207, 207, 205, 182, 187, 196, 255, 244, 214, 207, 230, 255, 255, 240, 223, 193, 167, 141, 156, 221, 216, 215, 214, 214, 213, 210, 210, 210, 207, 207, 206, 184, 191, 200, 250, 231, 214, 209, 231, 254, 241, 220, 201, 178, 151, 144, 215, 225, 222, 223, 221, 220, 218, 216, 215, 214, 216, 212, 204, 157, 156, 163, 238, 224, 221, 217, 226, 241, 231, 202, 175, 160, 137, 145, 164, 158, 158, 158, 157, 156, 157, 157, 157, 157, 146, 150, 180, 132, 129, 124, 227, 233, 228, 225, 229, 236, 220, 183, 159, 148, 131, 125, 121, 120, 120, 119, 117, 116, 117, 118, 109, 104, 142, 195, 207, 139, 140, 127, 226, 252, 231, 233, 231, 226, 207, 172, 151, 138, 131, 133, 133, 132, 131, 130, 129, 127, 128, 126, 142, 199, 229, 210, 185, 139, 140, 124, 218, 255, 236, 243, 224, 212, 202, 175, 142, 130, 131, 130, 132, 132, 130, 130, 129, 129, 123, 159, 230, 230, 198, 196, 172};

// TENSORFLOW LITE GLOBALS
tflite::ErrorReporter* error_reporter;
const tflite::Model* model;
tflite::MicroInterpreter* interpreter;
TfLiteTensor* input;
TfLiteTensor* output;

const int kTensorArenaSize = 136 * 1024;
uint8_t tensor_arena[kTensorArenaSize];

// FUNCTIONS PROTOTYPE
char get_letter_from_model(float* model_output);
void setup_tflite();

int main()
{
	stdio_init_all(); // INIT SERIAL
	sleep_ms(5000);

	// --------SETUP TENSORFLOW LITE--------
	printf("TENSORFLOW LITE SETUP \n\n");
	setup_tflite();

	// --------TEST THE MODEL--------
	printf("MODEL TEST \n\n");
	printf("LETTER N\n");
	for (size_t i = 0; i < 784; i++)
	{
		input->data.f[i] = letter_n[i];
	}
	
	if (kTfLiteOk != interpreter->Invoke()) {
		TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed.");
	}

	printf("Model found %c !!\n\n", get_letter_from_model(output->data.f));

	printf("LETTER B\n");
	for (size_t i = 0; i < 784; i++)
	{
		input->data.f[i] = letter_b[i];
	}
	
	if (kTfLiteOk != interpreter->Invoke()) {
		TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed.");
	}

	printf("Model found %c !!\n\n", get_letter_from_model(output->data.f));

	printf("LETTER Y\n");
	for (size_t i = 0; i < 784; i++)
	{
		input->data.f[i] = letter_y[i];
	}
	
	if (kTfLiteOk != interpreter->Invoke()) {
		TF_LITE_REPORT_ERROR(error_reporter, "Invoke failed.");
	}

	printf("Model found %c !!\n\n", get_letter_from_model(output->data.f));

	printf("MODEL TEST FINISHED \n\n");
	while (true)
	{

	}
	return 0;
}

void setup_tflite()
{
	tflite::InitializeTarget();

	// Set up logging
	static tflite::MicroErrorReporter micro_error_reporter;
	error_reporter = &micro_error_reporter;

	// Map the model into a usable data structure
	model = tflite::GetModel(sign_model);
	if (model->version() != TFLITE_SCHEMA_VERSION)
	{
		MicroPrintf(
			"Model provided is schema version %d not equal "
			"to supported version %d.",
			model->version(), TFLITE_SCHEMA_VERSION);
		return;
	}

	// Pull in only the operation implementations we need.
	static tflite::MicroMutableOpResolver<5> micro_op_resolver;
	micro_op_resolver.AddFullyConnected();
	micro_op_resolver.AddConv2D();
	micro_op_resolver.AddMaxPool2D();
	micro_op_resolver.AddSoftmax();
	micro_op_resolver.AddReshape();

	// Build an interpreter to run the model with.
	static tflite::MicroInterpreter static_interpreter(
	model, micro_op_resolver, tensor_arena, kTensorArenaSize);
	interpreter = &static_interpreter;

	// Allocate memory from the tensor_arena for the model's tensors.
	TfLiteStatus allocate_status = interpreter->AllocateTensors();
	if (allocate_status != kTfLiteOk) {
		TF_LITE_REPORT_ERROR(error_reporter, "AllocateTensors() failed");
		return;

	}

	// Get model input/output handlers
	input = interpreter->input(0);
	output = interpreter->output(0);
}

char get_letter_from_model(float* model_output)
{
	char ALPHABET[] = "ABCDEFGHIJKLMNOPQRSTUVWXY";
	float max = 0.0f;
	int letter_index = 25;

	for (int i = 0; i < 25; i++)
	{
		if(model_output[i] > max)
		{
			max = model_output[i];
			letter_index = i;
		}
	}

	return ALPHABET[letter_index];
}