add hunyuan_ocr

This commit is contained in:
jhqxxx
2025-12-03 17:21:01 +08:00
parent 7d72cb3baf
commit 697484cf23
29 changed files with 1756 additions and 190 deletions
+6 -49
View File
@@ -11,7 +11,11 @@ use num::integer::lcm;
use crate::{
models::qwen3vl::config::PreprocessorConfig,
utils::{ceil_by_factor, floor_by_factor, img_utils::get_image, round_by_factor},
utils::{
ceil_by_factor, floor_by_factor,
img_utils::{get_image, img_smart_resize, img_transform},
round_by_factor,
},
};
#[derive(Clone)]
@@ -136,22 +140,9 @@ impl Qwen3VLProcessor {
(self.img_process_cfg.patch_size * self.img_process_cfg.merge_size) as u32,
self.img_process_cfg.size.shortest_edge as u32,
self.img_process_cfg.size.longest_edge as u32,
None,
)?;
let img = img.resize_exact(resize_w, resize_h, image::imageops::FilterType::CatmullRom);
let img_vec = img.to_rgb8().into_raw();
// (h, w, c) => (c, h, w)
let img_tensor = Tensor::from_slice(
&img_vec,
(resize_h as usize, resize_w as usize, 3),
&self.device,
)?
.permute((2, 0, 1))?
.to_dtype(self.dtype)?;
// 0-255 rescale to 0-1
let img_tensor = img_tensor.affine(1.0 / 255.0, 0.)?;
// normalize
let img_tensor = img_tensor.broadcast_sub(img_mean)?.broadcast_div(img_std)?;
let img_tensor = img_transform(&img, img_mean, img_std, &self.device, self.dtype)?;
// (c, h, w) => (1, c, h, w)
let img_tensor = img_tensor.unsqueeze(0)?;
Ok(img_tensor)
@@ -426,40 +417,6 @@ impl Qwen3VLProcessor {
}
}
pub fn img_smart_resize(
img_h: u32,
img_w: u32,
factor: u32,
min_pixels: u32,
max_pixels: u32,
video_ratio: Option<u32>,
) -> Result<(u32, u32)> {
if std::cmp::max(img_h, img_w) / std::cmp::min(img_h, img_w) > 200 {
return Err(anyhow!(format!(
"absolute aspect ratio mush be smaller than {}, got {}",
200,
std::cmp::max(img_h, img_w) / std::cmp::min(img_h, img_w)
)));
}
let mut image_factor = factor;
if let Some(ratio) = video_ratio {
image_factor = lcm(image_factor, ratio);
}
let mut h_bar = std::cmp::max(image_factor, round_by_factor(img_h, image_factor));
let mut w_bar = std::cmp::max(image_factor, round_by_factor(img_w, image_factor));
if h_bar * w_bar > max_pixels {
let beta = ((img_h * img_w) as f32 / max_pixels as f32).sqrt();
h_bar = floor_by_factor(img_h as f32 / beta, image_factor);
w_bar = floor_by_factor(img_w as f32 / beta, image_factor);
} else if h_bar * w_bar < min_pixels {
let beta = (min_pixels as f32 / (img_h * img_w) as f32).sqrt();
h_bar = ceil_by_factor(img_h as f32 * beta, image_factor);
w_bar = ceil_by_factor(img_w as f32 * beta, image_factor);
}
Ok((h_bar, w_bar))
}
pub fn video_smart_resize(
num_frames: u32,
height: u32,