panlabel 0.6.0

The universal annotation converter
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
//! Label Studio JSON reader and writer.
//!
//! This adapter supports Label Studio task-export JSON (array of tasks) for
//! rectanglelabels object-detection bounding boxes.

use std::collections::{BTreeMap, BTreeSet};
use std::f64::consts::PI;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::path::Path;

use serde::{Deserialize, Serialize};

use super::model::{Annotation, Category, Dataset, DatasetInfo, Image};
use super::{AnnotationId, BBoxXYXY, CategoryId, ImageId, Pixel};
use crate::error::PanlabelError;

// ============================================================================
// Label Studio schema types (internal)
// ============================================================================

#[derive(Debug, Deserialize)]
struct LsTask {
    #[serde(default)]
    data: LsTaskData,
    #[serde(default)]
    annotations: Option<Vec<LsResultSet>>,
    #[serde(default)]
    completions: Option<Vec<LsResultSet>>,
    #[serde(default)]
    predictions: Option<Vec<LsResultSet>>,
}

#[derive(Debug, Default, Deserialize)]
struct LsTaskData {
    #[serde(default)]
    image: Option<String>,
    #[serde(default)]
    width: Option<u32>,
    #[serde(default)]
    height: Option<u32>,
}

#[derive(Debug, Default, Deserialize)]
struct LsResultSet {
    #[serde(default)]
    result: Vec<LsResult>,
}

#[derive(Debug, Deserialize)]
struct LsResult {
    #[serde(rename = "type")]
    result_type: String,
    #[serde(default)]
    value: Option<serde_json::Value>,
    #[serde(default)]
    original_width: Option<u32>,
    #[serde(default)]
    original_height: Option<u32>,
    #[serde(default)]
    rotation: Option<f64>,
    #[serde(default)]
    from_name: Option<String>,
    #[serde(default)]
    to_name: Option<String>,
    #[serde(default)]
    score: Option<f64>,
}

#[derive(Debug, Deserialize)]
struct LsRectangleValue {
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    #[serde(default)]
    rectanglelabels: Vec<String>,
}

#[derive(Debug, Serialize)]
struct LsTaskOut {
    id: u64,
    data: LsTaskDataOut,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    annotations: Vec<LsResultSetOut>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    predictions: Vec<LsResultSetOut>,
}

#[derive(Debug, Serialize)]
struct LsTaskDataOut {
    image: String,
    width: u32,
    height: u32,
}

#[derive(Debug, Serialize)]
struct LsResultSetOut {
    result: Vec<LsResultOut>,
}

#[derive(Debug, Serialize)]
struct LsResultOut {
    #[serde(rename = "type")]
    result_type: &'static str,
    value: LsRectangleValueOut,
    original_width: u32,
    original_height: u32,
    from_name: String,
    to_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    rotation: Option<f64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    score: Option<f64>,
}

#[derive(Debug, Serialize)]
struct LsRectangleValueOut {
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    rectanglelabels: Vec<String>,
}

#[derive(Debug)]
struct ParsedTask {
    file_name: String,
    image_ref: String,
    width: u32,
    height: u32,
    from_name: Option<String>,
    to_name: Option<String>,
    rows: Vec<ParsedAnnotation>,
}

#[derive(Debug)]
struct ParsedAnnotation {
    label: String,
    bbox: BBoxXYXY<Pixel>,
    confidence: Option<f64>,
    attributes: BTreeMap<String, String>,
}

// ============================================================================
// Public API
// ============================================================================

/// Read Label Studio task-export JSON into panlabel IR.
pub fn read_label_studio_json(path: &Path) -> Result<Dataset, PanlabelError> {
    let file = File::open(path).map_err(PanlabelError::Io)?;
    let reader = BufReader::new(file);

    let tasks: Vec<LsTask> =
        serde_json::from_reader(reader).map_err(|source| PanlabelError::LabelStudioJsonParse {
            path: path.to_path_buf(),
            source,
        })?;

    ls_to_ir(tasks, path)
}

