π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
mvp face thumbnail generation in rust
laurenspriemfe5ed4fd2c3d
diff --git a/mobile/apps/photos/lib/services/machine_learning/face_thumbnail_generator.dart b/mobile/apps/photos/lib/services/machine_learning/face_thumbnail_generator.dartindex 6e09744cff..e195c148a4 100644--- a/mobile/apps/photos/lib/services/machine_learning/face_thumbnail_generator.dart+++ b/mobile/apps/photos/lib/services/machine_learning/face_thumbnail_generator.dart@@ -3,6 +3,7 @@ import 'dart:typed_data' show Uint8List;import "package:logging/logging.dart";import "package:photos/models/ml/face/box.dart";+import "package:photos/service_locator.dart";import "package:photos/utils/image_ml_util.dart";import "package:photos/utils/isolate/isolate_operations.dart";import "package:photos/utils/isolate/super_isolate.dart";@@ -14,7 +15,7 @@ class FaceThumbnailGenerator extends SuperIsolate {final _logger = Logger('FaceThumbnailGenerator');@override- bool get isDartUiIsolate => true;+ bool get isDartUiIsolate => !flagService.useRustForFaceThumbnails;@overrideString get isolateName => "FaceThumbnailGenerator";@@ -46,6 +47,7 @@ class FaceThumbnailGenerator extends SuperIsolate {{'imagePath': imagePath,'faceBoxesList': faceBoxesJson,+ 'useRustForFaceThumbnails': flagService.useRustForFaceThumbnails,},).then((value) => value.cast<Uint8List>());_logger.info("Generated face thumbnails");diff --git a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dartindex 2724803833..c2c7a63b29 100644--- a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart+++ b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart@@ -12,6 +12,8 @@ import "package:photos/services/machine_learning/ml_result.dart";import "package:photos/services/machine_learning/semantic_search/clip/clip_text_encoder.dart";import "package:photos/services/machine_learning/semantic_search/clip/clip_text_tokenizer.dart";import "package:photos/services/machine_learning/semantic_search/query_result.dart";+import "package:photos/src/rust/api/image_processing_api.dart"+ as rust_image_processing;import "package:photos/src/rust/api/ml_indexing_api.dart" as rust_ml;import "package:photos/src/rust/frb_generated.dart" show EntePhotosRust;import "package:photos/utils/image_ml_util.dart";@@ -154,9 +156,30 @@ Future<dynamic> isolateFunction(/// MLComputercase IsolateOperation.generateFaceThumbnails:final imagePath = args['imagePath'] as String;+ final useRustForFaceThumbnails =+ args['useRustForFaceThumbnails'] as bool? ?? false;final faceBoxesJson = args['faceBoxesList'] as List<Map<String, dynamic>>;final List<FaceBox> faceBoxes =faceBoxesJson.map((json) => FaceBox.fromJson(json)).toList();+ if (useRustForFaceThumbnails) {+ await _ensureRustLoaded();+ final rustFaceBoxes = faceBoxes+ .map(+ (box) => rust_image_processing.RustFaceBox(+ x: box.x,+ y: box.y,+ width: box.width,+ height: box.height,+ ),+ )+ .toList(growable: false);+ final List<Uint8List> results =+ await rust_image_processing.generateFaceThumbnails(+ imagePath: imagePath,+ faceBoxes: rustFaceBoxes,+ );+ return List.from(results);+ }final List<Uint8List> results = await generateFaceThumbnailsUsingCanvas(imagePath,faceBoxes,diff --git a/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart b/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dartindex f67b31b1eb..2309a58c2e 100644--- a/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart+++ b/mobile/apps/photos/plugins/ente_feature_flag/lib/src/service.dart@@ -104,6 +104,8 @@ class FlagService {bool get useRustForML => internalUser;+ bool get useRustForFaceThumbnails => internalUser;+Future<void> tryRefreshFlags() async {try {await _fetch();diff --git a/mobile/apps/photos/rust/src/api/image_processing_api.rs b/mobile/apps/photos/rust/src/api/image_processing_api.rsnew file mode 100644index 0000000000..319961d412--- /dev/null+++ b/mobile/apps/photos/rust/src/api/image_processing_api.rs@@ -0,0 +1,53 @@+use crate::ml::{+ decode::decode_image_from_path,+ face::thumbnail::{FaceBox, generate_face_thumbnails as generate_face_thumbnails_impl},+};++#[derive(Clone, Debug)]+pub struct RustFaceBox {+ pub x: f64,+ pub y: f64,+ pub width: f64,+ pub height: f64,+}++pub fn generate_face_thumbnails(+ image_path: String,+ face_boxes: Vec<RustFaceBox>,+) -> Result<Vec<Vec<u8>>, String> {+ let decoded = decode_image_from_path(&image_path).map_err(|e| e.to_string())?;+ let face_boxes = face_boxes+ .into_iter()+ .enumerate()+ .map(|(index, face_box)| {+ FaceBox::try_from(face_box)+ .map_err(|e| format!("invalid face box at index {index}: {e}"))+ })+ .collect::<Result<Vec<_>, _>>()?;++ generate_face_thumbnails_impl(&decoded, &face_boxes).map_err(|e| e.to_string())+}++impl TryFrom<RustFaceBox> for FaceBox {+ type Error = String;++ fn try_from(value: RustFaceBox) -> Result<Self, Self::Error> {+ if !value.x.is_finite()+ || !value.y.is_finite()+ || !value.width.is_finite()+ || !value.height.is_finite()+ {+ return Err("non-finite values are not allowed".to_string());+ }+ if value.width <= 0.0 || value.height <= 0.0 {+ return Err("width and height must be greater than 0".to_string());+ }++ Ok(Self {+ x: value.x as f32,+ y: value.y as f32,+ width: value.width as f32,+ height: value.height as f32,+ })+ }+}diff --git a/mobile/apps/photos/rust/src/api/mod.rs b/mobile/apps/photos/rust/src/api/mod.rsindex 1c0956f642..0114b2665e 100644--- a/mobile/apps/photos/rust/src/api/mod.rs+++ b/mobile/apps/photos/rust/src/api/mod.rs@@ -1,3 +1,4 @@+pub mod image_processing_api;pub mod init;pub mod ml_indexing_api;pub mod motion_photo_api;diff --git a/mobile/apps/photos/rust/src/ml/face/align.rs b/mobile/apps/photos/rust/src/ml/face/align.rsindex 5c52d7b73a..4c4dbba9ae 100644--- a/mobile/apps/photos/rust/src/ml/face/align.rs+++ b/mobile/apps/photos/rust/src/ml/face/align.rs@@ -351,8 +351,7 @@ fn pad_image_for_direction(image: &[Vec<i32>], direction: FaceDirection) -> Vec<}{let (upper_rows, bottom_rows) = padded.split_at_mut(rows + 1);- bottom_rows[0][1..=copy_cols]- .copy_from_slice(&upper_rows[rows - 1][1..=copy_cols]);+ bottom_rows[0][1..=copy_cols].copy_from_slice(&upper_rows[rows - 1][1..=copy_cols]);}}for row in padded.iter_mut().take(rows + 2) {diff --git a/mobile/apps/photos/rust/src/ml/face/mod.rs b/mobile/apps/photos/rust/src/ml/face/mod.rsindex 470bf02d7f..88903b37eb 100644--- a/mobile/apps/photos/rust/src/ml/face/mod.rs+++ b/mobile/apps/photos/rust/src/ml/face/mod.rs@@ -1,3 +1,4 @@pub mod align;pub mod detect;pub mod embed;+pub mod thumbnail;diff --git a/mobile/apps/photos/rust/src/ml/face/thumbnail.rs b/mobile/apps/photos/rust/src/ml/face/thumbnail.rsnew file mode 100644index 0000000000..ad409d4068--- /dev/null+++ b/mobile/apps/photos/rust/src/ml/face/thumbnail.rs@@ -0,0 +1,297 @@+use image::{ColorType, ImageBuffer, ImageEncoder, Rgb, RgbImage, codecs::png::PngEncoder};++use crate::ml::{+ error::{MlError, MlResult},+ types::DecodedImage,+};++const REGULAR_PADDING: f64 = 0.4;+const MINIMUM_PADDING: f64 = 0.1;++#[derive(Clone, Debug)]+pub struct FaceBox {+ pub x: f32,+ pub y: f32,+ pub width: f32,+ pub height: f32,+}++#[derive(Clone, Debug)]+struct CropRect {+ x: f64,+ y: f64,+ width: f64,+ height: f64,+ output_width: u32,+ output_height: u32,+}++pub fn generate_face_thumbnails(+ decoded: &DecodedImage,+ face_boxes: &[FaceBox],+) -> MlResult<Vec<Vec<u8>>> {+ if face_boxes.is_empty() {+ return Ok(Vec::new());+ }+ if decoded.dimensions.width == 0 || decoded.dimensions.height == 0 {+ return Err(MlError::Decode(+ "decoded image dimensions cannot be empty".to_string(),+ ));+ }++ let source = rgb_image_from_decoded(decoded)?;+ let image_width = decoded.dimensions.width as f64;+ let image_height = decoded.dimensions.height as f64;+ let mut results = Vec::with_capacity(face_boxes.len());++ for (index, face_box) in face_boxes.iter().enumerate() {+ let crop = compute_crop_rect(face_box, image_width, image_height).map_err(|e| {+ MlError::InvalidRequest(format!("invalid face box at index {index}: {e}",))+ })?;+ let png_bytes = crop_and_encode_png(&source, &crop)?;+ results.push(png_bytes);+ }++ Ok(results)+}++fn rgb_image_from_decoded(decoded: &DecodedImage) -> MlResult<RgbImage> {+ ImageBuffer::<Rgb<u8>, _>::from_raw(+ decoded.dimensions.width,+ decoded.dimensions.height,+ decoded.rgb.clone(),+ )+ .ok_or_else(|| MlError::Decode("invalid decoded RGB buffer".to_string()))+}++fn compute_crop_rect(+ face_box: &FaceBox,+ image_width: f64,+ image_height: f64,+) -> Result<CropRect, String> {+ if image_width <= 0.0 || image_height <= 0.0 {+ return Err("image dimensions must be positive".to_string());+ }++ let x_min_abs = face_box.x as f64 * image_width;+ let y_min_abs = face_box.y as f64 * image_height;+ let width_abs = face_box.width as f64 * image_width;+ let height_abs = face_box.height as f64 * image_height;++ if width_abs <= 0.0 || height_abs <= 0.0 {+ return Err("face box width/height must map to positive absolute dimensions".to_string());+ }++ let x_crop = x_min_abs - width_abs * REGULAR_PADDING;+ let x_overshoot = x_crop.min(0.0).abs() / width_abs;+ let width_crop = width_abs * (1.0 + 2.0 * REGULAR_PADDING)+ - 2.0 * x_overshoot.min(REGULAR_PADDING - MINIMUM_PADDING) * width_abs;++ let y_crop = y_min_abs - height_abs * REGULAR_PADDING;+ let y_overshoot = y_crop.min(0.0).abs() / height_abs;+ let height_crop = height_abs * (1.0 + 2.0 * REGULAR_PADDING)+ - 2.0 * y_overshoot.min(REGULAR_PADDING - MINIMUM_PADDING) * height_abs;++ let x_crop_safe = x_crop.clamp(0.0, image_width);+ let y_crop_safe = y_crop.clamp(0.0, image_height);+ let width_crop_safe = width_crop.clamp(0.0, (image_width - x_crop_safe).max(0.0));+ let height_crop_safe = height_crop.clamp(0.0, (image_height - y_crop_safe).max(0.0));++ let output_width = float_to_output_dimension(width_crop_safe)+ .ok_or_else(|| format!("invalid output width from crop value {width_crop_safe}"))?;+ let output_height = float_to_output_dimension(height_crop_safe)+ .ok_or_else(|| format!("invalid output height from crop value {height_crop_safe}"))?;++ if output_width == 0 || output_height == 0 {+ return Err(format!(+ "crop dimensions resolve to zero-sized output ({output_width}x{output_height})",+ ));+ }++ Ok(CropRect {+ x: x_crop_safe,+ y: y_crop_safe,+ width: width_crop_safe,+ height: height_crop_safe,+ output_width,+ output_height,+ })+}++fn float_to_output_dimension(value: f64) -> Option<u32> {+ if !value.is_finite() || value < 0.0 {+ return None;+ }+ let truncated = value.trunc();+ if truncated > u32::MAX as f64 {+ return None;+ }+ Some(truncated as u32)+}++fn crop_and_encode_png(source: &RgbImage, crop: &CropRect) -> MlResult<Vec<u8>> {+ let mut cropped = ImageBuffer::<Rgb<u8>, Vec<u8>>::new(crop.output_width, crop.output_height);++ let scale_x = crop.width / f64::from(crop.output_width);+ let scale_y = crop.height / f64::from(crop.output_height);++ for y in 0..crop.output_height {+ for x in 0..crop.output_width {+ let source_x = crop.x + (f64::from(x) + 0.5) * scale_x - 0.5;+ let source_y = crop.y + (f64::from(y) + 0.5) * scale_y - 0.5;+ let rgb = sample_bilinear(source, source_x, source_y);+ cropped.put_pixel(x, y, Rgb(rgb));+ }+ }++ let mut png_bytes = Vec::new();+ PngEncoder::new(&mut png_bytes)+ .write_image(+ cropped.as_raw(),+ crop.output_width,+ crop.output_height,+ ColorType::Rgb8.into(),+ )+ .map_err(|e| MlError::Postprocess(format!("failed to encode PNG thumbnail: {e}")))?;+ Ok(png_bytes)+}++fn sample_bilinear(image: &RgbImage, x: f64, y: f64) -> [u8; 3] {+ let max_x = image.width().saturating_sub(1) as f64;+ let max_y = image.height().saturating_sub(1) as f64;+ let fx = x.clamp(0.0, max_x);+ let fy = y.clamp(0.0, max_y);++ let x0 = fx.floor() as u32;+ let x1 = fx.ceil() as u32;+ let y0 = fy.floor() as u32;+ let y1 = fy.ceil() as u32;++ let dx = fx - f64::from(x0);+ let dy = fy - f64::from(y0);+ let dx1 = 1.0 - dx;+ let dy1 = 1.0 - dy;++ let p1 = image.get_pixel(x0, y0).0;+ let p2 = image.get_pixel(x1, y0).0;+ let p3 = image.get_pixel(x0, y1).0;+ let p4 = image.get_pixel(x1, y1).0;++ let bilinear = |c1: u8, c2: u8, c3: u8, c4: u8| -> u8 {+ let value = f64::from(c1) * dx1 * dy1+ + f64::from(c2) * dx * dy1+ + f64::from(c3) * dx1 * dy+ + f64::from(c4) * dx * dy;+ value.round().clamp(0.0, 255.0) as u8+ };++ [+ bilinear(p1[0], p2[0], p3[0], p4[0]),+ bilinear(p1[1], p2[1], p3[1], p4[1]),+ bilinear(p1[2], p2[2], p3[2], p4[2]),+ ]+}++#[cfg(test)]+mod tests {+ use image::ImageFormat;++ use super::{FaceBox, compute_crop_rect, generate_face_thumbnails};+ use crate::ml::types::{DecodedImage, Dimensions};++ #[test]+ fn compute_crop_rect_matches_canvas_math_for_center_box() {+ let face_box = FaceBox {+ x: 0.25,+ y: 0.25,+ width: 0.5,+ height: 0.5,+ };++ let crop = compute_crop_rect(&face_box, 100.0, 80.0).expect("crop should be valid");++ assert!((crop.x - 5.0).abs() < f64::EPSILON);+ assert!((crop.y - 4.0).abs() < f64::EPSILON);+ assert!((crop.width - 90.0).abs() < f64::EPSILON);+ assert!((crop.height - 72.0).abs() < f64::EPSILON);+ assert_eq!(crop.output_width, 90);+ assert_eq!(crop.output_height, 72);+ }++ #[test]+ fn compute_crop_rect_handles_left_edge_overshoot_like_canvas() {+ let face_box = FaceBox {+ x: 0.0,+ y: 0.2,+ width: 0.2,+ height: 0.2,+ };++ let crop = compute_crop_rect(&face_box, 100.0, 100.0).expect("crop should be valid");++ assert!(crop.x.abs() < 1e-6);+ assert!((crop.width - 24.0).abs() < 1e-5);+ assert_eq!(crop.output_width, 24);+ }++ #[test]+ fn generate_face_thumbnails_returns_png_per_input_face() {+ let decoded = synthetic_decoded_image(16, 16);+ let face_boxes = vec![+ FaceBox {+ x: 0.1,+ y: 0.1,+ width: 0.4,+ height: 0.4,+ },+ FaceBox {+ x: 0.4,+ y: 0.2,+ width: 0.3,+ height: 0.5,+ },+ ];++ let thumbnails =+ generate_face_thumbnails(&decoded, &face_boxes).expect("thumbnails should generate");++ assert_eq!(thumbnails.len(), 2);+ for bytes in thumbnails {+ assert!(!bytes.is_empty());+ let decoded_png = image::load_from_memory_with_format(&bytes, ImageFormat::Png)+ .expect("thumbnail bytes should decode as PNG");+ assert!(decoded_png.width() > 0);+ assert!(decoded_png.height() > 0);+ }+ }++ #[test]+ fn generate_face_thumbnails_fails_for_zero_sized_face_box() {+ let decoded = synthetic_decoded_image(16, 16);+ let face_boxes = vec![FaceBox {+ x: 0.1,+ y: 0.2,+ width: 0.0,+ height: 0.3,+ }];++ let result = generate_face_thumbnails(&decoded, &face_boxes);++ assert!(result.is_err());+ }++ fn synthetic_decoded_image(width: u32, height: u32) -> DecodedImage {+ let mut rgb = Vec::with_capacity((width * height * 3) as usize);+ for y in 0..height {+ for x in 0..width {+ rgb.push((x % 256) as u8);+ rgb.push((y % 256) as u8);+ rgb.push(((x + y) % 256) as u8);+ }+ }+ DecodedImage {+ dimensions: Dimensions { width, height },+ rgb,+ }+ }+}