rcsaquino / rsautogui

rsautogui is a GUI automation rust crate for windows.
MIT License
21 stars 1 forks source link

Update screen mod.rs with locate image feature #4

Closed Bullesta closed 1 year ago

Bullesta commented 1 year ago

Created and implemented the first image recognition for this crate. It can search the entire screen 5x 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:

fn main() {
    let img = image::open("images.png").expect("Unable to locate file.");    
    match locate_img_center(&img, None, Some(0.9), Some(10)) {
        Some((x, y, confidence)) => {
            println!("Image center found at {}, {} with confidence {}", x, y, confidence);
            move_to(x.try_into().unwrap(), y.try_into().unwrap())
        },
        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")
    }
}
Bullesta commented 1 year ago

I forgot to add pub to function locate_img_center

Bullesta commented 1 year ago

Realized this is adding all my changes. Making it so that its only the screen mod.rs

Bullesta commented 1 year ago

Just leave the README.md out or merge information

Bullesta commented 1 year ago

As your crate is a wrapper, I feel it may be better for me to convert this into my own crate and add its dependency later so I can keep it maintained.