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: ocr panicking

Joe Goldin8a05df05c9ae
diff --git a/screenpipe-vision/src/core.rs b/screenpipe-vision/src/core.rs
index 2f395d732..e0bee4611 100644
--- a/screenpipe-vision/src/core.rs
+++ b/screenpipe-vision/src/core.rs
@@ -195,7 +195,9 @@ pub async fn process_ocr_task(
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
OcrEngine::Tesseract => perform_ocr_tesseract(&window_image),
#[cfg(target_os = "windows")]
- OcrEngine::WindowsNative => perform_ocr_windows(&window_image).await,
+ OcrEngine::WindowsNative => perform_ocr_windows(&window_image)
+ .await
+ .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?,
#[cfg(target_os = "macos")]
OcrEngine::AppleNative => parse_apple_ocr_result(&perform_ocr_apple(&window_image)),
_ => {
diff --git a/screenpipe-vision/src/microsoft.rs b/screenpipe-vision/src/microsoft.rs
index 1d95b2ed3..30a1ea63a 100644
--- a/screenpipe-vision/src/microsoft.rs
+++ b/screenpipe-vision/src/microsoft.rs
@@ -1,6 +1,8 @@
-use image::DynamicImage;
+use image::{DynamicImage, GenericImageView};
+use anyhow::{Result, bail};
+
#[cfg(target_os = "windows")]
-pub async fn perform_ocr_windows(image: &DynamicImage) -> (String, String, Option<f64>) {
+pub async fn perform_ocr_windows(image: &DynamicImage) -> Result<(String, String, Option<f64>)> {
use std::io::Cursor;
use windows::{
Graphics::Imaging::BitmapDecoder,
@@ -8,29 +10,33 @@ pub async fn perform_ocr_windows(image: &DynamicImage) -> (String, String, Optio
Storage::Streams::{DataWriter, InMemoryRandomAccessStream},
};
+ // Check image dimensions
+ let (width, height) = image.dimensions();
+ if width == 0 || height == 0 {
+ bail!("Invalid image dimensions: {}x{}", width, height);
+ }
+
let mut buffer = Vec::new();
image
.write_to(&mut Cursor::new(&mut buffer), image::ImageFormat::Png)
- .unwrap();
+ .map_err(|e| anyhow::anyhow!("Failed to write image to buffer: {}", e))?;
- let stream = InMemoryRandomAccessStream::new().unwrap();
- let writer = DataWriter::CreateDataWriter(&stream).unwrap();
- writer.WriteBytes(&buffer).unwrap();
- writer.StoreAsync().unwrap().get().unwrap();
- writer.FlushAsync().unwrap().get().unwrap();
- stream.Seek(0).unwrap();
+ let stream = InMemoryRandomAccessStream::new()?;
+ let writer = DataWriter::CreateDataWriter(&stream)?;
+ writer.WriteBytes(&buffer)?;
+ writer.StoreAsync()?.get()?;
+ writer.FlushAsync()?.get()?;
+ stream.Seek(0)?;
- let decoder = BitmapDecoder::CreateWithIdAsync(BitmapDecoder::PngDecoderId().unwrap(), &stream)
- .unwrap()
- .get()
- .unwrap();
+ let decoder = BitmapDecoder::CreateWithIdAsync(BitmapDecoder::PngDecoderId()?, &stream)?
+ .get()?;
- let bitmap = decoder.GetSoftwareBitmapAsync().unwrap().get().unwrap();
+ let bitmap = decoder.GetSoftwareBitmapAsync()?.get()?;
- let engine = WindowsOcrEngine::TryCreateFromUserProfileLanguages().unwrap();
- let result = engine.RecognizeAsync(&bitmap).unwrap().get().unwrap();
+ let engine = WindowsOcrEngine::TryCreateFromUserProfileLanguages()?;
+ let result = engine.RecognizeAsync(&bitmap)?.get()?;
- let text = result.Text().unwrap().to_string();
+ let text = result.Text()?.to_string();
let json_output = serde_json::json!([{
"text": text,
@@ -38,5 +44,5 @@ pub async fn perform_ocr_windows(image: &DynamicImage) -> (String, String, Optio
}])
.to_string();
- (text, json_output, Some(1.0))
+ Ok((text, json_output, Some(1.0)))
}