Skip to content

Commit

Permalink
Making cmake-js install globally
Browse files Browse the repository at this point in the history
  • Loading branch information
sumedhghaisas committed Mar 3, 2016
1 parent 10303ca commit 0c43ae6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 76 deletions.
9 changes: 6 additions & 3 deletions cmake_install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var https = require('https');
var fs = require('fs');
var tar = require('tar');
var child_process = require('child_process');
var http = require('http');

function onError(err) {
console.error('An error occurred:', err)
Expand Down Expand Up @@ -33,6 +34,8 @@ function downloadAndRun(filename, type, folder, url)
{
var tmpFilePath = filename + "." + type;

console.log(url);

https.get(url, function(response) {
response.on('data', function (data) {
fs.appendFileSync(tmpFilePath, data)
Expand All @@ -43,17 +46,17 @@ function downloadAndRun(filename, type, folder, url)
{
var zip = new AdmZip(tmpFilePath);
zip.extractAllTo("./");
fs.unlink(tmpFilePath);
//fs.unlink(tmpFilePath);
fs.renameSync('./' + folder, './cmake_binary');
runCmakeJS('.\\node_modules\\.bin\\cmake-js.cmd', 'cmake.exe', '\\');
runCmakeJS('cmake-js', 'cmake.exe', '\\');
}
else if(type == "tar.gz")
{
const ls = child_process.spawn('tar', ['-xzf', tmpFilePath]);
ls.stdout.on("end", function() {
fs.unlink(tmpFilePath);
fs.renameSync('./' + folder, './cmake_binary');
runCmakeJS('./node_modules/cmake-js/bin/cmake-js', 'cmake', '/');
runCmakeJS('cmake-js', 'cmake', '/');
});
}
else
Expand Down
12 changes: 10 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
'use strict';
var sceneText = require('bindings')('node_text_detector');
var util = require('util');
var fs = require('fs');
var result;

console.log('-----------------------------------------------------');
result = sceneText.GetTextSync('sample_images/3.jpg', __dirname);
console.log(util.inspect(result, {depth: 9, colors: true}));
fs.readFile('./sample_images/3.jpg', function(error, data) {
if (!error) {
var result = sceneText.GetTextSync(data, __dirname, '0123456789ABCDEFGHJKLMNPRSTUVWXYZ');
console.log(util.inspect(result, {depth: 9, colors: true}));
} else {
console.log('Load Image from Path failed');
return undefined;
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"bindings":"latest"
},
"scripts": {
"install": "node cmake_install.js",
"install": "npm install -g cmake-js && node cmake_install.js",
"test" : "node example.js"
}
}
Binary file modified sample_images/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 24 additions & 39 deletions src/ocr/ocr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,6 @@ class Regions{
vector< vector<Vec2i> > region_groups;
};

class decodedTxtRegion{
public:
int x1,x2,y1,y2;
vector<string> words;
vector<float> confidences;
};

class outputOCR{
public:
int x1,x2,y1,y2;
vector<decodedTxtRegion> decodedText;

};

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

void er_show(vector<Mat> &channels, vector<vector<ERStat> > &regions);
Expand All @@ -64,14 +50,14 @@ void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec

vector<Rect> computeGroupsWithMinArea(Mat &src,vector<Mat> &channels,float minArea);

vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,vector<vector<ERStat> > regions,vector< vector<Vec2i> > nm_region_groups,vector<Rect> nm_boxes);
vector<DecodedText> doOCR(string languageFile, string whitelist, Mat &image,vector<Mat> channels,vector<vector<ERStat> > regions,vector< vector<Vec2i> > nm_region_groups,vector<Rect> nm_boxes);

Regions computeRegionGroups(Mat &src,vector<Mat> &channels,float minArea);

OutputOCR detectAndDecode(string languageFile, Mat &src);

OutputOCR detectAndDecode(string languageFile, string whitelist, Mat &src);

outputOCR Ocr(string path,Rect box) {
/*
OutputOCR Ocr(string path,Rect box) {
// namedWindow("grouping",WINDOW_NORMAL);
// read the image
Mat src = imread(path);
Expand Down Expand Up @@ -111,9 +97,9 @@ outputOCR Ocr(string path,Rect box) {
outputOCR output;
output.decodedText=decodedTxtRegions;
return output;
}
}*/

OutputOCR* Ocr(char* buffer, unsigned int len, string languageFile)
OutputOCR* Ocr(char* buffer, unsigned int len, string languageFile, string whitelist)
{
Mat temp(1, len, CV_8UC3, buffer);
Mat src = imdecode(temp, 1);
Expand All @@ -122,10 +108,10 @@ OutputOCR* Ocr(char* buffer, unsigned int len, string languageFile)
LOG << "Unable to load image." << endl;
return NULL;
}
return new OutputOCR(detectAndDecode(languageFile, src));
return new OutputOCR(detectAndDecode(languageFile, whitelist, src));
}

OutputOCR* Ocr (string path, string languageFile) {
OutputOCR* Ocr (string path, string languageFile, string whitelist) {
// namedWindow("grouping",WINDOW_NORMAL);
//Mat src = imread(path);
char* buffer = NULL;
Expand Down Expand Up @@ -158,10 +144,10 @@ OutputOCR* Ocr (string path, string languageFile) {
LOG << src.type() << endl;
LOG << CV_8UC3 << endl;
LOG << "image loaded" << endl;
return new OutputOCR(detectAndDecode(languageFile, src));
return new OutputOCR(detectAndDecode(languageFile, whitelist, src));
}

OutputOCR detectAndDecode(string languageFile, Mat &src){
OutputOCR detectAndDecode(string languageFile, string whitelist, Mat &src){

// Extract channels to be processed individually
vector<Mat> channels;
Expand All @@ -188,7 +174,7 @@ OutputOCR detectAndDecode(string languageFile, Mat &src){
// draw groups
Rect group_box= groups_draw(src, groups_boxes);
imwrite("save.jpg",src);
vector<DecodedText> decodedTxt=doOCR(languageFile, src,channels,region.regions,region.region_groups,groups_boxes);
vector<DecodedText> decodedTxt=doOCR(languageFile, whitelist, src,channels,region.regions,region.region_groups,groups_boxes);


OutputOCR output(Box(group_box.tl().x, group_box.br().x, group_box.tl().y, group_box.br().y), decodedTxt);
Expand All @@ -200,11 +186,11 @@ OutputOCR detectAndDecode(string languageFile, Mat &src){
}
return output;
}
vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,vector<vector<ERStat> > regions,vector< vector<Vec2i> > nm_region_groups,vector<Rect> nm_boxes){
vector<DecodedText> doOCR(string languageFile, string whitelist, 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();

Ptr<OCRTesseract> ocr = OCRTesseract::create(languageFile.c_str());
Ptr<OCRTesseract> ocr = OCRTesseract::create(languageFile.c_str(), NULL, whitelist.c_str());
string output;
Mat out_img;
Mat out_img_detection;
Expand Down Expand Up @@ -236,10 +222,19 @@ vector<DecodedText> doOCR(string languageFile, Mat &image,vector<Mat> channels,v
vector<float> confidences;
ocr->run(group_img, output, &boxes, &words, &confidences, OCR_LEVEL_WORD);

string word = "";
for(auto it = words.begin();it != words.end();it++)
word += *it;
float confidence = 0;
for(auto it = confidences.begin();it != confidences.end();it++)
if(*it > confidence)
confidence = *it;

output.erase(remove(output.begin(), output.end(), '\n'), output.end());
LOG << "OCR output = \"" << output << "\" length = " << output.size() << endl;
LOG << "OCR output = \"" << output << "\" length = " << output.size() << " confidence = " << confidence << 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));
if(words.size() != 0)
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), word, confidence));
}
return decodedTxtRegions;
}
Expand Down Expand Up @@ -371,13 +366,3 @@ void er_draw(vector<Mat> &channels, vector<vector<ERStat> > &regions, vector<Vec
}
}
}
//int main(){
// string path="/Users/prjha/Documents/git/pranavjha/_scene-text-detector/samples/012.jpg";
//
// Point tl,br;
// tl.x=0;tl.y=0;
// br.x=0;br.y=0;
// Rect box(tl,br);
// outputOCR op= Ocr(path, box);
//
//}
4 changes: 2 additions & 2 deletions 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, Rect box);
OutputOCR* Ocr(std::string imageFile, std::string languageFile, std::string whitelist);
OutputOCR* Ocr(char* image, unsigned int len, std::string languageFile, std::string whitelist);
37 changes: 26 additions & 11 deletions src/sync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,35 @@ using Nan::Utf8String;

// Simple synchronous access to the `GetText()` function
NAN_METHOD(GetTextSync) {
// get the value of path
Utf8String *utfPath = new Utf8String(info[0]);
Utf8String *utfLanguageFile = new Utf8String(info[1]);
string path(**utfPath);
string languageFile(**utfLanguageFile);
// if the second argument is passed, we use it
if (info.Length() > 1){
//detectRegions = To<bool>(info[1]).FromJust();
OutputOCR* decodedText = NULL;
if(!node::Buffer::HasInstance(info[0]))
{
// get the value of path
Utf8String *utfPath = new Utf8String(info[0]);
Utf8String *utfLanguageFile = new Utf8String(info[1]);
Utf8String *utfWhitelist = new Utf8String(info[2]);
string path(**utfPath);
string languageFile(**utfLanguageFile);
string whitelist(**utfWhitelist);

decodedText = Ocr(path, languageFile, whitelist);
}
// call the decoder here
OutputOCR* decodedText = Ocr(path, languageFile);
else
{
char* image = (char*)node::Buffer::Data(info[0]->ToObject());
unsigned int len = node::Buffer::Length(info[0]->ToObject());
Utf8String *utfLanguageFile = new Utf8String(info[1]);
Utf8String *utfWhitelist = new Utf8String(info[2]);
string languageFile(**utfLanguageFile);
string whitelist(**utfWhitelist);

decodedText = Ocr(image, len, languageFile, whitelist);
}

if(decodedText == NULL)
info.GetReturnValue().Set(Nan::New<v8::Object>());
info.GetReturnValue().Set(Nan::Undefined());
else info.GetReturnValue().Set(Nan::New(decodedText->ToLocal()));

delete decodedText;
}

Expand Down
24 changes: 6 additions & 18 deletions src/util/decoded_text.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,22 @@

struct DecodedText
{
DecodedText(Box box, std::vector<std::string> words, std::vector<float> confs)
: box(box), words(words), confs(confs) {}
DecodedText(Box box, std::string word, float conf)
: box(box), word(word), conf(conf) {}

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

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++)
{
Nan::Set(l_words, i, Nan::New(words[i]).ToLocalChecked());
}

for(int i=0;i<confs.size();i++)
{
Nan::Set(l_confs, i, Nan::New(confs[i]));
}

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);
Nan::Set(out, Nan::New("value").ToLocalChecked(), Nan::New(word.c_str()).ToLocalChecked());
Nan::Set(out, Nan::New("confidence").ToLocalChecked(), Nan::New(conf));
return out;
}
};
Expand Down

0 comments on commit 0c43ae6

Please sign in to comment.