ultralytics-inference 0.0.11

Ultralytics YOLO inference library and CLI for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
// Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license

//! Image annotation utilities.
//!
//! This module provides functions for drawing bounding boxes, labels, and other
//! annotations on images based on inference results.

use crate::results::Results;
use crate::visualizer::color::{COLORS, POSE_COLORS};
use crate::visualizer::skeleton::{KPT_COLOR_INDICES, LIMB_COLOR_INDICES, SKELETON};
use ab_glyph::{Font, FontRef, PxScale, ScaleFont};
use image::{DynamicImage, Rgb};
use imageproc::drawing::{draw_filled_rect_mut, draw_hollow_rect_mut, draw_text_mut};
use imageproc::rect::Rect;
use std::fs::{self, File};
use std::io::{self, BufReader, Read};
use std::path::{Path, PathBuf};

/// Assets URL for downloading fonts
const ASSETS_URL: &str = "https://github.com/ultralytics/assets/releases/download/v0.0.0";

/// Get color for a class ID
#[must_use]
pub const fn get_class_color(class_id: usize) -> Rgb<u8> {
    let color = COLORS[class_id % COLORS.len()];
    Rgb(color)
}

/// Find the next available run directory (predict, predict2, predict3, etc.)
#[must_use]
pub fn find_next_run_dir(base: &str, prefix: &str) -> String {
    let base_path = Path::new(base);

    // First try without number
    let first = base_path.join(prefix);
    if !first.exists() {
        return first.to_string_lossy().to_string();
    }

    // Try with incrementing numbers
    for i in 2.. {
        let numbered = base_path.join(format!("{prefix}{i}"));
        if !numbered.exists() {
            return numbered.to_string_lossy().to_string();
        }
    }

    // Fallback (should never reach here)
    base_path.join(prefix).to_string_lossy().to_string()
}

/// Load image helper to bypass zune-jpeg stride issues
#[allow(clippy::missing_errors_doc)]
pub fn load_image(path: &str) -> image::ImageResult<DynamicImage> {
    let path_obj = Path::new(path);
    let ext = path_obj
        .extension()
        .and_then(|e| e.to_str())
        .map(str::to_lowercase);

    if let Some("jpg" | "jpeg") = ext.as_deref()
        && let Ok(file) = File::open(path)
    {
        let mut decoder = jpeg_decoder::Decoder::new(BufReader::new(file));
        if let Ok(pixels) = decoder.decode()
            && let Some(metadata) = decoder.info()
        {
            let width = u32::from(metadata.width);
            let height = u32::from(metadata.height);
            match metadata.pixel_format {
                jpeg_decoder::PixelFormat::RGB24 => {
                    if let Some(buffer) = image::ImageBuffer::from_raw(width, height, pixels) {
                        return Ok(DynamicImage::ImageRgb8(buffer));
                    }
                }
                jpeg_decoder::PixelFormat::L8 => {
                    if let Some(buffer) = image::ImageBuffer::from_raw(width, height, pixels) {
                        return Ok(DynamicImage::ImageLuma8(buffer));
                    }
                }
                _ => {}
            }
        }
    }
    // Fallback
    image::open(path)
}
/// Check if font exists locally or download it
#[must_use]
pub fn check_font(font: &str) -> Option<PathBuf> {
    let font_name = Path::new(font).file_name()?.to_string_lossy();
    let config_dir = dirs::config_dir()?.join("Ultralytics");
    let font_path = config_dir.join(font_name.as_ref());

    if font_path.exists() {
        return Some(font_path);
    }

    // Create config directory if it doesn't exist
    if let Err(e) = fs::create_dir_all(&config_dir) {
        eprintln!("Failed to create config directory: {e}");
        return None;
    }

    // Download font
    let url = format!("{ASSETS_URL}/{font_name}");
    println!("Downloading {url} to {}", font_path.display());

    match ureq::get(&url).call() {
        Ok(response) => {
            let mut file = match File::create(&font_path) {
                Ok(f) => f,
                Err(e) => {
                    eprintln!("Failed to create font file: {e}");
                    return None;
                }
            };

            let mut reader = response.into_body().into_reader();
            if let Err(e) = io::copy(&mut reader, &mut file) {
                eprintln!("Failed to download font: {e}");
                // Try to remove partial file
                let _ = fs::remove_file(&font_path);
                return None;
            }

            Some(font_path)
        }
        Err(e) => {
            eprintln!("Failed to download font from {url}: {e}");
            None
        }
    }
}

