summaryrefslogtreecommitdiff
path: root/sci_gateway1/cpp/opencv_ocr.cpp
blob: dc1dc6d9e2df9ca2857a815a53176e1696c501e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/***************************************************
Author : Rohit Suri
TODO   : Extract confidences for each character
       : Calculate Position of words
***************************************************/
#include <numeric>
#include <string.h>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <tesseract/baseapi.h>
using namespace cv; 
using namespace std;
using namespace tesseract;
extern "C"
{
    #include "api_scilab.h"
    #include "Scierror.h"
    #include "BOOL.h"
    #include <localization.h>
    #include "sciprint.h"
    #include "../common.h"
    
    /*Calling syntax: ocr(I) */

    int opencv_ocr(char *fname, unsigned long fname_len)
    {
        // Error management variables
        SciErr sciErr; 
        
        //------Local variables------//        
        Mat sourceImage;
        TessBaseAPI tesseract;
        int *wordConfidences = NULL;
        int wordCount = 0, characterCount = 0, count=0, coordinate;
        string word="";
        char *text = NULL;
        char **words = NULL;
        char *boxInformation = NULL;
        int *characterBoundingBoxes = NULL;
        int characterBoundingBoxesPos = 0;
        //------Check number of parameters------//
        CheckInputArgument(pvApiCtx, 1, 1); 
        CheckOutputArgument(pvApiCtx, 1, 4); 
        
        //------Get input arguments------//
        retrieveImage(sourceImage, 1); 
                
        //------Actual processing------//
        
        tesseract.Init(NULL, "eng", OEM_TESSERACT_ONLY);
        tesseract.SetPageSegMode(PSM_SINGLE_BLOCK);
        tesseract.SetVariable("tessedit_char_whitelist","0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-_=+[]{}:'\";\|,.<>/");
        tesseract.SetImage(sourceImage.data, sourceImage.cols, sourceImage.rows, sourceImage.elemSize(), sourceImage.step);
        wordConfidences = tesseract.AllWordConfidences();
        boxInformation = tesseract.GetBoxText(0);
        for(int iter = 0; ;iter++)
        {
            if(wordConfidences[iter]!=-1)
            {
                wordCount++;
            }
            else
            {
                break;          
            }
        }
        
        words = (char**) malloc(sizeof(char*)*wordCount);
        text = tesseract.GetUTF8Text();
        for(int iter = 0; count < wordCount; iter++)
        {
            
            if(count == wordCount)
            {
                break;
            }
            if(text[iter]==' ' || text[iter]=='\n')
            {
                words[count] = (char*) malloc(sizeof(char)*word.length()+1);
                for(int char_iter=0; char_iter<word.length();char_iter++)
                {
                    words[count][char_iter] = word[char_iter];
                }
                words[count][word.length()] = '\0';
                word = "";
                count++;
                
            }
            else
            {
                characterCount++;
                word = word + text[iter];
            }
        }
        characterBoundingBoxes = (int*) malloc(sizeof(int)*characterCount*4);
        
        for( int iter = 0; characterBoundingBoxesPos < characterCount ; iter++)
        {
            while( boxInformation[iter]!=' ')
            {
                iter++;
            }
            iter++;
            coordinate = 0;
            while(boxInformation[iter]!=' ')
            {
                coordinate = 10 * coordinate + boxInformation[iter] - '0';
                iter++;
            }
            characterBoundingBoxes [characterBoundingBoxesPos] = coordinate;
            coordinate = 0;
            iter++;
            while(boxInformation[iter]!=' ')
            {
                coordinate = 10 * coordinate + boxInformation[iter] - '0';
                iter++;
            }
            characterBoundingBoxes [characterCount + characterBoundingBoxesPos] = coordinate;
            coordinate = 0;
            iter++;
            while(boxInformation[iter]!=' ')
            {
                coordinate = 10 * coordinate + boxInformation[iter] - '0';
                iter++;
            }
            characterBoundingBoxes [2 * characterCount + characterBoundingBoxesPos] = coordinate;
            coordinate = 0;
            iter++;
            while(boxInformation[iter]!=' ')
            {
                coordinate = 10 * coordinate + boxInformation[iter] - '0';
                iter++;
            }
            characterBoundingBoxes [3 * characterCount + characterBoundingBoxesPos] = coordinate;
            characterBoundingBoxesPos++;
            iter+=2;
        }
        //------Create output arguments------//
        sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 1, &text);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 2, characterCount, 4, characterBoundingBoxes);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 3, wordCount, 1, words);
        if(sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        sciErr = createMatrixOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 4, wordCount, 1, wordConfidences); 
        if(sciErr.iErr)
        {
            printError(&sciErr, 0); 
            return 0; 
        }
        //------Return Arguments------//
        AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx)+1; 
        AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx)+2; 
        AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx)+3;
        AssignOutputVariable(pvApiCtx, 4) = nbInputArgument(pvApiCtx)+4;
        ReturnArguments(pvApiCtx); 
        return 0; 
    }
/* ==================================================================== */
}