torsh-vision 0.1.2

Computer vision utilities for ToRSh deep learning framework
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
//! Interactive visualization utilities for real-time image manipulation and viewing
//!
//! This module provides comprehensive interactive visualization capabilities for ToRSh Vision,
//! including web-based viewers, annotation systems, and real-time transform operations.
//!
//! # Features
//!
//! - **Interactive Viewer**: Web-based interface for real-time image manipulation
//! - **Annotation System**: Support for various annotation types (bounding boxes, points, polygons, etc.)
//! - **Transform Operations**: Interactive transform pipeline with parameter controls
//! - **HTML Generation**: Automatic generation of web interfaces for visualization
//!
//! # Examples
//!
//! ## Basic Interactive Viewer
//!
//! ```rust
//! use torsh_vision::utils::interactive::{create_interactive_viewer, Annotation, AnnotationType};
//! use torsh_tensor::Tensor;
//!
//! // Create a new interactive viewer
//! let mut viewer = create_interactive_viewer(8080);
//!
//! // Set an image (3D tensor with shape [C, H, W])
//! let image = Tensor::zeros(&[3, 224, 224]).unwrap();
//! viewer.set_image(image).unwrap();
//!
//! // Add annotations
//! let annotation = Annotation {
//!     annotation_type: AnnotationType::BoundingBox,
//!     coordinates: vec![10.0, 10.0, 50.0, 50.0],
//!     label: "Object".to_string(),
//!     color: (255, 0, 0),
//!     confidence: Some(0.95),
//! };
//! viewer.add_annotation(annotation);
//!
//! // Generate HTML interface
//! let html = viewer.generate_html_interface();
//!
//! // Start the viewer server
//! viewer.start_server().unwrap();
//! ```
//!
//! ## Interactive Transform Operations
//!
//! ```rust
//! use torsh_vision::utils::interactive::{InteractiveViewer, Parameter};
//!
//! struct BrightnessTransform {
//!     brightness: f32,
//! }
//!
//! impl torsh_vision::utils::interactive::TransformOp for BrightnessTransform {
//!     fn apply(&self, image: &Tensor<f32>) -> torsh_vision::Result<Tensor<f32>> {
//!         // Apply brightness adjustment
//!         image.clone() + self.brightness
//!     }
//!
//!     fn name(&self) -> &str {
//!         "Brightness"
//!     }
//!
//!     fn parameters(&self) -> Vec<Parameter> {
//!         vec![Parameter {
//!             name: "brightness".to_string(),
//!             value: self.brightness,
//!             min: -1.0,
//!             max: 1.0,
//!             step: 0.01,
//!         }]
//!     }
//! }
//! ```

use crate::{Result, VisionError};
use serde::{Deserialize, Serialize};
use torsh_tensor::Tensor;

/// Interactive visualization server for real-time image manipulation and viewing
///
/// The `InteractiveViewer` provides a comprehensive web-based interface for visualizing
/// and manipulating images in real-time. It supports various annotation types,
/// interactive transforms, and generates HTML interfaces for browser-based viewing.
///
/// # Features
///
/// - Real-time image display and manipulation
/// - Multiple annotation types (bounding boxes, points, polygons, etc.)
/// - Interactive transform pipeline
/// - Web-based HTML interface generation
/// - Annotation management and export
///
/// # Architecture
///
/// The viewer operates as a stateful server that maintains:
/// - Current image data as a 3D tensor
/// - Collection of annotations with metadata
/// - Pipeline of interactive transforms
/// - Configuration for web interface generation
pub struct InteractiveViewer {
    port: u16,
    current_image: Option<Tensor<f32>>,
    annotations: Vec<Annotation>,
    transforms: Vec<Box<dyn TransformOp>>,
}

