Spaces:
Sleeping
Sleeping
| use serde::{Deserialize, Serialize}; | |
| use uuid::Uuid; | |
| use utoipa::ToSchema; | |
| /// Model size selection for SAM2. | |
| /// Larger models are slower but more accurate. | |
| pub enum Sam2ModelSize { | |
| /// Fastest and smallest model. | |
| Tiny, | |
| /// Small model: balance of speed and accuracy. | |
| Small, | |
| /// Base+ model: higher accuracy. | |
| BasePlus, | |
| /// Largest and most accurate model. | |
| Large, | |
| } | |
| impl Sam2ModelSize { | |
| pub fn to_filename(&self) -> &'static str { | |
| match self { | |
| Sam2ModelSize::Tiny => "sam2_tiny.onnx", | |
| Sam2ModelSize::Small => "sam2_small.onnx", | |
| Sam2ModelSize::BasePlus => "sam2_base_plus.onnx", | |
| Sam2ModelSize::Large => "sam2_large.onnx", | |
| } | |
| } | |
| } | |
| /// Prompt point in model input space (1024x1024). | |
| pub struct Point { | |
| /// X coordinate in model space [0, 1024). | |
| pub x: f32, | |
| /// Y coordinate in model space [0, 1024). | |
| pub y: f32, | |
| /// Point label: 1 = positive (foreground), 0 = negative (background). | |
| pub label: i32, | |
| } | |
| /// Request body for segmentation. | |
| pub struct SegmentRequest { | |
| /// Optional client-supplied correlation ID. If not provided the server will generate one. | |
| pub request_id: Option<Uuid>, | |
| /// Model size to use for inference. | |
| pub model: Sam2ModelSize, | |
| /// Base64-encoded PNG or JPEG image data at original resolution. | |
| pub image_b64: String, | |
| /// Positive/negative prompt points in 1024x1024 model space. | |
| ))] | |
| pub points: Vec<Point>, | |
| /// Optional probability threshold (0..1). Default is 0.5. | |
| pub threshold: Option<f32>, | |
| } | |
| /// Segmentation result payload. | |
| pub struct SegmentResponse { | |
| /// Echoed or generated request ID for tracing. | |
| pub request_id: Uuid, | |
| /// Model actually used. | |
| pub model: Sam2ModelSize, | |
| /// IoU predictions for the 3 mask candidates. | |
| ))] | |
| pub iou: [f32; 3], | |
| /// Index of the best mask candidate by IoU (0..2). | |
| pub best_idx: usize, | |
| /// Wall-clock inference time in milliseconds. | |
| pub inference_ms: u128, | |
| /// 1024x1024 RGBA overlay PNG (blue fill/outline, transparent elsewhere). | |
| pub mask_png_b64: String, | |
| /// Original-resolution RGBA PNG where pixels outside the mask are transparent. | |
| pub masked_region_png_b64: Option<String>, | |
| } | |
| /// Available models response. | |
| pub struct ModelsResponse { | |
| /// List of available model sizes. | |
| ))] | |
| pub models: Vec<Sam2ModelSize>, | |
| } | |