#computer-vision #image-processing #screenshot #pattern-matching #image-recognition

spectrust

A fast, Rust-based computer vision library for locating images on the screen

2 unstable releases

0.2.0 Jul 19, 2023
0.1.0 Jul 17, 2023

#117 in Multimedia

Download history 1/week @ 2024-02-19 20/week @ 2024-02-26 1/week @ 2024-03-11 59/week @ 2024-03-25 24/week @ 2024-04-01

83 downloads per month

MIT/Apache

14KB
141 lines

SpectRust

A Rust-based Computer visioner for locating images on screen

It can search the entire screen a over 3x faster than pyautogui and does not require OpenCV for options like Confidence. I've also added an option called Tolerance that allows for leniency with pixel colors that are close to the original image's. Written in pure Rust.

locate_img_center(img: &DynamicImage, region: Option<(u16, u16, u16, u16)>, min_confidence: Option<f32>, tolerance: Option<u8>) -> Option<(u32, u32, f32)>)
locate_img(img: &DynamicImage, region: Option<(u16, u16, u16, u16)>, min_confidence: Option<f32>, tolerance: Option<u8>) 

img: required borrowed DynamicImage

region: requires tuple BoundingBox (x, y, width, height) (Default Entire Screen)

min_confidence: 0.1 - 1.0, percentage of how many of the pixels need to match (Default 0.95)

tolerance: 0 - 255, range of pixels to accept from image's pixels. So if an image has a pixel of 234, 52, 245 with a tolerance of 10, then the locator will accept values ranging from 224, 42, 235 - 244, 62, 255. (Default 5)

All of these requires (except img) are optional and require either a Some() or None

Examples:

use spectrust::*;
fn main () {
    let img = image::open("images.png").expect("Unable to locate file.");
    let region = None;
    let min_confidence = Some(0.8);
    let tolerance = Some(5);

    match locate_center_of_image(&img, region, min_confidence, tolerance) {
        Some((x, y, confidence)) => {
            println!("Image center found at {}, {} with confidence {}", x, y, confidence);
        },
        None => println!("Image not found"),
    }
}
fn main() {
    let img = image::open("images.png").expect("Unable to locate file.");

    match locate_image(&img, None, None, None) {
        Some((x, y, img_width, img_height, _confidence)) => {
            println!("x: {}, y: {}, width: {}, height: {}",x, y, img_width, img_height)
        },
        None => println!("Image not found")
    }
}

If you are having trouble with finding an image. Try lowering the confidence or increasing the tolerance.

Benchmark

Python: 150ms Rust: 53ms (Both finding the Rust icon with a size of 225x225px on a 1920x1080 screen)

Useful for Bot Creating

*Reaction Test (actual time is about 12ms)
*Running for 20 minutes on an online aim trainer

Dependencies

~13–41MB
~422K SLoC