/// Annotation structure for interactive visualization
///
/// Represents a single annotation with associated metadata including position,
/// type, label, visual styling, and optional confidence score.
///
/// # Fields
///
/// - `annotation_type`: Type of annotation (bounding box, point, etc.)
/// - `coordinates`: Position data specific to annotation type
/// - `label`: Human-readable label for the annotation
/// - `color`: RGB color tuple for visual rendering
/// - `confidence`: Optional confidence score [0.0, 1.0]
///
/// # Coordinate Formats
///
/// Different annotation types use coordinates differently:
/// - **BoundingBox**: [x_min, y_min, x_max, y_max]
/// - **Point**: [x, y]
/// - **Polygon**: [x1, y1, x2, y2, ..., xn, yn]
/// - **Text**: [x, y] (anchor point)
/// - **Arrow**: [x_start, y_start, x_end, y_end]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Annotation {
    pub annotation_type: AnnotationType,
    pub coordinates: Vec<f32>,
    pub label: String,
    pub color: (u8, u8, u8),
    pub confidence: Option<f32>,
}

/// Enumeration of supported annotation types
///
/// Each type has specific coordinate requirements and rendering behavior:
///
/// - **BoundingBox**: Rectangular bounding box with min/max coordinates
/// - **Point**: Single point marker
/// - **Polygon**: Multi-point polygon shape
/// - **Mask**: Pixel-level segmentation mask
/// - **Text**: Text label with anchor position
/// - **Arrow**: Directional arrow with start and end points
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnnotationType {
    BoundingBox,
    Point,
    Polygon,
    Mask,
    Text,
    Arrow,
}

/// Transform operation trait for interactive manipulation
///
/// Defines the interface for interactive image transforms that can be applied
/// in real-time through the web interface. Each transform provides:
///
/// - `apply()`: Core transformation logic
/// - `name()`: Human-readable name for UI display
/// - `parameters()`: Configurable parameters with constraints
///
/// # Thread Safety
///
/// Implementations must be thread-safe (`Send + Sync`) to support
/// concurrent web interface operations.
///
/// # Example Implementation
///
/// ```rust
/// struct RotationTransform {
///     angle: f32,
/// }
///
/// impl TransformOp for RotationTransform {
///     fn apply(&self, image: &Tensor<f32>) -> Result<Tensor<f32>> {
///         // Rotation logic here
///         Ok(image.clone())
///     }
///
///     fn name(&self) -> &str {
///         "Rotation"
///     }
///
///     fn parameters(&self) -> Vec<Parameter> {
///         vec![Parameter {
///             name: "angle".to_string(),
///             value: self.angle,
///             min: 0.0,
///             max: 360.0,
///             step: 1.0,
///         }]
///     }
/// }
/// ```
pub trait TransformOp: Send + Sync {
    /// Apply the transformation to an input image
    ///
    /// # Arguments
    ///
    /// * `image` - Input image tensor with shape [C, H, W]
    ///
    /// # Returns
    ///
    /// Transformed image tensor with same or compatible shape
    ///
    /// # Errors
    ///
    /// Returns `VisionError` if transformation fails due to:
    /// - Invalid input tensor shape
    /// - Unsupported data types
    /// - Transform-specific parameter errors
    fn apply(&self, image: &Tensor<f32>) -> Result<Tensor<f32>>;

    /// Get the human-readable name of this transform
    fn name(&self) -> &str;

    /// Get the configurable parameters for this transform
    ///
    /// Returns a vector of parameters that can be adjusted through
    /// the interactive interface. Each parameter includes constraints
    /// and current values.
    fn parameters(&self) -> Vec<Parameter>;
}

/// Configurable parameter for interactive transforms
///
/// Represents a single adjustable parameter with constraints and metadata
/// for rendering in the web interface. Supports slider-based controls
/// with defined ranges and step sizes.
///
/// # Fields
///
/// - `name`: Parameter identifier and display name
/// - `value`: Current parameter value
/// - `min`/`max`: Valid value range
/// - `step`: Granularity for UI controls
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parameter {
    pub name: String,
    pub value: f32,
    pub min: f32,
    pub max: f32,
    pub step: f32,
}

