Skip to content

Commit b36aff9

Browse files
authored
Add support for set_page_seg_mode (#36)
* Add support for set_page_seg_mode #33 * Add final new line to python file
1 parent b6b11b1 commit b36aff9

File tree

6 files changed

+167
-2
lines changed

6 files changed

+167
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tesseract"
3-
version = "0.12.1"
3+
version = "0.13.0"
44
authors = ["Kevin Kwok <[email protected]>", "Chris Couzens <[email protected]>"]
55
documentation = "https://docs.rs/tesseract"
66
repository = "https://github.com/antimatter15/tesseract-rs"
@@ -11,5 +11,5 @@ categories = ["api-bindings", "multimedia::images"]
1111

1212
[dependencies]
1313
tesseract-sys = "~0.5"
14-
tesseract-plumbing = "~0.8"
14+
tesseract-plumbing = "~0.9"
1515
thiserror = "1.0"

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
src/page_seg_mode.rs: page-seg-modes.txt build_page_seg_modes.py
2+
python build_page_seg_modes.py < page-seg-modes.txt | rustfmt > $@

build_page_seg_modes.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sys
2+
from itertools import islice
3+
4+
class PageSegMode:
5+
def __init__(self, name, comment):
6+
self.name = name
7+
self.comment = comment
8+
9+
def name_as_enum(self):
10+
return ''.join(n.capitalize() for n in self.name.split("_"))
11+
12+
def name_as_variable(self):
13+
return 'tesseract_sys::TessPageSegMode_' + self.name
14+
15+
name = None
16+
page_seg_modes = []
17+
i = 0;
18+
19+
for line in islice(sys.stdin, 1, None):
20+
if i == 0:
21+
name = line.rstrip('\n')
22+
elif i == 1:
23+
comment = line.rstrip('\n')
24+
page_seg_mode = PageSegMode(name, comment)
25+
page_seg_modes.append(page_seg_mode)
26+
i = (i + 1) % 3
27+
28+
print("// ⚠️ This file is generated")
29+
print("// ⚠️ Regenerate with `make src/page_seg_mode.rs`")
30+
print()
31+
print("use tesseract_sys::TessPageSegMode;")
32+
print()
33+
print("/// Enum representing different PageSegMode options accepted by Tesseract")
34+
print("#[derive(Debug, Clone, Copy, PartialEq, Eq)]")
35+
print("pub enum PageSegMode {")
36+
37+
for page_seg_mode in page_seg_modes:
38+
print(f" /// {page_seg_mode.comment}")
39+
print(f" {page_seg_mode.name_as_enum()},",)
40+
41+
print("}")
42+
print()
43+
print("impl PageSegMode {")
44+
print(" /// Get the page-seg-mode's value as used by Tesseract")
45+
print(" pub fn as_tess_page_seg_mode(&self) -> TessPageSegMode {")
46+
print(" match self {")
47+
48+
for page_seg_mode in page_seg_modes:
49+
print(f" PageSegMode::{page_seg_mode.name_as_enum()} => {page_seg_mode.name_as_variable()},")
50+
51+
print(" }")
52+
print(" }")
53+
print("}")

page-seg-modes.txt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copied from https://tesseract-ocr.github.io/tessapi/5.x/a01818.html#a338d4c8b5d497b5ec3e6e4269d8ac66aab76fe3ca390d99e070ea60b892ee18ef
2+
PSM_OSD_ONLY
3+
Orientation and script detection only.
4+
5+
PSM_AUTO_OSD
6+
Automatic page segmentation with orientation and script detection. (OSD)
7+
8+
PSM_AUTO_ONLY
9+
Automatic page segmentation, but no OSD, or OCR.
10+
11+
PSM_AUTO
12+
Fully automatic page segmentation, but no OSD.
13+
14+
PSM_SINGLE_COLUMN
15+
Assume a single column of text of variable sizes.
16+
17+
PSM_SINGLE_BLOCK_VERT_TEXT
18+
Assume a single uniform block of vertically aligned text.
19+
20+
PSM_SINGLE_BLOCK
21+
Assume a single uniform block of text. (Default.)
22+
23+
PSM_SINGLE_LINE
24+
Treat the image as a single text line.
25+
26+
PSM_SINGLE_WORD
27+
Treat the image as a single word.
28+
29+
PSM_CIRCLE_WORD
30+
Treat the image as a single word in a circle.
31+
32+
PSM_SINGLE_CHAR
33+
Treat the image as a single character.
34+
35+
PSM_SPARSE_TEXT
36+
Find as much text as possible in no particular order.
37+
38+
PSM_SPARSE_TEXT_OSD
39+
Sparse text with orientation and script det.
40+
41+
PSM_RAW_LINE
42+
Treat the image as a single text line, bypassing hacks that are Tesseract-specific.

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use std::ffi::CString;
77
use std::ffi::NulError;
88
use std::os::raw::c_int;
99
use std::str;
10+
mod page_seg_mode;
11+
12+
pub use page_seg_mode::PageSegMode;
1013

1114
use self::tesseract_sys::{
1215
TessOcrEngineMode, TessOcrEngineMode_OEM_DEFAULT, TessOcrEngineMode_OEM_LSTM_ONLY,
@@ -214,6 +217,10 @@ impl Tesseract {
214217
.to_string_lossy()
215218
.into_owned())
216219
}
220+
221+
pub fn set_page_seg_mode(&mut self, mode: PageSegMode) {
222+
self.0.set_page_seg_mode(mode.as_tess_page_seg_mode());
223+
}
217224
}
218225

