179 lines
4.9 KiB
C
179 lines
4.9 KiB
C
#include <allegro.h>
|
|
#include <time.h>
|
|
#include "percettroni.h"
|
|
//#include "mnist/mnist_manager.h"
|
|
|
|
//CIFAR_10
|
|
#define IMAGE_WIDTH 32
|
|
#define IMAGE_HEIGHT 32
|
|
//MNIST
|
|
/* #define IMAGE_WIDTH 28
|
|
#define IMAGE_HEIGHT 28*/
|
|
|
|
#define SCALE_FACTOR 2
|
|
|
|
//Cavalli
|
|
#define CATEGORIA 7
|
|
|
|
BITMAP *buffer;
|
|
BITMAP *image;
|
|
int *previsto;
|
|
ReteNeurale *rete_neurale;
|
|
Dataset *set;
|
|
|
|
|
|
void init_allegro();
|
|
void carica_immagine(int);
|
|
void disegna_interfaccia();
|
|
void evento_click_bottone(int);
|
|
int prevedi(int);
|
|
byte get_out_corretto(byte);
|
|
|
|
void main()
|
|
{
|
|
init_allegro();
|
|
|
|
//get_dataset("cifar-10-batches/test_batch.bin");
|
|
set = get_dataset(file_immagini);//get_dataset(file_immagini, file_label);
|
|
if (set == NULL) {
|
|
printf("Errore nel caricare il dataset\n");
|
|
return;
|
|
}
|
|
|
|
rete_neurale = caricaReteNeurale(file_pesi);
|
|
if (rete_neurale == NULL) {
|
|
printf("Errore nel caricare il modello\n");
|
|
return;
|
|
}
|
|
|
|
int indice_set = rand() % set->size;
|
|
|
|
// Carica la prima immagine
|
|
carica_immagine(indice_set);
|
|
|
|
while(!key[KEY_ESC]) {
|
|
disegna_interfaccia();
|
|
indice_set = rand() % set->size;
|
|
evento_click_bottone(indice_set);
|
|
rest(10);
|
|
}
|
|
|
|
destroy_bitmap(buffer);
|
|
destroy_bitmap(image);
|
|
allegro_exit();
|
|
}
|
|
|
|
void init_allegro() {
|
|
allegro_init();
|
|
install_keyboard();
|
|
install_mouse();
|
|
set_color_depth(32);
|
|
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
|
|
buffer = create_bitmap(800, 600);
|
|
image = create_bitmap(IMAGE_WIDTH, IMAGE_HEIGHT);
|
|
show_mouse(screen);
|
|
}
|
|
|
|
//cifar_10
|
|
void carica_immagine(int indice_set)
|
|
{
|
|
printf("Immagine indice: %d, categoria: %d, previsione: %d\n", indice_set, set->istanze[indice_set].classificazione, prevedi(indice_set));
|
|
|
|
for (int y = 0; y < IMAGE_HEIGHT; y++)
|
|
{
|
|
for (int x = 0; x < IMAGE_WIDTH; x++)
|
|
{
|
|
int r = set->istanze[indice_set].dati[y * IMAGE_WIDTH + x];
|
|
int g = set->istanze[indice_set].dati[1024 + y * IMAGE_WIDTH + x];
|
|
int b = set->istanze[indice_set].dati[2048 + y * IMAGE_WIDTH + x];
|
|
putpixel(image, x, y, makecol(r, g, b));
|
|
}
|
|
}
|
|
}
|
|
|
|
//MNIST
|
|
/* void carica_immagine(int indice_set)
|
|
{
|
|
// Stampa informazioni sull'immagine
|
|
printf("Immagine indice: %d, valore: %d. è un 7? %d\n", indice_set, set->istanze[indice_set].classificazione, prevedi(indice_set));
|
|
|
|
// Itera su ogni pixel dell'immagine
|
|
for (int y = 0; y < IMAGE_HEIGHT; y++)
|
|
{
|
|
for (int x = 0; x < IMAGE_WIDTH; x++)
|
|
{
|
|
// Ottieni il valore del pixel (scala di grigi, quindi un solo canale)
|
|
int gray_value = set->istanze[indice_set].dati[y * IMAGE_WIDTH + x];
|
|
|
|
// Converti il valore in scala di grigi in un colore RGB (r = g = b = gray_value)
|
|
int color = makecol(gray_value, gray_value, gray_value);
|
|
|
|
// Disegna il pixel sull'immagine
|
|
putpixel(image, x, y, color);
|
|
}
|
|
}
|
|
} */
|
|
|
|
void disegna_interfaccia()
|
|
{
|
|
clear_to_color(buffer, makecol(255, 255, 255));
|
|
|
|
// Calcola la posizione per centrare l'immagine ingrandita
|
|
int scaled_width = IMAGE_WIDTH * SCALE_FACTOR;
|
|
int scaled_height = IMAGE_HEIGHT * SCALE_FACTOR;
|
|
int image_x = (800 - scaled_width) / 2;
|
|
int image_y = (600 - scaled_height) / 2 - 20; // Sposta leggermente sopra per fare spazio al pulsante
|
|
|
|
// Disegna l'immagine ingrandita
|
|
stretch_blit(image, buffer, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, image_x, image_y, scaled_width, scaled_height);
|
|
|
|
// Disegna il pulsante "prossima"
|
|
int button_width = 150;
|
|
int button_height = 40;
|
|
int button_x = (800 - button_width) / 2;
|
|
int button_y = 600 - 60; // Posizione in basso
|
|
|
|
rectfill(buffer, button_x, button_y, button_x + button_width, button_y + button_height, makecol(200, 200, 200));
|
|
textout_centre_ex(buffer, font, "prossima", button_x + button_width / 2, button_y + 10, makecol(0, 0, 0), -1);
|
|
|
|
// Copia il buffer sullo schermo
|
|
blit(buffer, screen, 0, 0, 0, 0, 800, 600);
|
|
}
|
|
|
|
void evento_click_bottone(int indice_set)
|
|
{
|
|
if (mouse_b & 1)
|
|
{
|
|
int mx = mouse_x;
|
|
int my = mouse_y;
|
|
|
|
// Coordinate del pulsante
|
|
int button_width = 150;
|
|
int button_height = 40;
|
|
int button_x = (800 - button_width) / 2;
|
|
int button_y = 600 - 60;
|
|
|
|
// Controlla se il clic è avvenuto sul pulsante
|
|
if (mx >= button_x && mx <= button_x + button_width && my >= button_y && my <= button_y + button_height)
|
|
{
|
|
carica_immagine(indice_set);
|
|
rest(200); // Debounce
|
|
}
|
|
}
|
|
}
|
|
|
|
int prevedi(int indice_set)
|
|
{
|
|
double **sigmoidi = elabora_sigmoidi(*rete_neurale, set->istanze[indice_set]);
|
|
|
|
byte output_corretto = get_out_corretto(set->istanze[indice_set].classificazione);
|
|
|
|
return previsione(sigmoidi[rete_neurale->size - 1][0]);
|
|
}
|
|
|
|
byte get_out_corretto(byte categoria) {
|
|
if(categoria == CATEGORIA)
|
|
return 1;
|
|
else
|
|
return 0;
|
|
} |