/// Annotate an image with detection boxes and labels
#[must_use]
pub fn annotate_image(
    image: &DynamicImage,
    result: &Results,
    top_k: Option<usize>,
) -> DynamicImage {
    let mut img = image.to_rgb8();

    let font_name = if result.boxes.is_some() && result.names.values().any(|n| !n.is_ascii()) {
        "Arial.Unicode.ttf"
    } else {
        "Arial.ttf"
    };

    // Load font
    let font_path = check_font(font_name);
    let font_data = font_path.and_then(|path| {
        let mut file = File::open(path).ok()?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer).ok()?;
        Some(buffer)
    });

    let font = font_data
        .as_ref()
        .and_then(|data| FontRef::try_from_slice(data).ok());

    // Draw all annotations using helpers
    draw_detection(&mut img, result, font.as_ref());
    draw_pose(&mut img, result, None, None, None);
    draw_obb(&mut img, result, font.as_ref());
    draw_classification(&mut img, result, font.as_ref(), top_k.unwrap_or(5));

    DynamicImage::ImageRgb8(img)
}

/// Draw a line segment on an image
#[allow(
    clippy::cast_sign_loss,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap
)]
fn draw_line_segment(
    img: &mut image::RgbImage,
    x1: f32,
    y1: f32,
    x2: f32,
    y2: f32,
    color: Rgb<u8>,
    thickness: i32,
) {
    #[allow(
        clippy::cast_sign_loss,
        clippy::cast_possible_truncation,
        clippy::cast_possible_wrap
    )]
    let (width, height) = img.dimensions();

    // Bresenham's line algorithm with thickness
    let dx = (x2 - x1).abs();
    let dy = (y2 - y1).abs();
    let sx = if x1 < x2 { 1.0 } else { -1.0 };
    let sy = if y1 < y2 { 1.0 } else { -1.0 };
    let mut err = dx - dy;

    let mut x = x1;
    let mut y = y1;

    loop {
        // Draw a thick point
        let half_t = thickness / 2;
        for tx in -half_t..=half_t {
            for ty in -half_t..=half_t {
                let px = (x as i32 + tx).max(0).min(width as i32 - 1) as u32;
                let py = (y as i32 + ty).max(0).min(height as i32 - 1) as u32;
                img.put_pixel(px, py, color);
            }
        }

        if (x - x2).abs() < 1.0 && (y - y2).abs() < 1.0 {
            break;
        }

        let e2 = 2.0 * err;
        if e2 > -dy {
            err -= dy;
            x += sx;
        }
        if e2 < dx {
            err += dx;
            y += sy;
        }
    }
}

/// Draw a filled circle on an image
#[allow(
    clippy::cast_sign_loss,
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap
)]
fn draw_filled_circle(img: &mut image::RgbImage, cx: i32, cy: i32, radius: i32, color: Rgb<u8>) {
    let (width, height) = img.dimensions();

    for y in (cy - radius)..=(cy + radius) {
        for x in (cx - radius)..=(cx + radius) {
            let dx = x - cx;
            let dy = y - cy;
            if dx * dx + dy * dy <= radius * radius
                && x >= 0
                && y >= 0
                && x < width as i32
                && y < height as i32
            {
                img.put_pixel(x as u32, y as u32, color);
            }
        }
    }
}