/// Write panlabel IR as Label Studio task-export JSON.
pub fn write_label_studio_json(path: &Path, dataset: &Dataset) -> Result<(), PanlabelError> {
    let tasks = ir_to_ls(dataset, path)?;

    let file = File::create(path).map_err(PanlabelError::Io)?;
    let writer = BufWriter::new(file);

    serde_json::to_writer_pretty(writer, &tasks).map_err(|source| {
        PanlabelError::LabelStudioJsonWrite {
            path: path.to_path_buf(),
            source,
        }
    })
}

/// Parse Label Studio task-export JSON from string.
pub fn from_label_studio_str(json: &str) -> Result<Dataset, PanlabelError> {
    let path = Path::new("<string>");
    let tasks: Vec<LsTask> =
        serde_json::from_str(json).map_err(|source| PanlabelError::LabelStudioJsonParse {
            path: path.to_path_buf(),
            source,
        })?;
    ls_to_ir(tasks, path)
}

/// Parse Label Studio task-export JSON from bytes.
pub fn from_label_studio_slice(bytes: &[u8]) -> Result<Dataset, PanlabelError> {
    let path = Path::new("<bytes>");
    let tasks: Vec<LsTask> =
        serde_json::from_slice(bytes).map_err(|source| PanlabelError::LabelStudioJsonParse {
            path: path.to_path_buf(),
            source,
        })?;
    ls_to_ir(tasks, path)
}

/// Serialize panlabel IR to Label Studio task-export JSON string.
pub fn to_label_studio_string(dataset: &Dataset) -> Result<String, PanlabelError> {
    let path = Path::new("<string>");
    let tasks = ir_to_ls(dataset, path)?;
    serde_json::to_string_pretty(&tasks).map_err(|source| PanlabelError::LabelStudioJsonWrite {
        path: path.to_path_buf(),
        source,
    })
}

// ============================================================================
// Conversion: Label Studio -> IR
// ============================================================================