impl InteractiveViewer {
    /// Create a new interactive viewer on specified port
    ///
    /// Initializes an empty viewer that can be configured with images,
    /// annotations, and transforms before starting the web server.
    ///
    /// # Arguments
    ///
    /// * `port` - TCP port number for the web server (e.g., 8080)
    ///
    /// # Examples
    ///
    /// ```rust
    /// let viewer = InteractiveViewer::new(8080);
    /// ```
    pub fn new(port: u16) -> Self {
        Self {
            port,
            current_image: None,
            annotations: Vec::new(),
            transforms: Vec::new(),
        }
    }

    /// Set the current image for viewing
    ///
    /// Validates and stores the input image for display and manipulation.
    /// The image must be a 3D tensor with shape [C, H, W] where:
    /// - C: Number of channels (typically 1 for grayscale, 3 for RGB)
    /// - H: Height in pixels
    /// - W: Width in pixels
    ///
    /// # Arguments
    ///
    /// * `image` - Input image tensor with shape [C, H, W]
    ///
    /// # Returns
    ///
    /// - `Ok(())` if image is valid and set successfully
    /// - `Err(VisionError::InvalidShape)` if tensor shape is invalid
    ///
    /// # Examples
    ///
    /// ```rust
    /// use torsh_tensor::Tensor;
    ///
    /// let mut viewer = InteractiveViewer::new(8080);
    /// let image = Tensor::zeros(&[3, 224, 224]).unwrap();
    /// viewer.set_image(image).unwrap();
    /// ```
    pub fn set_image(&mut self, image: Tensor<f32>) -> Result<()> {
        // Validate image format
        let shape = image.shape();
        if shape.dims().len() != 3 {
            return Err(VisionError::InvalidShape(format!(
                "Expected 3D tensor (C, H, W), got {}D",
                shape.dims().len()
            )));
        }

        self.current_image = Some(image);
        Ok(())
    }

    /// Add annotation to current image
    ///
    /// Appends a new annotation to the current collection. Annotations
    /// are preserved across image changes and transform operations.
    ///
    /// # Arguments
    ///
    /// * `annotation` - Annotation to add with position, type, and metadata
    ///
    /// # Examples
    ///
    /// ```rust
    /// let annotation = Annotation {
    ///     annotation_type: AnnotationType::BoundingBox,
    ///     coordinates: vec![10.0, 10.0, 100.0, 100.0],
    ///     label: "Object".to_string(),
    ///     color: (255, 0, 0),
    ///     confidence: Some(0.95),
    /// };
    /// viewer.add_annotation(annotation);
    /// ```
    pub fn add_annotation(&mut self, annotation: Annotation) {
        self.annotations.push(annotation);
    }

    /// Clear all annotations
    ///
    /// Removes all annotations from the viewer while preserving
    /// the current image and transforms.
    pub fn clear_annotations(&mut self) {
        self.annotations.clear();
    }

    /// Add interactive transform
    ///
    /// Appends a transform operation to the processing pipeline.
    /// Transforms are applied in the order they were added.
    ///
    /// # Arguments
    ///
    /// * `transform` - Boxed transform implementing the TransformOp trait
    ///
    /// # Examples
    ///
    /// ```rust
    /// struct BrightnessTransform { brightness: f32 }
    ///
    /// // Implementation of TransformOp trait required
    ///
    /// let transform = Box::new(BrightnessTransform { brightness: 0.2 });
    /// viewer.add_transform(transform);
    /// ```
    pub fn add_transform(&mut self, transform: Box<dyn TransformOp>) {
        self.transforms.push(transform);
    }

