veltrixcode commited on
Commit
0e8bb83
·
verified ·
1 Parent(s): a1c8ed3

Upload 5 files

Browse files
Files changed (3) hide show
  1. Dockerfile +1 -3
  2. app.py +5 -2
  3. templates/index.html +374 -66
Dockerfile CHANGED
@@ -1,8 +1,6 @@
1
  FROM python:3.9
2
 
3
- # Download model from internet
4
- RUN mkdir -p /home/.u2net && \
5
- curl -L -o /home/.u2net/u2net.onnx https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx
6
 
7
  WORKDIR /app
8
 
 
1
  FROM python:3.9
2
 
3
+ # Model will be auto-downloaded by rembg on first use
 
 
4
 
5
  WORKDIR /app
6
 
app.py CHANGED
@@ -1,10 +1,13 @@
1
  from flask import Flask, render_template, request, send_file
2
- from rembg import remove
3
  from PIL import Image
4
  from io import BytesIO
5
 
6
  app = Flask(__name__)
7
 
 
 
 
8
  @app.route('/', methods=['GET', 'POST'])
9
  def upload_file():
10
  if request.method == 'POST':
@@ -15,7 +18,7 @@ def upload_file():
15
  return 'No file selected', 400
16
  if file:
17
  input_image = Image.open(file.stream)
18
- output_image = remove(input_image, post_process_mask=True)
19
  img_io = BytesIO()
20
  output_image.save(img_io, 'PNG')
21
  img_io.seek(0)
 
1
  from flask import Flask, render_template, request, send_file
2
+ from rembg import remove, new_session
3
  from PIL import Image
4
  from io import BytesIO
5
 
6
  app = Flask(__name__)
7
 
8
+ # Use isnet-general-use for better quality (alternatives: birefnet-general, u2net_human_seg)
9
+ session = new_session("isnet-general-use")
10
+
11
  @app.route('/', methods=['GET', 'POST'])
12
  def upload_file():
13
  if request.method == 'POST':
 
18
  return 'No file selected', 400
19
  if file:
20
  input_image = Image.open(file.stream)
21
+ output_image = remove(input_image, session=session, post_process_mask=True)
22
  img_io = BytesIO()
23
  output_image.save(img_io, 'PNG')
24
  img_io.seek(0)
templates/index.html CHANGED
@@ -1,80 +1,388 @@
1
  <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <title>✂️ rmbg</title>
 
 
 
5
  <style>
