Skip to content

Commit

Permalink
Rustfmt everything
Browse files Browse the repository at this point in the history
  • Loading branch information
awwaiid committed Dec 3, 2024
1 parent dfa3c92 commit ad8145a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 38 deletions.
48 changes: 32 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,13 @@ fn ghostwriter(args: &Args) -> Result<()> {
if let Some(output_file) = &args.output_file {
std::fs::write(output_file, svg_data)?;
}
draw_svg(svg_data, &mut keyboard, &mut pen, args.save_bitmap.as_ref(), args.no_draw)?;
draw_svg(
svg_data,
&mut keyboard,
&mut pen,
args.save_bitmap.as_ref(),
args.no_draw,
)?;
if let Some(model_output_file) = &args.model_output_file {
let params = json!({
"function": function_name,
Expand All @@ -287,12 +293,12 @@ fn ghostwriter(args: &Args) -> Result<()> {
}
_ => {
keyboard.progress_end()?;
return Err(anyhow::anyhow!("Unknown function called"))
return Err(anyhow::anyhow!("Unknown function called"));
}
}
} else {
keyboard.progress_end()?;
return Err(anyhow::anyhow!("No tool call found in response"))
return Err(anyhow::anyhow!("No tool call found in response"));
}

if args.no_loop {
Expand All @@ -310,7 +316,13 @@ fn draw_text(text: &str, keyboard: &mut Keyboard) -> Result<()> {
Ok(())
}

fn draw_svg(svg_data: &str, keyboard: &mut Keyboard, pen: &mut Pen, save_bitmap: Option<&String>, no_draw: bool) -> Result<()> {
fn draw_svg(
svg_data: &str,
keyboard: &mut Keyboard,
pen: &mut Pen,
save_bitmap: Option<&String>,
no_draw: bool,
) -> Result<()> {
keyboard.progress()?;
let bitmap = svg_to_bitmap(svg_data, REMARKABLE_WIDTH, REMARKABLE_HEIGHT)?;
if let Some(save_bitmap) = save_bitmap {
Expand All @@ -323,13 +335,11 @@ fn draw_svg(svg_data: &str, keyboard: &mut Keyboard, pen: &mut Pen, save_bitmap:
Ok(())
}


fn claude_assist(args: &Args) -> Result<()> {
let mut keyboard = Keyboard::new(args.no_draw, args.no_draw_progress);
let mut pen = Pen::new(args.no_draw);
let mut touch = Touch::new(args.no_draw);


// Default to regular text size
keyboard.key_cmd_body()?;

Expand Down Expand Up @@ -362,7 +372,10 @@ fn claude_assist(args: &Args) -> Result<()> {

// Analyze the image to get bounding box descriptions
let segmentation_description = if args.apply_segmentation {
let input_filename = args.input_png.clone().unwrap_or(args.save_screenshot.clone().unwrap());
let input_filename = args
.input_png
.clone()
.unwrap_or(args.save_screenshot.clone().unwrap());
match analyze_image(input_filename.as_str()) {
Ok(description) => description,
Err(e) => format!("Error analyzing image: {}", e),
Expand Down Expand Up @@ -493,24 +506,21 @@ fn claude_assist(args: &Args) -> Result<()> {

keyboard.progress()?;


let raw_response = ureq::post("https://api.anthropic.com/v1/messages")
.set("x-api-key", api_key.as_str())
.set("anthropic-version", "2023-06-01")
.set("Content-Type", "application/json")
.send_json(&body);

let response = match raw_response {
Ok(response) => { response }
Ok(response) => response,
Err(Error::Status(code, response)) => {
println!("Error: {}", code);
let json: serde_json::Value = response.into_json()?;
println!("Response: {}", json);
return Err(anyhow::anyhow!("API ERROR"))
}
Err(_) => {
return Err(anyhow::anyhow!("OTHER API ERROR"))
return Err(anyhow::anyhow!("API ERROR"));
}
Err(_) => return Err(anyhow::anyhow!("OTHER API ERROR")),
};

keyboard.progress()?;
Expand Down Expand Up @@ -545,16 +555,22 @@ fn claude_assist(args: &Args) -> Result<()> {
if let Some(output_file) = &args.output_file {
std::fs::write(output_file, svg_data)?;
}
draw_svg(svg_data, &mut keyboard, &mut pen, args.save_bitmap.as_ref(), args.no_draw)?;
draw_svg(
svg_data,
&mut keyboard,
&mut pen,
args.save_bitmap.as_ref(),
args.no_draw,
)?;
}
_ => {
keyboard.progress_end()?;
return Err(anyhow::anyhow!("Unknown function called"))
return Err(anyhow::anyhow!("Unknown function called"));
}
}
} else {
keyboard.progress_end()?;
return Err(anyhow::anyhow!("No tool call found in response"))
return Err(anyhow::anyhow!("No tool call found in response"));
}

if args.no_loop {
Expand Down
1 change: 0 additions & 1 deletion src/pen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl Pen {
Ok(())
}


// fn draw_dot(device: &mut Device, (x, y): (i32, i32)) -> Result<()> {
// // println!("Drawing at ({}, {})", x, y);
// goto_xy(device, (x, y))?;
Expand Down
6 changes: 5 additions & 1 deletion src/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ impl Screenshot {

// Resize the PNG to OUTPUT_WIDTH x OUTPUT_HEIGHT
let img = image::load_from_memory(&png_data)?;
let resized_img = img.resize(OUTPUT_WIDTH, OUTPUT_HEIGHT, image::imageops::FilterType::Lanczos3);
let resized_img = img.resize(
OUTPUT_WIDTH,
OUTPUT_HEIGHT,
image::imageops::FilterType::Lanczos3,
);

// Encode the resized image back to PNG
let mut resized_png_data = Vec::new();
Expand Down
50 changes: 31 additions & 19 deletions src/segmenter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,29 @@ impl ImageAnalyzer {
}
}

pub fn analyze_image(&self, image_path: &str) -> Result<SegmentationResult, Box<dyn std::error::Error>> {
pub fn analyze_image(
&self,
image_path: &str,
) -> Result<SegmentationResult, Box<dyn std::error::Error>> {
println!("Reading image from: {}", image_path);

// Read image and convert to grayscale
let img = image::open(image_path)?.to_rgb8();
let (width, height) = img.dimensions();
println!("Image loaded: {}x{}", width, height);

// Convert to grayscale
let gray: GrayImage = image::imageops::grayscale(&img);

// Simple thresholding
let binary = gray.clone().into_raw()
let binary = gray
.clone()
.into_raw()
.into_iter()
.map(|p| if p > 127 { 255 } else { 0 })
.collect::<Vec<u8>>();
let binary = GrayImage::from_raw(width, height, binary)
.ok_or("Failed to create binary image")?;
let binary =
GrayImage::from_raw(width, height, binary).ok_or("Failed to create binary image")?;

// Find contours
let contours = find_contours(&binary);
Expand All @@ -58,7 +63,7 @@ impl ImageAnalyzer {

for contour in contours {
let area = contour_area(&contour.points) as f32;

if area >= min_area {
let bounds = min_area_rect(&contour.points);
let x_min = bounds.iter().map(|p| p.x).min().unwrap_or(0) as u32;
Expand All @@ -68,7 +73,8 @@ impl ImageAnalyzer {
let width = x_max - x_min;
let height = y_max - y_min;

let contour_points: Vec<(u32, u32)> = contour.points
let contour_points: Vec<(u32, u32)> = contour
.points
.iter()
.map(|p| (p.x as u32, p.y as u32))
.collect();
Expand Down Expand Up @@ -123,15 +129,18 @@ impl ImageAnalyzer {
}

// Optional: Add a method to visualize the regions
pub fn visualize_regions(&self, result: &SegmentationResult) -> Result<RgbImage, Box<dyn std::error::Error>> {
pub fn visualize_regions(
&self,
result: &SegmentationResult,
) -> Result<RgbImage, Box<dyn std::error::Error>> {
let mut output = RgbImage::new(result.image_size.0, result.image_size.1);

// Draw regions in different colors
for (i, region) in result.regions.iter().enumerate() {
let color = Rgb([
((i * 90) % 255) as u8,
((i * 140) % 255) as u8,
((i * 200) % 255) as u8
((i * 200) % 255) as u8,
]);

// Draw contour
Expand All @@ -148,22 +157,24 @@ impl ImageAnalyzer {

pub fn analyze_image(image_path: &str) -> Result<String, Box<dyn std::error::Error>> {
println!("Reading image from: {}", image_path);

// Read image and convert to grayscale
let img = image::open(image_path)?.to_rgb8();
let (width, height) = img.dimensions();
println!("Image loaded: {}x{}", width, height);

// Convert to grayscale
let gray: GrayImage = image::imageops::grayscale(&img);

// Simple thresholding
let binary = gray.clone().into_raw()
let binary = gray
.clone()
.into_raw()
.into_iter()
.map(|p| if p > 127 { 255 } else { 0 })
.collect::<Vec<u8>>();
let binary = GrayImage::from_raw(width, height, binary)
.ok_or("Failed to create binary image")?;
let binary =
GrayImage::from_raw(width, height, binary).ok_or("Failed to create binary image")?;

// Find contours
let contours = find_contours(&binary);
Expand All @@ -177,7 +188,7 @@ pub fn analyze_image(image_path: &str) -> Result<String, Box<dyn std::error::Err
for contour in contours {
let area = contour_area(&contour.points) as f32;
println!("Contour area: {}", area);

if area >= min_area {
let bounds = min_area_rect(&contour.points);
let x_min = bounds.iter().map(|p| p.x).min().unwrap_or(0) as u32;
Expand All @@ -187,7 +198,8 @@ pub fn analyze_image(image_path: &str) -> Result<String, Box<dyn std::error::Err
let width = x_max - x_min;
let height = y_max - y_min;

let contour_points: Vec<(u32, u32)> = contour.points
let contour_points: Vec<(u32, u32)> = contour
.points
.iter()
.map(|p| (p.x as u32, p.y as u32))
.collect();
Expand Down
1 change: 0 additions & 1 deletion src/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub struct Touch {

impl Touch {
pub fn new(no_touch: bool) -> Self {

let device = if no_touch {
None
} else {
Expand Down

0 comments on commit ad8145a

Please sign in to comment.