    /// Apply all current transforms to the image
    ///
    /// Sequentially applies all registered transforms to the current image,
    /// returning the final processed result. If no image is set, returns None.
    ///
    /// # Returns
    ///
    /// - `Ok(Some(tensor))` - Successfully processed image
    /// - `Ok(None)` - No current image set
    /// - `Err(VisionError)` - Transform operation failed
    ///
    /// # Transform Pipeline
    ///
    /// Transforms are applied in registration order:
    /// 1. Original image
    /// 2. Transform 1 → Intermediate result 1
    /// 3. Transform 2 → Intermediate result 2
    /// 4. ... → Final result
    ///
    /// # Examples
    ///
    /// ```rust
    /// match viewer.apply_transforms()? {
    ///     Some(processed_image) => {
    ///         // Use processed image
    ///     }
    ///     None => {
    ///         // No image set
    ///     }
    /// }
    /// ```
    pub fn apply_transforms(&self) -> Result<Option<Tensor<f32>>> {
        if let Some(ref image) = self.current_image {
            let mut result = image.clone();
            for transform in &self.transforms {
                result = transform.apply(&result)?;
            }
            Ok(Some(result))
        } else {
            Ok(None)
        }
    }

    /// Generate HTML interface for web-based viewing
    ///
    /// Creates a complete HTML document with embedded JavaScript for
    /// interactive image viewing, annotation management, and transform controls.
    ///
    /// # Features of Generated Interface
    ///
    /// - **Canvas Display**: Interactive image canvas with zoom/pan
    /// - **Annotation Tools**: Click-to-add annotations with type selection
    /// - **Transform Controls**: Sliders and inputs for transform parameters
    /// - **Export Functions**: Save images and export annotation data
    /// - **Real-time Updates**: Live preview of transform effects
    ///
    /// # Returns
    ///
    /// Complete HTML document as a string ready for serving or saving
    ///
    /// # Browser Compatibility
    ///
    /// Generated HTML uses standard web APIs compatible with modern browsers:
    /// - HTML5 Canvas for image rendering
    /// - ES6 JavaScript for interactivity
    /// - CSS3 for styling and layout
    ///
    /// # Examples
    ///
    /// ```rust
    /// let html = viewer.generate_html_interface();
    /// std::fs::write("viewer.html", html).unwrap();
    /// ```
    pub fn generate_html_interface(&self) -> String {
        let html = String::from(
            r#"
<!DOCTYPE html>
<html>
<head>
    <title>ToRSh Vision Interactive Viewer</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        .container { display: flex; gap: 20px; }
        .image-panel { flex: 2; }
        .control-panel { flex: 1; background: #f5f5f5; padding: 20px; border-radius: 8px; }
        .annotation { margin: 10px 0; padding: 10px; background: white; border-radius: 4px; }
        .transform { margin: 10px 0; padding: 10px; background: white; border-radius: 4px; }
        #canvas { border: 1px solid #ccc; max-width: 100%; }
        button { padding: 8px 16px; margin: 4px; background: #007bff; color: white; border: none; border-radius: 4px; cursor: pointer; }
        button:hover { background: #0056b3; }
        input[type="range"] { width: 100%; }
    </style>
</head>
<body>
    <h1>ToRSh Vision Interactive Viewer</h1>
    <div class="container">
        <div class="image-panel">
            <canvas id="canvas" width="800" height="600"></canvas>
            <div>
                <button onclick="clearAnnotations()">Clear Annotations</button>
                <button onclick="saveImage()">Save Image</button>
                <button onclick="exportData()">Export Data</button>
            </div>
        </div>
        <div class="control-panel">
            <h3>Annotations</h3>
            <div id="annotations"></div>

            <h3>Transforms</h3>
            <div id="transforms"></div>

            <h3>Add Annotation</h3>
            <select id="annotationType">
                <option value="BoundingBox">Bounding Box</option>
                <option value="Point">Point</option>
                <option value="Polygon">Polygon</option>
                <option value="Text">Text</option>
            </select>
            <input type="text" id="annotationLabel" placeholder="Label" />
            <button onclick="addAnnotation()">Add</button>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        let currentMode = 'view';
        let annotations = [];

        // Canvas event listeners for interactive annotation
        canvas.addEventListener('click', handleCanvasClick);
        canvas.addEventListener('mousemove', handleMouseMove);

        function handleCanvasClick(event) {
            const rect = canvas.getBoundingClientRect();
            const x = event.clientX - rect.left;
            const y = event.clientY - rect.top;

            if (currentMode === 'annotate') {
                createAnnotation(x, y);
            }
        }

        function handleMouseMove(event) {
            // Update cursor or preview based on current mode
        }

        function createAnnotation(x, y) {
            const type = document.getElementById('annotationType').value;
            const label = document.getElementById('annotationLabel').value || 'Annotation';

            const annotation = {
                type: type,
                x: x,
                y: y,
                label: label,
                color: [255, 0, 0]
            };

            annotations.push(annotation);
            updateAnnotationsList();
            redrawCanvas();
        }

        function updateAnnotationsList() {
            const container = document.getElementById('annotations');
            container.innerHTML = '';

            annotations.forEach((ann, index) => {
                const div = document.createElement('div');
                div.className = 'annotation';
                div.innerHTML = `
                    <strong>${ann.label}</strong> (${ann.type})<br>
                    Position: (${ann.x.toFixed(0)}, ${ann.y.toFixed(0)})<br>
                    <button onclick="removeAnnotation(${index})">Remove</button>
                `;
                container.appendChild(div);
            });
        }

        function removeAnnotation(index) {
            annotations.splice(index, 1);
            updateAnnotationsList();
            redrawCanvas();
        }

        function clearAnnotations() {
            annotations = [];
            updateAnnotationsList();
            redrawCanvas();
        }

        function redrawCanvas() {
            // Clear canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);

            // Draw image (would be loaded from server)
            // Draw annotations
            annotations.forEach(ann => {
                ctx.strokeStyle = `rgb(${ann.color[0]}, ${ann.color[1]}, ${ann.color[2]})`;
                ctx.lineWidth = 2;

                if (ann.type === 'Point') {
                    ctx.beginPath();
                    ctx.arc(ann.x, ann.y, 5, 0, 2 * Math.PI);
                    ctx.stroke();
                } else if (ann.type === 'BoundingBox') {
                    ctx.strokeRect(ann.x - 25, ann.y - 25, 50, 50);
                }

                // Draw label
                ctx.fillStyle = 'white';
                ctx.fillRect(ann.x, ann.y - 20, ctx.measureText(ann.label).width + 4, 16);
                ctx.fillStyle = 'black';
                ctx.fillText(ann.label, ann.x + 2, ann.y - 6);
            });
        }

        function addAnnotation() {
            currentMode = 'annotate';
            canvas.style.cursor = 'crosshair';
        }

        function saveImage() {
            // Implementation for saving the current view
            alert('Save functionality would be implemented here');
        }

        function exportData() {
            const data = {
                annotations: annotations,
                timestamp: new Date().toISOString()
            };

            const dataStr = JSON.stringify(data, null, 2);
            const dataBlob = new Blob([dataStr], {type: 'application/json'});
            const url = URL.createObjectURL(dataBlob);

            const link = document.createElement('a');
            link.href = url;
            link.download = 'annotations.json';
            link.click();
        }

        // Initialize
        redrawCanvas();
    </script>
</body>
</html>
        "#,
        );

        html
    }

    /// Start the interactive viewer server
    ///
    /// Initializes and starts the web server to serve the interactive interface.
    /// This is currently a placeholder implementation that would start an HTTP
    /// server with WebSocket support for real-time updates.
    ///
    /// # Server Features (Planned)
    ///
    /// - **HTTP Server**: Serves HTML interface and static assets
    /// - **WebSocket**: Real-time communication for live updates
    /// - **Image Streaming**: Efficient tensor-to-image conversion and streaming
    /// - **State Synchronization**: Sync annotations and transforms across clients
    ///
    /// # Returns
    ///
    /// - `Ok(())` - Server started successfully
    /// - `Err(VisionError)` - Server startup failed
    ///
    /// # Current Implementation
    ///
    /// The current implementation is a placeholder that prints server information
    /// and would be replaced with actual HTTP server logic in production.
    ///
    /// # Examples
    ///
    /// ```rust
    /// let viewer = InteractiveViewer::new(8080);
    /// viewer.start_server().unwrap();
    /// // Navigate to http://localhost:8080 in browser
    /// ```
    pub fn start_server(&self) -> Result<()> {
        println!("Interactive viewer would start on port {}", self.port);
        println!(
            "HTML interface generated with {} annotations",
            self.annotations.len()
        );

        // In a real implementation, this would start an HTTP server
        // serving the HTML interface and handling WebSocket connections
        // for real-time updates

        Ok(())
    }
}

/// Create an interactive viewer for real-time image manipulation
///
/// Convenience function to create a new InteractiveViewer with default settings.
/// Equivalent to calling `InteractiveViewer::new(port)` directly.
///
/// # Arguments
///
/// * `port` - TCP port number for the web server
///
/// # Returns
///
/// New InteractiveViewer instance ready for configuration
///
/// # Examples
///
/// ```rust
/// let viewer = create_interactive_viewer(8080);
/// ```
pub fn create_interactive_viewer(port: u16) -> InteractiveViewer {
    InteractiveViewer::new(port)
}

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

    #[test]
    fn test_interactive_viewer_creation() {
        let viewer = InteractiveViewer::new(8080);
        assert_eq!(viewer.port, 8080);
        assert!(viewer.current_image.is_none());
        assert!(viewer.annotations.is_empty());
        assert!(viewer.transforms.is_empty());
    }

    #[test]
    fn test_annotation_management() {
        let mut viewer = InteractiveViewer::new(8080);

        let annotation = Annotation {
            annotation_type: AnnotationType::BoundingBox,
            coordinates: vec![10.0, 10.0, 50.0, 50.0],
            label: "Test".to_string(),
            color: (255, 0, 0),
            confidence: Some(0.95),
        };

        viewer.add_annotation(annotation);
        assert_eq!(viewer.annotations.len(), 1);

        viewer.clear_annotations();
        assert!(viewer.annotations.is_empty());
    }

    #[test]
    fn test_html_generation() {
        let viewer = InteractiveViewer::new(8080);
        let html = viewer.generate_html_interface();

        assert!(html.contains("<!DOCTYPE html>"));
        assert!(html.contains("ToRSh Vision Interactive Viewer"));
        assert!(html.contains("<canvas"));
    }

    #[test]
    fn test_parameter_serialization() {
        let param = Parameter {
            name: "brightness".to_string(),
            value: 0.5,
            min: 0.0,
            max: 1.0,
            step: 0.01,
        };

        let json = serde_json::to_string(&param).expect("serde json should succeed");
        let deserialized: Parameter =
            serde_json::from_str(&json).expect("serde json should succeed");

        assert_eq!(param.name, deserialized.name);
        assert_eq!(param.value, deserialized.value);
    }

    #[test]
    fn test_annotation_types() {
        let annotation_types = vec![
            AnnotationType::BoundingBox,
            AnnotationType::Point,
            AnnotationType::Polygon,
            AnnotationType::Mask,
            AnnotationType::Text,
            AnnotationType::Arrow,
        ];

        for annotation_type in annotation_types {
            let annotation = Annotation {
                annotation_type: annotation_type.clone(),
                coordinates: vec![0.0, 0.0],
                label: "Test".to_string(),
                color: (255, 255, 255),
                confidence: None,
            };

            // Test serialization
            let json = serde_json::to_string(&annotation).expect("serde json should succeed");
            let _deserialized: Annotation =
                serde_json::from_str(&json).expect("serde json should succeed");
        }
    }
}