static void learnOcr(ImageInput* pImageInput) { log4cpp::Category::getRoot().info("learnOcr"); Config config; config.loadConfig(); ImageProcessor proc(config); proc.debugWindow(); KNearestOcr ocr(config); ocr.loadTrainingData(); std::cout << "Entering OCR training mode!\n"; std::cout << "<0>..<9> to answer digit, <space> to ignore digit, <s> to save and quit, <q> to quit without saving.\n"; int key = 0; while (pImageInput->nextImage()) { proc.setInput(pImageInput->getImage()); proc.process(); key = ocr.learn(proc.getOutput()); std::cout << std::endl; if (key == 'q' || key == 's') { std::cout << "Quit\n"; break; } } if (key != 'q') { std::cout << "Saving training data\n"; ocr.saveTrainingData(); } }
TEST(test_algorithm7, OCR) { DIR *dip; struct dirent *dit; int count = -3; // . .. and answers.txt if ((dip = opendir("testpics/")) == NULL) { fprintf(stderr,"Could not find testpics folder.\n"); exit(1); } while ((dit = readdir(dip)) != NULL) { count++; } std::ifstream file; file.open("testpics/answers.txt"); std::stringstream ss; std::string scrot; std::string line; for (int i = 0; i < count; i++) { std::cout << count; ss << "testpics/scrot_" << i << ".png"; scrot = ocr(ss.str()); std::cout << "Picture: " << ss.str() << " - " << scrot << std::endl; std::getline(file,line); ss.str(""); EXPECT_EQ(scrot,line); } file.close(); }
LRESULT CMainDlg::OnOcr(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { USES_CONVERSION; POCR_ITEMS pItems = ocr("E:\\Projects\\SZGSW\\Sylla\\tocr\\bi.jpg"); POCR_ITEM pItem; CString strDebug; for (int i = 0; i < pItems->GetSize(); i++) { pItem = pItems->GetAt(i); strDebug.Format("%s\n", pItem->text); Log(strDebug); } return S_OK; }
static void writeData(ImageInput* pImageInput) { log4cpp::Category::getRoot().info("writeData"); Config config; config.loadConfig(); ImageProcessor proc(config); Plausi plausi; RRDatabase rrd("emeter.rrd"); struct stat st; KNearestOcr ocr(config); if (! ocr.loadTrainingData()) { std::cout << "Failed to load OCR training data\n"; return; } std::cout << "OCR training data loaded.\n"; std::cout << "<Ctrl-C> to quit.\n"; while (pImageInput->nextImage()) { proc.setInput(pImageInput->getImage()); proc.process(); if (proc.getOutput().size() == 7) { std::string result = ocr.recognize(proc.getOutput()); if (plausi.check(result, pImageInput->getTime())) { rrd.update(plausi.getCheckedTime(), plausi.getCheckedValue()); } } if (0 == stat("imgdebug", &st) && S_ISDIR(st.st_mode)) { // write debug image pImageInput->setOutputDir("imgdebug"); pImageInput->saveImage(); pImageInput->setOutputDir(""); } usleep(delay*1000L); } }
static void testOcr(ImageInput* pImageInput) { log4cpp::Category::getRoot().info("testOcr"); Config config; config.loadConfig(); ImageProcessor proc(config); proc.debugWindow(); proc.debugDigits(); Plausi plausi; KNearestOcr ocr(config); if (! ocr.loadTrainingData()) { std::cout << "Failed to load OCR training data\n"; return; } std::cout << "OCR training data loaded.\n"; std::cout << "<q> to quit.\n"; while (pImageInput->nextImage()) { proc.setInput(pImageInput->getImage()); proc.process(); std::string result = ocr.recognize(proc.getOutput()); std::cout << result; if (plausi.check(result, pImageInput->getTime())) { std::cout << " " << std::fixed << std::setprecision(1) << plausi.getCheckedValue() << std::endl; } else { std::cout << " -------" << std::endl; } int key = cv::waitKey(delay); if (key == 'q') { std::cout << "Quit\n"; break; } } }