Last active
October 19, 2018 09:36
-
-
Save nuttt/516dad4287381c12bdcb to your computer and use it in GitHub Desktop.
number recognition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <opencv2/core/core.hpp> | |
| #include <opencv2/highgui/highgui.hpp> | |
| #include <opencv2/imgproc/imgproc.hpp> | |
| #include <iostream> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string> | |
| #include <vector> | |
| #include <algorithm> | |
| using namespace cv; | |
| using namespace std; | |
| class TrainingSet { | |
| public: | |
| Mat data; | |
| int label; | |
| TrainingSet(Mat data, int label) { | |
| this->data = data; | |
| this->label = label; | |
| } | |
| }; | |
| int writeTrainingSet() { | |
| Mat image; | |
| int fileName; | |
| String fullFileName; | |
| vector<Mat> trainingData; | |
| vector<int> label; | |
| for(int folderName=0;folderName<=9;folderName++){ | |
| fileName = 1; | |
| while(true){ | |
| fullFileName = "/Users/nuttt/MNIST/train/"+to_string(folderName)+"/"+to_string(fileName)+".jpg"; | |
| image = imread(fullFileName, CV_LOAD_IMAGE_GRAYSCALE); | |
| cout << fullFileName << endl; | |
| if(!image.data){ | |
| cout << "Could not open or find the image" << std::endl ; | |
| break; | |
| } | |
| trainingData.push_back(image.reshape(0,1)); | |
| label.push_back(folderName); | |
| fileName++; | |
| // break; | |
| } | |
| // break; | |
| } | |
| FileStorage fs("/Users/nuttt/MNIST/train.yml.gz", FileStorage::WRITE); | |
| fs << "trainingData" << trainingData; | |
| fs << "trainingLabel" << label; | |
| fs.release(); | |
| return 0; | |
| } | |
| int writeTestSet() { | |
| Mat image; | |
| int fileName; | |
| String fullFileName; | |
| vector<Mat> trainingData; | |
| vector<int> label; | |
| for(int folderName=0;folderName<=9;folderName++){ | |
| fileName = 1; | |
| while(true){ | |
| fullFileName = "/Users/nuttt/MNIST/test/"+to_string(folderName)+"/"+to_string(fileName)+".jpg"; | |
| image = imread(fullFileName, CV_LOAD_IMAGE_GRAYSCALE); | |
| cout << fullFileName << endl; | |
| if(!image.data){ | |
| cout << "Could not open or find the image" << std::endl ; | |
| break; | |
| } | |
| trainingData.push_back(image.reshape(0,1)); | |
| label.push_back(folderName); | |
| fileName++; | |
| // break; | |
| } | |
| // break; | |
| } | |
| FileStorage fs("/Users/nuttt/MNIST/test.yml.gz", FileStorage::WRITE); | |
| fs << "testData" << trainingData; | |
| fs << "testLabel" << label; | |
| fs.release(); | |
| return 0; | |
| } | |
| vector<TrainingSet> readTrainingSet() { | |
| FileStorage fs("/Users/nuttt/MNIST/train.yml.gz", FileStorage::READ); | |
| vector<TrainingSet> output; | |
| vector<Mat> data; | |
| vector<int> label; | |
| fs["trainingData"] >> data; | |
| fs["trainingLabel"] >> label; | |
| for(int i=0; i<label.size(); i++){ | |
| output.push_back(TrainingSet(data[i], label[i])); | |
| } | |
| fs.release(); | |
| return output; | |
| } | |
| vector<TrainingSet> readTestSet() { | |
| FileStorage fs("/Users/nuttt/MNIST/test.yml.gz", FileStorage::READ); | |
| vector<TrainingSet> output; | |
| vector<Mat> data; | |
| vector<int> label; | |
| fs["testData"] >> data; | |
| fs["testLabel"] >> label; | |
| for(int i=0; i<label.size(); i++){ | |
| output.push_back(TrainingSet(data[i], label[i])); | |
| } | |
| fs.release(); | |
| return output; | |
| } | |
| int predict(vector<TrainingSet> trainingData, Mat testData){ | |
| vector<pair<double, int> > distance; | |
| int count[10] = {0,0,0,0,0,0,0,0,0,0}; | |
| double d; | |
| Mat test = testData.reshape(0, 1); | |
| for(int i=0; i<trainingData.size(); i++){ | |
| d = norm(trainingData[i].data, test); | |
| distance.push_back(pair<double, int> (d, trainingData[i].label)); | |
| } | |
| sort(distance.begin(), distance.end()); | |
| for(int i=0; i<10; i++){ | |
| count[distance[i].second]++; | |
| } | |
| int maxx = count[0]; | |
| int maxID = 0; | |
| for(int i=0; i<10; i++){ | |
| if(count[i] > maxx){ | |
| maxx = count[i]; | |
| maxID = i; | |
| } | |
| } | |
| return maxID; | |
| } | |
| int main(int, char**) { | |
| // writeTrainingSet(); | |
| // writeTestSet(); | |
| vector<pair<int,int> > result; | |
| vector<TrainingSet> trainingData; | |
| trainingData = readTrainingSet(); | |
| vector<TrainingSet> testData; | |
| testData = readTestSet(); | |
| cout << "Start" << endl; | |
| for(int i=0;i<testData.size();i++) { | |
| cout << i << endl; | |
| pair<int,int> r; | |
| r.first = testData[i].label; | |
| r.second = predict(trainingData, testData[i].data); | |
| result.push_back(r); | |
| if(i%1000 == 10){ | |
| i += 990; | |
| if(i >= testData.size()) | |
| break; | |
| } | |
| } | |
| int correct = 0; | |
| for(int i=0;i<result.size();i++) { | |
| if(result[i].first == result[i].second) { | |
| correct++; | |
| } | |
| } | |
| cout << correct << endl; | |
| cout << "Accuracy:" << (double)correct * 100.0 / (double)result.size() << endl; | |
| // cout << trainingData.size() << endl; | |
| /*vector<TrainingSet> trainingData; | |
| trainingData = readTrainingSet(); | |
| Mat image = trainingData[0].data.reshape(0,28); | |
| */ | |
| // namedWindow("test"); | |
| // imshow("test", image); | |
| // waitKey(0); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment