Skip to content

Commit 99f6788

Browse files
authored
Support frame buffer as input (#15)
* Tesseract handles frame buffer as input * Add ocr_from_frame function and unit test Include RAW TIFF image to avoid decoding process into unit tests * Expect input frame data as slice instead of Vec
1 parent e4575cb commit 99f6788

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

img.tiff

2.09 MB
Binary file not shown.

src/lib.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ use std::ptr;
88
use std::str;
99
use tesseract_sys::{
1010
TessBaseAPI, TessBaseAPICreate, TessBaseAPIDelete, TessBaseAPIGetUTF8Text, TessBaseAPIInit3,
11-
TessBaseAPIRecognize, TessBaseAPISetImage2, TessBaseAPISetVariable, TessDeleteText,
11+
TessBaseAPIRecognize, TessBaseAPISetImage, TessBaseAPISetImage2, TessBaseAPISetVariable,
12+
TessDeleteText,
1213
};
1314

1415
pub struct Tesseract {
@@ -50,6 +51,25 @@ impl Tesseract {
5051
pixFreeData(img);
5152
}
5253
}
54+
pub fn set_frame(
55+
&mut self,
56+
frame_data: &[u8],
57+
width: i32,
58+
height: i32,
59+
bytes_per_pixel: i32,
60+
bytes_per_line: i32,
61+
) {
62+
unsafe {
63+
TessBaseAPISetImage(
64+
self.raw,
65+
frame_data.as_ptr(),
66+
width,
67+
height,
68+
bytes_per_pixel,
69+
bytes_per_line,
70+
);
71+
}
72+
}
5373
pub fn set_variable(&mut self, name: &str, value: &str) -> i32 {
5474
let cs_name = cs(name);
5575
let cs_value = cs(value);
@@ -76,6 +96,21 @@ pub fn ocr(filename: &str, language: &str) -> String {
7696
cube.get_text()
7797
}
7898

99+
pub fn ocr_from_frame(
100+
frame_data: &[u8],
101+
width: i32,
102+
height: i32,
103+
bytes_per_pixel: i32,
104+
bytes_per_line: i32,
105+
language: &str,
106+
) -> String {
107+
let mut cube = Tesseract::new();
108+
cube.set_lang(language);
109+
cube.set_frame(frame_data, width, height, bytes_per_pixel, bytes_per_line);
110+
cube.recognize();
111+
cube.get_text()
112+
}
113+
79114
#[test]
80115
fn ocr_test() {
81116
assert_eq!(
@@ -84,6 +119,21 @@ fn ocr_test() {
84119
);
85120
}
86121

122+
#[test]
123+
fn ocr_from_frame_test() {
124+
use std::fs::File;
125+
use std::io::Read;
126+
127+
let mut img = File::open("img.tiff").unwrap();
128+
let mut buffer = Vec::new();
129+
let _read_data = img.read_to_end(&mut buffer).unwrap();
130+
131+
assert_eq!(
132+
ocr_from_frame(&buffer, 2256, 324, 3, 2256 * 3, "eng"),
133+
include_str!("../img.txt").to_string()
134+
);
135+
}
136+
87137
#[test]
88138
fn expanded_test() {
89139
let mut cube = Tesseract::new();

0 commit comments

Comments
 (0)