rvlib/tools_data/
coco_io.rs

1use std::{
2    collections::HashMap,
3    fmt::{Debug, Display},
4    mem,
5    path::{Path, PathBuf},
6    thread::{self, JoinHandle},
7    vec,
8};
9
10use serde::{Deserialize, Serialize};
11use tracing::info;
12
13use crate::{
14    cfg::ExportPath,
15    file_util::{self, path_to_str, PathPair},
16    image_util,
17    meta_data::MetaData,
18    result::trace_ok_warn,
19    tools_data::core::polygon_to_geofig,
20    util::version_label,
21    GeoFig,
22};
23use rvimage_domain::{rverr, to_rv, RvError, RvResult};
24use rvimage_domain::{BbF, Canvas, ShapeI, TPtF};
25
26use super::{
27    annotations::InstanceAnnotations,
28    brush_data::BrushAnnoMap,
29    core::{new_random_colors, CocoSegmentation, ExportAsCoco},
30    BboxToolData, BrushToolData, InstanceAnnotate, InstanceExportData, Rot90ToolData,
31};
32
33#[derive(Serialize, Deserialize, Debug, Default)]
34struct CocoInfo {
35    description: String,
36}
37impl Display for CocoInfo {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        f.write_str(&self.description)
40    }
41}
42
43#[derive(Serialize, Deserialize, Debug)]
44struct CocoImage {
45    id: u32,
46    width: u32,
47    height: u32,
48    file_name: String,
49}
50
51#[derive(Serialize, Deserialize, Debug)]
52struct CocoBboxCategory {
53    id: u32,
54    name: String,
55}
56
57#[derive(Serialize, Deserialize, Debug)]
58struct CocoAnnotation {
59    id: u32,
60    image_id: u32,
61    category_id: u32,
62    bbox: [TPtF; 4],
63    segmentation: Option<CocoSegmentation>,
64    area: Option<TPtF>,
65}
66
67fn colors_to_string(colors: &[[u8; 3]]) -> Option<String> {
68    colors
69        .iter()
70        .map(|[r, g, b]| format!("{r};{g};{b}"))
71        .reduce(|s1, s2| format!("{s1}_{s2}"))
72}
73
74fn string_to_colors(s: &str) -> RvResult<Vec<[u8; 3]>> {
75    let make_err = || rverr!("cannot convert str {} to rgb", s);
76    s.trim()
77        .split('_')
78        .map(|rgb_str| {
79            let mut rgb = [0; 3];
80            let mut it = rgb_str.split(';');
81            for c in &mut rgb {
82                *c = it
83                    .next()
84                    .and_then(|s| s.parse().ok())
85                    .ok_or_else(make_err)?;
86            }
87            Ok(rgb)
88        })
89        .collect::<RvResult<Vec<[u8; 3]>>>()
90}
91
92fn get_n_rotations(rotation_data: Option<&Rot90ToolData>, file_path: &str) -> u8 {
93    rotation_data
94        .and_then(|d| d.get_annos(file_path))
95        .map_or(0, |n_rot| n_rot.to_num())
96}
97
98fn insert_elt<A>(
99    elt: A,
100    annos: &mut HashMap<String, (Vec<A>, Vec<usize>, ShapeI)>,
101    cat_idx: usize,
102    n_rotations: u8,
103    path_as_key: String,
104    shape_coco: ShapeI,
105) where
106    A: InstanceAnnotate,
107{
108    let geo = trace_ok_warn(elt.rot90_with_image_ntimes(shape_coco, n_rotations));
109    if let Some(geo) = geo {
110        if let Some(annos_of_image) = annos.get_mut(&path_as_key) {
111            annos_of_image.0.push(geo);
112            annos_of_image.1.push(cat_idx);
113        } else {
114            annos.insert(
115                path_as_key,
116                (
117                    vec![geo],
118                    vec![cat_idx],
119                    ShapeI::new(shape_coco.w, shape_coco.h),
120                ),
121            );
122        }
123    }
124}
125
126fn instance_to_coco_anno<A>(
127    inst_anno: &A,
128    shape_im_unrotated: ShapeI,
129    n_rotations: u8,
130    is_export_coords_absolute: bool,
131    file_path: &str,
132) -> RvResult<([f64; 4], Option<CocoSegmentation>)>
133where
134    A: InstanceAnnotate,
135{
136    // to store data corresponding to the image on the disk, we need to invert the
137    // applied rotations
138    let n_rots_inverted = (4 - n_rotations) % 4;
139    let shape_rotated = shape_im_unrotated.rot90_with_image_ntimes(n_rotations);
140    let inst_anno = inst_anno
141        .clone()
142        .rot90_with_image_ntimes(shape_rotated, n_rots_inverted)?;
143
144    let bb = inst_anno.enclosing_bb();
145
146    let segmentation = inst_anno.to_cocoseg(shape_im_unrotated, is_export_coords_absolute)?;
147    let (imw, imh) = if is_export_coords_absolute {
148        (1.0, 1.0)
149    } else {
150        (
151            TPtF::from(shape_im_unrotated.w),
152            TPtF::from(shape_im_unrotated.h),
153        )
154    };
155
156    let bb_f = [bb.x / imw, bb.y / imh, bb.w / imw, bb.h / imh];
157    if bb_f[1] * bb_f[2] < 1e-6 {
158        tracing::warn!("annotation in {file_path} has no area {bb:?}.");
159    }
160    Ok((bb_f, segmentation))
161}
162
163struct WarnerCounting<'a> {
164    n_warnings: usize,
165    n_max_warnings: usize,
166    suppressing: bool,
167    suppress_str: &'a str,
168}
169impl<'a> WarnerCounting<'a> {
170    fn new(n_max_warnings: usize, suppress_str: &'a str) -> Self {
171        Self {
172            n_warnings: 0,
173            n_max_warnings,
174            suppressing: false,
175            suppress_str,
176        }
177    }
178    fn warn_str<'b>(&mut self, msg: &'b str) -> Option<&'b str>
179    where
180        'a: 'b,
181    {
182        if self.n_warnings < self.n_max_warnings {
183            self.n_warnings += 1;
184            Some(msg)
185        } else if !self.suppressing {
186            self.suppressing = true;
187            Some(self.suppress_str)
188        } else {
189            None
190        }
191    }
192    fn warn(&mut self, msg: &str) {
193        if let Some(msg) = self.warn_str(msg) {
194            tracing::warn!(msg);
195        }
196    }
197}
198
199#[derive(Serialize, Deserialize, Debug, Default)]
200pub struct CocoExportData {
201    info: CocoInfo,
202    images: Vec<CocoImage>,
203    annotations: Vec<CocoAnnotation>,
204    categories: Vec<CocoBboxCategory>,
205}
206impl CocoExportData {
207    pub fn from_tools_data<T, A>(
208        tools_data: T,
209        rotation_data: Option<&Rot90ToolData>,
210        prj_path: Option<&Path>,
211        double_check_shape: bool,
212    ) -> Self
213    where
214        T: ExportAsCoco<A>,
215        A: InstanceAnnotate + 'static,
216    {
217        type AnnoValue<'a, A> = (&'a String, &'a (Vec<A>, Vec<usize>, ShapeI));
218
219        let (options, label_info, anno_map, coco_file) = tools_data.separate_data();
220        let color_str = if let Some(s) = colors_to_string(label_info.colors()) {
221            format!(", {s}")
222        } else {
223            String::new()
224        };
225        let info_str = format!(
226            "created with RV Image {}, https://github.com/bertiqwerty/rvimage{color_str}",
227            version_label()
228        );
229        let info = CocoInfo {
230            description: info_str,
231        };
232        let export_data =
233            InstanceExportData::from_tools_data(&options, label_info, coco_file, anno_map);
234
235        let make_image_map = |(idx, (file_path, (_, _, shape))): (usize, AnnoValue<A>)| CocoImage {
236            id: idx as u32,
237            width: shape.w,
238            height: shape.h,
239            file_name: file_path.clone(),
240        };
241        let images = export_data
242            .annotations
243            .iter()
244            .enumerate()
245            .map(make_image_map)
246            .collect::<Vec<_>>();
247
248        let categories = export_data
249            .labels
250            .iter()
251            .zip(export_data.cat_ids.iter())
252            .map(|(label, cat_id)| CocoBboxCategory {
253                id: *cat_id,
254                name: label.clone(),
255            })
256            .collect::<Vec<_>>();
257
258        let mut box_id = 0;
259        let mut imagesum_elapsed = 0;
260        let mut n_images_exported = 1;
261        let make_anno_map =
262            |(image_idx, (file_path, (bbs, cat_idxs, shape))): (usize, AnnoValue<A>)| {
263                let now = std::time::Instant::now();
264                let prj_path = if let Some(prj_path) = prj_path {
265                    prj_path
266                } else {
267                    Path::new("")
268                };
269                let p = PathPair::new(file_path.clone(), prj_path);
270                let p_abs = p.path_absolute();
271                let shape = if Path::new(p_abs).exists() && double_check_shape {
272                    let im = trace_ok_warn(image_util::read_image(file_path));
273                    if let Some(im) = im {
274                        ShapeI::new(im.width(), im.height())
275                    } else {
276                        *shape
277                    }
278                } else {
279                    *shape
280                };
281                let n_rotations = get_n_rotations(rotation_data, file_path);
282                let annos = bbs
283                    .iter()
284                    .zip(cat_idxs.iter())
285                    .filter_map(|(inst_anno, cat_idx): (&A, &usize)| {
286                        trace_ok_warn(instance_to_coco_anno(
287                            inst_anno,
288                            shape,
289                            n_rotations,
290                            options.is_export_absolute,
291                            file_path,
292                        ))
293                        .map(|(bb_f, segmentation)| {
294                            box_id += 1;
295                            CocoAnnotation {
296                                id: box_id - 1,
297                                image_id: image_idx as u32,
298                                category_id: export_data.cat_ids[*cat_idx],
299                                bbox: bb_f,
300                                segmentation,
301                                area: Some(bb_f[2] * bb_f[3]),
302                            }
303                        })
304                    })
305                    .collect::<Vec<_>>();
306                let elapsed = now.elapsed();
307                imagesum_elapsed += elapsed.as_millis();
308                if imagesum_elapsed > 10000 && image_idx % n_images_exported == 0 {
309                    let ave = imagesum_elapsed as usize / (image_idx + 1);
310                    tracing::info!(
311                        "converting with {} ms/image, {}/{}, estimated time left: {} s",
312                        ave,
313                        image_idx + 1,
314                        export_data.annotations.len(),
315                        ave * (export_data.annotations.len() - image_idx - 1) / 1000
316                    );
317                    if n_images_exported == 1 {
318                        n_images_exported = image_idx;
319                        tracing::info!(
320                            "If your export is too slow, consider skipping the shape double check."
321                        )
322                    }
323                }
324                annos
325            };
326        let annotations = export_data
327            .annotations
328            .iter()
329            .enumerate()
330            .flat_map(make_anno_map)
331            .collect::<Vec<_>>();
332
333        CocoExportData {
334            info,
335            images,
336            annotations,
337            categories,
338        }
339    }
340
341    #[allow(clippy::too_many_lines)]
342    pub fn convert_to_toolsdata(
343        self,
344        coco_file: ExportPath,
345        rotation_data: Option<&Rot90ToolData>,
346    ) -> RvResult<(BboxToolData, BrushToolData)> {
347        let cat_ids: Vec<u32> = self.categories.iter().map(|coco_cat| coco_cat.id).collect();
348        let labels: Vec<String> = self
349            .categories
350            .into_iter()
351            .map(|coco_cat| coco_cat.name)
352            .collect();
353        let color_str = self.info.description.split(',').next_back();
354        let colors: Vec<[u8; 3]> = if let Some(s) = color_str {
355            string_to_colors(s).unwrap_or_else(|_| new_random_colors(labels.len()))
356        } else {
357            new_random_colors(labels.len())
358        };
359        let id_image_map = self
360            .images
361            .iter()
362            .map(|coco_image: &CocoImage| {
363                Ok((
364                    coco_image.id,
365                    (
366                        coco_image.file_name.as_str(),
367                        coco_image.width,
368                        coco_image.height,
369                    ),
370                ))
371            })
372            .collect::<RvResult<HashMap<u32, (&str, u32, u32)>>>()?;
373
374        let mut annotations_bbox: HashMap<String, (Vec<GeoFig>, Vec<usize>, ShapeI)> =
375            HashMap::new();
376        let mut annotations_brush: HashMap<String, (Vec<Canvas>, Vec<usize>, ShapeI)> =
377            HashMap::new();
378
379        let n_annotations = self.annotations.len();
380        let mut warner = WarnerCounting::new(
381            n_annotations / 10,
382            "suppressing further warnings during coco import",
383        );
384        for coco_anno in self.annotations {
385            let (file_path, w_coco, h_coco) = id_image_map[&coco_anno.image_id];
386
387            // The annotations in the coco files created by RV Image are stored
388            // ignoring any orientation meta-data. Hence, if the image has been loaded
389            // and rotated with RV Image we correct the rotation.
390            let n_rotations = get_n_rotations(rotation_data, file_path);
391            let shape_coco = ShapeI::new(w_coco, h_coco);
392
393            let path_as_key = if file_path.starts_with("http") {
394                file_util::url_encode(file_path)
395            } else {
396                file_path.to_string()
397            };
398
399            let cat_idx = cat_ids
400                .iter()
401                .position(|cat_id| *cat_id == coco_anno.category_id)
402                .ok_or_else(|| {
403                    rverr!(
404                        "could not find cat id {}, we only have {:?}",
405                        coco_anno.category_id,
406                        cat_ids
407                    )
408                })?;
409            let coords_absolute = coco_anno.bbox.iter().any(|x| *x > 1.0);
410            let (w_factor, h_factor) = if coords_absolute {
411                (1.0, 1.0)
412            } else {
413                (f64::from(w_coco), f64::from(h_coco))
414            };
415            let bbox = [
416                (w_factor * coco_anno.bbox[0]),
417                (h_factor * coco_anno.bbox[1]),
418                (w_factor * coco_anno.bbox[2]),
419                (h_factor * coco_anno.bbox[3]),
420            ];
421
422            let mut insert_geo = |geo| {
423                insert_elt(
424                    geo,
425                    &mut annotations_bbox,
426                    cat_idx,
427                    n_rotations,
428                    path_as_key.clone(),
429                    shape_coco,
430                );
431            };
432
433            let bb = BbF::from(&bbox);
434
435            match coco_anno.segmentation {
436                Some(CocoSegmentation::Polygon(poly)) => {
437                    let geofig = if poly.is_empty() {
438                        Ok(GeoFig::BB(bb))
439                    } else {
440                        polygon_to_geofig(&poly, w_factor, h_factor, bb, |s| warner.warn(s))
441                    };
442                    if let Ok(geofig) = geofig {
443                        insert_geo(geofig);
444                    } else {
445                        warner.warn(&format!("invalid segmentation in coco file {file_path}"));
446                    }
447                }
448                Some(CocoSegmentation::Rle(rle)) => {
449                    let canvas = rle.to_canvas(bb);
450                    if let Ok(canvas) = canvas {
451                        insert_elt(
452                            canvas,
453                            &mut annotations_brush,
454                            cat_idx,
455                            n_rotations,
456                            path_as_key,
457                            shape_coco,
458                        );
459                    } else {
460                        warner.warn(&format!("invalid segmentation in coco file {file_path}"));
461                    }
462                }
463                _ => {
464                    let geo = GeoFig::BB(bb);
465                    insert_geo(geo);
466                }
467            }
468        }
469        let bbox_data = BboxToolData::from_coco_export_data(InstanceExportData {
470            labels: labels.clone(),
471            colors: colors.clone(),
472            cat_ids: cat_ids.clone(),
473            annotations: annotations_bbox,
474            coco_file: coco_file.clone(),
475            is_export_absolute: false,
476        })?;
477        let brush_data = BrushToolData::from_coco_export_data(InstanceExportData {
478            labels,
479            colors,
480            cat_ids,
481            annotations: annotations_brush,
482            coco_file,
483            is_export_absolute: false,
484        })?;
485        Ok((bbox_data, brush_data))
486    }
487}
488
489fn meta_data_to_coco_path(meta_data: &MetaData) -> RvResult<PathBuf> {
490    let export_folder = Path::new(
491        meta_data
492            .export_folder
493            .as_ref()
494            .ok_or_else(|| RvError::new("no export folder given"))?,
495    );
496    let opened_folder = meta_data
497        .opened_folder
498        .as_ref()
499        .map(PathPair::path_absolute)
500        .ok_or_else(|| RvError::new("no folder open"))?;
501    let parent = Path::new(opened_folder)
502        .parent()
503        .and_then(|p| p.file_stem())
504        .and_then(|p| p.to_str());
505
506    let opened_folder_name = Path::new(opened_folder)
507        .file_stem()
508        .and_then(|of| of.to_str())
509        .ok_or_else(|| rverr!("cannot find folder name  of {}", opened_folder))?;
510    let file_name = if let Some(p) = parent {
511        format!("{p}_{opened_folder_name}_coco.json")
512    } else {
513        format!("{opened_folder_name}_coco.json")
514    };
515    Ok(export_folder.join(file_name))
516}
517fn get_cocofilepath(meta_data: &MetaData, coco_file: &ExportPath) -> RvResult<PathBuf> {
518    if path_to_str(&coco_file.path)?.is_empty() {
519        meta_data_to_coco_path(meta_data)
520    } else {
521        Ok(coco_file.path.clone())
522    }
523}
524
525pub fn to_per_file_crowd(brush_annotations_map: &mut BrushAnnoMap) {
526    for (i, (filename, (annos, _))) in brush_annotations_map.iter_mut().enumerate() {
527        if i % 10 == 0 {
528            info!("export - image #{i} converting {filename} to per-image-crowd");
529        }
530        if let Some(max_catidx) = annos.cat_idxs().iter().max() {
531            let mut canvas_idxes_of_cats = vec![vec![]; max_catidx + 1];
532            for i in 0..(annos.elts().len()) {
533                canvas_idxes_of_cats[annos.cat_idxs()[i]].push(i);
534            }
535            let mut merged_canvases = vec![None; max_catidx + 1];
536            for (cat_idx, canvas_idxes) in canvas_idxes_of_cats.iter().enumerate() {
537                let mut merged_canvas: Option<Canvas> = None;
538                for canvas_idx in canvas_idxes {
539                    let elt = &annos.elts()[*canvas_idx];
540                    if let Some(merged_canvas) = &mut merged_canvas {
541                        *merged_canvas = mem::take(merged_canvas).merge(elt);
542                    } else {
543                        merged_canvas = Some(elt.clone());
544                    }
545                }
546                merged_canvases[cat_idx] = merged_canvas;
547            }
548            let mut cat_idxes = vec![];
549            let elts = merged_canvases
550                .into_iter()
551                .enumerate()
552                .filter_map(|(i, cvs)| cvs.map(|cvs| (i, cvs)))
553                .map(|(i, cvs)| {
554                    cat_idxes.push(i);
555                    cvs
556                })
557                .collect();
558            let n_elts = cat_idxes.len();
559            let new_annos = trace_ok_warn(InstanceAnnotations::<Canvas>::new(
560                elts,
561                cat_idxes,
562                vec![false; n_elts],
563            ));
564            if let Some(new_annos) = new_annos {
565                *annos = new_annos;
566            }
567        }
568    }
569}
570
571/// Serialize annotations in Coco format. Any orientations changes applied with the rotation tool
572/// are reverted, since the rotation tool does not change the image file. Hence, the Coco file contains the annotation
573/// relative to the image as it is found in memory ignoring any meta-data.
574///
575/// # Errors
576/// - outpath name cannot be created due to weird characters or the like
577/// - serde write-to-json fails
578pub fn write_coco<T, A>(
579    meta_data: &MetaData,
580    tools_data: T,
581    rotation_data: Option<&Rot90ToolData>,
582    coco_file: &ExportPath,
583    double_check_shape: bool,
584) -> RvResult<(PathBuf, JoinHandle<RvResult<()>>)>
585where
586    T: ExportAsCoco<A> + Send + 'static,
587    A: InstanceAnnotate + 'static,
588{
589    let meta_data = meta_data.clone();
590    let coco_out_path = get_cocofilepath(&meta_data, coco_file)?;
591    let coco_out_path_for_thr = coco_out_path.clone();
592    let rotation_data = rotation_data.cloned();
593    let conn = coco_file.conn.clone();
594    let handle = thread::spawn(move || {
595        let coco_data = CocoExportData::from_tools_data(
596            tools_data,
597            rotation_data.as_ref(),
598            meta_data.prj_path(),
599            double_check_shape,
600        );
601        let data_str = serde_json::to_string(&coco_data)
602            .map_err(to_rv)
603            .inspect_err(|e| tracing::error!("export failed due to {e:?}"))?;
604
605        conn.write(
606            &data_str,
607            &coco_out_path_for_thr,
608            meta_data.ssh_cfg.as_ref(),
609        )
610        .inspect_err(|e| tracing::error!("export failed due to {e:?}"))?;
611        tracing::info!("exported coco labels to {coco_out_path_for_thr:?}");
612        Ok(())
613    });
614    Ok((coco_out_path, handle))
615}
616
617/// Import annotations in Coco format. Any orientations changes applied with the rotation tool
618/// to images that have annotations in the Coco file are applied to the annotations before importing. We expect, that
619/// the Coco file contains the annotations relative to the image as it is found in memory ignoring any meta-data.
620///
621/// # Errors
622/// - not a coco file
623/// - ssh connection problems
624pub fn read_coco(
625    meta_data: &MetaData,
626    coco_file: &ExportPath,
627    rotation_data: Option<&Rot90ToolData>,
628) -> RvResult<(BboxToolData, BrushToolData)> {
629    let coco_inpath = get_cocofilepath(meta_data, coco_file)?;
630    let coco_str = coco_file
631        .conn
632        .read(&coco_inpath, meta_data.ssh_cfg.as_ref())?;
633    let read_data: CocoExportData = serde_json::from_str(coco_str.as_str()).map_err(to_rv)?;
634    read_data.convert_to_toolsdata(coco_file.clone(), rotation_data)
635}
636
637#[cfg(test)]
638use {
639    super::core::CocoRle,
640    crate::{
641        cfg::{ExportPathConnection, SshCfg},
642        defer_file_removal,
643        meta_data::{ConnectionData, MetaDataFlags},
644        tracing_setup::init_tracing_for_tests,
645    },
646    file_util::DEFAULT_TMPDIR,
647    rvimage_domain::{make_test_bbs, BbI},
648    std::{fs, str::FromStr},
649};
650#[cfg(test)]
651fn make_meta_data(opened_folder: Option<&Path>) -> (MetaData, PathBuf) {
652    let opened_folder = if let Some(of) = opened_folder {
653        PathPair::new(of.to_str().unwrap().to_string(), Path::new(""))
654    } else {
655        PathPair::new("xi".to_string(), Path::new(""))
656    };
657    let test_export_folder = DEFAULT_TMPDIR.clone();
658
659    if !test_export_folder.exists() {
660        match fs::create_dir(&test_export_folder) {
661            Ok(_) => (),
662            Err(e) => {
663                println!("{e:?}");
664            }
665        }
666    }
667
668    let test_export_path = DEFAULT_TMPDIR.join(format!("{}.json", opened_folder.path_absolute()));
669    let mut meta = MetaData::from_filepath(
670        test_export_path
671            .with_extension("egal")
672            .to_str()
673            .unwrap()
674            .to_string(),
675        0,
676        Path::new("egal"),
677    );
678    meta.opened_folder = Some(opened_folder);
679    meta.export_folder = Some(test_export_folder.to_str().unwrap().to_string());
680    meta.connection_data = ConnectionData::Ssh(SshCfg::default());
681    (meta, test_export_path)
682}
683#[cfg(test)]
684fn make_data_brush(
685    image_file: &Path,
686    opened_folder: Option<&Path>,
687    export_absolute: bool,
688    n_boxes: Option<usize>,
689) -> (BrushToolData, MetaData, PathBuf, ShapeI) {
690    use super::InstanceLabelDisplay;
691
692    let shape = ShapeI::new(100, 40);
693    let mut bbox_data = BrushToolData::default();
694    bbox_data.options.core.is_export_absolute = export_absolute;
695    bbox_data.coco_file = ExportPath::default();
696    bbox_data
697        .label_info
698        .push("x".to_string(), None, None)
699        .unwrap();
700
701    bbox_data
702        .label_info
703        .remove_catidx(0, &mut bbox_data.annotations_map);
704
705    let mut bbs = make_test_bbs();
706    bbs.extend(bbs.clone());
707    bbs.extend(bbs.clone());
708    bbs.extend(bbs.clone());
709    bbs.extend(bbs.clone());
710    bbs.extend(bbs.clone());
711    bbs.extend(bbs.clone());
712    bbs.extend(bbs.clone());
713    if let Some(n) = n_boxes {
714        bbs = bbs[0..n].to_vec();
715    }
716
717    let annos = bbox_data.get_annos_mut(image_file.as_os_str().to_str().unwrap(), shape);
718    if let Some(a) = annos {
719        for bb in bbs {
720            let mut mask = vec![0; (bb.w * bb.h) as usize];
721            mask[4] = 1;
722            let c = Canvas {
723                bb: bb.into(),
724                mask,
725                intensity: 0.5,
726            };
727            a.add_elt(c, 0, InstanceLabelDisplay::None);
728        }
729    }
730
731    let (meta, test_export_path) = make_meta_data(opened_folder);
732    (bbox_data, meta, test_export_path, shape)
733}
734#[cfg(test)]
735pub fn make_data_bbox(
736    image_file: &Path,
737    opened_folder: Option<&Path>,
738    export_absolute: bool,
739    n_boxes: Option<usize>,
740) -> (BboxToolData, MetaData, PathBuf, ShapeI) {
741    let shape = ShapeI::new(20, 10);
742    let mut bbox_data = BboxToolData::new();
743    bbox_data.options.core.is_export_absolute = export_absolute;
744    bbox_data.coco_file = ExportPath::default();
745    bbox_data
746        .label_info
747        .push("x".to_string(), None, None)
748        .unwrap();
749
750    bbox_data
751        .label_info
752        .remove_catidx(0, &mut bbox_data.annotations_map);
753
754    let mut bbs = make_test_bbs();
755    bbs.extend(bbs.clone());
756    bbs.extend(bbs.clone());
757    bbs.extend(bbs.clone());
758    bbs.extend(bbs.clone());
759    bbs.extend(bbs.clone());
760    bbs.extend(bbs.clone());
761    bbs.extend(bbs.clone());
762    if let Some(n) = n_boxes {
763        bbs = bbs[0..n].to_vec();
764    }
765
766    let annos = bbox_data.get_annos_mut(image_file.as_os_str().to_str().unwrap(), shape);
767    if let Some(a) = annos {
768        for bb in bbs {
769            a.add_bb(bb, 0, super::InstanceLabelDisplay::IndexLr);
770        }
771    }
772    let (meta, test_export_path) = make_meta_data(opened_folder);
773    (bbox_data, meta, test_export_path, shape)
774}
775
776#[cfg(test)]
777fn is_image_duplicate_free(coco_data: &CocoExportData) -> bool {
778    let mut image_ids = coco_data.images.iter().map(|i| i.id).collect::<Vec<_>>();
779    image_ids.sort();
780    let len_prev = image_ids.len();
781    image_ids.dedup();
782    image_ids.len() == len_prev
783}
784
785#[cfg(test)]
786fn no_image_dups<P>(coco_file: P)
787where
788    P: AsRef<Path> + Debug,
789{
790    let s = file_util::read_to_string(&coco_file).unwrap();
791    let read_raw: CocoExportData = serde_json::from_str(s.as_str()).unwrap();
792
793    assert!(is_image_duplicate_free(&read_raw));
794}
795#[test]
796fn test_coco_export() {
797    fn assert_coco_eq<T, A>(data: T, read: T, coco_file: &PathBuf)
798    where
799        T: ExportAsCoco<A> + Send + 'static,
800        A: InstanceAnnotate + 'static + Debug,
801    {
802        assert_eq!(data.label_info().cat_ids(), read.label_info().cat_ids());
803        assert_eq!(data.label_info().labels(), read.label_info().labels());
804        for (brush_anno, read_anno) in data.anno_iter().zip(read.anno_iter()) {
805            let (name, (instance_annos, shape)) = brush_anno;
806            let (read_name, (read_instance_annos, read_shape)) = read_anno;
807            assert_eq!(instance_annos.cat_idxs(), read_instance_annos.cat_idxs());
808            assert_eq!(
809                instance_annos.elts().len(),
810                read_instance_annos.elts().len()
811            );
812            for (i, (a, b)) in instance_annos
813                .elts()
814                .iter()
815                .zip(read_instance_annos.elts().iter())
816                .enumerate()
817            {
818                assert_eq!(a, b, "annos at index {} differ", i);
819            }
820            assert_eq!(name, read_name);
821            assert_eq!(shape, read_shape);
822        }
823        no_image_dups(coco_file);
824    }
825    fn write_read<T, A>(meta: &MetaData, tools_data: T) -> ((BboxToolData, BrushToolData), PathBuf)
826    where
827        T: ExportAsCoco<A> + Send + 'static,
828        A: InstanceAnnotate + 'static,
829    {
830        let coco_file = tools_data.cocofile_conn();
831        let (coco_file, handle) = write_coco(meta, tools_data, None, &coco_file, true).unwrap();
832        handle.join().unwrap().unwrap();
833        (
834            read_coco(
835                meta,
836                &ExportPath {
837                    path: coco_file.clone(),
838                    conn: ExportPathConnection::Local,
839                },
840                None,
841            )
842            .unwrap(),
843            coco_file,
844        )
845    }
846    fn test_br(file_path: &Path, opened_folder: Option<&Path>, export_absolute: bool) {
847        let (brush_data, meta, _, _) =
848            make_data_brush(file_path, opened_folder, export_absolute, None);
849        let ((_, read), coco_file) = write_read(&meta, brush_data.clone());
850        defer_file_removal!(&coco_file);
851        assert_coco_eq(brush_data, read, &coco_file);
852    }
853    fn test_bb(file_path: &Path, opened_folder: Option<&Path>, export_absolute: bool) {
854        let (bbox_data, meta, _, _) =
855            make_data_bbox(file_path, opened_folder, export_absolute, None);
856        let ((read, _), coco_file) = write_read(&meta, bbox_data.clone());
857        defer_file_removal!(&coco_file);
858        assert_coco_eq(bbox_data, read, &coco_file);
859    }
860    let tmpdir = &DEFAULT_TMPDIR;
861    let file_path = tmpdir.join("test_image.png");
862    test_br(&file_path, None, true);
863    test_bb(&file_path, None, true);
864    let folder = Path::new("http://localhost:8000/some_path");
865    let file = Path::new("http://localhost:8000/some_path/xyz.png");
866    test_br(file, Some(folder), false);
867    test_bb(file, Some(folder), false);
868}
869
870#[cfg(test)]
871const TEST_DATA_FOLDER: &str = "resources/test_data/";
872
873#[test]
874fn test_coco_import_export() {
875    let meta = MetaData::new(
876        None,
877        None,
878        ConnectionData::None,
879        None,
880        Some(PathPair::new("ohm_somefolder".to_string(), Path::new(""))),
881        Some(TEST_DATA_FOLDER.to_string()),
882        MetaDataFlags::default(),
883        None,
884    );
885    let test_file_src = format!("{TEST_DATA_FOLDER}catids_12_coco_imwolab.json");
886    let test_file = "tmp_coco.json";
887    defer_file_removal!(&test_file);
888    fs::copy(test_file_src, test_file).unwrap();
889    let export_path = ExportPath {
890        path: PathBuf::from_str(test_file).unwrap(),
891        conn: ExportPathConnection::Local,
892    };
893
894    let (read, _) = read_coco(&meta, &export_path, None).unwrap();
895    let (_, handle) = write_coco(&meta, read.clone(), None, &export_path.clone(), true).unwrap();
896    handle.join().unwrap().unwrap();
897    no_image_dups(&read.coco_file.path);
898    let (read, _) = read_coco(&meta, &export_path, None).unwrap();
899    for anno in read.anno_iter() {
900        let (_, (annos, _)) = anno;
901        for a in annos.elts() {
902            println!("{a:?}");
903            assert!(a.enclosing_bb().w * a.enclosing_bb().h > 1e-3);
904        }
905    }
906}
907
908#[test]
909fn test_coco_import() -> RvResult<()> {
910    init_tracing_for_tests();
911    fn test(filename: &str, cat_ids: Vec<u32>, reference_bbs: &[(BbI, &str)]) {
912        tracing::debug!(filename);
913        let meta = MetaData::new(
914            None,
915            None,
916            ConnectionData::None,
917            None,
918            Some(PathPair::new(filename.to_string(), Path::new(""))),
919            Some(TEST_DATA_FOLDER.to_string()),
920            MetaDataFlags::default(),
921            None,
922        );
923        tracing::debug!("{meta:?}");
924        let (read, _) = read_coco(&meta, &ExportPath::default(), None).unwrap();
925        assert_eq!(read.label_info.cat_ids(), &cat_ids);
926        assert_eq!(
927            read.label_info.labels(),
928            &vec!["first label", "second label"]
929        );
930        for (bb, file_path) in reference_bbs {
931            let annos = read.get_annos(file_path);
932            println!();
933            println!("{file_path:?}");
934            println!("{annos:?}");
935            assert!(annos.unwrap().elts().contains(&GeoFig::BB((*bb).into())));
936        }
937    }
938
939    let bb_im_ref_abs1 = [
940        (
941            BbI::from_arr(&[1, 1, 5, 5]),
942            "http://localhost:5000/%2Bnowhere.png",
943        ),
944        (
945            BbI::from_arr(&[11, 11, 4, 7]),
946            "http://localhost:5000/%2Bnowhere.png",
947        ),
948        (
949            BbI::from_arr(&[1, 1, 5, 5]),
950            "http://localhost:5000/%2Bnowhere2.png",
951        ),
952    ];
953    let bb_im_ref_abs2 = [
954        (BbI::from_arr(&[1, 1, 5, 5]), "nowhere.png"),
955        (BbI::from_arr(&[11, 11, 4, 7]), "nowhere.png"),
956        (BbI::from_arr(&[1, 1, 5, 5]), "nowhere2.png"),
957    ];
958    let bb_im_ref_relative = [
959        (BbI::from_arr(&[10, 100, 50, 500]), "nowhere.png"),
960        (BbI::from_arr(&[91, 870, 15, 150]), "nowhere.png"),
961        (BbI::from_arr(&[10, 1, 50, 5]), "nowhere2.png"),
962    ];
963    test("catids_12", vec![1, 2], &bb_im_ref_abs1);
964    test("catids_01", vec![0, 1], &bb_im_ref_abs2);
965    test("catids_12_relative", vec![1, 2], &bb_im_ref_relative);
966    Ok(())
967}
968
969#[test]
970fn color_vs_str() {
971    let colors = vec![[0, 0, 7], [4, 0, 101], [210, 9, 0]];
972    let s = colors_to_string(&colors);
973    let colors_back = string_to_colors(&s.unwrap()).unwrap();
974    assert_eq!(colors, colors_back);
975}
976
977#[test]
978fn test_rotation_export_import() {
979    fn test<T, A>(
980        coco_file: &PathBuf,
981        bbox_specifics: T,
982        meta_data: MetaData,
983        shape: ShapeI,
984        read_f: impl Fn(&MetaData, &ExportPath, Option<&Rot90ToolData>) -> T,
985    ) where
986        T: ExportAsCoco<A> + Send + 'static + Clone,
987        A: InstanceAnnotate + 'static + Debug,
988    {
989        defer_file_removal!(&coco_file);
990        let mut rotation_data = Rot90ToolData::default();
991        let annos = rotation_data.get_annos_mut("some_path.png", shape);
992        if let Some(annos) = annos {
993            *annos = annos.increase();
994        }
995        let coco_file = bbox_specifics.cocofile_conn();
996        let (out_path, handle) = write_coco(
997            &meta_data,
998            bbox_specifics.clone(),
999            Some(&rotation_data),
1000            &coco_file,
1001            true,
1002        )
1003        .unwrap();
1004        handle.join().unwrap().unwrap();
1005        println!("write to {out_path:?}");
1006        let out_path = ExportPath {
1007            path: out_path,
1008            conn: ExportPathConnection::Local,
1009        };
1010        let read = read_f(&meta_data, &out_path, Some(&rotation_data));
1011
1012        for ((_, (anno_res, _)), (_, (anno_ref, _))) in
1013            bbox_specifics.anno_iter().zip(read.anno_iter())
1014        {
1015            for (read_elt, ref_elt) in anno_res.elts().iter().zip(anno_ref.elts().iter()) {
1016                assert_eq!(read_elt, ref_elt);
1017            }
1018        }
1019    }
1020    let (brush_specifics, meta_data, coco_file, shape) = make_data_brush(
1021        Path::new("some_path.png"),
1022        Some(Path::new("afolder")),
1023        false,
1024        None,
1025    );
1026    test(&coco_file, brush_specifics, meta_data, shape, |m, d, r| {
1027        read_coco(m, d, r).unwrap().1
1028    });
1029    let (bbox_specifics, meta_data, coco_file, shape) = make_data_bbox(
1030        Path::new("some_path.png"),
1031        Some(Path::new("afolder")),
1032        false,
1033        None,
1034    );
1035    test(&coco_file, bbox_specifics, meta_data, shape, |m, d, r| {
1036        read_coco(m, d, r).unwrap().0
1037    });
1038}
1039
1040#[test]
1041fn test_serialize_rle() {
1042    let rle = CocoRle {
1043        counts: vec![1, 2, 3, 4],
1044        size: (5, 6),
1045        intensity: None,
1046    };
1047    let rle = CocoSegmentation::Rle(rle);
1048    let s = serde_json::to_string(&rle).unwrap();
1049    println!("{s}");
1050    let rle2: CocoSegmentation = serde_json::from_str(&s).unwrap();
1051    assert_eq!(format!("{rle:?}"), format!("{rle2:?}"));
1052    let poly = CocoSegmentation::Polygon(vec![vec![1.0, 2.0]]);
1053    let s = serde_json::to_string(&poly).unwrap();
1054    println!("{s}");
1055    let poly2: CocoSegmentation = serde_json::from_str(&s).unwrap();
1056    assert_eq!(format!("{poly:?}"), format!("{poly2:?}"));
1057}
1058
1059#[test]
1060fn test_instance_to_coco() {
1061    let shape = ShapeI::new(2000, 2667);
1062    let bb = BbI::from_arr(&[1342, 1993, 8, 8]);
1063    let n_rot = 1;
1064    let canvas = Canvas {
1065        mask: vec![0; 64],
1066        bb,
1067        intensity: 0.5,
1068    };
1069    let coco_anno = instance_to_coco_anno(&canvas, shape, n_rot, false, "");
1070    assert!(coco_anno.is_err());
1071
1072    let shape_im = ShapeI::new(20, 40);
1073    let mut mask = vec![0; 4];
1074    mask[2] = 1;
1075    let canvas = Canvas {
1076        bb: BbI::from_arr(&[1, 1, 2, 2]),
1077        mask: mask.clone(),
1078        intensity: 0.5,
1079    };
1080    let n_rotations = 1;
1081
1082    let (_, segmentation) =
1083        instance_to_coco_anno(&canvas, shape_im, n_rotations, false, "").unwrap();
1084
1085    let coco_seg = canvas
1086        .rot90_with_image_ntimes(
1087            shape_im.rot90_with_image_ntimes(n_rotations),
1088            4 - n_rotations,
1089        )
1090        .unwrap()
1091        .to_cocoseg(shape_im, false)
1092        .unwrap();
1093    assert_ne!(coco_seg, None);
1094    assert_eq!(segmentation, coco_seg);
1095    let mut mask = [0; 4];
1096    mask[2] = 1;
1097    let geo = GeoFig::BB(BbF::from_arr(&[1.0, 1.0, 2.0, 8.0]));
1098
1099    let n_rotations = 1;
1100
1101    let (bb_rot, segmentation) =
1102        instance_to_coco_anno(&geo, shape_im, n_rotations, true, "").unwrap();
1103    println!("{bb_rot:?}");
1104    let coco_seg = geo
1105        .rot90_with_image_ntimes(
1106            shape_im.rot90_with_image_ntimes(n_rotations),
1107            4 - n_rotations,
1108        )
1109        .unwrap()
1110        .to_cocoseg(shape_im, true)
1111        .unwrap();
1112    assert_ne!(coco_seg, None);
1113    assert_eq!(segmentation, coco_seg);
1114}
1115
1116#[test]
1117fn test_warner() {
1118    let suppress_msg = "no further warnings";
1119    let mut warner = WarnerCounting::new(3, suppress_msg);
1120    assert_eq!(warner.warn_str("a"), Some("a"));
1121    assert_eq!(warner.warn_str("a"), Some("a"));
1122    assert_eq!(warner.warn_str("b"), Some("b"));
1123    assert_eq!(warner.warn_str("a"), Some(suppress_msg));
1124    assert_eq!(warner.warn_str("a"), None);
1125}