ocrs-cli 0.12.2

OCR CLI tool for extracting text from images
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
use std::collections::VecDeque;
use std::error::Error;
use std::fs;
use std::io::{BufWriter, IsTerminal, Read};

use anyhow::{anyhow, Context};
use ocrs::{DecodeMethod, DimOrder, ImageSource, OcrEngine, OcrEngineParams, OcrInput};
use rten_imageproc::RotatedRect;
use rten_tensor::prelude::*;
use rten_tensor::{NdTensor, NdTensorView};

mod models;
use models::{load_model, ModelSource};
mod output;
use output::{
    format_json_output, format_text_output, generate_annotated_png, FormatJsonArgs,
    GeneratePngArgs, OutputFormat,
};

/// Write a CHW image to a PNG file in `path`.
fn write_image(path: &str, img: NdTensorView<f32, 3>) -> anyhow::Result<()> {
    let img_width = img.size(2);
    let img_height = img.size(1);
    let color_type = match img.size(0) {
        1 => png::ColorType::Grayscale,
        3 => png::ColorType::Rgb,
        4 => png::ColorType::Rgba,
        chans => return Err(anyhow!("Unsupported channel count {}", chans)),
    };

    let hwc_img = img.permuted([1, 2, 0]); // CHW => HWC

    let out_img = image_from_tensor(hwc_img);
    let file = fs::File::create(path)?;
    let writer = BufWriter::new(file);
    let mut encoder = png::Encoder::new(writer, img_width as u32, img_height as u32);
    encoder.set_color(color_type);
    let mut writer = encoder.write_header()?;
    writer.write_image_data(&out_img)?;

    Ok(())
}

/// Convert an CHW float tensor with values in the range [0, 1] to `Vec<u8>`
/// with values scaled to [0, 255].
fn image_from_tensor(tensor: NdTensorView<f32, 3>) -> Vec<u8> {
    tensor
        .iter()
        .map(|x| (x.clamp(0., 1.) * 255.0) as u8)
        .collect()
}

/// Source of the input image.
enum InputSource {
    /// Read from a file path.
    File(String),
    /// Read from stdin.
    Stdin,
    /// Read from the system clipboard.
    Clipboard,
}

/// Extract images of individual text lines from `img`, apply the same
/// preprocessing that would be applied before text recognition, and save
/// in PNG format to `output_dir`.
fn write_preprocessed_text_line_images(
    input: &OcrInput,
    engine: &OcrEngine,
    line_rects: &[Vec<RotatedRect>],
    output_dir: &str,
) -> anyhow::Result<()> {
    std::fs::create_dir_all(output_dir)
        .with_context(|| format!("Failed to create dir {}/", output_dir))?;

    for (line_index, word_rects) in line_rects.iter().enumerate() {
        let filename = format!("{}/line-{}.png", output_dir, line_index);
        let mut line_img = engine.prepare_recognition_input(input, word_rects.as_slice())?;
        line_img.apply(|x| x + 0.5);
        let shape = [1, line_img.size(0), line_img.size(1)];
        let line_img = line_img.into_shape(shape);
        write_image(&filename, line_img.view())
            .with_context(|| format!("Failed to write line image to {}", filename))?;
    }

    Ok(())
}

struct Args {
    /// Path to text detection model.
    detection_model: Option<String>,

    /// Path to text recognition model.
    recognition_model: Option<String>,

    /// Source of the input image.
    input: InputSource,

    /// Enable debug output.
    debug: bool,

    output_format: OutputFormat,

    /// Output file path. Defaults to stdout.
    output_path: Option<String>,

    /// Use beam search for sequence decoding.
    beam_search: bool,

    /// Generate a text probability map.
    text_map: bool,

    /// Generate a text mask. This is the binarized version of the probability map.
    text_mask: bool,

    /// Extract each text line found and save as a PNG image.
    text_line_images: bool,

    /// Filter characters produced by text recognition
    /// This must be a sub-set of `alphabet`.
    allowed_chars: Option<String>,

    /// Alphabet used by the recognition model.
    /// If not provided, the default alphabet is used.
    alphabet: Option<String>,
}

