Hyunjun

add receipt code

......@@ -72,4 +72,8 @@ image_process.py : cropping을 통해 정사각형 형태를 만들고 32*32 크기로 resize,
15. params.pkl 추가
제작한 데이터셋을 트레이닝하고 나온 가중치를 저장한 params.pkl 파일 추가
\ No newline at end of file
제작한 데이터셋을 트레이닝하고 나온 가중치를 저장한 params.pkl 파일 추가
16. simple_convnet_rasberryPi 코드 추가
인식한 결과에 따라 계산서가 나오도록 코드를 추가
\ No newline at end of file
......
......@@ -18,7 +18,7 @@ struct conv_param {
};
class SimpleConvNet {
private:
private:
std::vector< Layer* > layers;
std::vector<Mat> W[7]; // weights
......@@ -26,9 +26,10 @@ private:
public:
SimpleConvNet() {}
~SimpleConvNet() {}
SimpleConvNet(input_dim id, conv_param cp, int hidden_size=512, int output_size=10, bool pretrained=true) {
if (pretrained)
load_trained("params.txt");
SimpleConvNet(input_dim id, conv_param cp, int hidden_size = 512, int output_size = 10, bool pretrained = true) {
if (pretrained)
load_trained("params_noLNORM.txt");
layers.push_back(new Convolution(W[0], 1, 1));
layers.push_back(new LightNormalization());
......@@ -47,14 +48,14 @@ public:
layers.push_back(new Convolution(W[3], 1, 0));
layers.push_back(new LightNormalization());
layers.push_back(new Relu());
layers.push_back(new DW_Convolution(W[4], 1, 1));
layers.push_back(new LightNormalization());
layers.push_back(new Relu());
layers.push_back(new Pooling(2, 2, 2));
layers.push_back(new Affine(W[5]));
layers.push_back(new LightNormalization());
//layers.push_back(new LightNormalization());
layers.push_back(new Relu());
layers.push_back(new Affine(W[6]));
......@@ -62,12 +63,15 @@ public:
std::vector< Mat > predict(std::vector<Mat>& x) {
for (int i = 0; i < layers.size(); i++) {
//printf("%d Layer : (%d, %d, %d, %d)\n",i, x.size(), x[0].dim, x[0].row, x[0].col);
x = layers[i]->forward(x);
}
//printf("Layer : (%d %d %d %d)\n", x.size(), x[0].dim, x[0].row, x[0].col);
return x;
}
double accuracy(std::vector< std::vector< unsigned char > > x, std::vector< int > ans, int batch_size=100) {
double accuracy(std::vector< std::vector< unsigned char > > x, std::vector< int > ans, int batch_size = 100) {
// ...
return 1.0;
}
......@@ -87,21 +91,21 @@ public:
return pred;
}
void load_trained(const char* filename="params.txt") {
void load_trained(const char* filename = "params.txt") {
FILE *f = fopen(filename, "r");
if (f == NULL) {
printf("File not found\n");
exit(1);
}
}
char line[10] = { 0 };
int keynum;
while (fscanf(f, "%s", line)==1) {
while (fscanf(f, "%s", line) == 1) {
char s[4][10] = { 0 };
keynum = line[1] - '0' - 1;
// get shape
fscanf(f, "%s", s[0]);
fscanf(f, "%s", s[1]);
fscanf(f, "%s", s[0]); // "(num
fscanf(f, "%s", s[1]); // num
if (s[1][strlen(s[1]) - 1] != '\"') {
fscanf(f, "%s", s[2]);
fscanf(f, "%s", s[3]);
......@@ -109,7 +113,7 @@ public:
// nw = number of weights : shape[0]
// size = input size of W[key]
int size = 1, nw=0;
int size = 1, nw = 0;
for (int i = 0; i < 4; i++) {
int val = 0;
for (int j = 0; j < strlen(s[i]); j++) {
......@@ -130,9 +134,9 @@ public:
for (int i = 0; i < size; i++) {
fscanf(f, "%lf", &mm[i%fsize]);
if (i%fsize == fsize - 1) {
if(shape[keynum].size() == 2)
if (shape[keynum].size() == 2)
W[keynum].push_back(Mat(1, 1, shape[keynum][1], std::vector<double>(mm, mm + fsize)));
else if(shape[keynum].size() == 4)
else if (shape[keynum].size() == 4)
W[keynum].push_back(Mat(shape[keynum][1], shape[keynum][2],
shape[keynum][3], std::vector<double>(mm, mm + fsize)));
}
......
#include"Layers.hpp"
#include"SimpleConvNet.hpp"
#include"fstream"
using namespace std;
int main() {
......@@ -7,7 +8,7 @@ int main() {
conv_param cp = { 32,32,64, 3,1,1 };
SimpleConvNet SCN(id, cp);
freopen("input.txt", "r", stdin);
freopen("input.txt", "r", stdin);
vector<Mat> X;
int nx = 1, dim = 3, row = 32, col = 32;
double tmp;
......@@ -43,7 +44,93 @@ int main() {
auto pred = SCN.argmax(x);
int num = 0, pd;
char receipt[100];
int total = 0;
printf("predict : %d ", pred[0]);
ifstream fin;
ofstream fout;
fin.open("receipt.txt");
char c;
int cnt=0;
while(fin.get(c)){
receipt[cnt] = c;
cnt++;
}
fin.close();
fout.open("receipt.txt");
for(int i=0; i<cnt; i++){
fout << receipt[i];
}
switch(pred[0]){
case 0:
cout << "can\n";
fout << 0;
break;
case 1:
cout << "ramen\n";
fout << 1;
break;
case 2:
cout << "cigarette\n";
fout << 2;
break;
case 3:
cout << "instant rice\n";
fout << 3;
break;
case 4:
cout << "wet tissue\n";
fout << 4;
break;
default:
cout << "try again\n";
break;
}
fout.close();
fin.open("receipt.txt");
cnt=0;
while(fin.get(c)){
receipt[cnt] = c;
cnt++;
}
fin.close();
cout << "\n\n===================receipt===================\n\n\n";
for(int i = 0; i<cnt; i++){
switch(receipt[i]){
case '0':
cout << "can---------------------------------1,200Won\n";
total += 1200;
break;
case '1':
cout << "ramen-------------------------------1,000Won\n";
total += 1000;
break;
case '2':
cout << "cigarette---------------------------4,500Won\n";
total += 4500;
break;
case '3':
cout << "instant rice------------------------1,500Won\n";
total += 1500;
break;
case '4':
cout << "wet tissue--------------------------2,000Won\n";
total += 2000;
break;
default:
break;
}
}
cout << "\nTotal-------------------------------" << total << "Won\n";
cout << "\n\n=============================================\n\n\n";
return 0;
}
\ No newline at end of file
}
......
......@@ -2,9 +2,14 @@ from numpy import array, savetxt
from PIL import Image
import sys
import picamera
import time
camera = picamera.PiCamera()
camera.start_preview()
time.sleep(3)
camera.stop_preview()
filename = 'image.jpg'
camera.capture(filename)
......
This diff could not be displayed because it is too large.
0201
112
\ No newline at end of file