veltrixcode commited on
Commit
a3c63f1
·
verified ·
1 Parent(s): f939316

Upload 6 files

Browse files
Files changed (6) hide show
  1. Dockerfile +17 -2
  2. README.md +7 -32
  3. app.py +27 -0
  4. basic.py +9 -0
  5. requirements.txt +3 -0
  6. templates/index.html +80 -0
Dockerfile CHANGED
@@ -1,2 +1,17 @@
1
- FROM huggingface/autotrain-advanced:latest
2
- CMD pip uninstall -y autotrain-advanced && pip install -U autotrain-advanced && autotrain app --host 0.0.0.0 --port 7860 --workers 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ # download this https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx
4
+ # copy model to avoid unnecessary download
5
+ COPY u2net.onnx /home/.u2net/u2net.onnx
6
+
7
+ WORKDIR /app
8
+
9
+ COPY requirements.txt .
10
+
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ COPY . .
14
+
15
+ EXPOSE 5100
16
+
17
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,35 +1,10 @@
1
- ---
2
- title: AutoTrain Advanced
3
- emoji: 🚀
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: docker
7
- pinned: false
8
- short_description: Create powerful AI models without code
9
- hf_oauth: true
10
- hf_oauth_expiration_minutes: 36000
11
- hf_oauth_scopes:
12
- - read-repos
13
- - write-repos
14
- - manage-repos
15
- - inference-api
16
- - read-billing
17
- tags:
18
- - autotrain
19
- ---
20
 
21
- # Docs
22
 
23
- https://huggingface.co/docs/autotrain
24
 
25
- # Citation
26
-
27
- @misc{thakur2024autotrainnocodetrainingstateoftheart,
28
- title={AutoTrain: No-code training for state-of-the-art models},
29
- author={Abhishek Thakur},
30
- year={2024},
31
- eprint={2410.15735},
32
- archivePrefix={arXiv},
33
- primaryClass={cs.AI},
34
- url={https://arxiv.org/abs/2410.15735},
35
- }
 
1
+ A simple flask app to remove the background of an image with [Rembg](https://github.com/danielgatis/rembg)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ Watch the [tutorial](https://youtu.be/cw34KMPSt4k) on YouTube
4
 
5
+ ## Run it
6
 
7
+ ```
8
+ pip install -r requirements.txt
9
+ python app.py
10
+ ```
 
 
 
 
 
 
 
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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':
11
+ if 'file' not in request.files:
12
+ return 'No file uploaded', 400
13
+ file = request.files['file']
14
+ if file.filename == '':
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)
22
+ # return send_file(img_io, mimetype='image/png') # Change download in separatre browser tab
23
+ return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='_rmbg.png')
24
+ return render_template('index.html')
25
+
26
+ if __name__ == '__main__':
27
+ app.run(host='0.0.0.0', debug=True, port=5100)
basic.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from rembg import remove
2
+ from PIL import Image
3
+
4
+
5
+ input = Image.open('dog.png')
6
+
7
+ output = remove(input)
8
+
9
+ output.save('out.png')
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ flask
2
+ rembg
3
+ pillow
templates/index.html ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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>