219226
pub fn ocr(filename: &str, language: &str) -> Result<String, TesseractError> {

src/page_seg_mode.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// ⚠️ This file is generated
2+
// ⚠️ Regenerate with `make src/page_seg_mode.rs`
3+
4+
use tesseract_sys::TessPageSegMode;
5+
6+
/// Enum representing different PageSegMode options accepted by Tesseract
7+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8+
pub enum PageSegMode {
9+
/// Orientation and script detection only.
10+
PsmOsdOnly,
11+
/// Automatic page segmentation with orientation and script detection. (OSD)
12+
PsmAutoOsd,
13+
/// Automatic page segmentation, but no OSD, or OCR.
14+
PsmAutoOnly,
15+
/// Fully automatic page segmentation, but no OSD.
16+
PsmAuto,
17+
/// Assume a single column of text of variable sizes.
18+
PsmSingleColumn,
19+
/// Assume a single uniform block of vertically aligned text.
20+
PsmSingleBlockVertText,
21+
/// Assume a single uniform block of text. (Default.)
22+
PsmSingleBlock,
23+
/// Treat the image as a single text line.
24+
PsmSingleLine,
25+
/// Treat the image as a single word.
26+
PsmSingleWord,
27+
/// Treat the image as a single word in a circle.
28+
PsmCircleWord,
29+
/// Treat the image as a single character.
30+
PsmSingleChar,
31+
/// Find as much text as possible in no particular order.
32+
PsmSparseText,
33+
/// Sparse text with orientation and script det.
34+
PsmSparseTextOsd,
35+
/// Treat the image as a single text line, bypassing hacks that are Tesseract-specific.
36+
PsmRawLine,
37+
}
38+
39+
impl PageSegMode {
40+
/// Get the page-seg-mode's value as used by Tesseract
41+
pub fn as_tess_page_seg_mode(&self) -> TessPageSegMode {
42+
match self {
43+
PageSegMode::PsmOsdOnly => tesseract_sys::TessPageSegMode_PSM_OSD_ONLY,
44+
PageSegMode::PsmAutoOsd => tesseract_sys::TessPageSegMode_PSM_AUTO_OSD,
45+
PageSegMode::PsmAutoOnly => tesseract_sys::TessPageSegMode_PSM_AUTO_ONLY,
46+
PageSegMode::PsmAuto => tesseract_sys::TessPageSegMode_PSM_AUTO,
47+
PageSegMode::PsmSingleColumn => tesseract_sys::TessPageSegMode_PSM_SINGLE_COLUMN,
48+
PageSegMode::PsmSingleBlockVertText => {
49+
tesseract_sys::TessPageSegMode_PSM_SINGLE_BLOCK_VERT_TEXT
50+
}
51+
PageSegMode::PsmSingleBlock => tesseract_sys::TessPageSegMode_PSM_SINGLE_BLOCK,
52+
PageSegMode::PsmSingleLine => tesseract_sys::TessPageSegMode_PSM_SINGLE_LINE,
53+
PageSegMode::PsmSingleWord => tesseract_sys::TessPageSegMode_PSM_SINGLE_WORD,
54+
PageSegMode::PsmCircleWord => tesseract_sys::TessPageSegMode_PSM_CIRCLE_WORD,
55+
PageSegMode::PsmSingleChar => tesseract_sys::TessPageSegMode_PSM_SINGLE_CHAR,
56+
PageSegMode::PsmSparseText => tesseract_sys::TessPageSegMode_PSM_SPARSE_TEXT,
57+
PageSegMode::PsmSparseTextOsd => tesseract_sys::TessPageSegMode_PSM_SPARSE_TEXT_OSD,
58+
PageSegMode::PsmRawLine => tesseract_sys::TessPageSegMode_PSM_RAW_LINE,
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)