mod utils;
use clap::Parser;
use oar_ocr::predictors::TextDetectionPredictor;
use oar_ocr::utils::load_image;
use std::path::PathBuf;
use std::time::Instant;
use tracing::{error, info, warn};
use utils::device_config::parse_device_config;
use utils::visualization::{Detection, DetectionVisConfig, save_rgb_image, visualize_detections};
#[derive(Parser)]
#[command(name = "text_detection")]
#[command(about = "Text Detection Example - detects text regions in images")]
struct Args {
#[arg(short, long)]
model_path: PathBuf,
#[arg(required = true)]
images: Vec<PathBuf>,
#[arg(short, long)]
output_dir: Option<PathBuf>,
#[arg(long)]
vis: bool,
#[arg(short, long, default_value = "cpu")]
device: String,
#[arg(long, default_value = "0.3")]
thresh: f32,
#[arg(long, default_value = "0.6")]
box_thresh: f32,
#[arg(long, default_value = "1.5")]
unclip_ratio: f32,
#[arg(long, default_value = "1000")]
max_candidates: usize,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
utils::init_tracing();
let args = Args::parse();
info!("Text Detection Example");
if !args.model_path.exists() {
error!("Model file not found: {}", args.model_path.display());
return Err("Model file not found".into());
}
let existing_images: Vec<PathBuf> = args
.images
.iter()
.filter(|path| {
let exists = path.exists();
if !exists {
error!("Image file not found: {}", path.display());
}
exists
})
.cloned()
.collect();
if existing_images.is_empty() {
error!("No valid image files found");
return Err("No valid image files found".into());
}
info!("Using device: {}", args.device);
let ort_config = parse_device_config(&args.device)?.unwrap_or_default();
if ort_config.execution_providers.is_some() {
info!("CUDA execution provider configured successfully");
}
let predictor = TextDetectionPredictor::builder()
.score_threshold(args.thresh)
.box_threshold(args.box_thresh)
.unclip_ratio(args.unclip_ratio)
.max_candidates(args.max_candidates)
.with_ort_config(ort_config)
.build(&args.model_path)?;
info!("Detection predictor built successfully");
info!("Processing {} images...", existing_images.len());
let mut images = Vec::new();
for image_path in &existing_images {
match load_image(image_path) {
Ok(rgb_img) => {
images.push(rgb_img);
}
Err(e) => {
error!("Failed to load image {}: {}", image_path.display(), e);
continue;
}
}
}
if images.is_empty() {
error!("No images could be loaded for processing");
return Err("No images could be loaded".into());
}
info!("Running text detection...");
let start = Instant::now();
let result = predictor.predict(images.clone())?;
let duration = start.elapsed();
info!(
"Detection completed in {:.2}ms",
duration.as_secs_f64() * 1000.0
);
for (idx, (image_path, detections)) in existing_images
.iter()
.zip(result.detections.iter())
.enumerate()
{
info!("\n=== Results for image {} ===", idx + 1);
info!("Image: {}", image_path.display());
info!("Total text regions detected: {}", detections.len());
if detections.is_empty() {
warn!("No text regions found in this image");
} else {
for (i, detection) in detections.iter().enumerate() {
let bbox = &detection.bbox;
let score = detection.score;
let (min_x, max_x, min_y, max_y) = bbox.points.iter().fold(
(
f32::INFINITY,
f32::NEG_INFINITY,
f32::INFINITY,
f32::NEG_INFINITY,
),
|(min_x, max_x, min_y, max_y), p| {
(
min_x.min(p.x),
max_x.max(p.x),
min_y.min(p.y),
max_y.max(p.y),
)
},
);
info!(
" Box #{}: [{:.0}, {:.0}, {:.0}, {:.0}] confidence {:.2}%",
i + 1,
min_x,
min_y,
max_x,
max_y,
score * 100.0
);
}
}
}
if args.vis {
let output_dir = args
.output_dir
.as_ref()
.ok_or("--output-dir is required when --vis is enabled")?;
std::fs::create_dir_all(output_dir)?;
info!("\nSaving visualizations to: {}", output_dir.display());
let vis_config = DetectionVisConfig::default();
for (image_path, rgb_img, detections) in existing_images
.iter()
.zip(images.iter())
.zip(result.detections.iter())
.map(|((path, img), detections)| (path, img, detections))
{
if !detections.is_empty() {
let vis_detections: Vec<Detection> = detections
.iter()
.map(|d| Detection::new(&d.bbox, d.score))
.collect();
let output_filename = image_path
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("unknown.jpg");
let output_path = output_dir.join(output_filename);
let visualized = visualize_detections(rgb_img, &vis_detections, &vis_config);
save_rgb_image(&visualized, &output_path)
.map_err(|e| format!("Failed to save visualization: {}", e))?;
info!(" Saved: {}", output_path.display());
} else {
warn!(
" Skipping visualization for {} (no detections)",
image_path.display()
);
}
}
}
Ok(())
}