Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv_contrib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv_contrib
Commits
5073d5bc
Commit
5073d5bc
authored
Aug 13, 2014
by
lluis
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactors OCRTesseract interface
parent
47f61f1c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
84 deletions
+83
-84
ocr.hpp
modules/text/include/opencv2/text/ocr.hpp
+24
-38
end_to_end_recognition.cpp
modules/text/samples/end_to_end_recognition.cpp
+1
-1
webcam_demo.cpp
modules/text/samples/webcam_demo.cpp
+4
-5
ocr.cpp
modules/text/src/ocr.cpp
+47
-40
precomp.hpp
modules/text/src/precomp.hpp
+7
-0
No files found.
modules/text/include/opencv2/text/ocr.hpp
View file @
5073d5bc
...
...
@@ -44,24 +44,15 @@
#ifndef __OPENCV_TEXT_OCR_HPP__
#define __OPENCV_TEXT_OCR_HPP__
#include "text_config.hpp"
#ifdef HAVE_TESSERACT
#include <tesseract/baseapi.h>
#include <tesseract/resultiterator.h>
#endif
#include "opencv2/core.hpp"
#include <vector>
#include <string>
using
namespace
std
;
namespace
cv
{
namespace
text
{
using
namespace
std
;
enum
{
...
...
@@ -69,40 +60,35 @@ enum
OCR_LEVEL_TEXTLINE
};
#ifdef HAVE_TESSERACT
class
CV_EXPORTS
OCRTesseract
//base class BaseOCR declares a common API that would be used in a typical text recognition scenario
class
CV_EXPORTS
BaseOCR
{
private
:
tesseract
::
TessBaseAPI
tess
;
public
:
//Default constructor
OCRTesseract
(
const
char
*
datapath
=
NULL
,
const
char
*
language
=
NULL
,
const
char
*
char_whitelist
=
NULL
,
tesseract
::
OcrEngineMode
oem
=
tesseract
::
OEM_DEFAULT
,
tesseract
::
PageSegMode
psmode
=
tesseract
::
PSM_AUTO
);
~
OCRTesseract
();
void
run
(
Mat
&
image
,
string
&
output_text
,
vector
<
Rect
>*
component_rects
=
NULL
,
vector
<
string
>*
component_texts
=
NULL
,
vector
<
float
>*
component_confidences
=
NULL
,
int
component_level
=
0
);
virtual
~
BaseOCR
()
{};
virtual
void
run
(
Mat
&
image
,
string
&
output_text
,
vector
<
Rect
>*
component_rects
=
NULL
,
vector
<
string
>*
component_texts
=
NULL
,
vector
<
float
>*
component_confidences
=
NULL
,
int
component_level
=
0
)
=
0
;
};
#else
//stub
class
CV_EXPORTS
OCRTesseract
class
CV_EXPORTS
OCRTesseract
:
public
BaseOCR
{
public
:
//Default constructor
OCRTesseract
(
const
char
*
datapath
=
NULL
,
const
char
*
language
=
NULL
,
const
char
*
char_whitelist
=
NULL
,
int
oem
=
0
,
int
psmode
=
0
);
~
OCRTesseract
();
void
run
(
Mat
&
image
,
string
&
output_text
,
vector
<
Rect
>*
component_rects
=
NULL
,
vector
<
string
>*
component_texts
=
NULL
,
vector
<
float
>*
component_confidences
=
NULL
,
int
component_level
=
0
);
virtual
void
run
(
Mat
&
image
,
string
&
output_text
,
vector
<
Rect
>*
component_rects
=
NULL
,
vector
<
string
>*
component_texts
=
NULL
,
vector
<
float
>*
component_confidences
=
NULL
,
int
component_level
=
0
)
{
CV_Assert
(
(
image
.
type
()
==
CV_8UC1
)
||
(
image
.
type
()
==
CV_8UC1
)
);
CV_Assert
(
(
component_level
==
OCR_LEVEL_TEXTLINE
)
||
(
component_level
==
OCR_LEVEL_WORD
)
);
output_text
.
clear
();
if
(
component_rects
!=
NULL
)
component_rects
->
clear
();
if
(
component_texts
!=
NULL
)
component_texts
->
clear
();
if
(
component_confidences
!=
NULL
)
component_confidences
->
clear
();
}
static
Ptr
<
OCRTesseract
>
create
(
const
char
*
datapath
=
NULL
,
const
char
*
language
=
NULL
,
const
char
*
char_whitelist
=
NULL
,
int
oem
=
3
,
int
psmode
=
3
);
};
#endif
}
...
...
modules/text/samples/end_to_end_recognition.cpp
View file @
5073d5bc
...
...
@@ -102,7 +102,7 @@ int main(int argc, char* argv[])
/*Text Recognition (OCR)*/
double
t_r
=
(
double
)
getTickCount
();
OCRTesseract
*
ocr
=
new
OCRTesseract
();
Ptr
<
OCRTesseract
>
ocr
=
OCRTesseract
::
create
();
cout
<<
"TIME_OCR_INITIALIZATION = "
<<
((
double
)
getTickCount
()
-
t_r
)
*
1000
/
getTickFrequency
()
<<
endl
;
string
output
;
...
...
modules/text/samples/webcam_demo.cpp
View file @
5073d5bc
...
...
@@ -54,12 +54,12 @@ private:
vector
<
vector
<
Rect
>
>
&
boxes
;
vector
<
vector
<
string
>
>
&
words
;
vector
<
vector
<
float
>
>
&
confidences
;
vector
<
OCRTesseract
*
>
&
ocrs
;
vector
<
Ptr
<
OCRTesseract
>
>
&
ocrs
;
public
:
Parallel_OCR
(
vector
<
Mat
>
&
_detections
,
vector
<
string
>
&
_outputs
,
vector
<
vector
<
Rect
>
>
&
_boxes
,
vector
<
vector
<
string
>
>
&
_words
,
vector
<
vector
<
float
>
>
&
_confidences
,
vector
<
OCRTesseract
*
>
&
_ocrs
)
vector
<
Ptr
<
OCRTesseract
>
>
&
_ocrs
)
:
detections
(
_detections
),
outputs
(
_outputs
),
boxes
(
_boxes
),
words
(
_words
),
confidences
(
_confidences
),
ocrs
(
_ocrs
)
{}
...
...
@@ -120,11 +120,10 @@ int main(int argc, char* argv[])
//Initialize OCR engine (we initialize 10 instances in order to work several recognitions in parallel)
int
num_ocrs
=
10
;
vector
<
OCRTesseract
*
>
ocrs
;
vector
<
Ptr
<
OCRTesseract
>
>
ocrs
;
for
(
int
o
=
0
;
o
<
num_ocrs
;
o
++
)
{
OCRTesseract
*
ocr
=
new
OCRTesseract
();
ocrs
.
push_back
(
ocr
);
ocrs
.
push_back
(
OCRTesseract
::
create
());
}
//cout << "TIME_OCR_INITIALIZATION_ALT = "<< ((double)getTickCount() - t_r)*1000/getTickFrequency() << endl;
...
...
modules/text/src/ocr.cpp
View file @
5073d5bc
...
...
@@ -47,25 +47,32 @@
#include <iostream>
#include <fstream>
#include <queue>
using
namespace
std
;
namespace
cv
{
namespace
text
{
using
namespace
std
;
#ifdef HAVE_TESSERACT
//Default constructor
OCRTesseract
::
OCRTesseract
(
const
char
*
datapath
,
const
char
*
language
,
const
char
*
char_whitelist
,
tesseract
::
OcrEngineMode
oemode
,
tesseract
::
PageSegMode
psmode
)
class
OCRTesseractImpl
:
public
OCRTesseract
{
private
:
#ifdef HAVE_TESSERACT
tesseract
::
TessBaseAPI
tess
;
#endif
public
:
//Default constructor
OCRTesseractImpl
(
const
char
*
datapath
,
const
char
*
language
,
const
char
*
char_whitelist
,
int
oemode
,
int
psmode
)
{
#ifdef HAVE_TESSERACT
const
char
*
lang
=
"eng"
;
if
(
language
!=
NULL
)
lang
=
language
;
if
(
tess
.
Init
(
datapath
,
lang
,
oemode
))
if
(
tess
.
Init
(
datapath
,
lang
,
(
tesseract
::
OcrEngineMode
)
oemode
))
{
cout
<<
"OCRTesseract: Could not initialize tesseract."
<<
endl
;
throw
1
;
...
...
@@ -73,7 +80,7 @@ OCRTesseract::OCRTesseract(const char* datapath, const char* language, const cha
//cout << "OCRTesseract: tesseract version " << tess.Version() << endl;
tesseract
::
PageSegMode
pagesegmode
=
psmode
;
tesseract
::
PageSegMode
pagesegmode
=
(
tesseract
::
PageSegMode
)
psmode
;
tess
.
SetPageSegMode
(
pagesegmode
);
if
(
char_whitelist
!=
NULL
)
...
...
@@ -82,18 +89,33 @@ OCRTesseract::OCRTesseract(const char* datapath, const char* language, const cha
tess
.
SetVariable
(
"tessedit_char_whitelist"
,
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
);
tess
.
SetVariable
(
"save_best_choices"
,
"T"
);
#else
cout
<<
"OCRTesseract("
<<
oemode
<<
psmode
<<
"): Tesseract not found."
<<
endl
;
if
(
datapath
!=
NULL
)
cout
<<
" "
<<
datapath
<<
endl
;
if
(
language
!=
NULL
)
cout
<<
" "
<<
language
<<
endl
;
if
(
char_whitelist
!=
NULL
)
cout
<<
" "
<<
char_whitelist
<<
endl
;
#endif
}
}
~
OCRTesseractImpl
()
{
#ifdef HAVE_TESSERACT
tess
.
End
();
#endif
}
OCRTesseract
::~
OCRTesseract
()
{
tess
.
End
();
}
void
run
(
Mat
&
image
,
string
&
output
,
vector
<
Rect
>*
component_rects
=
NULL
,
vector
<
string
>*
component_texts
=
NULL
,
vector
<
float
>*
component_confidences
=
NULL
,
int
component_level
=
0
)
{
CV_Assert
(
(
image
.
type
()
==
CV_8UC1
)
||
(
image
.
type
()
==
CV_8UC1
)
);
#ifdef HAVE_TESSERACT
void
OCRTesseract
::
run
(
Mat
&
image
,
string
&
output
,
vector
<
Rect
>*
component_rects
,
vector
<
string
>*
component_texts
,
vector
<
float
>*
component_confidences
,
int
component_level
)
{
CV_Assert
(
(
image
.
type
()
==
CV_8UC1
)
||
(
image
.
type
()
==
CV_8UC1
)
);
if
(
component_texts
!=
0
)
component_texts
->
clear
();
if
(
component_rects
!=
0
)
...
...
@@ -135,30 +157,8 @@ void OCRTesseract::run(Mat& image, string& output, vector<Rect>* component_rects
}
tess
.
Clear
();
}
#else
//Stub constructor
OCRTesseract
::
OCRTesseract
(
const
char
*
datapath
,
const
char
*
language
,
const
char
*
char_whitelist
,
int
oemode
,
int
psmode
)
{
cout
<<
"OCRTesseract("
<<
oemode
<<
psmode
<<
"): Tesseract not found."
<<
endl
;
if
(
datapath
!=
NULL
)
cout
<<
" "
<<
datapath
<<
endl
;
if
(
language
!=
NULL
)
cout
<<
" "
<<
language
<<
endl
;
if
(
char_whitelist
!=
NULL
)
cout
<<
" "
<<
char_whitelist
<<
endl
;
}
//Stub destructor
OCRTesseract
::~
OCRTesseract
()
{
}
//Stub method, does nothing
void
OCRTesseract
::
run
(
Mat
&
image
,
string
&
output
,
vector
<
Rect
>*
component_rects
,
vector
<
string
>*
component_texts
,
vector
<
float
>*
component_confidences
,
int
component_level
)
{
CV_Assert
(
(
image
.
type
()
==
CV_8UC1
)
||
(
image
.
type
()
==
CV_8UC1
)
);
#else
cout
<<
"OCRTesseract("
<<
component_level
<<
image
.
type
()
<<
"): Tesseract not found."
<<
endl
;
output
.
clear
();
...
...
@@ -168,9 +168,16 @@ void OCRTesseract::run(Mat& image, string& output, vector<Rect>* component_rects
component_texts
->
clear
();
if
(
component_confidences
)
component_confidences
->
clear
();
}
#endif
}
};
Ptr
<
OCRTesseract
>
OCRTesseract
::
create
(
const
char
*
datapath
,
const
char
*
language
,
const
char
*
char_whitelist
,
int
oem
,
int
psmode
)
{
return
makePtr
<
OCRTesseractImpl
>
(
datapath
,
language
,
char_whitelist
,
oem
,
psmode
);
}
}
...
...
modules/text/src/precomp.hpp
View file @
5073d5bc
...
...
@@ -45,4 +45,11 @@
#include "opencv2/text.hpp"
#include "text_config.hpp"
#ifdef HAVE_TESSERACT
#include <tesseract/baseapi.h>
#include <tesseract/resultiterator.h>
#endif
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment