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(vision): graceful error handling in macOS OCR pipeline

Louis Beaumont74f101e6db43
diff --git a/Cargo.lock b/Cargo.lock
index 07a3ad79b..fecf97ce5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6628,7 +6628,7 @@ dependencies = [
[[package]]
name = "screenpipe-audio"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"anyhow",
"bytemuck",
@@ -6681,7 +6681,7 @@ dependencies = [
[[package]]
name = "screenpipe-core"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"accessibility",
"accessibility-sys",
@@ -6732,7 +6732,7 @@ dependencies = [
[[package]]
name = "screenpipe-db"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"anyhow",
"chrono",
@@ -6756,7 +6756,7 @@ dependencies = [
[[package]]
name = "screenpipe-events"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"anyhow",
"chrono",
@@ -6774,7 +6774,7 @@ dependencies = [
[[package]]
name = "screenpipe-integrations"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"anyhow",
"image",
@@ -6789,7 +6789,7 @@ dependencies = [
[[package]]
name = "screenpipe-server"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"anyhow",
"async-trait",
@@ -6858,7 +6858,7 @@ dependencies = [
[[package]]
name = "screenpipe-vision"
-version = "0.3.39"
+version = "0.3.41"
dependencies = [
"accessibility-sys",
"anyhow",
diff --git a/Cargo.toml b/Cargo.toml
index a8d032aa3..dab7aaaf5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,7 +6,7 @@ exclude = ["apps/screenpipe-app-tauri/src-tauri"]
resolver = "2"
[workspace.package]
-version = "0.3.41"
+version = "0.3.42"
authors = ["louis030195 <[email protected]>"]
description = ""
repository = "https://github.com/screenpipe/screenpipe"
diff --git a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
index 015118cb0..eb13fa163 100644
--- a/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "2.0.247"
+version = "2.0.248"
description = "24/7 memory for your desktop. 100% local."
authors = ["you"]
license = ""
diff --git a/crates/screenpipe-vision/src/apple.rs b/crates/screenpipe-vision/src/apple.rs
index 1d9d8d730..2b0a67b96 100644
--- a/crates/screenpipe-vision/src/apple.rs
+++ b/crates/screenpipe-vision/src/apple.rs
@@ -95,12 +95,18 @@ pub fn perform_ocr_apple(
Some(overall_confidence),
);
+ // Guard against zero-dimension images that would cause CoreVideo errors
+ if width == 0 || height == 0 {
+ error!("Cannot perform OCR on zero-dimension image ({}x{})", width, height);
+ return default_ocr_result;
+ }
+
let width = usize::try_from(width).unwrap();
let height = usize::try_from(height).unwrap();
let mut pixel_buf_out = None;
- let pixel_buf = unsafe {
+ let pixel_buf = match unsafe {
PixelBuf::create_with_bytes_in(
width,
height,
@@ -114,10 +120,21 @@ pub fn perform_ocr_apple(
None,
)
.to_result_unchecked(pixel_buf_out)
- }
- .unwrap();
+ } {
+ Ok(buf) => buf,
+ Err(e) => {
+ error!("Failed to create pixel buffer for OCR ({}x{}): {:?}", width, height, e);
+ return default_ocr_result;
+ }
+ };
- let handler = ImageRequestHandler::with_cv_pixel_buf(&pixel_buf, None).unwrap();
+ let handler = match ImageRequestHandler::with_cv_pixel_buf(&pixel_buf, None) {
+ Some(h) => h,
+ None => {
+ error!("Failed to create image request handler for OCR");
+ return default_ocr_result;
+ }
+ };
let mut request = RecognizeTextRequest::new();
request.set_recognition_langs(&languages_array);
request.set_uses_lang_correction(false);
@@ -133,13 +150,19 @@ pub fn perform_ocr_apple(
let mut ocr_results_vec: Vec<serde_json::Value> = Vec::new();
let mut ocr_text: String = String::new();
results.iter().for_each(|result| {
- let observation_result = result.top_candidates(1).get(0).unwrap();
+ let Ok(observation_result) = result.top_candidates(1).get(0) else {
+ return;
+ };
let text = observation_result.string();
let confidence = observation_result.confidence() as f64;
- let bbox = observation_result
- .bounding_box_for_range(ns::Range::new(0, text.len()))
- .unwrap()
- .bounding_box();
+ let Ok(bbox_result) = observation_result
+ .bounding_box_for_range(ns::Range::new(0, text.len())) else {
+ // Skip bounding box if it fails, still capture the text
+ overall_confidence += confidence;
+ ocr_text.push_str(text.to_string().as_str());
+ return;
+ };
+ let bbox = bbox_result.bounding_box();
let x = bbox.origin.x;
let y = bbox.origin.y;
let height = bbox.size.height;