fn ls_to_ir(tasks: Vec<LsTask>, path: &Path) -> Result<Dataset, PanlabelError> {
    if tasks.is_empty() {
        return Ok(Dataset::default());
    }

    let mut parsed_tasks = Vec::with_capacity(tasks.len());
    let mut seen_file_names = BTreeSet::new();

    for (task_idx, task) in tasks.into_iter().enumerate() {
        let image_ref = task
            .data
            .image
            .ok_or_else(|| invalid(path, format!("task[{task_idx}] missing data.image")))?;

        let file_name = derive_image_file_name(&image_ref).ok_or_else(|| {
            invalid(
                path,
                format!(
                    "task[{task_idx}] data.image '{}' does not contain a valid filename",
                    image_ref
                ),
            )
        })?;

        if !seen_file_names.insert(file_name.clone()) {
            return Err(invalid(
                path,
                format!(
                    "duplicate image basename '{}' derived from data.image; panlabel requires unique basenames",
                    file_name
                ),
            ));
        }

        let annotation_results =
            select_annotation_results(task.annotations, task.completions, path, task_idx)?;

        let prediction_results = select_prediction_results(task.predictions, path, task_idx)?;

        let mut from_names = BTreeSet::new();
        let mut to_names = BTreeSet::new();

        let mut dims: Option<(u32, u32)> = None;
        let mut rows = Vec::new();

        for (result_idx, result) in annotation_results.iter().enumerate() {
            let parsed = parse_result(
                result,
                path,
                task_idx,
                result_idx,
                "annotations",
                &mut dims,
                &mut from_names,
                &mut to_names,
            )?;
            rows.push(parsed);
        }

        for (result_idx, result) in prediction_results.iter().enumerate() {
            let parsed = parse_result(
                result,
                path,
                task_idx,
                result_idx,
                "predictions",
                &mut dims,
                &mut from_names,
                &mut to_names,
            )?;
            rows.push(parsed);
        }

        let (width, height) = if let Some((w, h)) = dims {
            (w, h)
        } else if let (Some(w), Some(h)) = (task.data.width, task.data.height) {
            (w, h)
        } else {
            return Err(invalid(
                path,
                format!(
                    "task[{task_idx}] has no results with original_width/original_height and no data.width/data.height fallback"
                ),
            ));
        };

        if from_names.len() > 1 {
            return Err(invalid(
                path,
                format!("task[{task_idx}] has inconsistent from_name values across results"),
            ));
        }
        if to_names.len() > 1 {
            return Err(invalid(
                path,
                format!("task[{task_idx}] has inconsistent to_name values across results"),
            ));
        }

        let from_name = from_names.into_iter().next();
        let to_name = to_names.into_iter().next();

        parsed_tasks.push(ParsedTask {
            file_name,
            image_ref,
            width,
            height,
            from_name,
            to_name,
            rows,
        });
    }

    parsed_tasks.sort_by(|left, right| left.file_name.cmp(&right.file_name));

    let mut category_names = BTreeSet::new();
    for task in &parsed_tasks {
        for row in &task.rows {
            category_names.insert(row.label.clone());
        }
    }

    let categories: Vec<Category> = category_names
        .into_iter()
        .enumerate()
        .map(|(idx, name)| Category::new((idx + 1) as u64, name))
        .collect();

    let category_id_by_name: BTreeMap<String, CategoryId> = categories
        .iter()
        .map(|category| (category.name.clone(), category.id))
        .collect();

    let mut images = Vec::with_capacity(parsed_tasks.len());
    let mut image_id_by_name = BTreeMap::new();

    for (idx, task) in parsed_tasks.iter().enumerate() {
        let mut image = Image::new(
            (idx + 1) as u64,
            task.file_name.clone(),
            task.width,
            task.height,
        );
        image
            .attributes
            .insert("ls_image_ref".to_string(), task.image_ref.clone());
        if let Some(from_name) = &task.from_name {
            image
                .attributes
                .insert("ls_from_name".to_string(), from_name.clone());
        }
        if let Some(to_name) = &task.to_name {
            image
                .attributes
                .insert("ls_to_name".to_string(), to_name.clone());
        }

        image_id_by_name.insert(task.file_name.clone(), image.id);
        images.push(image);
    }

    let mut annotations = Vec::new();
    let mut next_annotation_id: u64 = 1;

    for task in parsed_tasks {
        let image_id = image_id_by_name
            .get(&task.file_name)
            .copied()
            .ok_or_else(|| {
                invalid(
                    path,
                    format!(
                        "internal error: missing image mapping for '{}'",
                        task.file_name
                    ),
                )
            })?;

        for parsed in task.rows {
            let category_id = category_id_by_name
                .get(&parsed.label)
                .copied()
                .ok_or_else(|| {
                    invalid(
                        path,
                        format!(
                            "internal error: missing category mapping for '{}'",
                            parsed.label
                        ),
                    )
                })?;

            let mut annotation = Annotation::new(
                AnnotationId::new(next_annotation_id),
                image_id,
                category_id,
                parsed.bbox,
            );
            annotation.confidence = parsed.confidence;
            annotation.attributes = parsed.attributes;
            annotations.push(annotation);
            next_annotation_id += 1;
        }
    }

    Ok(Dataset {
        info: DatasetInfo::default(),
        licenses: vec![],
        images,
        categories,
        annotations,
    })
}

fn select_annotation_results(
    annotations: Option<Vec<LsResultSet>>,
    completions: Option<Vec<LsResultSet>>,
    path: &Path,
    task_idx: usize,
) -> Result<Vec<LsResult>, PanlabelError> {
    if annotations.is_some() && completions.is_some() {
        return Err(invalid(
            path,
            format!("task[{task_idx}] has both annotations and completions; expected only one key"),
        ));
    }

    let selected = annotations.or(completions);
    let Some(mut sets) = selected else {
        return Ok(vec![]);
    };

    if sets.len() > 1 {
        return Err(invalid(
            path,
            format!(
                "task[{task_idx}] has {} annotation sets; panlabel currently requires <= 1",
                sets.len()
            ),
        ));
    }

    Ok(sets.pop().map(|set| set.result).unwrap_or_default())
}

