Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: normalize OCR bounding boxes to 0-1 range on Windows and Linux

louis030195aba745133998
diff --git a/crates/screenpipe-server/src/routes/frames.rs b/crates/screenpipe-server/src/routes/frames.rs
index be5933fb9..47d7080ab 100644
--- a/crates/screenpipe-server/src/routes/frames.rs
+++ b/crates/screenpipe-server/src/routes/frames.rs
@@ -819,17 +819,28 @@ pub async fn run_frame_ocr(
};
// Run OCR on the image
+ #[cfg(target_os = "macos")]
let ocr_result = tokio::task::spawn_blocking(move || {
- #[cfg(target_os = "macos")]
- {
- let (text, json, _confidence) = screenpipe_vision::perform_ocr_apple(&image, &[]);
- (text, json)
- }
- #[cfg(not(target_os = "macos"))]
- {
- let _ = image;
+ let (text, json, _confidence) = screenpipe_vision::perform_ocr_apple(&image, &[]);
+ (text, json)
+ })
+ .await
+ .unwrap_or_else(|_| (String::new(), "[]".to_string()));
+
+ #[cfg(target_os = "windows")]
+ let ocr_result = match screenpipe_vision::perform_ocr_windows(&image).await {
+ Ok((text, json, _confidence)) => (text, json),
+ Err(e) => {
+ error!("Windows on-demand OCR failed: {}", e);
(String::new(), "[]".to_string())
}
+ };
+
+ #[cfg(not(any(target_os = "macos", target_os = "windows")))]
+ let ocr_result = tokio::task::spawn_blocking(move || {
+ let (text, json, _confidence) =
+ screenpipe_vision::perform_ocr_tesseract(&image, vec![]);
+ (text, json)
})
.await
.unwrap_or_else(|_| (String::new(), "[]".to_string()));
@@ -838,7 +849,12 @@ pub async fn run_frame_ocr(
// Store in DB for future reads (ignore errors — the result is still returned)
if !ocr_text.is_empty() {
+ #[cfg(target_os = "macos")]
let engine = Arc::new(screenpipe_db::OcrEngine::AppleNative);
+ #[cfg(target_os = "windows")]
+ let engine = Arc::new(screenpipe_db::OcrEngine::WindowsNative);
+ #[cfg(not(any(target_os = "macos", target_os = "windows")))]
+ let engine = Arc::new(screenpipe_db::OcrEngine::Tesseract);
if let Err(e) = state
.db
.insert_ocr_text(frame_id, &ocr_text, &ocr_text_json, engine)
diff --git a/crates/screenpipe-vision/src/microsoft.rs b/crates/screenpipe-vision/src/microsoft.rs
index d21884df2..9689e4cc7 100644
--- a/crates/screenpipe-vision/src/microsoft.rs
+++ b/crates/screenpipe-vision/src/microsoft.rs
@@ -54,14 +54,16 @@ pub async fn perform_ocr_windows(image: &DynamicImage) -> Result<(String, String
}
full_text.push_str(&text_str);
- // Get bounding box for PII redaction support
+ // Get bounding box and normalize to 0-1 range (matching Apple Vision output)
let rect = word.BoundingRect()?;
+ let img_w = width as f32;
+ let img_h = height as f32;
ocr_results.push(serde_json::json!({
"text": text_str,
- "left": rect.X.to_string(),
- "top": rect.Y.to_string(),
- "width": rect.Width.to_string(),
- "height": rect.Height.to_string(),
+ "left": (rect.X / img_w).to_string(),
+ "top": (rect.Y / img_h).to_string(),
+ "width": (rect.Width / img_w).to_string(),
+ "height": (rect.Height / img_h).to_string(),
"conf": "1.0" // Windows OCR doesn't provide word-level confidence
}));
}
diff --git a/crates/screenpipe-vision/src/tesseract.rs b/crates/screenpipe-vision/src/tesseract.rs
index ae6a977c3..fc55d37d3 100644
--- a/crates/screenpipe-vision/src/tesseract.rs
+++ b/crates/screenpipe-vision/src/tesseract.rs
@@ -1,4 +1,4 @@
-use image::DynamicImage;
+use image::{DynamicImage, GenericImageView};
use rusty_tesseract::{Args, DataOutput, Image};
use screenpipe_core::{Language, TESSERACT_LANGUAGES};
use std::collections::HashMap;
@@ -72,7 +72,8 @@ pub fn perform_ocr_tesseract(
// Extract text from data output
let text = data_output_to_text(&data_output);
- let json_output = data_output_to_json(&data_output);
+ let (img_w, img_h) = image.dimensions();
+ let json_output = data_output_to_json(&data_output, img_w, img_h);
let overall_confidence = calculate_overall_confidence(&data_output);
@@ -92,8 +93,10 @@ fn data_output_to_text(data_output: &DataOutput) -> String {
text
}
-fn data_output_to_json(data_output: &DataOutput) -> String {
+fn data_output_to_json(data_output: &DataOutput, img_w: u32, img_h: u32) -> String {
let mut words: Vec<HashMap<String, String>> = Vec::new();
+ let w = if img_w > 0 { img_w as f64 } else { 1.0 };
+ let h = if img_h > 0 { img_h as f64 } else { 1.0 };
for record in &data_output.data {
// Only include records that have text (word_num > 0 means it's a word)
@@ -101,11 +104,11 @@ fn data_output_to_json(data_output: &DataOutput) -> String {
let mut word_data = HashMap::new();
word_data.insert("text".to_string(), record.text.clone());
word_data.insert("conf".to_string(), format!("{:.2}", record.conf));
- // Include bounding box coordinates for PII redaction
- word_data.insert("left".to_string(), record.left.to_string());
- word_data.insert("top".to_string(), record.top.to_string());
- word_data.insert("width".to_string(), record.width.to_string());
- word_data.insert("height".to_string(), record.height.to_string());
+ // Normalize bounding box coordinates to 0-1 range (matching Apple Vision output)
+ word_data.insert("left".to_string(), (record.left as f64 / w).to_string());
+ word_data.insert("top".to_string(), (record.top as f64 / h).to_string());
+ word_data.insert("width".to_string(), (record.width as f64 / w).to_string());
+ word_data.insert("height".to_string(), (record.height as f64 / h).to_string());
// Include position metadata
word_data.insert("level".to_string(), record.level.to_string());
word_data.insert("page_num".to_string(), record.page_num.to_string());