fn parse_args() -> Result<Args, lexopt::Error> {
    use lexopt::prelude::*;

    let mut values = VecDeque::new();
    let mut allowed_chars = None;
    let mut alphabet = None;
    let mut beam_search = false;
    let mut clipboard = false;
    let mut debug = false;
    let mut detection_model = None;
    let mut output_format = OutputFormat::Text;
    let mut output_path = None;
    let mut recognition_model = None;
    let mut text_line_images = false;
    let mut text_map = false;
    let mut text_mask = false;

    let mut parser = lexopt::Parser::from_env();
    while let Some(arg) = parser.next()? {
        match arg {
            Value(val) => values.push_back(val.string()?),
            Long("allowed-chars") => {
                allowed_chars = Some(parser.value()?.string()?);
            }
            Short('a') | Long("alphabet") => {
                alphabet = Some(parser.value()?.string()?);
            }
            Long("beam") => {
                beam_search = true;
            }
            Short('c') | Long("clipboard") => {
                clipboard = true;
            }
            Long("debug") => {
                debug = true;
            }
            Long("detect-model") => {
                detection_model = Some(parser.value()?.string()?);
            }
            Short('j') | Long("json") => {
                output_format = OutputFormat::Json;
            }
            Short('o') | Long("output") => {
                output_path = Some(parser.value()?.string()?);
            }
            Short('p') | Long("png") => {
                output_format = OutputFormat::Png;
            }
            Long("rec-model") => {
                recognition_model = Some(parser.value()?.string()?);
            }
            Long("text-line-images") => {
                text_line_images = true;
            }
            Long("text-map") => {
                text_map = true;
            }
            Long("text-mask") => {
                text_mask = true;
            }
            Long("help") => {
                println!(
                    "Extract text from an image.

Usage: {bin_name} [OPTIONS] [image]

  If no image path is given, reads from stdin.

Options:

  --allowed-chars <chars>

    Filter characters produced by text recognition

  -a, --alphabet <chars>

    Specify the alphabet used by the recognition model

  -c, --clipboard

    Read image from system clipboard

  --detect-model <path>

    Use a custom text detection model

  -j, --json

    Output text and structure in JSON format

  -o, --output <path>

    Output file path (defaults to stdout)

  -p, --png

    Output annotated copy of input image in PNG format

  --rec-model <path>

    Use a custom text recognition model

  --version

    Display version info

Advanced options:

  (Note: These options are unstable and may change between releases)

  --beam

    Use beam search for decoding

  --debug

    Enable debug logging

  --text-line-images

    Export images of identified text lines

  --text-map

    Generate a text probability map for the input image

  --text-mask

    Generate a binary text mask for the input image
",
                    bin_name = parser.bin_name().unwrap_or("ocrs")
                );
                std::process::exit(0);
            }
            Long("version") => {
                println!("ocrs {}", env!("CARGO_PKG_VERSION"));
                std::process::exit(0);
            }
            _ => return Err(arg.unexpected()),
        }
    }

    let image = values.pop_front();

    let stdin_is_pipe = !std::io::stdin().is_terminal();

    let input = match (clipboard, image, stdin_is_pipe) {
        (true, Some(_), _) => {
            return Err("cannot use both --clipboard and an image path".into());
        }
        (true, _, true) => {
            return Err("cannot use both --clipboard and stdin".into());
        }
        (true, None, false) => InputSource::Clipboard,
        (false, Some(path), _) => InputSource::File(path),
        (false, None, true) => InputSource::Stdin,
        (false, None, false) => {
            return Err("missing `<image>` arg (or use --clipboard / pipe to stdin)".into());
        }
    };

    Ok(Args {
        alphabet,
        beam_search,
        debug,
        detection_model,
        input,
        output_format,
        output_path,
        recognition_model,
        text_map,
        text_mask,
        text_line_images,
        allowed_chars,
    })
}

/// Default text detection model.
const DETECTION_MODEL: &str = "https://ocrs-models.s3-accelerate.amazonaws.com/text-detection.rten";

/// Default text recognition model.
const RECOGNITION_MODEL: &str =
    "https://ocrs-models.s3-accelerate.amazonaws.com/text-recognition.rten";

/// Convert a decoded image into an HWC tensor.
fn image_to_tensor(image: image::DynamicImage) -> NdTensor<u8, 3> {
    let image = image.into_rgb8();
    let (width, height) = image.dimensions();
    NdTensor::from_data([height as usize, width as usize, 3], image.into_vec())
}

/// Load an image from a file path.
fn load_image_from_file(path: &str) -> anyhow::Result<NdTensor<u8, 3>> {
    image::open(path)
        .map(image_to_tensor)
        .with_context(|| format!("Failed to read image from {}", path))
}

/// Load an image from stdin.
fn load_image_from_stdin() -> anyhow::Result<NdTensor<u8, 3>> {
    let mut buf = Vec::new();
    std::io::stdin()
        .read_to_end(&mut buf)
        .context("Failed to read image from stdin")?;
    let image = image::load_from_memory(&buf).context("Failed to decode image from stdin")?;
    Ok(image_to_tensor(image))
}

/// Load an image from the system clipboard.
#[cfg(feature = "clipboard")]
fn load_image_from_clipboard() -> anyhow::Result<NdTensor<u8, 3>> {
    use arboard::Clipboard;

    let mut clipboard = Clipboard::new().context("Failed to access clipboard")?;

    let image_data = clipboard
        .get_image()
        .context("Failed to get image from clipboard. Is there an image copied?")?;

    // arboard returns RGBA, convert to RGB
    let rgba_bytes = image_data.bytes.into_owned();
    let rgb_bytes: Vec<u8> = rgba_bytes
        .chunks_exact(4)
        .flat_map(|chunk| [chunk[0], chunk[1], chunk[2]])
        .collect();

    Ok(NdTensor::from_data(
        [image_data.height, image_data.width, 3],
        rgb_bytes,
    ))
}