fn select_prediction_results(
    predictions: Option<Vec<LsResultSet>>,
    path: &Path,
    task_idx: usize,
) -> Result<Vec<LsResult>, PanlabelError> {
    let Some(mut sets) = predictions else {
        return Ok(vec![]);
    };

    if sets.len() > 1 {
        return Err(invalid(
            path,
            format!(
                "task[{task_idx}] has {} prediction sets; panlabel currently requires <= 1",
                sets.len()
            ),
        ));
    }

    Ok(sets.pop().map(|set| set.result).unwrap_or_default())
}

#[allow(clippy::too_many_arguments)]
fn parse_result(
    result: &LsResult,
    path: &Path,
    task_idx: usize,
    result_idx: usize,
    result_source: &str,
    dims: &mut Option<(u32, u32)>,
    from_names: &mut BTreeSet<String>,
    to_names: &mut BTreeSet<String>,
) -> Result<ParsedAnnotation, PanlabelError> {
    if result.result_type != "rectanglelabels" {
        return Err(invalid(
            path,
            format!(
                "task[{task_idx}] {result_source}[{result_idx}] unsupported result type '{}'; only rectanglelabels is supported",
                result.result_type
            ),
        ));
    }

    let raw_value = result.value.as_ref().ok_or_else(|| {
        invalid(
            path,
            format!("task[{task_idx}] {result_source}[{result_idx}] missing value object"),
        )
    })?;

    let value: LsRectangleValue = serde_json::from_value(raw_value.clone()).map_err(|source| {
        invalid(
            path,
            format!(
                "task[{task_idx}] {result_source}[{result_idx}] has invalid rectanglelabels value: {source}"
            ),
        )
    })?;

    if value.rectanglelabels.len() != 1 {
        return Err(invalid(
            path,
            format!(
                "task[{task_idx}] {result_source}[{result_idx}] rectanglelabels length is {}; expected exactly 1",
                value.rectanglelabels.len()
            ),
        ));
    }

    let original_width = result.original_width.ok_or_else(|| {
        invalid(
            path,
            format!("task[{task_idx}] {result_source}[{result_idx}] missing original_width"),
        )
    })?;

    let original_height = result.original_height.ok_or_else(|| {
        invalid(
            path,
            format!("task[{task_idx}] {result_source}[{result_idx}] missing original_height"),
        )
    })?;

    if let Some((existing_w, existing_h)) = dims {
        if *existing_w != original_width || *existing_h != original_height {
            return Err(invalid(
                path,
                format!(
                    "task[{task_idx}] has inconsistent original dimensions: ({existing_w}, {existing_h}) vs ({original_width}, {original_height})"
                ),
            ));
        }
    } else {
        *dims = Some((original_width, original_height));
    }

    if let Some(from_name) = result.from_name.as_ref().filter(|name| !name.is_empty()) {
        from_names.insert(from_name.clone());
    }
    if let Some(to_name) = result.to_name.as_ref().filter(|name| !name.is_empty()) {
        to_names.insert(to_name.clone());
    }

    let rotation = result.rotation.unwrap_or(0.0);
    let bbox = percent_bbox_to_pixel(
        value.x,
        value.y,
        value.width,
        value.height,
        original_width,
        original_height,
        rotation,
    );

    let mut attributes = BTreeMap::new();
    if rotation != 0.0 {
        attributes.insert("ls_rotation_deg".to_string(), rotation.to_string());
    }

    Ok(ParsedAnnotation {
        label: value.rectanglelabels[0].clone(),
        bbox,
        confidence: result.score,
        attributes,
    })
}

// ============================================================================
// Conversion: IR -> Label Studio
// ============================================================================