6
- body {
7
- height: 100vh;
8
- display: flex;
9
-
10
- align-items: center;
11
- justify-content: center;
12
- text-align: center;
13
- }
14
-
15
- .drop-zone {
16
- border: 2px dashed #ccc;
17
- padding: 20px;
18
- width: 80vw;
19
- height: 80vh;
20
- display: flex;
21
- flex-direction: column;
22
- align-items: center;
23
- justify-content: center;
24
- cursor: pointer;
25
- }
26
-
27
- .drop-zone.dragover {
28
- background-color: #f0f0f0;
29
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  </style>
31
- </head>
32
- <body>
33
- <div id="dropZone" class="drop-zone">
34
- <h1>rmbg</h1>
35
- <form
36
- id="uploadForm"
37
- action="/"
38
- method="post"
39
- enctype="multipart/form-data"
40
- >
41
- <input id="fileInput" type="file" name="file" />
42
- <input type="submit" value="rm" />
43
- </form>
44
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
  <script>
47
- let dropZone = document.getElementById("dropZone");
48
- let fileInput = document.getElementById("fileInput");
49
- let uploadForm = document.getElementById("uploadForm");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- dropZone.addEventListener("click", function () {
52
- fileInput.click();
53
- });
 
 
 
54
 
55
- fileInput.addEventListener("change", function () {
56
- if (fileInput.files.length > 0) {
57
- uploadForm.submit();
 
 
 
 
 
 
 
 
 
 
 
 
58
  }
59
- });
60
 
61
- dropZone.addEventListener("dragover", function (e) {
62
- this.classList.add("dragover");
63
- });
 
 
 
 
 
 
 
 
 
 
64
 
65
- dropZone.addEventListener("dragleave", function (e) {
66
- this.classList.remove("dragover");
67
- });
68
 
69
- dropZone.addEventListener("drop", function (e) {
70
- e.preventDefault();
71
- e.stopPropagation();
72
- this.classList.remove("dragover");
73
 
74
- let file = e.dataTransfer.files[0];
75
- fileInput.files = e.dataTransfer.files;
76
- uploadForm.submit();
77
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  </script>
79
- </body>
80
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Remove Background - Fast & Free</title>
7
+ <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
8
  <style>
9
+ :root {
10
+ --primary: #0066ff;
11
+ --primary-hover: #0052cc;
12
+ --bg-color: #f5f7fa;
13
+ --card-bg: #ffffff;
14
+ --text-main: #1a1a1a;
15
+ --text-sub: #667085;
16
+ --border: #e4e7ec;
17
+ }
18
+
19
+ body {
20
+ font-family: 'Inter', sans-serif;
21
+ background-color: var(--bg-color);
22
+ margin: 0;
23
+ padding: 0;
24
+ color: var(--text-main);
25
+ min-height: 100vh;
26
+ display: flex;
27
+ flex-direction: column;
28
+ }
29
+
30
+ /* Header */
31
+ header {
32
+ background: var(--card-bg);
33
+ padding: 1rem 2rem;
34
+ box-shadow: 0 1px 3px rgba(0,0,0,0.05);
35
+ display: flex;
36
+ align-items: center;
37
+ justify-content: space-between;
38
+ }
39
+
40
+ .logo {
41
+ font-weight: 700;
42
+ font-size: 1.5rem;
43
+ color: var(--primary);
44
+ display: flex;
45
+ align-items: center;
46
+ gap: 0.5rem;
47
+ }
48
+
49
+ /* Main Content */
50
+ main {
51
+ flex: 1;
52
+ display: flex;
53
+ flex-direction: column;
54
+ align-items: center;
55
+ justify-content: center;
56
+ padding: 2rem;
57
+ max-width: 1200px;
58
+ margin: 0 auto;
59
+ width: 100%;
60
+ box-sizing: border-box;
61
+ }
62
+
63
+ h1 {
64
+ font-size: 2.5rem;
65
+ margin-bottom: 1rem;
66
+ text-align: center;
67
+ color: #101828;
68
+ }
69
+
70
+ p.subtitle {
71
+ color: var(--text-sub);
72
+ font-size: 1.1rem;
73
+ margin-bottom: 3rem;
74
+ text-align: center;
75
+ }
76
+
77
+ /* Upload Card */
78
+ .upload-card {
79
+ background: var(--card-bg);
80
+ border-radius: 12px;
81
+ box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
82
+ padding: 3rem;
83
+ width: 100%;
84
+ max-width: 600px;
85
+ text-align: center;
86
+ transition: all 0.3s ease;
87
+ }
88
+
89
+ .drop-zone {
90
+ border: 2px dashed #cdcdcd;
91
+ border-radius: 8px;
92
+ padding: 3rem 1rem;
93
+ background: #fafafa;
94
+ cursor: pointer;
95
+ transition: all 0.2s ease;
96
+ position: relative;
97
+ }
98
+
99
+ .drop-zone:hover, .drop-zone.dragover {
100
+ border-color: var(--primary);
101
+ background: #f0f7ff;
102
+ }
103
+
104
+ .btn-upload {
105
+ background-color: var(--primary);
106
+ color: white;
107
+ padding: 12px 28px;
108
+ border-radius: 50px;
109
+ font-weight: 600;
110
+ font-size: 1.1rem;
111
+ border: none;
112
+ cursor: pointer;
113
+ transition: background 0.2s;
114
+ display: inline-flex;
115
+ align-items: center;
116
+ gap: 8px;
117
+ box-shadow: 0 4px 12px rgba(0, 102, 255, 0.2);
118
+ }
119
+
120
+ .btn-upload:hover {
121
+ background-color: var(--primary-hover);
122
+ transform: translateY(-1px);
123
+ }
124
+
125
+ .drop-text {
126
+ margin-top: 1rem;
127
+ color: var(--text-sub);
128
+ font-size: 0.95rem;
129
+ }
130
+
131
+ /* Result View */
132
+ .result-container {
133
+ display: none;
134
+ width: 100%;
135
+ max-width: 1000px;
136
+ background: white;
137
+ border-radius: 16px;
138
+ box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
139
+ overflow: hidden;
140
+ flex-direction: column;
141
+ }
142
+
143
+ .image-comparison {
144
+ display: grid;
145
+ grid-template-columns: 1fr 1fr;
146
+ gap: 2px;
147
+ background: #f0f0f0;
148
+ min-height: 400px;
149
+ }
150
+
151
+ .img-box {
152
+ position: relative;
153
+ background: #fff;
154
+ display: flex;
155
+ align-items: center;
156
+ justify-content: center;
157
+ overflow: hidden;
158
+ height: 500px;
159
+ }
160
+
161
+ .img-box img {
162
+ max-width: 100%;
163
+ max-height: 100%;
164
+ object-fit: contain;
165
+ }
166
+
167
+ /* Checkerboard background */
168
+ .checkerboard {
169
+ background-image: linear-gradient(45deg, #eee 25%, transparent 25%),
170
+ linear-gradient(-45deg, #eee 25%, transparent 25%),
171
+ linear-gradient(45deg, transparent 75%, #eee 75%),
172
+ linear-gradient(-45deg, transparent 75%, #eee 75%);
173
+ background-size: 20px 20px;
174
+ background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
175
+ }
176
+
177
+ .result-actions {
178
+ padding: 1.5rem;
179
+ border-top: 1px solid var(--border);
180
+ display: flex;
181
+ justify-content: space-between;
182
+ align-items: center;
183
+ background: white;
184
+ }
185
+
186
+ .btn-download {
187
+ background: var(--primary);
188
+ color: white;
189
+ padding: 10px 24px;
190
+ border-radius: 8px;
191
+ text-decoration: none;
192
+ font-weight: 600;
193
+ display: inline-flex;
194
+ align-items: center;
195
+ gap: 8px;
196
+ }
197
+
198
+ .btn-new {
199
+ color: var(--text-sub);
200
+ background: transparent;
201
+ border: 1px solid var(--border);
202
+ padding: 10px 20px;
203
+ border-radius: 8px;
204
+ cursor: pointer;
205
+ font-weight: 500;
206
+ }
207
+
208
+ /* Loading */
209
+ .loader {
210
+ display: none;
211
+ flex-direction: column;
212
+ align-items: center;
213
+ margin-top: 2rem;
214
+ }
215
+
216
+ .spinner {
217
+ width: 40px;
218
+ height: 40px;
219
+ border: 4px solid #f3f3f3;
220
+ border-top: 4px solid var(--primary);
221
+ border-radius: 50%;
222
+ animation: spin 1s linear infinite;
223
+ margin-bottom: 1rem;
224
+ }
225
+
226
+ @keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
227
+
228
+ /* Responsive */
229
+ @media (max-width: 768px) {
230
+ .image-comparison {
231
+ grid-template-columns: 1fr;
232
+ grid-template-rows: 1fr 1fr;
233
+ height: auto;
234
+ }
235
+ .img-box {
236
+ height: 300px;
237
+ }
238
+ }
239
  </style>
240
+ </head>
241
+ <body>
242
+
243
+ <header>
244
+ <div class="logo">✂️ RemoveBG</div>
245
+ </header>
246
+
247
+ <main>
248
+
249
+ <div id="uploadView">
250
+ <h1>Remove Image Background</h1>
251
+ <p class="subtitle">100% Automatically and Free</p>
252
+
253
+ <div class="upload-card">
254
+ <div class="drop-zone" id="dropZone">
255
+ <button class="btn-upload">Upload Image</button>
256
+ <div class="drop-text">or drop a file here</div>
257
+ <input type="file" id="fileInput" name="file" accept="image/*" hidden>
258
+ </div>
259
+
260
+ <div class="loader" id="loader">
261
+ <div class="spinner"></div>
262
+ <div>Removing background...</div>
263
+ </div>
264
+ </div>
265
+ </div>
266
+
267
+ <div class="result-container" id="resultView">
268
+ <div class="image-comparison">
269
+ <div class="img-box bg-original">
270
+ <p style="position: absolute; top: 10px; left: 10px; background: rgba(0,0,0,0.5); color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: 600;">Original</p>
271
+ <img id="originalImg" src="" alt="Original">
272
+ </div>
273
+ <div class="img-box checkerboard">
274
+ <p style="position: absolute; top: 10px; left: 10px; background: rgba(0,0,0,0.5); color: white; padding: 4px 8px; border-radius: 4px; font-size: 12px; font-weight: 600;">Removed Background</p>
275
+ <img id="resultImg" src="" alt="Result">
276
+ </div>
277
+ </div>
278
+ <div class="result-actions">
279
+ <button class="btn-new" id="btnNew"> Try another</button>
280
+ <a id="btnDownload" class="btn-download" download="no-bg.png" href="#">Download HD</a>
281
+ </div>
282
+ </div>
283
+
284
+ </main>
285
 
286
  <script>
287
+ const dropZone = document.getElementById('dropZone');
288
+ const fileInput = document.getElementById('fileInput');
289
+ const uploadView = document.getElementById('uploadView');
290
+ const resultView = document.getElementById('resultView');
291
+ const loader = document.getElementById('loader');
292
+ const originalImg = document.getElementById('originalImg');
293
+ const resultImg = document.getElementById('resultImg');
294
+ const btnDownload = document.getElementById('btnDownload');
295
+ const btnNew = document.getElementById('btnNew');
296
+
297
+ // Trigger file input
298
+ dropZone.addEventListener('click', () => fileInput.click());
299
+
300
+ // Handle Drag & Drop
301
+ dropZone.addEventListener('dragover', (e) => {
302
+ e.preventDefault();
303
+ dropZone.classList.add('dragover');
304
+ });
305
+
306
+ dropZone.addEventListener('dragleave', () => {
307
+ dropZone.classList.remove('dragover');
308
+ });
309
+
310
+ dropZone.addEventListener('drop', (e) => {
311
+ e.preventDefault();
312
+ dropZone.classList.remove('dragover');
313
+ if (e.dataTransfer.files.length) {
314
+ handleFile(e.dataTransfer.files[0]);
315
+ }
316
+ });
317
 
318
+ // Handle File Selection
319
+ fileInput.addEventListener('change', (e) => {
320
+ if (e.target.files.length) {
321
+ handleFile(e.target.files[0]);
322
+ }
323
+ });
324
 
325
+ function handleFile(file) {
326
+ if (!file.type.startsWith('image/')) {
327
+ alert('Please upload an image file.');
328
+ return;
329
+ }
330
+
331
+ // Show original image
332
+ const reader = new FileReader();
333
+ reader.onload = (e) => {
334
+ originalImg.src = e.target.result;
335
+ };
336
+ reader.readAsDataURL(file);
337
+
338
+ // Upload and Process
339
+ uploadFile(file);
340
  }
 
341
 
342
+ async function uploadFile(file) {
343
+ const formData = new FormData();
344
+ formData.append('file', file);
345
+
346
+ // Show loading
347
+ document.querySelector('.drop-zone').style.display = 'none';
348
+ loader.style.display = 'flex';
349
+
350
+ try {
351
+ const response = await fetch('/', {
352
+ method: 'POST',
353
+ body: formData
354
+ });
355
 
356
+ if (!response.ok) throw new Error('Processing failed');
 
 
357
 
358
+ const blob = await response.blob();
359
+ const url = URL.createObjectURL(blob);
 
 
360
 
361
+ // Show result
362
+ resultImg.src = url;
363
+ btnDownload.href = url;
364
+
365
+ uploadView.style.display = 'none';
366
+ resultView.style.display = 'flex';
367
+
368
+ } catch (err) {
369
+ alert('Error processing image. Please try again.');
370
+ resetUI();
371
+ }
372
+ }
373
+
374
+ // Reset UI
375
+ btnNew.addEventListener('click', resetUI);
376
+
377
+ function resetUI() {
378
+ uploadView.style.display = 'block';
379
+ resultView.style.display = 'none';
380
+ document.querySelector('.drop-zone').style.display = 'block';
381
+ loader.style.display = 'none';
382
+ fileInput.value = '';
383
+ originalImg.src = '';
384
+ resultImg.src = '';
385
+ }
386
  </script>
387
+ </body>
388
  </html>