Skip to content

Commit

Permalink
refactor(ocr): init TESSERACT on main thread
Browse files Browse the repository at this point in the history
if the image list have only one element, the ocr processing is called on
the main thread by `into_par_iter`.
If the var `TESSERACT` is not initialized on the main thread,
the function crash on the unwrap.

This should fix issue
[Error: called `Option::unwrap()` on a `None` value](#6)
  • Loading branch information
gwen-lg committed Feb 8, 2025
1 parent 6ad3ce3 commit bfb964c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ocr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ where
Img: IntoParallelIterator<Item = GrayImage>,
{
std::env::set_var("OMP_THREAD_LIMIT", "1");
// Init tesseract

// Init tesseract on the main thread:
let tesseract = TesseractWrapper::new(opt.tessdata_dir.as_deref(), opt.lang, opt.config)?;
if TESSERACT.replace(Some(tesseract)).is_some() {
return Err(Error::AlreadyInitialized);
};
// and on threadpool:
broadcast(|ctx| {
profiling::scope!("Tesseract Init Wrapper");
trace!(
Expand Down Expand Up @@ -109,14 +115,18 @@ where
})
.collect::<Vec<Result<String>>>();

// Clean tesseract from Thread local vars
// Clean tesseract from Thread local vars for Threadpool
broadcast(|ctx| {
profiling::scope!("Tesseract Drop Wrapper");
trace!("Drop TesseractWrapper local var on thread {}", ctx.index());
if let Some(tesseract) = TESSERACT.take() {
drop(tesseract);
}
});
// ... for main thread
if let Some(tesseract) = TESSERACT.take() {
drop(tesseract);
}

Ok(subs)
}
Expand Down

0 comments on commit bfb964c

Please sign in to comment.