$search
00001 #include "opencv2/core/core.hpp" 00002 #include "cv.h" 00003 #include "highgui.h" 00004 00005 #include <ros/ros.h> 00006 00007 #include <iostream> 00008 #include <fstream> 00009 #include <stack> 00010 #include <stdio.h> 00011 #include <dirent.h> 00012 00013 class img 00014 { 00015 public: 00016 img(std::string img_name) : 00017 img_name(img_name) 00018 { 00019 count++; 00020 } 00021 ~img() 00022 { 00023 } 00024 void setRect(cv::Rect r) 00025 { 00026 rects.push_back(r); 00027 } 00028 void setText(std::string t) 00029 { 00030 texts.push_back(t); 00031 } 00032 00033 std::string img_name; 00034 std::vector<cv::Rect> rects; 00035 std::vector<std::string> texts; 00036 00037 static int count; 00038 }; 00039 00040 int img::count = 0; 00041 00042 int cutout(std::string label, std::string output) 00043 { 00044 //cutout x,y,width,height 00045 if (output.find(label) != std::string::npos) 00046 { 00047 return atoi((output.substr(output.find(label) + label.length() + 1, output.length() - 2 - label.length())).c_str()); 00048 } 00049 else 00050 return -1; 00051 } 00052 00053 int readInEstimates(std::vector<img> &images, std::vector<img> &ocrImages, std::string path) 00054 { 00055 for (int i = 0; i < images.size(); i++) 00056 { 00057 // new ocrImage 00058 img * im = new img(images[i].img_name); 00059 ocrImages.push_back(*im); 00060 00061 // run read_text 00062 std::string imgpath = path.substr(0, path.find_last_of("/") + 1); 00063 imgpath.append(images[i].img_name); 00064 std::string cmd_ = "$(rospack find read_text)/bin/run_detect " + imgpath 00065 + " $(rospack find read_text)/fonts/correlation.txt $(rospack find read_text)/dictionary/full-dictionary"; 00066 //system(cmd_.c_str()); 00067 00068 // get read_text results 00069 std::ifstream ocrfile; 00070 std::string textname = images[i].img_name.substr(0, images[i].img_name.find_last_of(".")) + ".txt"; 00071 ocrfile.open(textname.c_str()); 00072 if (!ocrfile) 00073 { 00074 std::cout << "While opening read_text results file an error is encountered" << std::endl; 00075 return -1; 00076 } 00077 00078 std::string input; 00079 00080 if (ocrfile.is_open()) 00081 { 00082 for (int a = 0; !ocrfile.eof(); a++) 00083 { 00084 ocrfile >> input; 00085 static int x, y, width, height; 00086 00087 // read x,y,width,height 00088 if (a % 4 == 0) 00089 x = atoi(input.c_str()); 00090 if (a % 4 == 1) 00091 y = atoi(input.c_str()); 00092 if (a % 4 == 2) 00093 width = atoi(input.c_str()); 00094 if (a % 4 == 3) 00095 { 00096 height = atoi(input.c_str()); 00097 cv::Rect * r = new cv::Rect(x, y, width, height); 00098 ocrImages[img::count - 1].setRect(*r); 00099 } 00100 } 00101 ocrfile.close(); 00102 } 00103 textname = images[i].img_name.substr(0, images[i].img_name.find_last_of(".")) + "t.txt"; 00104 ocrfile.open(textname.c_str()); 00105 if (!ocrfile) 00106 { 00107 std::cout << "While opening read_text results file an error is encountered" << std::endl; 00108 return -1; 00109 } 00110 00111 if (ocrfile.is_open()) 00112 { 00113 std::string s; 00114 while (getline(ocrfile, s)) 00115 { 00116 ocrImages[img::count - 1].setText(s); 00117 } 00118 } 00119 } 00120 return 0; 00121 } 00122 00123 void readInSolution(std::vector<img> &images, std::string filename) 00124 { 00125 std::string label[] = {"<imageName>", "x=", "y=", "width=", "height=", "text="}; 00126 00127 std::ifstream imgxml; 00128 imgxml.open(filename.c_str()); 00129 if (imgxml.is_open()) 00130 { 00131 while (!imgxml.eof()) 00132 { 00133 std::string filename; 00134 int x, y, width, height; 00135 std::string text; 00136 00137 std::string output; 00138 imgxml >> output; 00139 00140 // label[0] = <imageName> => new image starts in xmlfile 00141 if (output.find(label[0]) != std::string::npos) 00142 { 00143 filename 00144 = output.substr(output.find(label[0]) + label[0].length(), output.length() - 2 * label[0].length() - 1); 00145 img * i = new img(filename); 00146 images.push_back(*i); 00147 } 00148 00149 // x=, y=, width=, height= in xmlfile 00150 if (cutout(label[1], output) != -1) 00151 x = cutout(label[1], output); 00152 if (cutout(label[2], output) != -1) 00153 y = cutout(label[2], output); 00154 if (cutout(label[3], output) != -1) 00155 width = cutout(label[3], output); 00156 if (cutout(label[4], output) != -1) 00157 height = cutout(label[4], output); 00158 00159 // text= in xmlfile 00160 if (output.find(label[5]) != std::string::npos) 00161 { 00162 text = output.substr(output.find(label[5]) + label[5].length() + 1, output.length() - label[5].length() - 2); 00163 cv::Rect * r = new cv::Rect(x, y, width, height); 00164 images[img::count - 1].setRect(*r); 00165 images[img::count - 1].setText(text); 00166 } 00167 } 00168 imgxml.close(); 00169 } 00170 } 00171 00172 void deleteIdentical(std::vector<img> &ocrImages) 00173 { 00174 //delete identical found rects 00175 std::vector<cv::Rect>::iterator it; 00176 for (int h = 0; h < ocrImages.size(); h++) 00177 { 00178 for (int i = 0; i < ocrImages[h].rects.size(); i++) 00179 { 00180 for (int j = i + 1; j < ocrImages[h].rects.size(); j++) 00181 { 00182 if (ocrImages[h].rects[i].x == ocrImages[h].rects[j].x && ocrImages[h].rects[i].y == ocrImages[h].rects[j].y 00183 && ocrImages[h].rects[i].height == ocrImages[h].rects[j].height && ocrImages[h].rects[i].width 00184 == ocrImages[h].rects[j].width) 00185 { 00186 it = find(ocrImages[h].rects.begin(), ocrImages[h].rects.end(), ocrImages[h].rects[j]); 00187 ocrImages[h].rects.erase(it); 00188 } 00189 } 00190 } 00191 } 00192 } 00193 00194 void showRects(std::vector<img> &images, std::vector<img> &ocrImages, std::string path) 00195 { 00196 for (int h = 0; h < ocrImages.size(); h++) 00197 { 00198 //show image with all boxes 00199 std::string imgpath = path.substr(0, path.find_last_of("/") + 1); 00200 imgpath.append(ocrImages[h].img_name); 00201 cv::Mat OriginalImage_ = cv::imread(imgpath); 00202 cv::Mat Image_(OriginalImage_.rows, OriginalImage_.cols + 600, OriginalImage_.type()); 00203 Image_.setTo(cv::Scalar(0, 0, 0, 0)); 00204 cv::Mat roi = cv::Mat(Image_, cv::Rect(cv::Point(0, 0), OriginalImage_.size())); 00205 OriginalImage_.copyTo(roi); 00206 00207 cv::putText(Image_, "Found:", cv::Point(OriginalImage_.cols + 25, OriginalImage_.rows / 2 + 25), 00208 cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(255, 0, 0, 0), 1.5, 8, false); 00209 cv::putText(Image_, "Correct:", cv::Point(OriginalImage_.cols + 25, 25), cv::FONT_HERSHEY_SIMPLEX, 0.75, 00210 cv::Scalar(0, 255, 0, 0), 1.5, 8, false); 00211 cv::putText(Image_, "Evaluation:", cv::Point(OriginalImage_.cols + 375, 25), cv::FONT_HERSHEY_SIMPLEX, 0.75, 00212 cv::Scalar(0, 0, 255, 0), 1.5, 8, false); 00213 cv::line(Image_, cv::Point(OriginalImage_.cols + 250, 0), cv::Point(OriginalImage_.cols + 250, Image_.rows), 00214 cv::Scalar(50, 50, 50, 0), 1, 1, 0); 00215 00216 for (int i = 0; i < ocrImages[h].rects.size(); i++) 00217 { 00218 cv::rectangle(Image_, cv::Point(ocrImages[h].rects[i].x, ocrImages[h].rects[i].y), 00219 cv::Point(ocrImages[h].rects[i].x + ocrImages[h].rects[i].width, ocrImages[h].rects[i].y 00220 + ocrImages[h].rects[i].height), cvScalar((255 - 10 * i), (0), (0)), 2); 00221 00222 std::string output; 00223 std::stringstream out; 00224 out << i << ": [ " << ocrImages[h].rects[i].x << " | " << ocrImages[h].rects[i].y << " | " 00225 << ocrImages[h].rects[i].width << " | " << ocrImages[h].rects[i].height << " ]"; 00226 output = out.str(); 00227 00228 cv::putText(Image_, output, cv::Point(OriginalImage_.cols + 5, OriginalImage_.rows / 2 + 25 + 25 * (i + 1)), 00229 cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar((255 - 10 * i), 0, 0, 0), 1.5, 8, false); 00230 } 00231 00232 for (int i = 0; i < images[h].rects.size(); i++) 00233 { 00234 cv::rectangle(Image_, cv::Point(images[h].rects[i].x, images[h].rects[i].y), cv::Point(images[h].rects[i].x 00235 + images[h].rects[i].width, images[h].rects[i].y + images[h].rects[i].height), cvScalar((0), (255 - 50 * i), 00236 (0)), 2); 00237 std::string output; 00238 std::stringstream out; 00239 out << i << ": [ " << images[h].rects[i].x << " | " << images[h].rects[i].y << " | " << images[h].rects[i].width 00240 << " | " << images[h].rects[i].height << " ]"; 00241 output = out.str(); 00242 00243 cv::putText(Image_, output, cv::Point(OriginalImage_.cols + 5, 25 + 25 * (i + 1)), cv::FONT_HERSHEY_SIMPLEX, 0.5, 00244 cv::Scalar(0, (255 - 50 * i), 0, 0), 1.5, 8, false); 00245 } 00246 00247 //cv::putText(Image_, out.str(), cv::Point(OriginalImage_.cols + 260, 25 + 25 * (i + 1)), 00248 // cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 255, 0), 1.5, 8, false); 00249 cv::imshow(ocrImages[h].img_name, Image_); 00250 cv::waitKey(0); 00251 } 00252 } 00253 00254 void calculateResults(std::vector<img> &images, std::vector<img> &ocrImages) 00255 { 00256 for (int i = 0; i < images.size(); i++) 00257 { 00258 std::cout << images[i].img_name << ":" << std::endl; 00259 for (int j = 0; j < images[i].rects.size(); j++) 00260 { 00261 std::cout << images[i].rects[j].x << "|" << images[i].rects[j].y << "|" << images[i].rects[j].width << "|" 00262 << images[i].rects[j].height << " [" << images[i].texts[j] << "]" << std::endl; 00263 } 00264 } 00265 00266 for (int i = 0; i < images.size(); i++) 00267 { 00268 std::cout << ocrImages[i].img_name << ":" << std::endl; 00269 for (int j = 0; j < ocrImages[i].rects.size(); j++) 00270 { 00271 std::cout << ocrImages[i].rects[j].x << "|" << ocrImages[i].rects[j].y << "|" << ocrImages[i].rects[j].width 00272 << "|" << ocrImages[i].rects[j].height << " [" << ocrImages[i].texts[j] << "]" << std::endl; 00273 } 00274 } 00275 00276 for (int i = 0; i < ocrImages.size(); i++) 00277 { 00278 int retrieved = (int)ocrImages[i].rects.size(); 00279 int relevant = (int)images[i].rects.size(); 00280 00281 for (int j = 0; j < ocrImages[i].rects.size(); j++) 00282 for (int k = 0; k < images[i].rects.size(); k++) 00283 { 00284 { 00285 int intersectionPixels = (ocrImages[i].rects[j] & images[i].rects[k]).area(); 00286 unsigned int percent = (intersectionPixels / (images[i].rects[k].area())) * 100; 00287 00288 if (ocrImages[i].rects[j].area() > images[i].rects[k].area()) 00289 { 00290 00291 percent -= (((ocrImages[i].rects[j].area() - images[i].rects[k].area()) / images[i].rects[k].area()) / 8) 00292 * 100; 00293 } 00294 std::string output; 00295 std::stringstream out; 00296 out << "overlap: correct[" << i << "] & found[" << j << "]: " << percent << "%"; 00297 } 00298 } 00299 } 00300 } 00301 00302 int main(int argc, char **argv) 00303 { 00304 if (argc < 2) 00305 { 00306 ROS_ERROR( "not enought input: eval_read_text <img_list.xml>"); 00307 return 1; 00308 } 00309 00310 std::vector<img> images; 00311 std::vector<img> ocrImages; 00312 00313 //read everything from img_list.xml in images 00314 readInSolution(images, argv[1]); 00315 00316 img::count = 0; 00317 00318 //run read_text and write results in ocrImages 00319 readInEstimates(images, ocrImages, argv[1]); 00320 00321 calculateResults(images, ocrImages); 00322 00323 showRects(images, ocrImages, argv[1]); 00324 00325 return 0; 00326 }