Spaces:
Running
Running
create a tool to smart select clothes on uploaded image
Browse files- index.html +17 -6
- script.js +99 -2
index.html
CHANGED
|
@@ -63,19 +63,30 @@
|
|
| 63 |
<i data-feather="moon" class="mr-2"></i>
|
| 64 |
Toggle Theme
|
| 65 |
</button>
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
<h2 class="text-xl font-semibold mb-4 text-gray-800 dark:text-white">Upload Image</h2>
|
| 69 |
<input type="file" id="imageUpload" accept="image/*" class="hidden">
|
| 70 |
<label for="imageUpload" class="cursor-pointer px-4 py-2 bg-secondary-500 hover:bg-secondary-600 text-white rounded-lg inline-flex items-center">
|
| 71 |
<i data-feather="upload" class="mr-2"></i>
|
| 72 |
-
|
| 73 |
</label>
|
| 74 |
-
<div id="imagePreview" class="mt-4 hidden">
|
| 75 |
<img id="previewImage" class="max-w-full h-auto rounded-lg" src="#" alt="Preview">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
</div>
|
| 77 |
</div>
|
| 78 |
-
|
| 79 |
</main>
|
| 80 |
|
| 81 |
<custom-footer></custom-footer>
|
|
|
|
| 63 |
<i data-feather="moon" class="mr-2"></i>
|
| 64 |
Toggle Theme
|
| 65 |
</button>
|
| 66 |
+
<div class="bg-white dark:bg-gray-800 p-6 rounded-xl shadow-lg max-w-3xl mx-auto">
|
| 67 |
+
<h2 class="text-xl font-semibold mb-4 text-gray-800 dark:text-white">Smart Clothing Selector</h2>
|
|
|
|
| 68 |
<input type="file" id="imageUpload" accept="image/*" class="hidden">
|
| 69 |
<label for="imageUpload" class="cursor-pointer px-4 py-2 bg-secondary-500 hover:bg-secondary-600 text-white rounded-lg inline-flex items-center">
|
| 70 |
<i data-feather="upload" class="mr-2"></i>
|
| 71 |
+
Upload Image
|
| 72 |
</label>
|
| 73 |
+
<div id="imagePreview" class="mt-4 hidden relative">
|
| 74 |
<img id="previewImage" class="max-w-full h-auto rounded-lg" src="#" alt="Preview">
|
| 75 |
+
<canvas id="selectionCanvas" class="absolute inset-0 w-full h-full pointer-events-none"></canvas>
|
| 76 |
+
</div>
|
| 77 |
+
<div id="toolsPanel" class="mt-4 hidden space-x-2">
|
| 78 |
+
<button id="selectClothes" class="px-4 py-2 bg-primary-500 hover:bg-primary-600 text-white rounded-lg">
|
| 79 |
+
<i data-feather="box" class="mr-2"></i>Select Clothes
|
| 80 |
+
</button>
|
| 81 |
+
<button id="clearSelection" class="px-4 py-2 bg-gray-500 hover:bg-gray-600 text-white rounded-lg">
|
| 82 |
+
<i data-feather="x" class="mr-2"></i>Clear
|
| 83 |
+
</button>
|
| 84 |
+
<button id="downloadSelection" class="px-4 py-2 bg-green-500 hover:bg-green-600 text-white rounded-lg">
|
| 85 |
+
<i data-feather="download" class="mr-2"></i>Download
|
| 86 |
+
</button>
|
| 87 |
</div>
|
| 88 |
</div>
|
| 89 |
+
</div>
|
| 90 |
</main>
|
| 91 |
|
| 92 |
<custom-footer></custom-footer>
|
script.js
CHANGED
|
@@ -40,11 +40,20 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 40 |
}
|
| 41 |
}
|
| 42 |
});
|
| 43 |
-
|
| 44 |
-
// Image upload and preview functionality
|
| 45 |
const imageUpload = document.getElementById('imageUpload');
|
| 46 |
const imagePreview = document.getElementById('imagePreview');
|
| 47 |
const previewImage = document.getElementById('previewImage');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
imageUpload.addEventListener('change', function(e) {
|
| 50 |
const file = e.target.files[0];
|
|
@@ -52,11 +61,99 @@ document.addEventListener('DOMContentLoaded', () => {
|
|
| 52 |
const reader = new FileReader();
|
| 53 |
|
| 54 |
reader.onload = function(event) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
previewImage.src = event.target.result;
|
| 56 |
imagePreview.classList.remove('hidden');
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
reader.readAsDataURL(file);
|
| 60 |
}
|
| 61 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
});
|
|
|
|
| 40 |
}
|
| 41 |
}
|
| 42 |
});
|
| 43 |
+
// Image upload and clothing selection functionality
|
|
|
|
| 44 |
const imageUpload = document.getElementById('imageUpload');
|
| 45 |
const imagePreview = document.getElementById('imagePreview');
|
| 46 |
const previewImage = document.getElementById('previewImage');
|
| 47 |
+
const toolsPanel = document.getElementById('toolsPanel');
|
| 48 |
+
const selectClothesBtn = document.getElementById('selectClothes');
|
| 49 |
+
const clearSelectionBtn = document.getElementById('clearSelection');
|
| 50 |
+
const downloadSelectionBtn = document.getElementById('downloadSelection');
|
| 51 |
+
const selectionCanvas = document.getElementById('selectionCanvas');
|
| 52 |
+
const ctx = selectionCanvas.getContext('2d');
|
| 53 |
+
|
| 54 |
+
let imageLoaded = false;
|
| 55 |
+
let selectionMode = false;
|
| 56 |
+
let selectionPoints = [];
|
| 57 |
|
| 58 |
imageUpload.addEventListener('change', function(e) {
|
| 59 |
const file = e.target.files[0];
|
|
|
|
| 61 |
const reader = new FileReader();
|
| 62 |
|
| 63 |
reader.onload = function(event) {
|
| 64 |
+
previewImage.onload = function() {
|
| 65 |
+
// Set canvas dimensions to match image
|
| 66 |
+
selectionCanvas.width = previewImage.width;
|
| 67 |
+
selectionCanvas.height = previewImage.height;
|
| 68 |
+
imageLoaded = true;
|
| 69 |
+
toolsPanel.classList.remove('hidden');
|
| 70 |
+
};
|
| 71 |
previewImage.src = event.target.result;
|
| 72 |
imagePreview.classList.remove('hidden');
|
| 73 |
+
selectionPoints = [];
|
| 74 |
+
ctx.clearRect(0, 0, selectionCanvas.width, selectionCanvas.height);
|
| 75 |
}
|
| 76 |
|
| 77 |
reader.readAsDataURL(file);
|
| 78 |
}
|
| 79 |
});
|
| 80 |
+
|
| 81 |
+
// Initialize clothing selection
|
| 82 |
+
selectClothesBtn.addEventListener('click', function() {
|
| 83 |
+
selectionMode = true;
|
| 84 |
+
selectionPoints = [];
|
| 85 |
+
ctx.clearRect(0, 0, selectionCanvas.width, selectionCanvas.height);
|
| 86 |
+
previewImage.style.cursor = 'crosshair';
|
| 87 |
+
});
|
| 88 |
+
|
| 89 |
+
// Clear selection
|
| 90 |
+
clearSelectionBtn.addEventListener('click', function() {
|
| 91 |
+
selectionPoints = [];
|
| 92 |
+
ctx.clearRect(0, 0, selectionCanvas.width, selectionCanvas.height);
|
| 93 |
+
previewImage.style.cursor = 'default';
|
| 94 |
+
selectionMode = false;
|
| 95 |
+
});
|
| 96 |
+
|
| 97 |
+
// Download selection
|
| 98 |
+
downloadSelectionBtn.addEventListener('click', function() {
|
| 99 |
+
if (selectionPoints.length > 0) {
|
| 100 |
+
const tempCanvas = document.createElement('canvas');
|
| 101 |
+
const tempCtx = tempCanvas.getContext('2d');
|
| 102 |
+
tempCanvas.width = previewImage.width;
|
| 103 |
+
tempCanvas.height = previewImage.height;
|
| 104 |
+
|
| 105 |
+
// Draw the selected area
|
| 106 |
+
tempCtx.drawImage(previewImage, 0, 0);
|
| 107 |
+
|
| 108 |
+
// Create mask for selection
|
| 109 |
+
tempCtx.globalCompositeOperation = 'destination-in';
|
| 110 |
+
tempCtx.fillStyle = 'black';
|
| 111 |
+
tempCtx.beginPath();
|
| 112 |
+
tempCtx.moveTo(selectionPoints[0].x, selectionPoints[0].y);
|
| 113 |
+
for (let i = 1; i < selectionPoints.length; i++) {
|
| 114 |
+
tempCtx.lineTo(selectionPoints[i].x, selectionPoints[i].y);
|
| 115 |
+
}
|
| 116 |
+
tempCtx.closePath();
|
| 117 |
+
tempCtx.fill();
|
| 118 |
+
|
| 119 |
+
// Create download link
|
| 120 |
+
const link = document.createElement('a');
|
| 121 |
+
link.download = 'selected-clothing.png';
|
| 122 |
+
link.href = tempCanvas.toDataURL('image/png');
|
| 123 |
+
link.click();
|
| 124 |
+
}
|
| 125 |
+
});
|
| 126 |
+
|
| 127 |
+
// Handle selection drawing
|
| 128 |
+
imagePreview.addEventListener('click', function(e) {
|
| 129 |
+
if (!selectionMode || !imageLoaded) return;
|
| 130 |
+
|
| 131 |
+
const rect = previewImage.getBoundingClientRect();
|
| 132 |
+
const x = e.clientX - rect.left;
|
| 133 |
+
const y = e.clientY - rect.top;
|
| 134 |
+
|
| 135 |
+
selectionPoints.push({x, y});
|
| 136 |
+
|
| 137 |
+
// Draw selection
|
| 138 |
+
ctx.clearRect(0, 0, selectionCanvas.width, selectionCanvas.height);
|
| 139 |
+
ctx.strokeStyle = '#3B82F6';
|
| 140 |
+
ctx.lineWidth = 2;
|
| 141 |
+
ctx.fillStyle = 'rgba(59, 130, 246, 0.2)';
|
| 142 |
+
|
| 143 |
+
if (selectionPoints.length > 0) {
|
| 144 |
+
ctx.beginPath();
|
| 145 |
+
ctx.moveTo(selectionPoints[0].x, selectionPoints[0].y);
|
| 146 |
+
|
| 147 |
+
for (let i = 1; i < selectionPoints.length; i++) {
|
| 148 |
+
ctx.lineTo(selectionPoints[i].x, selectionPoints[i].y);
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
if (selectionPoints.length > 2) {
|
| 152 |
+
ctx.closePath();
|
| 153 |
+
ctx.fill();
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
ctx.stroke();
|
| 157 |
+
}
|
| 158 |
+
});
|
| 159 |
});
|