Ocrs crate crashes my entire computer

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();
    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") =>
                    "Usage: {bin_name} <image>",
                    bin_name = parser.bin_name().unwrap_or("hello_ocrs")
            _ => return Err(arg.unexpected()),

    let image = values.pop_front().ok_or("missing `image` arg")?;

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),

    //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();
    //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
    // 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?

Looks like ocrs uses RTen for the tensor operations. RTen only supports executing on the CPU. Are you overclocking your CPU? And is your PSU powerful enough for the peak power usage of your system?

I am not overclocking anything.

not too sure I mean its a gaming custom computer built 10 years ago so I would magine the technician would have put in a good PSU, also the second it reaches that line of code it crashes, shouldn't it take a bit longer before it crashes? And like I tested this out on my more modern non gaming laptop with a weaker CPU and no issues there.

DO you think I should over volt my CPU in the BIOS settings?

by the way I am using unstalbe rust, using stable rust will fail to compile this project.

There is nothing Rust should be able to do to make this happen. Even if a Rust program was actively trying to kill your machine, the operating system should stop it.

I second the suspicion at your power supply. However, it could also potentially be your CPU, motherboard, or RAM. A few years ago, I had intermittent hard crashes on my system (which also manifested as other weird problems like programs stalling, and compilation randomly failing during linking), and it turned out to be an issue with Zen2 CPUs. Had to RMA it and get it replaced.

I can't offer any concrete advice for how to go about diagnosing precisely what's at fault, as it depends a lot on what you have access to. I don't want to start recommending under-clocking your system, as I don't really know how to do that safely. All I can do is tell you that I've been there, it sucks, and you have my commiserations.


What is your processor? As a totally wild guess, I wonder if RTen is using SIMD instructions not supported by your CPU, and it halts when it encounters an unknown instruction.

That shouldn't be possible on CPUs supported by Windows - ever since the 80286 (in protected mode, not real mode), and since day 1 for ARM, unknown instructions cause a jump to the kernel via the "undefined instruction" vector.


It uses AVX2 instructions. There was a mistake in an earlier version that would try to use them in environments where they aren't available (mostly emulated CPUs rather than actual old CPUs it seems) and that resulted in the expected "illegal instruction" crash of just the process.


its Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz as @robertknight has suggested It slightly increases voltage and I am not maybe giving enough volts to the CPU.

Reading an article I may need to offset AVX in the BIOS.