added api for ai; got backend working
This commit is contained in:
66
src-tauri/src/background_removal.rs
Normal file
66
src-tauri/src/background_removal.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use std::process::Command;
|
||||
use serde_json;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct BackgroundRemovalStatus {
|
||||
pub available: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct BackgroundRemovalResult {
|
||||
pub output_path: String,
|
||||
}
|
||||
|
||||
/// Check if background removal is available
|
||||
pub fn is_available() -> Result<bool, String> {
|
||||
let python_exe = crate::paths::python_exe();
|
||||
let python_exe = python_exe.to_str().unwrap_or_default();
|
||||
let script_path = crate::paths::backend_script("background_removal.py");
|
||||
let script_path = script_path.to_str().unwrap_or_default();
|
||||
|
||||
let output = Command::new(python_exe)
|
||||
.args(&[script_path, "is_available"])
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run Python script: {}", e))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("Python script failed: {}", stderr));
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let result: BackgroundRemovalStatus = serde_json::from_str(&stdout.trim())
|
||||
.map_err(|e| format!("Failed to parse JSON: {}", e))?;
|
||||
|
||||
Ok(result.available)
|
||||
}
|
||||
|
||||
/// Remove background on export (placeholder for Phase 5)
|
||||
pub fn remove_background_on_export(
|
||||
input_path: &str,
|
||||
output_path: &str,
|
||||
replacement: &str,
|
||||
replacement_value: &str,
|
||||
) -> Result<String, String> {
|
||||
let python_exe = crate::paths::python_exe();
|
||||
let python_exe = python_exe.to_str().unwrap_or_default();
|
||||
let script_path = crate::paths::backend_script("background_removal.py");
|
||||
let script_path = script_path.to_str().unwrap_or_default();
|
||||
|
||||
let output = Command::new(python_exe)
|
||||
.args(&[script_path, "remove_background_on_export", input_path, output_path, replacement, replacement_value])
|
||||
.output()
|
||||
.map_err(|e| format!("Failed to run Python script: {}", e))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
return Err(format!("Python script failed: {}", stderr));
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let result: BackgroundRemovalResult = serde_json::from_str(&stdout.trim())
|
||||
.map_err(|e| format!("Failed to parse JSON: {}", e))?;
|
||||
|
||||
Ok(result.output_path)
|
||||
}
|
||||
Reference in New Issue
Block a user