From bfb964c0844f5f71573390a93ddb27463bfab1fb Mon Sep 17 00:00:00 2001 From: Gwen Lg Date: Sat, 8 Feb 2025 21:31:13 +0100 Subject: [PATCH] refactor(ocr): init `TESSERACT` on main thread 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](https://github.com/gwen-lg/subtile-ocr/issues/6) --- src/ocr.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ocr.rs b/src/ocr.rs index 9b679a1..6228dd8 100644 --- a/src/ocr.rs +++ b/src/ocr.rs @@ -77,7 +77,13 @@ where Img: IntoParallelIterator, { 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!( @@ -109,7 +115,7 @@ where }) .collect::>>(); - // 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()); @@ -117,6 +123,10 @@ where drop(tesseract); } }); + // ... for main thread + if let Some(tesseract) = TESSERACT.take() { + drop(tesseract); + } Ok(subs) }