fn ir_to_ls(dataset: &Dataset, path: &Path) -> Result<Vec<LsTaskOut>, PanlabelError> {
    let image_by_id: BTreeMap<ImageId, &Image> = dataset
        .images
        .iter()
        .map(|image| (image.id, image))
        .collect();
    let category_name_by_id: BTreeMap<CategoryId, String> = dataset
        .categories
        .iter()
        .map(|category| (category.id, category.name.clone()))
        .collect();

    let mut annotations_by_image: BTreeMap<ImageId, Vec<&Annotation>> = BTreeMap::new();
    for annotation in &dataset.annotations {
        if !image_by_id.contains_key(&annotation.image_id) {
            return Err(invalid(
                path,
                format!(
                    "annotation {} references missing image {}",
                    annotation.id.as_u64(),
                    annotation.image_id.as_u64()
                ),
            ));
        }

        if !category_name_by_id.contains_key(&annotation.category_id) {
            return Err(invalid(
                path,
                format!(
                    "annotation {} references missing category {}",
                    annotation.id.as_u64(),
                    annotation.category_id.as_u64()
                ),
            ));
        }

        annotations_by_image
            .entry(annotation.image_id)
            .or_default()
            .push(annotation);
    }

    for annotations in annotations_by_image.values_mut() {
        annotations.sort_by_key(|annotation| annotation.id);
    }

    let mut image_ref_by_id: BTreeMap<ImageId, String> = BTreeMap::new();
    let mut seen_basenames = BTreeSet::new();
    for image in &dataset.images {
        let image_ref = image
            .attributes
            .get("ls_image_ref")
            .cloned()
            .unwrap_or_else(|| image.file_name.clone());

        let basename = derive_image_file_name(&image_ref).ok_or_else(|| {
            invalid(
                path,
                format!(
                    "image '{}' maps to invalid Label Studio image reference '{}'",
                    image.file_name, image_ref
                ),
            )
        })?;

        if !seen_basenames.insert(basename.clone()) {
            return Err(invalid(
                path,
                format!(
                    "multiple images map to basename '{}' for Label Studio output; unique basenames are required",
                    basename
                ),
            ));
        }

        image_ref_by_id.insert(image.id, image_ref);
    }

    let mut images_sorted: Vec<&Image> = dataset.images.iter().collect();
    images_sorted.sort_by(|left, right| left.file_name.cmp(&right.file_name));

    let mut tasks = Vec::with_capacity(images_sorted.len());

    for (idx, image) in images_sorted.into_iter().enumerate() {
        let image_ref = image_ref_by_id.get(&image.id).cloned().ok_or_else(|| {
            invalid(
                path,
                format!(
                    "internal error: missing image reference mapping for image {}",
                    image.id.as_u64()
                ),
            )
        })?;

        let from_name = image
            .attributes
            .get("ls_from_name")
            .cloned()
            .unwrap_or_else(|| "label".to_string());

        let to_name = image
            .attributes
            .get("ls_to_name")
            .cloned()
            .unwrap_or_else(|| "image".to_string());

        let image_annotations = annotations_by_image.remove(&image.id).unwrap_or_default();
        let mut annotation_results = Vec::new();
        let mut prediction_results = Vec::new();

        for annotation in image_annotations {
            let category_name = category_name_by_id
                .get(&annotation.category_id)
                .ok_or_else(|| {
                    invalid(
                        path,
                        format!(
                            "internal error: missing category {} while writing",
                            annotation.category_id.as_u64()
                        ),
                    )
                })?
                .clone();

            let rotation = annotation
                .attributes
                .get("ls_rotation_deg")
                .and_then(|value| value.parse::<f64>().ok());

            let (x, y, width, height) =
                pixel_bbox_to_percent(&annotation.bbox, image.width, image.height).ok_or_else(
                    || {
                        invalid(
                            path,
                            format!(
                        "image '{}' has zero width/height; cannot convert bbox {} to percentages",
                        image.file_name,
                        annotation.id.as_u64()
                    ),
                        )
                    },
                )?;

            let result = LsResultOut {
                result_type: "rectanglelabels",
                value: LsRectangleValueOut {
                    x,
                    y,
                    width,
                    height,
                    rectanglelabels: vec![category_name],
                },
                original_width: image.width,
                original_height: image.height,
                from_name: from_name.clone(),
                to_name: to_name.clone(),
                rotation,
                score: annotation.confidence,
            };

            if annotation.confidence.is_some() {
                prediction_results.push(result);
            } else {
                annotation_results.push(result);
            }
        }

        let task = LsTaskOut {
            id: (idx + 1) as u64,
            data: LsTaskDataOut {
                image: image_ref,
                width: image.width,
                height: image.height,
            },
            annotations: if annotation_results.is_empty() {
                vec![]
            } else {
                vec![LsResultSetOut {
                    result: annotation_results,
                }]
            },
            predictions: if prediction_results.is_empty() {
                vec![]
            } else {
                vec![LsResultSetOut {
                    result: prediction_results,
                }]
            },
        };

        tasks.push(task);
    }

    Ok(tasks)
}

