forked from royshil/FoodcamClassifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual_classifier.cpp
191 lines (168 loc) · 3.97 KB
/
manual_classifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* manual_classifier.cpp
* FoodcamClassifier
*
* Created by Roy Shilkrot on 8/19/11.
* Copyright 2011 MIT. All rights reserved.
*
*/
#include <iostream>
#include <opencv2/opencv.hpp>
#include <fstream>
#include <iostream>
#include <string>
#include <set>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
using namespace cv;
using namespace std;
Point origin;
Rect selection;
Mat image;
bool selectObject;
void onMouse( int event, int x, int y, int, void* )
{
if( selectObject )
{
selection.x = MIN(x, origin.x);
selection.y = MIN(y, origin.y);
selection.width = std::abs(x - origin.x);
selection.height = std::abs(y - origin.y);
selection &= Rect(0, 0, image.cols, image.rows);
}
switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = Point(x,y);
selection = Rect(x,y,0,0);
selectObject = true;
break;
case CV_EVENT_LBUTTONUP:
selectObject = false;
break;
}
}
string char_to_class(char c) {
switch (c) {
case 'h':
case 'H':
return "chinese";
break;
case 'p':
case 'P':
return "pizza";
break;
case 'i':
case 'I':
return "indian";
break;
case 'w':
case 'W':
return "wraps";
break;
case 's':
case 'S':
return "sandwiches";
break;
case 'a':
case 'A':
return "salad";
break;
case 'c':
case 'C':
return "cookies";
break;
case 'm':
case 'M':
return "mexican";
break;
case 'f':
case 'F':
return "fruit_veggie";
break;
case 'l':
case 'L':
return "misc";
break;
case 't':
case 'T':
return "italian";
break;
default:
return "misc";
}
}
int main(int argc, char * const argv[]) {
if(argc < 3) {
cerr << "USAGE: manual_classifier <input_directory/> <output_file.txt>"<<endl;
return 1;
}
string dir(argv[1]), filepath;
DIR *dp;
struct dirent *dirp;
struct stat filestat;
cout << "C'h'inease\nI't'alian\n'P'izza\n'I'ndian\n'W'raps\n'S'andwiches\nS'a'lad\n'C'ookies/Cake\n'M'exican\n'F'ruit/Veggies\nMisce'l'laneous" << endl;
vector<pair<string, string> > classified;
namedWindow("pic");
setMouseCallback( "pic", onMouse, 0 );
ifstream ifs(argv[2],ifstream::in);
set<string> files_already_listed;
if (ifs.is_open() && !ifs.eof()) {
//something in here, get everything already listed
char buf[255];
while (!ifs.eof()) {
ifs.getline(buf, 255);
string line(buf);
files_already_listed.insert(line.substr(0, line.find(" ")));
}
}
ifs.close();
ofstream ofs(argv[2], fstream::app);
int count = 0;
Mat img;
bool running = true;
dp = opendir( dir.c_str() );
while (count++ < 1000 && (dirp = readdir( dp )) && running)
{
filepath = dir + "/" + dirp->d_name;
// If the file is a directory (or is in some way invalid) we'll skip it
if (stat( filepath.c_str(), &filestat )) continue; //can't be opened...
if (S_ISDIR( filestat.st_mode )) continue; //a directory
if (dirp->d_name[0] == '.') continue; //hidden file!
if (files_already_listed.count(filepath)>0) continue; //already did that one
ofs << filepath;
img = imread(filepath);
Point text_place(20,40);
while (true) {
img.copyTo(image);
if( selection.width > 0 && selection.height > 0 )
{
Mat roi(image, selection);
bitwise_not(roi, roi);
}
imshow("pic", image);
int c = waitKey(10);
if (c == ' ') {
ofs << endl;
break;
} else if (c == 27) {
running = false;
break;
} else if (c != -1) {
if(selection.width != 0)
ofs << " " << selection.x << "," << selection.y << "," << selection.width << "," << selection.height;
ofs << " " << char_to_class(c);
putText(img, char_to_class(c), text_place, CV_FONT_HERSHEY_PLAIN, 3.0, Scalar(255), 2);
text_place += Point(0,40);
selection = Rect();
}
}
}
closedir(dp);
for (int i=0; i<classified.size(); i++) {
ofs << classified[i].first << " " << classified[i].second << endl;
}
ofs.close();
}