This is my code using ocrs:
use std::{collections::VecDeque, path::PathBuf};
use image;
use ocrs::{OcrEngine, OcrEngineParams};
use rten;
use rten_imageio::read_image;
use rten_tensor::AsView;
const SAMPLE_CONTENTS_DIR: &str = "testing_images";
const MODELS_DIR: &str = "models";
mod ocrs_extras;
/// Read a file from a path that is relative to the crate root.
//fn read_file(path: &str) -> Result<Vec<u8>, std::io::Error>
fn read_file(path: &str) -> Vec<u8>
{
//let mut abs_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let mut abs_path = PathBuf::new();
abs_path.push(path);
return std::fs::read(abs_path).unwrap();
}
struct Args
{
image: String,
}
//fn parse_args() -> Result<Args, lexopt::Error>
fn parse_args() -> Result<Args, lexopt::Error>
{
use lexopt::prelude::*;
let mut values = VecDeque::new();
let mut parser = lexopt::Parser::from_env();
while let Some(arg) = parser.next()?
{
match arg
{
Value(val) => values.push_back(val.string()?),
Long("help") =>
{
println!(
"Usage: {bin_name} <image>",
bin_name = parser.bin_name().unwrap_or("hello_ocrs")
);
std::process::exit(0);
}
_ => return Err(arg.unexpected()),
}
}
let image = values.pop_front().ok_or("missing `image` arg")?;
Ok(Args{image})
}
fn main()
{
let detection_model_data = read_file(&format!("{}/text-detection.rten", MODELS_DIR));
let rec_model_data = read_file(&format!("{}/text-recognition.rten", MODELS_DIR));
let detection_model = rten::Model::load(&detection_model_data).unwrap();
let recognition_model = rten::Model::load(&rec_model_data).unwrap();
let engine = OcrEngine::new(OcrEngineParams
{
detection_model: Some(detection_model),
recognition_model: Some(recognition_model),
..Default::default()
}).unwrap();
//let img = image::open(&format!("{}/image.jpeg", SAMPLE_CONTENTS_DIR)).map(|image| image.into_rgb8()).unwrap();
let img = read_image(&format!("{}/image.jpeg", SAMPLE_CONTENTS_DIR)).unwrap();
//ocrs_extras::preprocess::test();
//let img_source = ocrs_extras::preprocess::ImageSource::from_bytes(img.as_raw(), img.dimensions()).unwrap();
let ocr_input = engine.prepare_input(img.view()).unwrap();
// Get oriented bounding boxes of text words in input image.
let word_rects = engine.detect_words(&ocr_input).unwrap(); // Slow
// Group words into lines. Each line is represented by a list of word
// bounding boxes.
let line_rects = engine.find_text_lines(&ocr_input, &word_rects);
// Recognize the characters in each line.
let line_texts = engine.recognize_text(&ocr_input, &line_rects).unwrap(); // Slow
for line in line_texts
.iter()
.flatten()
// Filter likely spurious detections. With future model improvements
// this should become unnecessary.
.filter(|l| l.to_string().len() > 1)
{
println!("for loop: {}", line);
}
}
using the command cargo r
runs fine but runs too slowly. I was suggestd to use --release
flag as it runs much faster,
So after running cargo r --release
my computer crashes the second it runs. The computer will switch itself off so this is at a hardware level rather than at OS level. I tested this on both Windows and Linux same issue.
After adding some extra code to pause before executing the next line until I press a key, it turns out it crashes when I end up right at this line:
let word_rects = engine.detect_words(&ocr_input).unwrap(); // Slow
I am not too sure how to resolve this at all, especialyl when my PC shuts itself off immediately as soon as it reaches this line. Not too sure if this is doing GPU task or CPU task?