/// Draw object detection results (boxes and masks)
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::manual_clamp,
    clippy::too_many_lines
)]
fn draw_detection(img: &mut image::RgbImage, result: &Results, font: Option<&FontRef>) {
    let (width, height) = img.dimensions();

    // Calculate dynamic scale factor based on image size (reference 640x640)
    let max_dim = width.max(height) as f32;
    let scale_factor = (max_dim / 640.0).max(1.0);

    // Scale thickness and font size
    let thickness = (1.0 * scale_factor).round().max(1.0) as i32;
    let font_scale = (11.0 * scale_factor).max(10.0); // Min font size 10

    if let Some(ref boxes) = result.boxes {
        let xyxy = boxes.xyxy();
        let conf = boxes.conf();
        let cls = boxes.cls();

        // Create an overlay image for masks to handle overlaps correctly
        let mut overlay = img.clone();
        let mut mask_present = false;

        // Draw masks onto the overlay
        if let Some(ref masks) = result.masks {
            let mask_n = masks.data.dim().0;

            for i in 0..boxes.len() {
                if i >= mask_n {
                    break;
                }

                let class_id = cls[i] as usize;
                let color = get_class_color(class_id);
                let (r, g, b) = (color.0[0], color.0[1], color.0[2]);

                mask_present = true;

                let x1 = xyxy[[i, 0]].max(0.0).min(width as f32) as u32;
                let y1 = xyxy[[i, 1]].max(0.0).min(height as f32) as u32;
                let x2 = xyxy[[i, 2]].max(0.0).min(width as f32) as u32;
                let y2 = xyxy[[i, 3]].max(0.0).min(height as f32) as u32;

                for y in y1..y2 {
                    for x in x1..x2 {
                        if masks.data[[i, y as usize, x as usize]] > 0.5 {
                            let pixel = overlay.get_pixel_mut(x, y);
                            pixel.0[0] = r;
                            pixel.0[1] = g;
                            pixel.0[2] = b;
                        }
                    }
                }
            }
        }

        // Blend overlay with original image
        if mask_present {
            let alpha = 0.3;
            for y in 0..height {
                for x in 0..width {
                    let p_img = img.get_pixel_mut(x, y);
                    let p_overlay = overlay.get_pixel(x, y);

                    p_img.0[0] = f32::from(p_overlay.0[0])
                        .mul_add(alpha, f32::from(p_img.0[0]) * (1.0 - alpha))
                        as u8;
                    p_img.0[1] = f32::from(p_overlay.0[1])
                        .mul_add(alpha, f32::from(p_img.0[1]) * (1.0 - alpha))
                        as u8;
                    p_img.0[2] = f32::from(p_overlay.0[2])
                        .mul_add(alpha, f32::from(p_img.0[2]) * (1.0 - alpha))
                        as u8;
                }
            }
        }

        // Keep track of occupied label areas to avoid overlap
        let mut labels_rects: Vec<Rect> = Vec::new();

        // Draw boxes and labels
        for i in 0..boxes.len() {
            let class_id = cls[i] as usize;
            let confidence = conf[i];

            let mut x1 = xyxy[[i, 0]].round() as i32;
            let mut y1 = xyxy[[i, 1]].round() as i32;
            let mut x2 = xyxy[[i, 2]].round() as i32;
            let mut y2 = xyxy[[i, 3]].round() as i32;

            if x1 > x2 {
                std::mem::swap(&mut x1, &mut x2);
            }
            if y1 > y2 {
                std::mem::swap(&mut y1, &mut y2);
            }

            x1 = x1.max(0).min(width as i32 - 1);
            y1 = y1.max(0).min(height as i32 - 1);
            x2 = x2.max(0).min(width as i32 - 1);
            y2 = y2.max(0).min(height as i32 - 1);

            if x2 <= x1 || y2 <= y1 {
                continue;
            }

            let color = get_class_color(class_id);

            // Draw box
            for t in 0..thickness {
                let tx1 = (x1 + t).min(x2);
                let ty1 = (y1 + t).min(y2);
                let tx2 = (x2 - t).max(tx1);
                let ty2 = (y2 - t).max(ty1);
                if tx2 > tx1 && ty2 > ty1 {
                    let rect = Rect::at(tx1, ty1).of_size((tx2 - tx1) as u32, (ty2 - ty1) as u32);
                    draw_hollow_rect_mut(img, rect, color);
                }
            }

            // Draw label
            let class_name = result.names.get(&class_id).map_or("object", String::as_str);
            let label = format!(" {class_name} {confidence:.2} ");

            if let Some(ref f) = font {
                let scale = PxScale::from(font_scale);
                let scaled_font = f.as_scaled(scale);
                let mut text_w = 0.0;
                for c in label.chars() {
                    text_w += scaled_font.h_advance(scaled_font.glyph_id(c));
                }
                let text_w = text_w.ceil() as i32;
                let text_h = scale.y.ceil() as i32;

                // Smart label placement
                // Default: above the box
                let mut text_x = x1;
                let mut text_y = y1 - text_h;

                // If label is out of image (top), move inside
                if text_y < 0 {
                    text_y = y1;
                }

                // If label is out of image (left), move right
                if text_x < 0 {
                    text_x = 0;
                }

                // If label is out of image (right), move left
                if text_x + text_w >= width as i32 {
                    text_x = width as i32 - text_w - 1;
                }

                // Check bounds one last time (bottom)
                if text_y + text_h >= height as i32 {
                    text_y = height as i32 - text_h - 1;
                }

                // Overlap avoidance
                // Check against existing labels. If overlap, move down.
                let mut attempts = 0;
                let max_attempts = 10;
                let mut current_rect =
                    Rect::at(text_x, text_y).of_size(text_w as u32, text_h as u32);

                'placement: while attempts < max_attempts {
                    if !labels_rects
                        .iter()
                        .any(|existing| rect_intersect(&current_rect, existing))
                    {
                        break 'placement;
                    }

                    // Move down
                    text_y += text_h; // stack below

                    // Check bounds again
                    if text_y + text_h >= height as i32 {
                        // Reached bottom, maybe try moving right?
                        // For now just stop here or loop around?
                        // Let's try moving x a bit and resetting y
                        text_y = y1 - text_h;
                        if text_y < 0 {
                            text_y = y1;
                        }
                        text_x += 10; // Shift right slightly
                        if text_x + text_w >= width as i32 {
                            // Screen full, just give up and draw wherever
                            break 'placement;
                        }
                    }

                    current_rect = Rect::at(text_x, text_y).of_size(text_w as u32, text_h as u32);
                    attempts += 1;
                }

                // Add to occupied list
                labels_rects.push(current_rect);

                if text_x >= 0
                    && text_y >= 0
                    && text_x + text_w < width as i32
                    && text_y + text_h < height as i32
                {
                    draw_filled_rect_mut(img, current_rect, color);
                    let text_color = get_text_color(color);
                    draw_text_mut(img, text_color, text_x, text_y, scale, f, &label);
                }
            }
        }
    }
}

