121 lines
4.3 KiB
Rust
121 lines
4.3 KiB
Rust
use std::fs;
|
||
use std::path::Path;
|
||
use image::Rgb;
|
||
use ddddocr_rs::{DdddOcr, DdddOcrBuilder}; // 假设你的包名是这个
|
||
use ddddocr_rs::slide_model::Slide;
|
||
fn load_image<P: AsRef<Path>>(path: P) -> anyhow::Result<image::DynamicImage> {
|
||
// 1. 先将泛型转为具体的 &Path 引用
|
||
let path_ref = path.as_ref();
|
||
|
||
// 2. 调用 open 时传入引用(image::open 支持 AsRef<Path>)
|
||
image::open(path_ref)
|
||
.map_err(|e| {
|
||
// 3. 此时 path_ref 依然有效,可以安全地在闭包中使用
|
||
anyhow::anyhow!("无法加载图片 {:?}: {}", path_ref, e)
|
||
})
|
||
}
|
||
/// 将检测结果绘制在图像上并保存
|
||
fn save_debug_image( image_bytes: &[u8], bboxes: &Vec<Vec<i32>>, output_path: &str) -> anyhow::Result<()> {
|
||
|
||
|
||
let dynamic_img = image::load_from_memory(image_bytes)?;
|
||
let mut img = dynamic_img.to_rgb8();
|
||
let (width, height) = img.dimensions();
|
||
let red = Rgb([255u8, 0, 0]);
|
||
|
||
for bbox in bboxes {
|
||
// 基础边界检查
|
||
let x1 = bbox[0].max(0).min(width as i32 - 1) as u32;
|
||
let y1 = bbox[1].max(0).min(height as i32 - 1) as u32;
|
||
let x2 = bbox[2].max(0).min(width as i32 - 1) as u32;
|
||
let y2 = bbox[3].max(0).min(height as i32 - 1) as u32;
|
||
|
||
// 绘制横向线条
|
||
for x in x1..=x2 {
|
||
img.put_pixel(x, y1, red);
|
||
img.put_pixel(x, y2, red);
|
||
// 如果要加粗,多画一行
|
||
if y1 + 1 < height { img.put_pixel(x, y1 + 1, red); }
|
||
if y2.saturating_sub(1) > 0 { img.put_pixel(x, y2 - 1, red); }
|
||
}
|
||
// 绘制纵向线条
|
||
for y in y1..=y2 {
|
||
img.put_pixel(x1, y, red);
|
||
img.put_pixel(x2, y, red);
|
||
// 如果要加粗,多画一列
|
||
if x1 + 1 < width { img.put_pixel(x1 + 1, y, red); }
|
||
if x2.saturating_sub(1) > 0 { img.put_pixel(x2 - 1, y, red); }
|
||
}
|
||
}
|
||
|
||
img.save(output_path)?;
|
||
Ok(())
|
||
}
|
||
#[test]
|
||
fn test_full_classification() {
|
||
// 1. 初始化模型
|
||
let ocr = DdddOcrBuilder::new().build().expect("模型加载失败");
|
||
|
||
// 2. 加载测试图片
|
||
let img = image::open("samples/code3.png").expect("测试图片不存在");
|
||
|
||
// 3. 执行识别
|
||
let result = ocr.classification(&img).expect("识别过程出错");
|
||
|
||
println!("识别结果: {}", result);
|
||
assert!(!result.is_empty());
|
||
}
|
||
#[test]
|
||
fn test_det_load()->anyhow::Result<()>{
|
||
let det = DdddOcrBuilder::new().det().build()?;
|
||
let image_path = "samples/det1.png";
|
||
let image_bytes = fs::read(image_path)
|
||
.map_err(|e| anyhow::anyhow!("无法读取图片 {}: {}", image_path, e))?;
|
||
|
||
println!("图片读取成功,字节大小: {}", image_bytes.len());
|
||
let bboxes =det.detection(&image_bytes)?;
|
||
println!(":?{}",det);
|
||
println!("检测到的目标数量: {}", bboxes.len());
|
||
if bboxes.is_empty() {
|
||
println!("未检测到任何目标。");
|
||
} else {
|
||
save_debug_image(&image_bytes, &bboxes, "samples/result.jpg")?;
|
||
for (i, bbox) in bboxes.iter().enumerate() {
|
||
println!("目标 [{}]: x1={}, y1={}, x2={}, y2={}", i, bbox[0], bbox[1], bbox[2], bbox[3]);
|
||
}
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
|
||
#[test]
|
||
fn test_real_slide_match() {
|
||
let engine = Slide::new();
|
||
|
||
// 1. 加载你准备好的测试图
|
||
// 假设图片放在项目根目录下的 assets 文件夹
|
||
let target_img = load_image("samples/hua.png")
|
||
.expect("请确保 samples/hua.png 存在");
|
||
let bg_img = load_image("samples/huatu.png")
|
||
.expect("请确保 samples/huatu.png 存在");
|
||
|
||
// 2. 执行匹配
|
||
// 如果是那种带有明显阴影边缘的复杂滑块,建议 simple_target 传 false
|
||
let start = std::time::Instant::now();
|
||
let result = engine.slide_match(&target_img, &bg_img, false)
|
||
.expect("Slide match 执行失败");
|
||
let duration = start.elapsed();
|
||
|
||
// 3. 打印结果
|
||
println!("-------------------------------------------");
|
||
println!("滑块匹配测试结果:");
|
||
println!("检测坐标: [x: {}, y: {}]", result.target_x, result.target_y);
|
||
println!("置信度: {:.4}", result.confidence);
|
||
println!("耗时: {:?}", duration);
|
||
println!("-------------------------------------------");
|
||
|
||
// 验证基本逻辑:坐标不应为 0 (除非匹配失败)
|
||
assert_eq!(result.target_x, 237);
|
||
assert_eq!(result.target_y, 77);
|
||
assert!(result.confidence > 0.0);
|
||
} |