Skip to content

Commit

Permalink
more opinionated changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-ha458 committed Jan 7, 2024
1 parent 6c102ce commit 2b65437
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 22 deletions.
32 changes: 17 additions & 15 deletions ocrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ impl OcrEngine {
/// Returns an unordered list of the oriented bounding rectangles of each
/// word found.
pub fn detect_words(&self, input: &OcrInput) -> Result<Vec<RotatedRect>, Box<dyn Error>> {
let Some(detector) = self.detector.as_ref() else {
return Err("Detection model not loaded".into());
};
detector.detect_words(input.image.view(), self.debug)
if let Some(detector) = self.detector.as_ref() {
detector.detect_words(input.image.view(), self.debug)
} else {
Err("Detection model not loaded".into())
}
}

/// Perform layout analysis to group words into lines and sort them in
Expand Down Expand Up @@ -128,17 +129,18 @@ impl OcrEngine {
input: &OcrInput,
lines: &[Vec<RotatedRect>],
) -> Result<Vec<Option<TextLine>>, Box<dyn Error>> {
let Some(recognizer) = self.recognizer.as_ref() else {
return Err("Recognition model not loaded".into());
};
recognizer.recognize_text_lines(
input.image.view(),
lines,
RecognitionOpt {
debug: self.debug,
decode_method: self.decode_method,
},
)
if let Some(recognizer) = self.recognizer.as_ref() {
recognizer.recognize_text_lines(
input.image.view(),
lines,
RecognitionOpt {
debug: self.debug,
decode_method: self.decode_method,
},
)
} else {
Err("Recognition model not loaded".into())
}
}

/// Convenience API that extracts all text from an image as a single string.
Expand Down
8 changes: 3 additions & 5 deletions ocrs/src/preprocess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn greyscale_image<F: Fn(f32) -> f32>(
) -> NdTensor<f32, 3> {
let [chans, height, width] = img.shape();
assert!(
chans == 1 || chans == 3 || chans == 4,
matches!(chans, 1 | 3 | 4),
"expected greyscale, RGB or RGBA input image"
);

Expand All @@ -39,10 +39,8 @@ fn greyscale_image<F: Fn(f32) -> f32>(

for y in 0..height {
for x in 0..width {
let mut pixel = 0.;
for c in 0..used_chans {
pixel += img[[c, y, x]] * chan_weights[c];
}
let pixel: f32 =
(0..used_chans).fold(0.2, |pixel, c| pixel + img[[c, y, x]] * chan_weights[c]);
out_lum_chan[[y, x]] = normalize_pixel(pixel);
}
}
Expand Down
4 changes: 2 additions & 2 deletions ocrs/src/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ pub fn gen_rect_grid(
rect_size: (i32, i32),
gap_size: (i32, i32),
) -> Vec<Rect> {
let mut rects = Vec::new();

let (rows, cols) = grid_shape;
let (rect_h, rect_w) = rect_size;
let (gap_h, gap_w) = gap_size;

let mut rects = Vec::with_capacity((rows * cols) as usize);

for r in 0..rows {
for c in 0..cols {
let top = top_left.y + r * (rect_h + gap_h);
Expand Down

0 comments on commit 2b65437

Please sign in to comment.