/// Calculate appropriate text color (black or white) based on background luminance
fn get_text_color(bg: Rgb<u8>) -> Rgb<u8> {
    let r = f32::from(bg.0[0]);
    let g = f32::from(bg.0[1]);
    let b = f32::from(bg.0[2]);

    // Calculate luminance (standard formula)
    let luminance = 0.114_f32.mul_add(b, 0.299_f32.mul_add(r, 0.587 * g));

    // Threshold usually around 128-186. Using 150 for better safety on mid-tones.
    if luminance > 150.0 {
        Rgb([0, 0, 0]) // Black text for light backgrounds
    } else {
        Rgb([255, 255, 255]) // White text for dark backgrounds
    }
}
fn rect_intersect(r1: &Rect, r2: &Rect) -> bool {
    let r1_left = r1.left();
    let r1_right = r1.right();
    let r1_top = r1.top();
    let r1_bottom = r1.bottom();

    let r2_left = r2.left();
    let r2_right = r2.right();
    let r2_top = r2.top();
    let r2_bottom = r2.bottom();

    !(r2_left >= r1_right || r2_right <= r1_left || r2_top >= r1_bottom || r2_bottom <= r1_top)
}

/// Draw pose estimation results (skeleton and keypoints)
///
/// # Arguments
///
/// * `img` - The image to draw on
/// * `result` - The inference results containing keypoints
/// * `skeleton` - Optional custom skeleton structure (pairs of keypoint indices).
///                If `None`, uses the default human pose skeleton from `SKELETON`.
/// * `limb_colors` - Optional custom color indices for limbs.
///                   If `None`, uses the default from `LIMB_COLOR_INDICES`.
/// * `kpt_colors` - Optional custom color indices for keypoints.
///                  If `None`, uses the default from `KPT_COLOR_INDICES`.
///
/// # Examples
///
/// ```ignore
/// // Use default human pose configuration
/// draw_pose(&mut img, result, None, None, None);
///
/// // Use custom skeleton for animals
/// const ANIMAL_SKELETON: [[usize; 2]; 10] = [...];
/// const ANIMAL_LIMB_COLORS: [usize; 10] = [0, 0, 9, 9, ...];
/// const ANIMAL_KPT_COLORS: [usize; 15] = [16, 16, 0, 0, ...];
/// draw_pose(
///     &mut img,
///     result,
///     Some(&ANIMAL_SKELETON),
///     Some(&ANIMAL_LIMB_COLORS),
///     Some(&ANIMAL_KPT_COLORS),
/// );
/// ```
#[allow(
    clippy::doc_overindented_list_items,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap
)]
fn draw_pose(
    img: &mut image::RgbImage,
    result: &Results,
    skeleton: Option<&[[usize; 2]]>,
    limb_colors: Option<&[usize]>,
    kpt_colors: Option<&[usize]>,
) {
    let (width, height) = img.dimensions();

    // Calculate dynamic scale factor based on image size (reference 640x640)
    let max_dim = width.max(height) as f32;
    let scale_factor = (max_dim / 640.0).max(1.0);

    // Scale thickness and radius
    let thickness = (1.0 * scale_factor).round().max(1.0) as i32;
    let radius = (3.0 * scale_factor).round() as i32;

    if let Some(ref keypoints) = result.keypoints {
        // Use provided parameters or defaults
        let skeleton = skeleton.unwrap_or(&SKELETON);
        let limb_colors = limb_colors.unwrap_or(&LIMB_COLOR_INDICES);
        let kpt_colors = kpt_colors.unwrap_or(&KPT_COLOR_INDICES);

        let kpt_data = &keypoints.data;
        let n_persons = kpt_data.shape()[0];
        let n_kpts = kpt_data.shape()[1];

        for person_idx in 0..n_persons {
            for (limb_idx, &[kpt_a, kpt_b]) in skeleton.iter().enumerate() {
                if kpt_a >= n_kpts || kpt_b >= n_kpts {
                    continue;
                }

                let x1 = kpt_data[[person_idx, kpt_a, 0]];
                let y1 = kpt_data[[person_idx, kpt_a, 1]];
                let conf1 = kpt_data[[person_idx, kpt_a, 2]];
                let x2 = kpt_data[[person_idx, kpt_b, 0]];
                let y2 = kpt_data[[person_idx, kpt_b, 1]];
                let conf2 = kpt_data[[person_idx, kpt_b, 2]];

                if conf1 > 0.5 && conf2 > 0.5 {
                    let color_idx = limb_colors[limb_idx % limb_colors.len()];
                    let color = Rgb(POSE_COLORS[color_idx]);
                    draw_line_segment(img, x1, y1, x2, y2, color, thickness);
                }
            }

            for kpt_idx in 0..n_kpts {
                let x = kpt_data[[person_idx, kpt_idx, 0]];
                let y = kpt_data[[person_idx, kpt_idx, 1]];
                let conf = kpt_data[[person_idx, kpt_idx, 2]];

                if conf > 0.5 && x >= 0.0 && y >= 0.0 && x < width as f32 && y < height as f32 {
                    let color_idx = kpt_colors[kpt_idx % kpt_colors.len()];
                    let color = Rgb(POSE_COLORS[color_idx]);
                    draw_filled_circle(img, x as i32, y as i32, radius, color);
                }
            }
        }
    }
}

