| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- {% load static %}
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Background Remover</title>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
- <script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource/source-sans-3@5.0.12/index.css"
- integrity="sha256-tXJfXfp6Ewt1ilPzLDtQnJV4hclT9XuaZUKyUvmyr+Q=" crossorigin="anonymous">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/styles/overlayscrollbars.min.css"
- integrity="sha256-dSokZseQNT08wYEWiz5iLI8QPlKxG+TswNRD8k35cpg=" crossorigin="anonymous">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.min.css"
- integrity="sha256-Qsx5lrStHZyR9REqhUF8iQt73X06c8LGIUPzpOhwRrI=" crossorigin="anonymous">
- <link rel="stylesheet" href="{% static './css/adminlte.css' %}">
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/apexcharts@3.37.1/dist/apexcharts.css"
- integrity="sha256-4MX+61mt9NVvvuPjUWdUdyfZfxSB1/Rf9WtqRHgG5S0=" crossorigin="anonymous">
- <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/css/select2.min.css" rel="stylesheet" />
- <link rel="stylesheet" href="{% static './css/select2-bootstrap4.min.css' %}">
- <link rel="stylesheet" href="{% static './css/custom.css' %}">
- <script src="https://cdn.tailwindcss.com"></script>
- <style>
- .bg-remover-container .nav-pills .nav-link { color: #64748b; font-weight: 600; border-radius: 12px; padding: 10px 25px; transition: all 0.3s; border: none; background: none; }
- .bg-remover-container .nav-pills .nav-link.active { background-color: #3b82f6 !important; color: white !important; box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3); }
- .bg-remover-container .upload-card { transition: all 0.3s ease; border: 2px dashed #cbd5e1; background: #ffffff; }
- .upload-card:hover { border-color: #3b82f6; background-color: #f8fafc; transform: translateY(-2px); }
- .preview-box { background: #ffffff; border-radius: 12px; padding: 15px; box-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1); }
- .checkerboard-bg {
- background-image: linear-gradient(45deg, #eee 25%, transparent 25%), linear-gradient(-45deg, #eee 25%, transparent 25%),
- linear-gradient(45deg, transparent 75%, #eee 75%), linear-gradient(-45deg, transparent 75%, #eee 75%);
- background-size: 20px 20px; background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
- }
- </style>
- </head>
- <body class="layout-fixed sidebar-expand-lg sidebar-mini app-loaded sidebar-collapse">
- <div class="app-wrapper">
- {% include 'header.html' %}
- {% include 'sidebar.html' %}
- <main class="app-main">
- <div class="app-content-header">
- <div class="container-fluid">
- <div class="row">
- <div class="col-sm-6">
- <h3 class="mb-0">🪄 Image Background Remover</h3>
- </div>
- <div class="col-sm-6">
- <ol class="breadcrumb float-sm-end">
- <li class="breadcrumb-item"><a href="{% url 'file-upload' %}">Home</a></li>
- <li class="breadcrumb-item active" aria-current="page"><a href="{% url 'product-attributes' %}"></a>
- 🪄 Image Background Remover</a>
- </li>
- </ol>
- </div>
- </div>
- </div>
- </div>
- <div class="app-content pt-10 bg-remover-container">
- <div class="mx-auto px-4">
- <div class="mb-10 flex flex-col md:flex-row md:items-end md:justify-between gap-4 border-b border-gray-100 pb-6">
- <div class="text-left">
- <h1 class="text-4xl font-black text-gray-900 tracking-tight mb-2">
- Image <span class="text-blue-600">Background</span> Remover
- </h1>
- <p class="text-gray-500 font-medium ">
- Professional-grade background removal. Process single images or
- <span class="text-gray-800 font-semibold">batch process ZIP archives </span> instantly.
- </p>
- </div>
- <div class="flex items-center">
- <a href="/bg-remover/tasks/history/"
- class="inline-flex items-center gap-2 px-5 py-2.5 bg-white border border-gray-200 text-gray-700 text-sm font-bold rounded-xl shadow-sm hover:shadow-md hover:border-blue-300 hover:text-blue-600 transition-all duration-200 group">
- <i class="bi bi-clock-history text-gray-400 group-hover:text-blue-500 transition-colors"></i>
- Task History
- <span class="flex h-2 w-2 rounded-full bg-blue-500 ml-1"></span>
- </a>
- </div>
- </div>
- <ul class="nav nav-pills justify-content-center mb-8 gap-3" id="pills-tab" role="tablist">
- <li class="nav-item" role="presentation">
- <button class="nav-link active" id="pills-single-tab" data-bs-toggle="pill" data-bs-target="#pills-single" type="button" role="tab">
- <i class="bi bi-image mr-2"></i> Single Image
- </button>
- </li>
- <li class="nav-item" role="presentation">
- <button class="nav-link" id="pills-bulk-tab" data-bs-toggle="pill" data-bs-target="#pills-bulk" type="button" role="tab">
- <i class="bi bi-images mr-2"></i> Bulk & ZIP Process
- </button>
- </li>
- </ul>
- <div class="tab-content" id="pills-tabContent">
- <!-- NEW: Choose Action -->
-
- <!-- SINGLE IMAGE TAB -->
- <div class="tab-pane fade show active" id="pills-single" role="tabpanel">
- <div class="mt-6 flex justify-center gap-6">
- <label class="inline-flex items-center gap-2">
- <input type="radio" name="action" value="remove_bg" checked>
- <span>Remove Background</span>
- </label>
- <label class="inline-flex items-center gap-2">
- <input type="radio" name="action" value="caption">
- <span>Generate Caption</span>
- </label>
- </div>
- <div id="uploadContainer" class="bg-white p-8 rounded-3xl shadow-sm border border-gray-200 mb-8">
- <div id="drop-zone" class="upload-card flex flex-col items-center justify-center rounded-2xl p-12 cursor-pointer" onclick="document.getElementById('file-input').click()">
- <input type="file" id="file-input" class="hidden" accept="image/*">
- <div class="bg-blue-50 p-5 rounded-full mb-4 text-blue-600">
- <i class="bi bi-cloud-arrow-up text-4xl"></i>
- </div>
- <p class="text-lg font-semibold text-gray-700">Click or Drag & Drop Image</p>
- <p class="text-sm text-gray-400 mt-1">Supports PNG, JPG, WEBP</p>
- </div>
- </div>
-
- <!-- RESULTS -->
- <div id="result-container" class="hidden space-y-8 pb-20">
- <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
- <div class="space-y-3">
- <span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Original</span>
- <div class="preview-box"><img id="original-preview" class="rounded-lg w-full h-auto object-contain max-h-[400px]"></div>
- </div>
- <div class="space-y-3">
- <div class="flex justify-between items-center px-1">
- <span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Processed</span>
- <span id="processing-badge" class="hidden animate-pulse bg-blue-100 text-blue-700 px-2 py-0.5 rounded text-[10px] font-bold">PROCESSING...</span>
- </div>
- <div class="preview-box relative min-h-[200px] flex items-center justify-center checkerboard-bg rounded-lg p-4">
- <div id="result-loader" class="absolute inset-0 bg-white/60 backdrop-blur-sm flex flex-col items-center justify-center rounded-lg z-10 hidden">
- <div class="w-10 h-10 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
- </div>
- <!-- Image element -->
- <img id="processed-preview" class="rounded-lg w-full h-auto object-contain max-h-[400px]">
- <!-- Caption element -->
- <div id="caption-text" class="hidden text-center text-gray-800 font-semibold text-lg"></div>
- </div>
- </div>
- <!-- <div class="space-y-3">
- <div class="flex justify-between items-center px-1">
- <span class="text-xs font-bold uppercase text-gray-400 tracking-wider">Processed</span>
- <span id="processing-badge" class="hidden animate-pulse bg-blue-100 text-blue-700 px-2 py-0.5 rounded text-[10px] font-bold">PROCESSING...</span>
- </div>
- <div class="preview-box relative min-h-[200px] flex items-center justify-center checkerboard-bg rounded-lg">
- <div id="result-loader" class="absolute inset-0 bg-white/60 backdrop-blur-sm flex flex-col items-center justify-center rounded-lg z-10 hidden">
- <div class="w-10 h-10 border-4 border-blue-600 border-t-transparent rounded-full animate-spin"></div>
- </div>
- <img id="processed-preview" class="rounded-lg w-full h-auto object-contain max-h-[400px]">
- </div>
- </div> -->
- </div>
- <!-- NEW CAPTION SECTION -->
- <div id="caption-container" class="hidden mt-4 text-center">
- <h3 class="text-lg font-semibold text-gray-700">AI Generated Caption:</h3>
- <p id="caption-text-below" class="text-blue-600 font-bold text-xl mt-1"></p>
- </div>
- <div class="flex gap-3 justify-center mt-4">
- <button onclick="location.reload()" class="px-6 py-2 bg-gray-100 rounded-xl font-bold hover:bg-gray-200">New Upload</button>
- <a id="download-btn" class="hidden px-8 py-2 bg-green-600 text-white rounded-xl font-bold shadow-lg hover:bg-green-700">Download PNG</a>
- </div>
- </div>
- </div>
- <!-- BULK TAB -->
- <div class="tab-pane fade" id="pills-bulk" role="tabpanel">
- <div class="mb-6 flex justify-center gap-6">
- <label class="inline-flex items-center gap-2">
- <input type="radio" name="bulk-action" value="remove_bg" checked>
- <span class="font-semibold text-gray-700">Bulk Remove Background</span>
- </label>
- <label class="inline-flex items-center gap-2">
- <input type="radio" name="bulk-action" value="caption">
- <span class="font-semibold text-gray-700">Bulk Generate Caption</span>
- </label>
- </div>
- <div class="bg-white p-8 rounded-3xl shadow-sm border border-gray-200 mb-8">
- <div id="bulk-drop-zone" class="upload-card flex flex-col items-center justify-center rounded-2xl p-12 cursor-pointer transition-all border-indigo-200" onclick="document.getElementById('bulk-file-input').click()">
- <input type="file" id="bulk-file-input" class="hidden" accept="image/*,.zip" multiple>
- <div class="bg-indigo-50 p-5 rounded-full mb-4 text-indigo-600">
- <i class="bi bi-file-earmark-zip text-4xl"></i>
- </div>
- <h3 class="text-xl font-bold text-gray-800">Bulk & ZIP Upload</h3>
- <p class="text-gray-500 mt-1 text-center">Upload multiple images or a <b>single ZIP file</b></p>
- </div>
- <div id="bulk-list-container" class="hidden mt-8 border-t pt-8">
- <div class="flex justify-between items-center mb-6">
- <div>
- <h3 class="font-extrabold text-gray-900 text-lg">Batch Queue</h3>
- <p class="text-xs text-gray-400 uppercase font-bold tracking-widest"><span id="file-count">0</span> Items Ready</p>
- </div>
- <button id="start-bulk-btn" class="bg-indigo-600 text-white px-8 py-3 rounded-xl font-bold hover:bg-indigo-700 shadow-lg">Start Processing</button>
- </div>
- <div id="file-queue" class="grid grid-cols-2 sm:grid-cols-4 md:grid-cols-6 gap-4"></div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- <div id="bulk-loader" class="hidden fixed inset-0 bg-black/60 backdrop-blur-md z-[9999] flex flex-col items-center justify-center text-white">
- <div class="flex flex-col items-center p-8 bg-gray-900/80 rounded-3xl border border-white/10 shadow-2xl">
- <div class="w-16 h-16 border-4 border-indigo-500 border-t-transparent rounded-full animate-spin mb-4"></div>
- <h2 class="text-2xl font-bold">Processing Batch...</h2>
- <p class="text-gray-400 mt-2">AI is removing backgrounds and preparing your ZIP.</p>
- <p class="text-xs text-indigo-400 mt-4 uppercase tracking-widest font-bold">Please do not refresh the page</p>
- </div>
- </div>
- </main>
- {% include 'footer.html' %}
- </div>
- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.8/dist/umd/popper.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/overlayscrollbars@2.3.0/browser/overlayscrollbars.browser.es6.min.js"></script>
- <script src="{% static './js/adminlte.js' %}"></script>
- <script>
- // SINGLE IMAGE UPLOAD + CAPTION
- const fileInput = document.getElementById('file-input');
- if (fileInput) {
- fileInput.onchange = (e) => handleSingleUpload(e.target.files[0]);
- }
- // async function handleSingleUpload(file) {
- // if (!file) return;
- // document.getElementById('uploadContainer').classList.add('hidden');
- // document.getElementById('result-container').classList.remove('hidden');
- // document.getElementById('result-loader').classList.remove('hidden');
- // document.getElementById('original-preview').src = URL.createObjectURL(file);
- // const formData = new FormData();
- // formData.append('image', file);
- // try {
- // // --- REMOVE BG ---
- // const response = await fetch('/bg-remover/remove-bg/', {
- // method: 'POST',
- // body: formData,
- // headers: {'X-CSRFToken': '{{ csrf_token }}'}
- // });
- // if (!response.ok) throw new Error("Processing failed");
- // const blob = await response.blob();
- // const url = URL.createObjectURL(blob);
- // document.getElementById('processed-preview').src = url;
- // const btn = document.getElementById('download-btn');
- // btn.href = url;
- // btn.download = `cleared_${file.name.split('.')[0]}.png`;
- // btn.classList.remove('hidden');
- // // --- GET CAPTION ---
- // const captionResp = await fetch('/bg-remover/caption/', {
- // method: 'POST',
- // body: formData,
- // headers: {'X-CSRFToken': '{{ csrf_token }}'}
- // });
- // if (captionResp.ok) {
- // const data = await captionResp.json();
- // document.getElementById('caption-text').innerText = data.caption;
- // document.getElementById('caption-container').classList.remove('hidden');
- // }
- // } catch (err) {
- // alert("Error processing image: " + err.message);
- // location.reload();
- // } finally {
- // document.getElementById('result-loader').classList.add('hidden');
- // }
- // }
- async function handleSingleUpload(file) {
- if (!file) return;
- const selectedAction = document.querySelector('input[name="action"]:checked').value;
- document.getElementById('uploadContainer').classList.add('hidden');
- document.getElementById('result-container').classList.remove('hidden');
- document.getElementById('result-loader').classList.remove('hidden');
- document.getElementById('original-preview').src = URL.createObjectURL(file);
- const formData = new FormData();
- formData.append('image', file);
- try {
- if (selectedAction === 'remove_bg') {
- // --- REMOVE BG ---
- const response = await fetch('/bg-remover/remove-bg/', {
- method: 'POST',
- body: formData,
- headers: {'X-CSRFToken': '{{ csrf_token }}'}
- });
- if (!response.ok) throw new Error("Background removal failed");
- const blob = await response.blob();
- const url = URL.createObjectURL(blob);
- document.getElementById('processed-preview').src = url;
- document.getElementById('processed-preview').classList.remove('hidden');
- document.getElementById('caption-text').classList.add('hidden');
- const btn = document.getElementById('download-btn');
- btn.href = url;
- btn.download = `cleared_${file.name.split('.')[0]}.png`;
- btn.classList.remove('hidden');
- document.getElementById('caption-container').classList.add('hidden'); // hide caption
- document.getElementById('caption-text-below').classList.add('hidden'); // hide caption
-
- } else if (selectedAction === 'caption') {
- // --- GENERATE CAPTION ---
- const captionResp = await fetch('/bg-remover/caption/', {
- method: 'POST',
- body: formData,
- headers: {'X-CSRFToken': '{{ csrf_token }}'}
- });
- if (!captionResp.ok) throw new Error("Caption generation failed");
- const data = await captionResp.json();
- document.getElementById('caption-text').innerText = data.caption;
- document.getElementById('caption-container').classList.remove('hidden');
- document.getElementById('caption-text-below').classList.remove('hidden');
-
- document.getElementById('caption-text').classList.remove('hidden');
- document.getElementById('processed-preview').classList.add('hidden');
- document.getElementById('download-btn').classList.add('hidden');
- // document.getElementById('processed-preview').src = ''; // no image
- // document.getElementById('download-btn').classList.add('hidden');
- }
- } catch (err) {
- alert("Error: " + err.message);
- location.reload();
- } finally {
- document.getElementById('result-loader').classList.add('hidden');
- }
- }
- // BULK LOGIC PRESERVED
- const bulkInput = document.getElementById('bulk-file-input');
- const fileQueue = document.getElementById('file-queue');
- const bulkList = document.getElementById('bulk-list-container');
- const startBulkBtn = document.getElementById('start-bulk-btn');
- const bulkLoader = document.getElementById('bulk-loader');
- if (bulkInput) {
- bulkInput.onchange = (e) => {
- const files = Array.from(e.target.files);
- if (files.length === 0) return;
- bulkList.classList.remove('hidden');
- document.getElementById('file-count').innerText = files.length;
- fileQueue.innerHTML = '';
- files.forEach(file => {
- const div = document.createElement('div');
- div.className = "relative bg-gray-50 rounded-xl border border-gray-100 p-2 text-center";
- if (file.name.toLowerCase().endsWith('.zip')) {
- div.innerHTML = `<div class="aspect-square flex flex-col items-center justify-center text-indigo-500"><i class="bi bi-file-earmark-zip-fill text-4xl"></i><span class="text-[10px] font-bold mt-1 truncate w-full px-2">${file.name}</span></div>`;
- } else {
- const reader = new FileReader();
- reader.onload = (event) => {
- div.innerHTML = `<div class="aspect-square rounded-lg overflow-hidden bg-white mb-1"><img src="${event.target.result}" class="w-full h-full object-cover"></div><span class="text-[9px] font-bold text-gray-400 truncate block">${file.name}</span>`;
- };
- reader.readAsDataURL(file);
- }
- fileQueue.appendChild(div);
- });
- };
- }
- if (startBulkBtn) {
- startBulkBtn.onclick = async () => {
- const files = bulkInput.files;
- if (!files.length) return;
- // 1. Identify which action is selected for bulk
- const selectedAction = document.querySelector('input[name="bulk-action"]:checked').value;
- const formData = new FormData();
-
- // Use 'images' as the key to match your single image logic/backend expectation
- for (let i = 0; i < files.length; i++) {
- formData.append('images', files[i]);
- }
-
- const loader = document.getElementById('bulk-loader');
- const loaderTitle = loader.querySelector('h2');
- const loaderSub = loader.querySelector('p');
- // Update loader text based on action
- if (selectedAction === 'caption') {
- loaderTitle.innerText = "Generating Captions...";
- loaderSub.innerText = "AI is describing your images and preparing a report.";
- } else {
- loaderTitle.innerText = "Processing Batch...";
- loaderSub.innerText = "AI is removing backgrounds and preparing your ZIP.";
- }
- loader.classList.remove('hidden');
- try {
- // 2. Determine URL based on action
- const endpoint = selectedAction === 'caption'
- ? '/bg-remover/caption/bulk/'
- : '/bg-remover/remove-bg/bulk/';
- const response = await fetch(endpoint, {
- method: 'POST',
- body: formData,
- headers: {'X-CSRFToken': '{{ csrf_token }}'}
- });
- if (response.ok) {
- const data = await response.json();
-
- if (selectedAction === 'caption') {
- // If your caption bulk returns an Excel or CSV file
- if (data.file_url) {
- window.location.href = data.file_url;
- } else {
- alert("Captions generated successfully. Check Task History.");
- }
- } else {
- // Standard ZIP download for background removal
- window.location.href = data.zip_url;
- }
- } else {
- const errorData = await response.json();
- alert("Batch processing failed: " + (errorData.error || "Unknown error"));
- }
- } catch(err) {
- console.error(err);
- alert("An error occurred during batch processing.");
- } finally {
- loader.classList.add('hidden');
- }
- };
- }
- // if (startBulkBtn) {
- // startBulkBtn.onclick = async () => {
- // const files = bulkInput.files;
- // if (!files.length) return;
- // const formData = new FormData();
- // for (let i = 0; i < files.length; i++) formData.append('images', files[i]);
-
- // document.getElementById('bulk-loader').classList.remove('hidden');
- // try {
- // const response = await fetch('/bg-remover/remove-bg/bulk/', {
- // method: 'POST',
- // body: formData,
- // headers: {'X-CSRFToken': '{{ csrf_token }}'}
- // });
- // if (response.ok) {
- // window.location.href = await response.json().then(data => data.zip_url);
- // } else { alert("Batch processing failed."); }
- // } catch(err) { alert(err); }
- // finally { bulkLoader.classList.add('hidden'); }
- // };
- // }
- </script>
- </body>
- </html>
|