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

adding text lines array and raw tesseract tsv output to the object

matthew-heartful015a38cef8ca
diff --git a/screenpipe-vision/src/core.rs b/screenpipe-vision/src/core.rs
index 2d46a9010..38beef2d6 100644
--- a/screenpipe-vision/src/core.rs
+++ b/screenpipe-vision/src/core.rs
@@ -22,12 +22,14 @@ pub enum ControlMessage {
pub struct CaptureResult {
pub image: Arc<DynamicImage>,
pub text: String,
+ pub text_json: Vec<String>,
pub frame_number: u64,
pub timestamp: Instant,
+ pub tsv_output: String,
}
const MAX_THREADS: usize = 4; // Adjust based on your needs
-const MAX_QUEUE_SIZE: usize = 6; // Maximum number of frames to keep in the queue. 64/8 ocr task = 8
+const MAX_QUEUE_SIZE: usize = 6; // Maximum number of frames to keep in the queue. 64/8 o
// seems kinda counter intuitive but less threads for OCR = more CPU usage = less frame dropping
pub async fn continuous_capture(
@@ -57,6 +59,7 @@ pub async fn continuous_capture(
let cache = Arc::clone(&cache);
let should_stop = Arc::clone(&should_stop);
task::spawn(async move {
+ info!("OCR task {} started", id);
while !*should_stop.lock().await {
match ocr_rx.recv().await {
Ok((image_arc, image_hash, frame_number, timestamp, result_tx)) => {
@@ -64,20 +67,22 @@ pub async fn continuous_capture(
if frame_number % pool_size as u64 == id as u64 {
let start_time = Instant::now();
let mut cache = cache.lock().await;
- let text = if let Some(cached_text) = cache.get(&image_hash) {
- cached_text.clone()
+ let (text, tsv_output) = if let Some(cached_text) = cache.get(&image_hash) {
+ (cached_text.clone(), String::new())
} else {
- let new_text = perform_ocr(&image_arc);
+ let (new_text, new_tsv_output) = perform_ocr(&image_arc);
cache.insert(image_hash, new_text.clone());
- new_text
+ (new_text, new_tsv_output)
};
if let Err(e) = result_tx
.send(CaptureResult {
image: image_arc.into(),
- text,
+ text: text.clone(),
+ text_json: text.lines().map(String::from).collect(),
frame_number,
timestamp,
+ tsv_output,
})
.await
{
@@ -101,6 +106,7 @@ pub async fn continuous_capture(
},
}
}
+ info!("OCR task {} stopped", id);
})
})
.collect();
@@ -201,9 +207,43 @@ fn calculate_hash(image: &DynamicImage) -> u64 {
hasher.finish()
}
-fn perform_ocr(image: &DynamicImage) -> String {
- let args = Args::default();
+pub fn perform_ocr(image: &DynamicImage) -> (String, String) {
+ let args = Args {
+ lang: "eng".to_string(),
+ config_variables: HashMap::from([
+ ("tessedit_create_tsv".into(), "1".into()),
+ ("tessedit_create_txt".into(), "1".into()),
+ ]),
+ ..Args::default()
+ };
let ocr_image = Image::from_dynamic_image(image).unwrap();
- rusty_tesseract::image_to_string(&ocr_image, &args)
- .unwrap_or_else(|_| String::from("OCR failed"))
+ let text = rusty_tesseract::image_to_string(&ocr_image, &args)
+ .unwrap_or_else(|_| String::from("OCR failed"));
+ let tsv_output = rusty_tesseract::image_to_data(&ocr_image, &args)
+ .map(|data_output| data_output.output)
+ .unwrap_or_else(|_| String::from("TSV output failed"));
+ (text, tsv_output)
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use image::DynamicImage;
+ use std::path::PathBuf;
+
+ #[test]
+ fn test_perform_ocr() {
+ // Use the correct path to the testing_OCR.png file
+ let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
+ path.push("tests/testing_OCR.png");
+ let image = image::open(path).expect("Failed to open image");
+ let (text, tsv_output) = perform_ocr(&image);
+
+ // Print the results
+ println!("OCR Text: {}", text);
+ println!("TSV Output: {}", tsv_output);
+
+ assert!(!text.is_empty(), "OCR text should not be empty");
+ assert!(!tsv_output.is_empty(), "TSV output should not be empty");
+ }
+}
\ No newline at end of file