/// Draw oriented bounding boxes (OBB)
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::manual_clamp
)]
fn draw_obb(img: &mut image::RgbImage, result: &Results, font: Option<&FontRef>) {
    let (width, height) = img.dimensions();

    // Calculate dynamic scale factor based on image size (reference 640x640)
    let max_dim = width.max(height) as f32;
    let scale_factor = (max_dim / 640.0).max(1.0);

    // Scale thickness and font size
    let thickness = (1.0 * scale_factor).round().max(1.0) as i32;
    let font_scale = (11.0 * scale_factor).max(10.0); // Min font size 10

    if let Some(ref obb) = result.obb {
        let corners = obb.xyxyxyxy();
        let conf = obb.conf();
        let cls = obb.cls();

        // Keep track of occupied label areas to avoid overlap
        let mut labels_rects: Vec<Rect> = Vec::new();

        for i in 0..obb.len() {
            let class_id = cls[i] as usize;
            let color = get_class_color(class_id);

            for j in 0..4 {
                let next_j = (j + 1) % 4;
                let x1 = corners[[i, j, 0]];
                let y1 = corners[[i, j, 1]];
                let x2 = corners[[i, next_j, 0]];
                let y2 = corners[[i, next_j, 1]];
                draw_line_segment(img, x1, y1, x2, y2, color, thickness);
            }

            let class_name = result.names.get(&class_id).map_or("object", String::as_str);
            let label = format!(" {} {:.2} ", class_name, conf[i]);

            if let Some(ref f) = font {
                let scale = PxScale::from(font_scale);
                let scaled_font = f.as_scaled(scale);
                let mut text_w = 0.0;
                for c in label.chars() {
                    text_w += scaled_font.h_advance(scaled_font.glyph_id(c));
                }
                let text_w = text_w.ceil() as i32;
                let text_h = scale.y.ceil() as i32;

                // Smart label placement
                // Default: at the first corner (usually top-left-ish)
                let mut text_x = corners[[i, 0, 0]] as i32;
                let mut text_y = (corners[[i, 0, 1]] as i32 - text_h).max(0);

                // If label is out of image (left), move right
                if text_x < 0 {
                    text_x = 0;
                }

                // If label is out of image (right), move left
                if text_x + text_w >= width as i32 {
                    text_x = width as i32 - text_w - 1;
                }

                // Check bounds one last time (bottom)
                if text_y + text_h >= height as i32 {
                    text_y = height as i32 - text_h - 1;
                }

                // Overlap avoidance
                // Check against existing labels. If overlap, move down.
                let mut attempts = 0;
                let max_attempts = 10;
                let mut current_rect =
                    Rect::at(text_x, text_y).of_size(text_w as u32, text_h as u32);

                'placement: while attempts < max_attempts {
                    if !labels_rects
                        .iter()
                        .any(|existing| rect_intersect(&current_rect, existing))
                    {
                        break 'placement;
                    }

                    // Move down
                    text_y += text_h; // stack below

                    // Check bounds again
                    if text_y + text_h >= height as i32 {
                        // Reached bottom, try resetting y and moving x
                        text_y = (corners[[i, 0, 1]] as i32 - text_h).max(0);
                        text_x += 10; // Shift right
                        if text_x + text_w >= width as i32 {
                            break 'placement;
                        }
                    }

                    current_rect = Rect::at(text_x, text_y).of_size(text_w as u32, text_h as u32);
                    attempts += 1;
                }

                // Add to occupied list
                labels_rects.push(current_rect);

                if text_x >= 0
                    && text_y >= 0
                    && text_x + text_w < width as i32
                    && text_y + text_h < height as i32
                {
                    draw_filled_rect_mut(img, current_rect, color);
                    let text_color = get_text_color(color);
                    draw_text_mut(img, text_color, text_x, text_y, scale, f, &label);
                }
            }
        }
    }
}
/// Draw a transparent rectangle on an image
#[allow(
    clippy::many_single_char_names,
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::manual_clamp
)]
fn draw_transparent_rect(
    img: &mut image::RgbImage,
    x: i32,
    y: i32,
    w: u32,
    h: u32,
    color: Rgb<u8>,
    alpha: f32,
) {
    let (width, height) = img.dimensions();
    let alpha = alpha.max(0.0).min(1.0);
    let inv_alpha = 1.0 - alpha;

    let r = f32::from(color[0]);
    let g = f32::from(color[1]);
    let b = f32::from(color[2]);

    for dy in 0..h {
        let py = y + dy as i32;
        if py < 0 || py >= height as i32 {
            continue;
        }

        for dx in 0..w {
            let px = x + dx as i32;
            if px < 0 || px >= width as i32 {
                continue;
            }

            let pixel = img.get_pixel_mut(px as u32, py as u32);
            let current = pixel.0;

            let new_r = f32::from(current[0]).mul_add(inv_alpha, r * alpha) as u8;
            let new_g = f32::from(current[1]).mul_add(inv_alpha, g * alpha) as u8;
            let new_b = f32::from(current[2]).mul_add(inv_alpha, b * alpha) as u8;

            *pixel = Rgb([new_r, new_g, new_b]);
        }
    }
}