// ============================================================================
// Helpers
// ============================================================================

fn invalid(path: &Path, message: impl Into<String>) -> PanlabelError {
    PanlabelError::LabelStudioJsonInvalid {
        path: path.to_path_buf(),
        message: message.into(),
    }
}

fn derive_image_file_name(image_ref: &str) -> Option<String> {
    let no_query = image_ref.split('?').next().unwrap_or(image_ref);
    let no_fragment = no_query.split('#').next().unwrap_or(no_query);
    let normalized = no_fragment.replace('\\', "/");
    let candidate = normalized.rsplit('/').next()?;
    if candidate.is_empty() {
        return None;
    }
    Some(candidate.to_string())
}

fn percent_bbox_to_pixel(
    x: f64,
    y: f64,
    width: f64,
    height: f64,
    image_width: u32,
    image_height: u32,
    rotation_deg: f64,
) -> BBoxXYXY<Pixel> {
    let w = image_width as f64;
    let h = image_height as f64;

    let xmin = (x / 100.0) * w;
    let ymin = (y / 100.0) * h;
    let xmax = ((x + width) / 100.0) * w;
    let ymax = ((y + height) / 100.0) * h;

    if rotation_deg == 0.0 {
        return BBoxXYXY::from_xyxy(xmin, ymin, xmax, ymax);
    }

    rotated_envelope_bbox(xmin, ymin, xmax, ymax, rotation_deg)
}

fn rotated_envelope_bbox(
    xmin: f64,
    ymin: f64,
    xmax: f64,
    ymax: f64,
    rotation_deg: f64,
) -> BBoxXYXY<Pixel> {
    let theta = rotation_deg * (PI / 180.0);
    let cos_t = theta.cos();
    let sin_t = theta.sin();

    let cx = (xmin + xmax) / 2.0;
    let cy = (ymin + ymax) / 2.0;

    let corners = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)];

    let mut min_x = f64::INFINITY;
    let mut min_y = f64::INFINITY;
    let mut max_x = f64::NEG_INFINITY;
    let mut max_y = f64::NEG_INFINITY;

    for (x, y) in corners {
        let dx = x - cx;
        let dy = y - cy;
        let rx = cx + (dx * cos_t) - (dy * sin_t);
        let ry = cy + (dx * sin_t) + (dy * cos_t);

        min_x = min_x.min(rx);
        min_y = min_y.min(ry);
        max_x = max_x.max(rx);
        max_y = max_y.max(ry);
    }

    BBoxXYXY::from_xyxy(min_x, min_y, max_x, max_y)
}