#[cfg(not(feature = "clipboard"))]
fn load_image_from_clipboard() -> anyhow::Result<NdTensor<u8, 3>> {
    Err(anyhow!(
        "ocrs was compiled without clipboard support. Use `cargo install ocrs-cli --features clipboard` to enable it."
    ))
}

fn main() -> Result<(), Box<dyn Error>> {
    let args = parse_args()?;

    // Fetch and load ML models.
    let detection_model_src = args
        .detection_model
        .as_ref()
        .map_or(ModelSource::Url(DETECTION_MODEL), |path| {
            ModelSource::Path(path)
        });
    let detection_model = load_model(detection_model_src).with_context(|| {
        format!(
            "Failed to load text detection model from {}",
            detection_model_src
        )
    })?;

    let recognition_model_src = args
        .recognition_model
        .as_ref()
        .map_or(ModelSource::Url(RECOGNITION_MODEL), |path| {
            ModelSource::Path(path)
        });
    let recognition_model = load_model(recognition_model_src).with_context(|| {
        format!(
            "Failed to load text recognition model from {}",
            recognition_model_src
        )
    })?;

    // Initialize OCR engine.
    #[allow(clippy::needless_update)]
    let engine = OcrEngine::new(OcrEngineParams {
        detection_model: Some(detection_model),
        recognition_model: Some(recognition_model),
        debug: args.debug,
        alphabet: args.alphabet,
        decode_method: if args.beam_search {
            DecodeMethod::BeamSearch { width: 100 }
        } else {
            DecodeMethod::Greedy
        },
        allowed_chars: args.allowed_chars,
        ..Default::default()
    })?;

    // Read image into HWC tensor.
    let (color_img, input_path): (NdTensor<u8, 3>, String) = match &args.input {
        InputSource::Clipboard => (load_image_from_clipboard()?, "<clipboard>".to_string()),
        InputSource::File(path) => (load_image_from_file(path)?, path.clone()),
        InputSource::Stdin => (load_image_from_stdin()?, "<stdin>".to_string()),
    };

    // Preprocess image for use with OCR engine.
    let color_img_source = ImageSource::from_tensor(color_img.view(), DimOrder::Hwc)?;
    let ocr_input = engine.prepare_input(color_img_source)?;

    if args.text_map || args.text_mask {
        let text_map = engine.detect_text_pixels(&ocr_input)?;
        let [height, width] = text_map.shape();
        let text_map = text_map.into_shape([1, height, width]);
        if args.text_map {
            write_image("text-map.png", text_map.view())?;
        }

        if args.text_mask {
            let threshold = engine.detection_threshold();
            let text_mask = text_map.map(|x| if *x > threshold { 1. } else { 0. });
            write_image("text-mask.png", text_mask.view())?;
        }
    }

    let word_rects = engine.detect_words(&ocr_input)?;

    let line_rects = engine.find_text_lines(&ocr_input, &word_rects);
    if args.text_line_images {
        write_preprocessed_text_line_images(&ocr_input, &engine, &line_rects, "lines")?;
        // write_text_line_images(color_img.view(), &line_rects, "lines")?;
    }

    let line_texts = engine.recognize_text(&ocr_input, &line_rects)?;

    let write_output_str = |content: String| -> Result<(), Box<dyn Error>> {
        if let Some(output_path) = &args.output_path {
            std::fs::write(output_path, content.into_bytes())
                .with_context(|| format!("Failed to write output to {}", output_path))?;
        } else {
            println!("{}", content);
        }
        Ok(())
    };

    match args.output_format {
        OutputFormat::Text => {
            let content = format_text_output(&line_texts);
            write_output_str(content)?;
        }
        OutputFormat::Json => {
            let content = format_json_output(FormatJsonArgs {
                input_path: &input_path,
                input_hw: color_img.shape()[1..].try_into()?,
                text_lines: &line_texts,
            });
            write_output_str(content)?;
        }
        OutputFormat::Png => {
            let png_args = GeneratePngArgs {
                img: color_img.view(),
                line_rects: &line_rects,
                text_lines: &line_texts,
            };
            let annotated_img = generate_annotated_png(png_args);
            let Some(output_path) = args.output_path else {
                return Err("Output path must be specified when generating annotated PNG".into());
            };
            write_image(&output_path, annotated_img.view())
                .with_context(|| format!("Failed to write output to {}", &output_path))?;
        }
    }

    if args.debug {
        println!(
            "Found {} words, {} lines in image of size {}x{}",
            word_rects.len(),
            line_rects.len(),
            color_img.size(2),
            color_img.size(1),
        );
    }

    Ok(())
}