/// Draw classification results
#[allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    clippy::cast_possible_wrap,
    clippy::manual_clamp
)]
fn draw_classification(
    img: &mut image::RgbImage,
    result: &Results,
    font: Option<&FontRef>,
    top_k: usize,
) {
    if let Some(ref probs) = result.probs {
        let top_indices = probs.top_k(top_k);
        let (width, _height) = img.dimensions();

        if let Some(ref f) = font {
            // Adaptive font scale based on image width
            let scale_factor = (width as f32 / 600.0).max(0.6).min(2.0);
            let base_size = 30.0;
            let scale = PxScale::from(base_size * scale_factor);
            let line_height = (scale.y * 1.2) as i32;

            let x_pos = (20.0 * scale_factor) as i32;
            let mut y_pos = (20.0 * scale_factor) as i32;

            // Calculate max text width for background box
            let mut max_width = 0;
            let mut entries = Vec::new();

            for &class_id in &top_indices {
                let score = probs.data[class_id];
                if score < 0.01 {
                    continue;
                }

                let class_name = result.names.get(&class_id).map_or("class", String::as_str);

                let label = format!("{class_name} {score:.2}");
                entries.push(label);
            }

            // Basic approximation: chars * scale * 0.5 (average char width)
            for label in &entries {
                let w = (label.len() as f32 * scale.x * 0.5) as u32;
                if w > max_width {
                    max_width = w;
                }
            }

            // Draw background for all entries with padding
            let box_height = (entries.len() as i32 * line_height) + 10;
            let box_width = max_width + 20;

            if !entries.is_empty() {
                draw_transparent_rect(
                    img,
                    x_pos - 5,
                    y_pos - 5,
                    box_width,
                    box_height as u32,
                    Rgb([0, 0, 0]),
                    0.4, // 40% opacity black tint
                );
            }

            for label in entries {
                // Draw text (white)
                draw_text_mut(img, Rgb([255, 255, 255]), x_pos, y_pos, scale, f, &label);

                y_pos += line_height;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::results::Speed;
    use ndarray::Array3;
    use std::collections::HashMap;

    #[test]
    fn test_get_class_color() {
        let c = get_class_color(0);
        assert_eq!(c, Rgb([4, 42, 255]));
    }

    #[test]
    fn test_draw_line_segment() {
        let mut img = image::RgbImage::new(100, 100);
        let color = Rgb([255, 0, 0]);
        // Draw horizontal line
        draw_line_segment(&mut img, 10.0, 10.0, 50.0, 10.0, color, 1);

        // Check a pixel in the middle
        assert_eq!(*img.get_pixel(30, 10), color);
        // Check pixel outside
        assert_eq!(*img.get_pixel(30, 20), Rgb([0, 0, 0]));
    }

    #[test]
    fn test_annotate_image_empty() {
        let img = DynamicImage::new_rgb8(100, 100);

        let orig_img = Array3::<u8>::zeros((100, 100, 3));
        let path = "test.jpg".to_string();
        let names = HashMap::new();
        let speed = Speed::new(0.0, 0.0, 0.0);

        // Correct constructor matching src/results.rs:101
        let results = Results::new(orig_img, path, names, speed, (640, 640));

        let annotated = annotate_image(&img, &results, None);
        // Should return same dimensions
        assert_eq!(annotated.width(), 100);
        assert_eq!(annotated.height(), 100);
    }
}