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.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
export async function detectMediaDimensionsAndAspect(
|
||||
url: string,
|
||||
type: 'video' | 'image'
|
||||
): Promise<{ width: number; height: number; format: 'video' | 'image'; aspect: string }> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (type === 'image') {
|
||||
const img = new Image();
|
||||
img.onload = () => {
|
||||
resolve({
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
format: 'image',
|
||||
aspect: `${img.width}:${img.height}`,
|
||||
});
|
||||
};
|
||||
img.onerror = () => reject(new Error('Failed to load image to detect dimensions.'));
|
||||
img.src = url;
|
||||
} else {
|
||||
const video = document.createElement('video');
|
||||
video.onloadedmetadata = () => {
|
||||
resolve({
|
||||
width: video.videoWidth,
|
||||
height: video.videoHeight,
|
||||
format: 'video',
|
||||
aspect: `${video.videoWidth}:${video.videoHeight}`,
|
||||
});
|
||||
};
|
||||
video.onerror = () => reject(new Error('Failed to load video to detect dimensions.'));
|
||||
video.src = url;
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user