-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kiwi
committed
Dec 5, 2024
1 parent
d1c25b4
commit 4f9bf14
Showing
14 changed files
with
276 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
fn main() { | ||
tonic_build::compile_protos("src/grpc/proto/common.proto").unwrap(); | ||
tauri_build::build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use std::time::Instant; | ||
|
||
use kiwi_lib::grpc::FindImageRequest; | ||
|
||
fn main() { | ||
let time = Instant::now(); | ||
let path = "payment".to_string(); | ||
let start_x = 15; | ||
let start_y = 20; | ||
let end_x = 100; | ||
let end_y = 100; | ||
let threshold = 0.957; | ||
let request = FindImageRequest::new(path, start_x, start_y, end_x, end_y, threshold); | ||
let result = request.send(); | ||
println!("{:?}", result); | ||
println!("eslaped: {:?}", time.elapsed()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use std::time::Duration; | ||
|
||
use kiwi_lib::grpc::{self, RUN_TIME}; | ||
|
||
fn main() { | ||
RUN_TIME.spawn(async move { | ||
grpc::run().await.unwrap(); | ||
}); | ||
std::thread::sleep(Duration::from_secs(1000)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use crate::capture::FRAME; | ||
use crate::common::PROJECT_DIR; | ||
use crate::find as system_find; | ||
use crate::utils::fs::exists; | ||
|
||
use super::RUN_TIME; | ||
use anyhow::Result; | ||
use find::{ | ||
find_service_client::FindServiceClient, find_service_server::FindService, FindImageReply, | ||
FindImageRequest, | ||
}; | ||
use std::path::PathBuf; | ||
use std::sync::LazyLock; | ||
use std::sync::Mutex; | ||
use tonic::transport::channel::Channel; | ||
use tonic::{Request, Response, Status}; | ||
|
||
use super::EMPTY_WEIGHT_POINT; | ||
pub mod find { | ||
tonic::include_proto!("find_kiwi"); | ||
} | ||
#[derive(Debug, Default)] | ||
pub struct FindServiceInstance {} | ||
#[tonic::async_trait] | ||
impl FindService for FindServiceInstance { | ||
async fn find_image( | ||
&self, | ||
request: Request<FindImageRequest>, | ||
) -> Result<Response<FindImageReply>, Status> { | ||
if PROJECT_DIR.lock().unwrap().is_none() { | ||
return Ok(FindImageReply::empty().response()); | ||
} | ||
if FRAME.lock().unwrap().is_none() { | ||
return Ok(FindImageReply::empty().response()); | ||
} | ||
let request = request.into_inner(); | ||
let frame = FRAME.lock().unwrap().clone().unwrap(); | ||
let project_path = PROJECT_DIR.lock().unwrap().clone().unwrap(); | ||
let project_pathbuf = PathBuf::from(project_path); | ||
let full_path = format!( | ||
"{}{}", | ||
project_pathbuf | ||
.join("resources") | ||
.join(request.path) | ||
.to_str() | ||
.unwrap() | ||
.to_string(), | ||
".png" | ||
); | ||
println!("full path is {:?}", full_path.clone()); | ||
if let Err(error) = exists(full_path.clone()) { | ||
println!("exists caused error: {}", error.to_string()); | ||
return Ok(FindImageReply::empty().response()); | ||
} | ||
if !exists(full_path.clone()).unwrap() { | ||
println!("file not exist"); | ||
return Ok(FindImageReply::empty().response()); | ||
} | ||
let (width, height) = ( | ||
request.end_x - request.start_x, | ||
request.end_y - request.start_y, | ||
); | ||
if width <= 0 || height <= 0 { | ||
println!("width ({}) or height ({}) is zero", width, height); | ||
return Ok(FindImageReply::empty().response()); | ||
} | ||
let template = image::open(full_path.clone()).unwrap().to_rgba8(); | ||
if let Ok(weight_point) = system_find::image::find_one( | ||
frame, | ||
template, | ||
request.start_x, | ||
request.start_y, | ||
width, | ||
height, | ||
request.threshold, | ||
) { | ||
let (x, y, weight) = ( | ||
weight_point.point.x as i32, | ||
weight_point.point.y as i32, | ||
weight_point.weight, | ||
); | ||
return Ok(FindImageReply::new(x, y, weight).response()); | ||
} | ||
Ok(FindImageReply::empty().response()) | ||
} | ||
} | ||
|
||
impl FindImageReply { | ||
pub fn new(x: i32, y: i32, threshold: f64) -> Self { | ||
Self { x, y, threshold } | ||
} | ||
pub fn empty() -> Self { | ||
let (x, y, threshold) = EMPTY_WEIGHT_POINT; | ||
Self { x, y, threshold } | ||
} | ||
pub fn response(self) -> Response<Self> { | ||
Response::new(self) | ||
} | ||
|
||
pub fn to_tuple(self) -> (i32, i32, f64) { | ||
(self.x, self.y, self.threshold) | ||
} | ||
} | ||
|
||
impl FindImageRequest { | ||
pub fn new( | ||
path: String, | ||
start_x: u32, | ||
start_y: u32, | ||
end_x: u32, | ||
end_y: u32, | ||
threshold: f64, | ||
) -> Self { | ||
Self { | ||
path, | ||
start_x, | ||
start_y, | ||
end_x, | ||
end_y, | ||
threshold, | ||
} | ||
} | ||
pub fn send(self) -> FindImageReply { | ||
let result = RUN_TIME.block_on(async move { | ||
if CLIENT.lock().unwrap().is_none() { | ||
init_client().await; | ||
} | ||
let mut client = CLIENT.lock().unwrap().clone().unwrap(); | ||
client.find_image(self).await.unwrap() | ||
}); | ||
result.into_inner() | ||
} | ||
} | ||
|
||
async fn init_client() { | ||
let client = FindServiceClient::connect(format!("http://{}", super::ADDR)) | ||
.await | ||
.unwrap(); | ||
*CLIENT.lock().unwrap() = Some(client); | ||
} | ||
|
||
pub static CLIENT: LazyLock<Mutex<Option<FindServiceClient<Channel>>>> = | ||
LazyLock::new(|| Mutex::new(None)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
mod find; | ||
mod server; | ||
|
||
use crate::common::HexColor; | ||
use std::sync::LazyLock; | ||
use tokio::runtime::Runtime; | ||
|
||
pub use find::find::FindImageRequest; | ||
pub use server::{run, run_spawn}; | ||
|
||
const ADDR: &str = "[::1]:50555"; | ||
pub static RUN_TIME: LazyLock<Runtime> = LazyLock::new(|| tokio::runtime::Runtime::new().unwrap()); | ||
|
||
const EMPTY_WEIGHT_POINT: (i32, i32, f64) = (-1, -1, 0.0); | ||
const EMPTY_WEIGHT_POINTS: Vec<(i32, i32, f64)> = Vec::new(); | ||
const EMPTY_POINT: (i32, i32) = (-1, -1); | ||
const EMPTY_LOCATING_COLORS: Vec<(i32, i32, HexColor)> = Vec::new(); | ||
const EMPTY_TEXT: &str = ""; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
syntax = "proto3"; | ||
package find_kiwi; | ||
service FindService { | ||
rpc FindImage(FindImageRequest) returns (FindImageReply); | ||
} | ||
message FindImageRequest { | ||
string path = 1; | ||
uint32 start_x = 2; | ||
uint32 start_y = 3; | ||
uint32 end_x = 4; | ||
uint32 end_y = 5; | ||
double threshold = 6; | ||
} | ||
message FindImageReply { | ||
int32 x = 1; | ||
int32 y = 2; | ||
double threshold = 3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use super::find::{find::find_service_server::FindServiceServer, FindServiceInstance}; | ||
use super::RUN_TIME; | ||
use anyhow::Result; | ||
use tonic::transport::Server; | ||
|
||
pub async fn run() -> Result<()> { | ||
let addr = super::ADDR.parse()?; | ||
let instance = FindServiceInstance::default(); | ||
Server::builder() | ||
.add_service(FindServiceServer::new(instance)) | ||
.serve(addr) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
pub fn run_spawn() { | ||
RUN_TIME.spawn(async { | ||
run().await.unwrap(); | ||
}); | ||
} | ||
//grpcurl -plaintext -import-path /Users/kiwi/Documents/Rust/kiwi/src-tauri/src/grpc/proto -proto common.proto -d '{"path": "payment", "startX": 457, "startY": 88, "endX": 711, "endY": 946, "threshold": 590.2428442764207}' '[::1]:50555' find_kiwi.FindService/FindImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.