Shish
Dependencies: mbed USBDevice GLCD
Diff: main.cpp
- Revision:
- 5:2dce07562de8
- Parent:
- 4:fc3364c83df6
- Child:
- 6:3a0fd4eef371
--- a/main.cpp Fri Jul 30 09:51:38 2021 +0000 +++ b/main.cpp Fri Nov 05 11:31:35 2021 +0000 @@ -5,29 +5,102 @@ #include "HzLib.c" #include "USBSerial.h" #include "TouchPanel.h" - +#include "DeepNeuralNetwork.h" + #define DEBUG +DeepNeuralNetwork dnn; + +const int sample_2[28][28] = { + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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, 1, 1, 1, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + }; + +uint8_t *letter; + +uint8_t *letter0 = (uint8_t *)"0"; +uint8_t *letter1 = (uint8_t *)"1"; +uint8_t *letter2 = (uint8_t *)"2"; +uint8_t *letter3 = (uint8_t *)"3"; +uint8_t *letter4 = (uint8_t *)"4"; +uint8_t *letter5 = (uint8_t *)"5"; +uint8_t *letter6 = (uint8_t *)"6"; +uint8_t *letter7 = (uint8_t *)"7"; +uint8_t *letter8 = (uint8_t *)"8"; +uint8_t *letter9 = (uint8_t *)"9"; + +uint8_t *letterA = (uint8_t *)"A"; +uint8_t *letterB = (uint8_t *)"B"; +uint8_t *letterC = (uint8_t *)"C"; +uint8_t *letterD = (uint8_t *)"D"; +uint8_t *letterE = (uint8_t *)"E"; +uint8_t *letterF = (uint8_t *)"F"; +uint8_t *letterG = (uint8_t *)"G"; +uint8_t *letterH = (uint8_t *)"H"; +uint8_t *letterI = (uint8_t *)"I"; +uint8_t *letterJ = (uint8_t *)"J"; +uint8_t *letterK = (uint8_t *)"K"; +uint8_t *letterL = (uint8_t *)"L"; +uint8_t *letterM = (uint8_t *)"M"; +uint8_t *letterN = (uint8_t *)"N"; +uint8_t *letterO = (uint8_t *)"O"; +uint8_t *letterP = (uint8_t *)"P"; +uint8_t *letterQ = (uint8_t *)"Q"; +uint8_t *letterR = (uint8_t *)"R"; +uint8_t *letterS = (uint8_t *)"S"; +uint8_t *letterT = (uint8_t *)"T"; +uint8_t *letterU = (uint8_t *)"U"; +uint8_t *letterV = (uint8_t *)"V"; +uint8_t *letterW = (uint8_t *)"W"; +uint8_t *letterX = (uint8_t *)"X"; +uint8_t *letterY = (uint8_t *)"Y"; +uint8_t *letterZ = (uint8_t *)"Z"; + //FUNZIONI GIORGIO void readtouch(); - + //VARIABILI GIORGIO Coordinate tocco; int x,y,k,avg_x,avg_y,ready; - + int startGame=0; int isWritten=0; int inferenceStarted=0; -uint8_t drawnPoints[28][28]; -uint8_t finalMatrix[28][28]; -int threshold = 16; +int drawnPoints[28][28]; +int threshold = 32; USBSerial serial; - + int charPos; - + /*Buttons*/ typedef enum {space,infer,del} button; - + /* Private function prototypes -----------------------------------------------*/ void spaceText(int x1, int x2, int yText, int spessore, uint16_t color); void clearPad(void); @@ -38,7 +111,7 @@ void start(void); void drawTriangle(int posX, int posY, int lungMax); void readTouch(void); - + int main() { LCD_Initialization(); TP_Init(); @@ -55,7 +128,7 @@ wait_us(500); } } - + /******************************************************************************* * Function Name : spaceText * Description : @@ -71,7 +144,7 @@ } return; } - + /******************************************************************************* * Function Name : clearPad * Description : @@ -86,14 +159,12 @@ LCD_DrawLine(8,52+i,232,52+i,White); } for(i=0;i<28;i++) //also clear the matrix - for(j=0;j<28;j++){ + for(j=0;j<28;j++) drawnPoints[i][j]=0; - finalMatrix[i][j]=0; - } return; } - + /******************************************************************************* * Function Name : showCompression * Description : @@ -109,12 +180,12 @@ } for(i=0;i<28;i++) for(j=0;j<28;j++) - if(finalMatrix[i][j]==1) + if(drawnPoints[i][j]==1) LCD_SetPoint(i+110,j+150,Black); return; } - + /******************************************************************************* * Function Name : clearBar * Description : @@ -130,7 +201,7 @@ } return; } - + /******************************************************************************* * Function Name : drawButton * Description : @@ -163,7 +234,7 @@ } return; } - + /******************************************************************************* * Function Name : drawButtons * Description : @@ -178,7 +249,7 @@ drawButton(del,White); return; } - + /******************************************************************************* * Function Name : drawTriangle * Description : @@ -197,7 +268,7 @@ LCD_DrawLine(posX+5,posY+lungMax+i+1,posX+5+(lungMax-2)-i,posY+lungMax+i+1,color); } } - + /******************************************************************************* * Function Name : start * Description : @@ -223,8 +294,92 @@ return; } +int predict() { + int image[28][28]; + for (int i=0; i<28; i++) + for(int j=0; j<28; j++) + image[i][j] = drawnPoints[j][i]; + + int prediction = dnn.inference(image); + + if (prediction == 0) + letter = letter0; + else if (prediction == 1) + letter = letter1; + else if (prediction == 2) + letter = letter2; + else if (prediction == 3) + letter = letter3; + else if (prediction == 4) + letter = letter4; + else if (prediction == 5) + letter = letter5; + else if (prediction == 6) + letter = letter6; + else if (prediction == 7) + letter = letter7; + else if (prediction == 8) + letter = letter8; + else if (prediction == 9) + letter = letter9; + else if (prediction == 10) + letter = letterA; + else if (prediction == 11) + letter = letterB; + else if (prediction == 12) + letter = letterC; + else if (prediction == 13) + letter = letterD; + else if (prediction == 14) + letter = letterE; + else if (prediction == 15) + letter = letterF; + else if (prediction == 16) + letter = letterG; + else if (prediction == 17) + letter = letterH; + else if (prediction == 18) + letter = letterI; + else if (prediction == 19) + letter = letterJ; + else if (prediction == 20) + letter = letterK; + else if (prediction == 21) + letter = letterL; + else if (prediction == 22) + letter = letterM; + else if (prediction == 23) + letter = letterN; + else if (prediction == 24) + letter = letterO; + else if (prediction == 25) + letter = letterP; + else if (prediction == 26) + letter = letterQ; + else if (prediction == 27) + letter = letterR; + else if (prediction == 28) + letter = letterS; + else if (prediction == 29) + letter = letterT; + else if (prediction == 30) + letter = letterU; + else if (prediction == 31) + letter = letterV; + else if (prediction == 32) + letter = letterW; + else if (prediction == 33) + letter = letterX; + else if (prediction == 34) + letter = letterY; + else if (prediction == 35) + letter = letterZ; + + return prediction; +} + // FUNZIONI GIORGIO - + void readtouch(){ if(Read_X()!=4095){ wait(0.1); @@ -245,9 +400,9 @@ else ready=0; } - + void readTouch(){ - int i,j,k,m; + int i,j; Coordinate p; getDisplayPoint(&display, Read_Ads7846(), &matrix ) ; //resto in attesa di un punto che venga toccato if(display.x <= 240 && display.x > 0){ @@ -266,19 +421,13 @@ isWritten=1; p.x=display.x-8; p.y=display.y-52; - /* - for(i=-1;i<2;i++) //for each point, we select a square matrix 3x3 around it + for(i=-1;i<2;i++) for(j=-1;j<2;j++){ if((p.x+i)>=0&&(p.y+j)>=0&&(p.x+i)<224&&(p.y+j)<224){ TP_DrawPoint(display.x+i,display.y+j); - if(((p.x/8)+i)>=0&&((p.y/8)+j)>=0&&((p.x/8)+i)<28&&((p.y/8)+j)<28) - drawnPoints[(p.x/8)+i][(p.y+j/8)+j]++; //count +1 for the macrocell containing this pixel + drawnPoints[(p.x+i)/8][(p.y+j)/8]++; //count +1 for the macrocell containing this pixel } } - */ - TP_DrawPoint(display.x,display.y); - drawnPoints[p.x/8][p.y/8]++; //count +1 for the macrocell containing this pixel - } } } @@ -286,7 +435,7 @@ if(display.y >= 283 && display.y <= 315 && display.x >=8 && display.x <= 87 && startGame && !isWritten && !inferenceStarted){//punto in tasto Space if(charPos<26*8){ //topbar bound: otherwise the topbar is full drawButton(space,Yellow); - + //space operation: writing a whitespace into the topbar //--TO BE COMPLETED--// @@ -313,29 +462,35 @@ //--TO BE COMPLETED--// for(i=0;i<28;i++) //if the counter of the macrocell is greater than threshold, set the final cell to 1, otherwise it would be a 0-cell for(j=0;j<28;j++) - if(drawnPoints[i][j]>=threshold){ - for(k=-1;k<2;k++) //for each point, we select a square matrix 3x3 around it - for(m=-1;m<2;m++) - if((i+k)>=0&&(j+m)>=0&&(i+k)<28&&(j+m)<28){ - finalMatrix[i+k][j+m]=1; - } - - } + drawnPoints[i][j]=drawnPoints[i][j]>=threshold?1:0; + #ifdef DEBUG showCompression(); - if(charPos!=0) - LCD_DrawLine(15+charPos-8, 35, 15+charPos+8-8, 35, White); //delete last cursor - GUI_Text(15+charPos, 20, (uint8_t *)"T", Black, White,1); //testing text on topbar - LCD_DrawLine(15+charPos, 35, 15+charPos+8, 35, Black); //cursor - charPos+=8; - #else + #endif + + // Send the image via USB to predict on PC for(i=0;i<28;i++) { for(j=0;j<28;j++) { - serial.printf("%d",static_cast<int>(finalMatrix[j][i])); + serial.printf("%d",static_cast<int>(drawnPoints[j][i])); } } + // Predict on board + int prediction = predict(); + // Send the prediction via USB to show the prediction + wait(0.5); + serial.printf("%02d", prediction); + + // Visualize the prediction on top bar + if(charPos!=0) + LCD_DrawLine(15+charPos-8, 35, 15+charPos+8-8, 35, White); //delete last cursor + GUI_Text(15+charPos, 20, letter, Black, White,1); //testing text on topbar + LCD_DrawLine(15+charPos, 35, 15+charPos+8, 35, Black); //cursor + charPos+=8; + + + #ifndef DEBUG clearPad(); //here the matix is also cleaned #endif