Refactor: remove AGPL imgly dependency and migrate background removal to python backend
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
import { uploadMedia } from './mediaUploader';
|
||||
|
||||
const API_BASE_URL = 'http://localhost:8000'; // Default port for the background-remover service
|
||||
|
||||
export async function removeImageBackground(imageSrc: string | Blob): Promise<string> {
|
||||
try {
|
||||
let sourceBlob: Blob;
|
||||
if (typeof imageSrc === 'string') {
|
||||
// Fetch the image as a Blob first
|
||||
const res = await fetch(imageSrc);
|
||||
if (!res.ok) throw new Error('Failed to fetch image source');
|
||||
sourceBlob = await res.blob();
|
||||
} else {
|
||||
sourceBlob = imageSrc;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', sourceBlob, 'image_to_process.png');
|
||||
|
||||
console.log('[AI Background Removal] Enviando imagen al servicio Python local...');
|
||||
const response = await fetch(`${API_BASE_URL}/api/v1/remove-image-background`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Error en el servicio de IA: ${response.statusText}`);
|
||||
}
|
||||
|
||||
const imageBlob = await response.blob();
|
||||
|
||||
// Convertimos el blob devuelto por la IA en un archivo físico virtual
|
||||
const file = new File([imageBlob], `removed-bg-${Date.now()}.png`, { type: 'image/png' });
|
||||
|
||||
// Subimos la imagen al servidor interno para tener una URL persistente (/api/media/...)
|
||||
const result = await uploadMedia(file);
|
||||
return result.url;
|
||||
} catch (error) {
|
||||
console.error('Error al remover el fondo con IA:', error);
|
||||
throw new Error('No se pudo remover el fondo de la imagen.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user