Skip to content

Commit

Permalink
feat: Add cpp code for two tasks
Browse files Browse the repository at this point in the history
Task 1: Run tesseract and output JSON
Task 2: Fix skew on an image using leptonica
  • Loading branch information
shahrukhqasim committed Dec 5, 2017
1 parent c9cbffb commit e6a6327
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.7)
project(cpp)

set(CMAKE_CXX_STANDARD 11)

link_directories(/usr/local/lib/ /usr/lib/ ${Boost_LIBRARY_DIR})
include_directories(/usr/local/include/ DocProc/src/ ${Boost_INCLUDE_DIR})

set(SOURCE_FILES main.cpp main_skew.cc)
add_executable(tessocr ${SOURCE_FILES})
add_executable(fixskew main_skew.cc)

target_link_libraries(tessocr lept tesseract opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs pthread gomp docproc)
target_link_libraries(fixskew lept tesseract opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs pthread gomp docproc)
81 changes: 81 additions & 0 deletions cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>
#include <docproc/binarize/binarize.h>
#include "opencv2/core.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <memory>
#include <docproc/utility/timer.h>
#include <docproc/transform/transform.h>
#include <docproc/clean/clean.h>
#include <docproc/segment/segment.h>
#include <docproc/post_process/post_process.h>
#include <docproc/core/core.h>
#include <leptonica/allheaders.h>
#include <json/json/json.h>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
if (argc != 3) {
cout << "Oops! Check arguments!" << endl;
return -1;
}
string imageFilePath = argv[1];
string jsonFilePath = argv[2];
cout<<jsonFilePath<<endl;

Mat originalImage = imread(imageFilePath, 1);

Mat grayScaleImage;
cvtColor(originalImage, grayScaleImage, CV_BGR2GRAY);

shared_ptr <tesseract::TessBaseAPI> api = shared_ptr<tesseract::TessBaseAPI>(new tesseract::TessBaseAPI());

if (api->Init(NULL, "eng", tesseract::OEM_TESSERACT_ONLY)) {
throw 1; // TODO: Convert to proper exception
}
api->SetVariable("tessedit_char_whitelist", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789,.-");

api->SetImage(docproc::core::mat2PixGray(grayScaleImage)); // Gray scale is actually binary as per the UNLV dataset
api->Recognize(0);
tesseract::ResultIterator *itr = api->GetIterator();

vector<string>words;
vector<Rect>boundingBoxes;

{
tesseract::ResultIterator *ri = api->GetIterator();
if (ri != 0) {
do {
int left, top, right, bottom;
ri->BoundingBox(tesseract::RIL_WORD, &left, &top, &right, &bottom);
Rect rect = Rect(left, top, right - left, bottom - top);
words.push_back(ri->GetUTF8Text(tesseract::RIL_WORD));
boundingBoxes.push_back(rect);

} while (ri->Next(tesseract::RIL_WORD));
}
}

Json::Value ocrJson;
for (int i = 0; i < words.size(); i++) {
string word = words[i];
Rect boundingBox = boundingBoxes[i];
Json::Value oneWord;
oneWord["word"] = word;
Json::Value bounds;
bounds["x"] = boundingBox.x;
bounds["y"] = boundingBox.y;
bounds["width"] = boundingBox.width;
bounds["height"] = boundingBox.height;
oneWord["rect"] = bounds;

ocrJson[i] = oneWord;
}
ofstream outputFileStream(jsonFilePath);
outputFileStream << ocrJson;
outputFileStream.close();

return 0;
}
33 changes: 33 additions & 0 deletions cpp/main_skew.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <iostream>
#include <docproc/binarize/binarize.h>
#include "opencv2/core.hpp"
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <memory>
#include <docproc/utility/timer.h>
#include <docproc/transform/transform.h>
#include <docproc/clean/clean.h>
#include <docproc/segment/segment.h>
#include <docproc/post_process/post_process.h>
#include <docproc/core/core.h>
#include <leptonica/allheaders.h>
#include <json/json/json.h>

using namespace std;
using namespace cv;

int main(int argc, char **argv) {
if (argc != 2) {
cout << "Oops! Check arguments!" << endl;
return -1;
}
string imagePath = argv[1];

Mat binarizedImage = imread(imagePath, 0);

binarizedImage = docproc::transform::fixSkewAngle(binarizedImage);

imwrite(imagePath, binarizedImage);

return 0;
}

0 comments on commit e6a6327

Please sign in to comment.