Skip to content

Commit

Permalink
Removed V8 dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sumedhghaisas committed Mar 1, 2016
1 parent 5467564 commit 6e6ef61
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 53 deletions.
3 changes: 1 addition & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ var result;

console.log('-----------------------------------------------------');
result = sceneText.GetTextSync('sample_images/3.jpg', __dirname);
console.log(util.inspect(result, {depth: 9, colors: true}));

console.log(util.inspect(result, {depth: 9, colors: true}));
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"bindings":"latest"
},
"scripts": {
"install": "node cmake_install.js"
"install": "node cmake_install.js",
"test" : "node example.js"
}
}
75 changes: 62 additions & 13 deletions src/ocr/ocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
* Author: Lluis Gomez i Bigorda <lgomez AT cvc.uab.es>
*/

#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include "opencv2/text.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
Expand All @@ -23,6 +27,14 @@ using namespace std;
using namespace cv;
using namespace cv::text;

stringstream nullout;

#if defined DEBUG
#define LOG cout
#else
#define LOG nullout
#endif

class Regions{
public:
vector<vector<ERStat> > regions;
Expand All @@ -44,9 +56,6 @@ class outputOCR{

};

OutputOCR Ocr(string path, string languageFile);
outputOCR Ocr(string path,Rect box);

Rect groups_draw(Mat &src, vector<Rect> &groups);

void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions);
Expand Down Expand Up @@ -87,7 +96,7 @@ outputOCR Ocr(string path,Rect box) {
string op;
Ptr<OCRTesseract> ocr = OCRTesseract::create();
ocr->run(croppedImage, op, &boxes, &words, &confidences, OCR_LEVEL_WORD);
cout << "OCR output = " << op << " length = " << op.size() << endl;
LOG << "OCR output = " << op << " length = " << op.size() << endl;
vector<decodedTxtRegion> decodedTxtRegions;

decodedTxtRegion decodedTxt;
Expand All @@ -104,11 +113,52 @@ outputOCR Ocr(string path,Rect box) {
return output;
}

OutputOCR Ocr (string path, string languageFile) {
OutputOCR* Ocr(char* buffer, unsigned int len, string languageFile)
{
Mat temp(1, len, CV_8UC3, buffer);
Mat src = imdecode(temp, 1);
if ( src.data == NULL )
{
LOG << "Unable to load image." << endl;
return NULL;
}
return new OutputOCR(detectAndDecode(languageFile, src));
}

OutputOCR* Ocr (string path, string languageFile) {
// namedWindow("grouping",WINDOW_NORMAL);
Mat src = imread(path);
cout << "image loaded" << endl;
return detectAndDecode(languageFile, src);
//Mat src = imread(path);
char* buffer = NULL;
unsigned int len = 0;
ifstream f;
f.open(path.c_str(), std::ios::binary);
if(!f.is_open())
{
LOG << "lol" << endl;
}
else
{
f.seekg(0, std::ios::end);
len = f.tellg();
f.seekg(0, std::ios::beg);

buffer = new char[len];
f.read(buffer, len);

f.close();
}

Mat temp(1, len, CV_8UC3, buffer);
Mat src = imdecode(temp, 1);
if ( src.data == NULL )
{
LOG << "Unable to decode image." << endl;
return NULL;
}
LOG << src.type() << endl;
LOG << CV_8UC3 << endl;
LOG << "image loaded" << endl;
return new OutputOCR(detectAndDecode(languageFile, src));
}

OutputOCR detectAndDecode(string languageFile, Mat &src){
Expand All @@ -133,7 +183,7 @@ OutputOCR detectAndDecode(string languageFile, Mat &src){
if(groups_boxes.size()==0){
groups_boxes=computeGroupsWithMinArea(src,channels,minArea);
}
cout<<"received boxes: "<<groups_boxes.size()<<endl;
LOG <<"received boxes: "<<groups_boxes.size()<<endl;

// draw groups
Rect group_box= groups_draw(src, groups_boxes);
Expand All @@ -153,8 +203,7 @@ OutputOCR detectAndDecode(string languageFile, Mat &src){
vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,vector<vector<ERStat> > regions,vector< vector<Vec2i> > nm_region_groups,vector<Rect> nm_boxes){
// Text Recognition (OCR)
double t_r = (double)getTickCount();

cout << languageFile << endl;

Ptr<OCRTesseract> ocr = OCRTesseract::create(languageFile.c_str());
string output;
Mat out_img;
Expand All @@ -180,7 +229,6 @@ vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,v
//image(nm_boxes[i]).copyTo(group_img);
group_img(nm_boxes[i]).copyTo(group_img);


copyMakeBorder(group_img,group_img,15,15,15,15,BORDER_CONSTANT,Scalar(0));

vector<Rect> boxes;
Expand All @@ -189,7 +237,8 @@ vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,v
ocr->run(group_img, output, &boxes, &words, &confidences, OCR_LEVEL_WORD);

output.erase(remove(output.begin(), output.end(), '\n'), output.end());
cout << "OCR output = \"" << output << "\" length = " << output.size() << endl;
LOG << "OCR output = \"" << output << "\" length = " << output.size() << endl;

decodedTxtRegions.push_back(DecodedText(Box(nm_boxes[i].tl().x, nm_boxes[i].br().x, nm_boxes[i].tl().y, nm_boxes[i].br().y), words, confidences));
}
return decodedTxtRegions;
Expand Down
2 changes: 1 addition & 1 deletion src/ocr/ocr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ using namespace std;
using namespace cv;
using namespace cv::text;

OutputOCR Ocr(std::string, std::string);
OutputOCR* Ocr(std::string, std::string);
OutputOCR Ocr(std::string, Rect box);
8 changes: 5 additions & 3 deletions src/sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ NAN_METHOD(GetTextSync) {
//detectRegions = To<bool>(info[1]).FromJust();
}
// call the decoder here
OutputOCR decodedText = Ocr(path, languageFile);
// set the return value
info.GetReturnValue().Set(Nan::New(decodedText.ToLocal()));
OutputOCR* decodedText = Ocr(path, languageFile);
if(decodedText == NULL)
info.GetReturnValue().Set(Nan::New<v8::Object>());
else info.GetReturnValue().Set(Nan::New(decodedText->ToLocal()));
delete decodedText;
}

NAN_MODULE_INIT(Init)
Expand Down
5 changes: 2 additions & 3 deletions src/util/box.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ struct Box
{
v8::Local<v8::Array> ToLocal()
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Array> array = v8::Array::New(4);
array->Set(0,v8::Integer::New(x1));
v8::Local<v8::Array> array = Nan::New<v8::Array>();
Nan::Set(array, 0, v8::Integer::New(x1));
array->Set(1,v8::Integer::New(x2));
array->Set(2,v8::Integer::New(y1));
array->Set(3,v8::Integer::New(y2));
Expand Down
50 changes: 26 additions & 24 deletions src/util/decoded_text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,35 @@

struct DecodedText
{
DecodedText(Box box, std::vector<std::string> words, std::vector<float> confs)
: box(box), words(words), confs(confs) {}
Box box;
std::vector<std::string> words;
std::vector<float> confs;
v8::Local<v8::Object> object;
DecodedText(Box box, std::vector<std::string> words, std::vector<float> confs)
: box(box), words(words), confs(confs) {}

Box box;
std::vector<std::string> words;
std::vector<float> confs;
v8::Local<v8::Object> object;

v8::Local<v8::Object> ToLocal()
{
v8::Isolate* isolate = NULL;
v8::Local<v8::Array> l_box = box.ToLocal();
v8::Local<v8::Array> l_words = v8::Array::New(words.size());
v8::Local<v8::Array> l_confs = v8::Array::New(confs.size());
v8::Local<v8::Object> ToLocal()
{
v8::Local<v8::Array> l_box = box.ToLocal();
v8::Local<v8::Array> l_words = Nan::New<v8::Array>();
v8::Local<v8::Array> l_confs = Nan::New<v8::Array>();

for(int i=0;i<words.size();i++){
l_words->Set(i,Nan::New(words[i]).ToLocalChecked());
}
for(int i = 0;i < words.size();i++)
{
Nan::Set(l_words, i, Nan::New(words[i]).ToLocalChecked());
}

for(int i=0;i<confs.size();i++){
l_confs->Set(i,v8::NumberObject::New(confs[i]));
for(int i=0;i<confs.size();i++)
{
Nan::Set(l_confs, i, Nan::New(confs[i]));
}
v8::Local<v8::Object> out = v8::Object::New();
out->Set(Nan::New("box").ToLocalChecked(), l_box);
out->Set(Nan::New("words").ToLocalChecked(), l_words);
out->Set(Nan::New("confidences").ToLocalChecked(), l_confs);
return out;
}

v8::Local<v8::Object> out = Nan::New<v8::Object>();
Nan::Set(out, Nan::New("box").ToLocalChecked(), l_box);
Nan::Set(out, Nan::New("words").ToLocalChecked(), l_words);
Nan::Set(out, Nan::New("confidences").ToLocalChecked(), l_confs);
return out;
}
};
#endif
15 changes: 9 additions & 6 deletions src/util/output_ocr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <node.h>
#include <nan.h>
#include <sstream>

#include "box.hpp"
#include "decoded_text.hpp"
Expand All @@ -19,14 +20,16 @@ struct OutputOCR
v8::Local<v8::Object> ToLocal()
{
v8::Local<v8::Array> l_box = box.ToLocal();
v8::Local<v8::Array> l_decodedArr= v8::Array::New(text_vec.size());
for(int i=0;i<text_vec.size();i++){
v8::Local<v8::Array> l_decodedArr = Nan::New<v8::Array>();
for(int i=0;i<text_vec.size();i++)
{
v8::Local<v8::Object> l_decodedtext = text_vec[i].ToLocal();
l_decodedArr ->Set(i,l_decodedtext);
Nan::Set(l_decodedArr, i, l_decodedtext);
}
v8::Local<v8::Object> out = v8::Object::New();
out->Set(Nan::New("box").ToLocalChecked(), l_box);
out->Set(Nan::New("decodedText").ToLocalChecked(),l_decodedArr);

v8::Local<v8::Object> out = Nan::New<v8::Object>();
Nan::Set(out, Nan::New("box").ToLocalChecked(), l_box);
Nan::Set(out, Nan::New("decodedText").ToLocalChecked(),l_decodedArr);
return out;
}
};
Expand Down

0 comments on commit 6e6ef61

Please sign in to comment.