fix(tesseract): catches panic in init thread
This commit is contained in:
parent
093503cd97
commit
43a869660f
@ -338,7 +338,14 @@ pub async fn analyze_card_libtesseract(
|
|||||||
let name_thread = task::spawn_blocking(move || {
|
let name_thread = task::spawn_blocking(move || {
|
||||||
// let mut leptess =
|
// let mut leptess =
|
||||||
// libtesseract::init_tesseract(false).expect("Failed to initialize Tesseract");
|
// libtesseract::init_tesseract(false).expect("Failed to initialize Tesseract");
|
||||||
let binding = unsafe { libtesseract::get_tesseract() };
|
let binding = unsafe {
|
||||||
|
match libtesseract::get_tesseract() {
|
||||||
|
Ok(b) => b,
|
||||||
|
Err(why) => {
|
||||||
|
panic!("{}", format!("Failed to get Tesseract: {:?}", why));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut leptess = binding.lock().unwrap();
|
let mut leptess = binding.lock().unwrap();
|
||||||
let name_img = image_with_white_padding(card_clone.crop_imm(
|
let name_img = image_with_white_padding(card_clone.crop_imm(
|
||||||
CARD_NAME_X_OFFSET,
|
CARD_NAME_X_OFFSET,
|
||||||
@ -366,7 +373,14 @@ pub async fn analyze_card_libtesseract(
|
|||||||
let series_thread = task::spawn_blocking(move || {
|
let series_thread = task::spawn_blocking(move || {
|
||||||
// let mut leptess =
|
// let mut leptess =
|
||||||
// libtesseract::init_tesseract(false).expect("Failed to initialize Tesseract");
|
// libtesseract::init_tesseract(false).expect("Failed to initialize Tesseract");
|
||||||
let binding = unsafe { libtesseract::get_tesseract() };
|
let binding = unsafe {
|
||||||
|
match libtesseract::get_tesseract() {
|
||||||
|
Ok(b) => b,
|
||||||
|
Err(why) => {
|
||||||
|
panic!("{}", format!("Failed to get Tesseract: {:?}", why));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut leptess = binding.lock().unwrap();
|
let mut leptess = binding.lock().unwrap();
|
||||||
let series_img = image_with_white_padding(card_clone.crop_imm(
|
let series_img = image_with_white_padding(card_clone.crop_imm(
|
||||||
CARD_SERIES_X_OFFSET,
|
CARD_SERIES_X_OFFSET,
|
||||||
@ -572,8 +586,7 @@ pub async fn analyze_drop_message(message: &Message) -> Result<Vec<DroppedCard>,
|
|||||||
(i, execute_analyze_drop(card_img, i).await)
|
(i, execute_analyze_drop(card_img, i).await)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
let mut handles: Vec<task::JoinHandle<(u32, Result<DroppedCard, String>)>> =
|
let mut handles: Vec<task::JoinHandle<(u32, Result<DroppedCard, String>)>> = Vec::new();
|
||||||
Vec::new();
|
|
||||||
for job in jobs {
|
for job in jobs {
|
||||||
let handle = task::spawn(job);
|
let handle = task::spawn(job);
|
||||||
handles.push(handle);
|
handles.push(handle);
|
||||||
|
@ -1,21 +1,29 @@
|
|||||||
pub use leptess::{LepTess, Variable};
|
pub use leptess::{LepTess, Variable};
|
||||||
use std::{
|
use std::{
|
||||||
|
panic::catch_unwind,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
thread,
|
thread,
|
||||||
};
|
};
|
||||||
|
use swordfish_common::error;
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
|
|
||||||
static mut TESSERACT_VEC: Vec<Arc<Mutex<LepTess>>> = Vec::new();
|
static mut TESSERACT_VEC: Vec<Arc<Mutex<LepTess>>> = Vec::new();
|
||||||
static mut TESSERACT_NUMERIC_VEC: Vec<Arc<Mutex<LepTess>>> = Vec::new();
|
static mut TESSERACT_NUMERIC_VEC: Vec<Arc<Mutex<LepTess>>> = Vec::new();
|
||||||
|
|
||||||
pub unsafe fn get_tesseract() -> Arc<Mutex<LepTess>> {
|
pub unsafe fn get_tesseract() -> Result<Arc<Mutex<LepTess>>, String> {
|
||||||
let lep_tess: Arc<Mutex<LepTess>>;
|
let lep_tess: Arc<Mutex<LepTess>>;
|
||||||
if TESSERACT_VEC.len() == 0 {
|
if TESSERACT_VEC.len() == 0 {
|
||||||
lep_tess = Arc::new(Mutex::new(create_tesseract(false).unwrap()));
|
let ocr = match create_tesseract(false) {
|
||||||
|
Ok(ocr) => ocr,
|
||||||
|
Err(why) => {
|
||||||
|
return Err(format!("Failed to create Tesseract: {:?}", why));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
lep_tess = Arc::new(Mutex::new(ocr));
|
||||||
} else {
|
} else {
|
||||||
lep_tess = TESSERACT_VEC.pop().unwrap();
|
lep_tess = TESSERACT_VEC.pop().unwrap();
|
||||||
}
|
}
|
||||||
lep_tess
|
Ok(lep_tess)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub unsafe fn get_tesseract_numeric() -> Arc<Mutex<LepTess>> {
|
pub unsafe fn get_tesseract_numeric() -> Arc<Mutex<LepTess>> {
|
||||||
@ -72,12 +80,38 @@ pub async fn init() {
|
|||||||
task::spawn_blocking(|| loop {
|
task::spawn_blocking(|| loop {
|
||||||
unsafe {
|
unsafe {
|
||||||
if TESSERACT_VEC.len() < 9 {
|
if TESSERACT_VEC.len() < 9 {
|
||||||
let ocr = create_tesseract(false).unwrap();
|
match catch_unwind(|| {
|
||||||
|
let ocr = match create_tesseract(false) {
|
||||||
|
Ok(ocr) => ocr,
|
||||||
|
Err(why) => {
|
||||||
|
error!("Failed to create Tesseract: {:?}", why);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
TESSERACT_VEC.push(Arc::new(Mutex::new(ocr)));
|
TESSERACT_VEC.push(Arc::new(Mutex::new(ocr)));
|
||||||
|
}) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(why) => {
|
||||||
|
error!("Failed to create Tesseract: {:?}", why);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if TESSERACT_NUMERIC_VEC.len() < 9 {
|
if TESSERACT_NUMERIC_VEC.len() < 9 {
|
||||||
let ocr = create_tesseract(true).unwrap();
|
match catch_unwind(|| {
|
||||||
|
let ocr = match create_tesseract(true) {
|
||||||
|
Ok(ocr) => ocr,
|
||||||
|
Err(why) => {
|
||||||
|
error!("Failed to create Tesseract: {:?}", why);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
TESSERACT_NUMERIC_VEC.push(Arc::new(Mutex::new(ocr)));
|
TESSERACT_NUMERIC_VEC.push(Arc::new(Mutex::new(ocr)));
|
||||||
|
}) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(why) => {
|
||||||
|
error!("Failed to create Tesseract (numeric): {:?}", why);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
thread::sleep(tokio::time::Duration::from_millis(500));
|
thread::sleep(tokio::time::Duration::from_millis(500));
|
||||||
|
Loading…
Reference in New Issue
Block a user