-
Notifications
You must be signed in to change notification settings - Fork 598
/
websocket.rs
234 lines (192 loc) · 6.65 KB
/
websocket.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use anyhow::Result;
use base64::{engine::general_purpose, Engine as _};
use clap::Parser;
use futures_util::{SinkExt, StreamExt};
use image::ImageEncoder;
use screenpipe_vision::{
continuous_capture, monitor::get_default_monitor, CaptureResult, OcrEngine,
};
use serde::Serialize;
use std::sync::Arc;
use std::time::Duration;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::mpsc::channel;
use tokio_tungstenite::tungstenite::Message;
use tracing_subscriber::{fmt::format::FmtSpan, EnvFilter};
#[derive(Clone, Serialize)]
struct SimplifiedResult {
windows: Vec<SimplifiedWindowResult>,
timestamp: u64,
}
#[derive(Clone, Serialize)]
pub struct SimplifiedWindowResult {
pub image: String, // Changed to String to store base64-encoded image
pub window_name: String,
pub app_name: String,
pub text: String,
pub focused: bool,
pub confidence: f64,
}
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Save text files
#[arg(long, default_value_t = false)]
save_text_files: bool,
/// FPS for continuous recording
/// 1 FPS = 30 GB / month
/// 5 FPS = 150 GB / month
/// Optimise based on your needs.
/// Your screen rarely change more than 1 times within a second, right?
#[cfg_attr(not(target_os = "macos"), arg(short, long, default_value_t = 1.0))]
#[cfg_attr(target_os = "macos", arg(short, long, default_value_t = 0.2))]
fps: f64,
/// WebSocket port
#[arg(long, default_value_t = 8080)]
ws_port: u16,
/// List of windows to ignore (by title) for screen recording
#[arg(long)]
ignored_windows: Vec<String>,
/// List of windows to include (by title) for screen recording
#[arg(long)]
included_windows: Vec<String>,
}
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::fmt()
.with_env_filter(
EnvFilter::from_default_env()
.add_directive(tracing::Level::DEBUG.into())
.add_directive("tokenizers=error".parse().unwrap()),
)
.with_span_events(FmtSpan::CLOSE)
.init();
let cli = Cli::parse();
let (result_tx, result_rx) = channel(512);
let save_text_files = cli.save_text_files;
let ws_port = cli.ws_port;
let monitor = get_default_monitor().await;
let id = monitor.id();
tokio::spawn(async move {
continuous_capture(
result_tx,
Duration::from_secs_f64(1.0 / cli.fps),
save_text_files,
// if apple use apple otherwise if windows use windows native otherwise use tesseract
if cfg!(target_os = "macos") {
OcrEngine::AppleNative
} else if cfg!(target_os = "windows") {
OcrEngine::WindowsNative
} else {
OcrEngine::Tesseract
},
id,
&cli.ignored_windows,
&cli.included_windows,
)
.await
});
// Start WebSocket server
tokio::spawn(async move { run_websocket_server(ws_port, result_rx).await });
// Keep the main thread alive
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
async fn run_websocket_server(
port: u16,
mut result_rx: tokio::sync::mpsc::Receiver<CaptureResult>,
) -> Result<()> {
let addr = format!("127.0.0.1:{}", port);
let listener = TcpListener::bind(&addr).await?;
println!("WebSocket server listening on: {}", addr);
let (tx, _) = tokio::sync::broadcast::channel::<SimplifiedResult>(512);
let tx = Arc::new(tx);
let tx_clone = tx.clone();
tokio::spawn(async move {
while let Some(result) = result_rx.recv().await {
let simplified = SimplifiedResult {
windows: result
.window_ocr_results
.into_iter()
.map(|window| {
let mut buffer = Vec::new();
let encoder = image::codecs::png::PngEncoder::new(&mut buffer);
encoder
.write_image(
window.image.as_bytes(),
window.image.width(),
window.image.height(),
window.image.color().into(),
)
.expect("Failed to encode image");
let base64_image = general_purpose::STANDARD.encode(buffer);
SimplifiedWindowResult {
image: base64_image,
window_name: window.window_name,
app_name: window.app_name,
text: window.text,
focused: window.focused,
confidence: window.confidence,
}
})
.collect(),
timestamp: result.timestamp.elapsed().as_secs(),
};
let _ = tx_clone.send(simplified);
}
});
while let Ok((stream, _)) = listener.accept().await {
let rx = tx.subscribe();
tokio::spawn(handle_connection(stream, rx));
}
Ok(())
}
async fn handle_connection(
stream: TcpStream,
mut result_rx: tokio::sync::broadcast::Receiver<SimplifiedResult>,
) {
let ws_stream = tokio_tungstenite::accept_async(stream)
.await
.expect("Error during WebSocket handshake");
println!("New WebSocket connection");
let (mut ws_sender, _) = ws_stream.split();
while let Ok(result) = result_rx.recv().await {
let message = serde_json::to_string(&result).expect("Failed to serialize result");
if let Err(e) = ws_sender.send(Message::Text(message)).await {
eprintln!("WebSocket send error: {:?}", e);
break;
}
}
}
/*
first: cargo run --example screenpipe-vision-websocket
Python one-liner to connect and print WebSocket data:
virtualenv /tmp/screenpipe-vision
source /tmp/screenpipe-vision/bin/activate
pip install websockets
# open python3 and copy paste the code and press enter
python3
import asyncio
import websockets
async def main():
async with websockets.connect('ws://localhost:8080') as ws:
print(await ws.recv())
asyncio.run(main())
*/
/*
or npm i ws
node and paste this
const WebSocket = require('ws');
const ws = new WebSocket('ws://localhost:8080');
ws.on('open', function open() {
console.log('Connected to WebSocket server');
});
ws.on('message', function incoming(data) {
console.log('Received:', data.toString());
ws.close();
});
ws.on('close', function close() {
console.log('Disconnected from WebSocket server');
});
*/