fn pixel_bbox_to_percent(
    bbox: &BBoxXYXY<Pixel>,
    image_width: u32,
    image_height: u32,
) -> Option<(f64, f64, f64, f64)> {
    if image_width == 0 || image_height == 0 {
        return None;
    }

    let w = image_width as f64;
    let h = image_height as f64;

    let x = (bbox.xmin() / w) * 100.0;
    let y = (bbox.ymin() / h) * 100.0;
    let width = ((bbox.xmax() - bbox.xmin()) / w) * 100.0;
    let height = ((bbox.ymax() - bbox.ymin()) / h) * 100.0;

    Some((x, y, width, height))
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    fn sample_json() -> &'static str {
        r#"[
  {
    "data": {"image": "https://example.com/assets/img_b.jpg"},
    "annotations": [
      {
        "result": [
          {
            "type": "rectanglelabels",
            "from_name": "bbox",
            "to_name": "image",
            "value": {
              "x": 10.0,
              "y": 10.0,
              "width": 40.0,
              "height": 50.0,
              "rectanglelabels": ["dog"]
            },
            "original_width": 200,
            "original_height": 100
          }
        ]
      }
    ],
    "predictions": [
      {
        "result": [
          {
            "type": "rectanglelabels",
            "from_name": "bbox",
            "to_name": "image",
            "value": {
              "x": 50.0,
              "y": 20.0,
              "width": 10.0,
              "height": 20.0,
              "rectanglelabels": ["cat"]
            },
            "original_width": 200,
            "original_height": 100,
            "score": 0.9
          }
        ]
      }
    ]
  },
  {
    "data": {"image": "img_a.jpg"},
    "annotations": [
      {
        "result": [
          {
            "type": "rectanglelabels",
            "from_name": "bbox",
            "to_name": "image",
            "value": {
              "x": 0.0,
              "y": 0.0,
              "width": 20.0,
              "height": 50.0,
              "rectanglelabels": ["cat"]
            },
            "original_width": 100,
            "original_height": 100
          }
        ]
      }
    ]
  }
]"#
    }

    #[test]
    fn parse_assigns_deterministic_ids() {
        let dataset = from_label_studio_str(sample_json()).expect("parse dataset");

        assert_eq!(dataset.images.len(), 2);
        assert_eq!(dataset.categories.len(), 2);
        assert_eq!(dataset.annotations.len(), 3);

        // Images sorted by derived basename.
        assert_eq!(dataset.images[0].file_name, "img_a.jpg");
        assert_eq!(dataset.images[0].id.as_u64(), 1);
        assert_eq!(dataset.images[1].file_name, "img_b.jpg");
        assert_eq!(dataset.images[1].id.as_u64(), 2);

        // Categories sorted lexicographically.
        assert_eq!(dataset.categories[0].name, "cat");
        assert_eq!(dataset.categories[0].id.as_u64(), 1);
        assert_eq!(dataset.categories[1].name, "dog");
        assert_eq!(dataset.categories[1].id.as_u64(), 2);

        // prediction score -> confidence
        assert!(dataset
            .annotations
            .iter()
            .any(|ann| ann.confidence == Some(0.9)));

        assert_eq!(
            dataset.images[1].attributes.get("ls_image_ref"),
            Some(&"https://example.com/assets/img_b.jpg".to_string())
        );
    }

    #[test]
    fn parse_rotation_sets_attribute() {
        let json = r#"[
  {
    "data": {"image": "img_rot.jpg"},
    "annotations": [
      {
        "result": [
          {
            "type": "rectanglelabels",
            "value": {
              "x": 10.0,
              "y": 20.0,
              "width": 30.0,
              "height": 40.0,
              "rectanglelabels": ["box"]
            },
            "rotation": 35.0,
            "original_width": 100,
            "original_height": 200
          }
        ]
      }
    ]
  }
]"#;

        let dataset = from_label_studio_str(json).expect("parse rotated");
        let ann = &dataset.annotations[0];
        assert_eq!(
            ann.attributes.get("ls_rotation_deg"),
            Some(&"35".to_string())
        );
    }

    #[test]
    fn parse_rejects_multiple_annotation_sets() {
        let json = r#"[
  {
    "data": {"image": "img.jpg"},
    "annotations": [{"result": []}, {"result": []}]
  }
]"#;

        let err = from_label_studio_str(json).expect_err("expected invalid error");
        match err {
            PanlabelError::LabelStudioJsonInvalid { message, .. } => {
                assert!(message.contains("annotation sets"));
            }
            other => panic!("expected LabelStudioJsonInvalid, got {other:?}"),
        }
    }

    #[test]
    fn parse_rejects_unsupported_result_type() {
        let json = r#"[
  {
    "data": {"image": "img.jpg"},
    "annotations": [
      {
        "result": [
          {
            "type": "choices",
            "value": {"choices": ["yes"]},
            "original_width": 10,
            "original_height": 10
          }
        ]
      }
    ]
  }
]"#;

        let err = from_label_studio_str(json).expect_err("expected invalid error");
        match err {
            PanlabelError::LabelStudioJsonInvalid { message, .. } => {
                assert!(message.contains("unsupported result type"));
            }
            other => panic!("expected LabelStudioJsonInvalid, got {other:?}"),
        }
    }

    #[test]
    fn parse_rejects_duplicate_basenames() {
        let json = r#"[
  {
    "data": {"image": "https://a.example/x/img.jpg", "width": 10, "height": 10},
    "annotations": [{"result": []}],
    "predictions": []
  },
  {
    "data": {"image": "https://b.example/y/img.jpg", "width": 10, "height": 10},
    "annotations": [{"result": []}],
    "predictions": []
  }
]"#;

        let err = from_label_studio_str(json).expect_err("expected invalid error");
        match err {
            PanlabelError::LabelStudioJsonInvalid { message, .. } => {
                assert!(message.contains("duplicate image basename"));
            }
            other => panic!("expected LabelStudioJsonInvalid, got {other:?}"),
        }
    }

    #[test]
    fn write_then_read_roundtrip_semantic() {
        let dataset = from_label_studio_str(sample_json()).expect("parse original");
        let json = to_label_studio_string(&dataset).expect("write string");
        let restored = from_label_studio_str(&json).expect("parse restored");

        assert_eq!(dataset.images.len(), restored.images.len());
        assert_eq!(dataset.categories.len(), restored.categories.len());
        assert_eq!(dataset.annotations.len(), restored.annotations.len());

        for (left, right) in dataset.annotations.iter().zip(restored.annotations.iter()) {
            assert!((left.bbox.xmin() - right.bbox.xmin()).abs() < 1e-6);
            assert!((left.bbox.ymin() - right.bbox.ymin()).abs() < 1e-6);
            assert!((left.bbox.xmax() - right.bbox.xmax()).abs() < 1e-6);
            assert!((left.bbox.ymax() - right.bbox.ymax()).abs() < 1e-6);
        }
    }

    #[test]
    fn parse_rejects_inconsistent_from_name() {
        let json = r#"[
  {
    "data": {"image": "img.jpg"},
    "annotations": [
      {
        "result": [
          {
            "type": "rectanglelabels",
            "from_name": "bbox_a",
            "to_name": "image",
            "value": {
              "x": 10.0,
              "y": 10.0,
              "width": 10.0,
              "height": 10.0,
              "rectanglelabels": ["cat"]
            },
            "original_width": 100,
            "original_height": 100
          },
          {
            "type": "rectanglelabels",
            "from_name": "bbox_b",
            "to_name": "image",
            "value": {
              "x": 20.0,
              "y": 20.0,
              "width": 10.0,
              "height": 10.0,
              "rectanglelabels": ["cat"]
            },
            "original_width": 100,
            "original_height": 100
          }
        ]
      }
    ]
  }
]"#;

        let err = from_label_studio_str(json).expect_err("expected inconsistent from_name error");
        match err {
            PanlabelError::LabelStudioJsonInvalid { message, .. } => {
                assert!(message.contains("inconsistent from_name"));
            }
            other => panic!("expected LabelStudioJsonInvalid, got {other:?}"),
        }
    }

    #[test]
    fn writer_rejects_duplicate_output_basenames() {
        let dataset = Dataset {
            images: vec![
                Image::new(1u64, "train/shared.jpg", 100, 100),
                Image::new(2u64, "val/shared.jpg", 100, 100),
            ],
            categories: vec![Category::new(1u64, "cat")],
            annotations: vec![],
            ..Default::default()
        };

        let err = to_label_studio_string(&dataset).expect_err("expected duplicate basename error");
        match err {
            PanlabelError::LabelStudioJsonInvalid { message, .. } => {
                assert!(message.contains("unique basenames are required"));
            }
            other => panic!("expected LabelStudioJsonInvalid, got {other:?}"),
        }
    }
}