Sharan Thakur commited on
Commit
11aa79a
·
1 Parent(s): 6ee2c22

Adds app and dockerfile

Browse files
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
2
+
3
+ RUN mkdir src
4
+ RUN mkdir app
5
+
6
+ WORKDIR /src/app
7
+
8
+ COPY ./requirements.txt ./
9
+
10
+ RUN pip3 install -r requirements.txt
11
+
12
+ COPY . .
13
+
14
+ EXPOSE 7860
15
+
16
+ CMD [ "python3", "app.py" ]
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Sharan Thakur
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+
3
+ from rembg import remove, new_session
4
+
5
+ from typing import Annotated
6
+ from fastapi import FastAPI, UploadFile, Request, File, Form
7
+ from fastapi.templating import Jinja2Templates
8
+ from fastapi.responses import RedirectResponse, HTMLResponse, Response
9
+
10
+ import uvicorn
11
+
12
+ import os
13
+
14
+ os.environ['U2NET_HOME'] = './models/'
15
+
16
+ model_names = [
17
+ 'u2net',
18
+ 'u2net_human_seg',
19
+ 'u2net_cloth_seg',
20
+ 'isnet-general-use',
21
+ ]
22
+
23
+ sessions = {
24
+ 'u2net': new_session(model_name=model_names[0]),
25
+ }
26
+
27
+ app = FastAPI()
28
+ templates = Jinja2Templates(directory="templates")
29
+
30
+
31
+ @app.get("/")
32
+ def health(request: Request):
33
+ return templates.TemplateResponse('dynamic.html', { "request": request })
34
+
35
+
36
+ @app.post("/remove")
37
+ async def remove_bg(
38
+ request: Request,
39
+ file: Annotated[UploadFile, File()],
40
+ mask_only: Annotated[str, Form()] = 'off',
41
+ name_of_model: Annotated[str, Form()] = 'u2net'
42
+ ) -> HTMLResponse:
43
+ try:
44
+ if name_of_model not in sessions.keys():
45
+ sessions[name_of_model] = new_session(model_name=name_of_model)
46
+
47
+ current_session = sessions[name_of_model]
48
+
49
+ only_mask = mask_only == 'on'
50
+ data = file.file.read()
51
+
52
+ output_array = remove(data, only_mask=only_mask, session=current_session)
53
+
54
+ output_img = base64.b64encode(output_array).decode('utf-8')
55
+
56
+ file.file.close()
57
+
58
+ encoded_image = base64.b64encode(data).decode('utf-8')
59
+
60
+ return templates.TemplateResponse(
61
+ 'dynamic.html',
62
+ {
63
+ "request" : request,
64
+ "image" : encoded_image,
65
+ "output_img" : output_img
66
+ }
67
+ )
68
+ except Exception as error_msg:
69
+ return templates.TemplateResponse(
70
+ "error.html",
71
+ {
72
+ "request" : request,
73
+ "error_msg" : str(error_msg),
74
+ }
75
+ )
76
+
77
+
78
+ @app.post("/remove_bg")
79
+ async def remove_bg(
80
+ request: Request,
81
+ file: UploadFile,
82
+ mask_only: str = 'off',
83
+ name_of_model: str = 'u2net'
84
+ ):
85
+ try:
86
+ if name_of_model not in sessions.keys():
87
+ sessions[name_of_model] = new_session(model_name=name_of_model)
88
+
89
+ current_session = sessions[name_of_model]
90
+
91
+ only_mask = mask_only == 'on'
92
+ data = file.file.read()
93
+
94
+ output_array = remove(data, only_mask=only_mask, session=current_session)
95
+
96
+ file.file.close()
97
+
98
+ return Response(content=output_array, media_type="image/png")
99
+ except Exception as error:
100
+ return f"Oopss!!!! {error}"
101
+
102
+
103
+ @app.get("/remove")
104
+ def remove_bg_redirect():
105
+ return RedirectResponse('/')
106
+
107
+ if __name__ == '__main__':
108
+ uvicorn.run(app, host='0.0.0.0', port=7860)
models/isnet-general-use.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:60920e99c45464f2ba57bee2ad08c919a52bbf852739e96947fbb4358c0d964a
3
+ size 178648008
models/u2net.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8d10d2f3bb75ae3b6d527c77944fc5e7dcd94b29809d47a739a7a728a912b491
3
+ size 175997641
models/u2net_cloth_seg.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6d2cbc27bfbdc989e1fd325656d65902ecc6a3ccbe94b2d3655ec114efcb128e
3
+ size 176194565
models/u2net_human_seg.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:01eb6a29a5c4d8edb30b56adad9bb3a2a0535338e480724a213e0acfd2d1c73c
3
+ size 175997641
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ rembg==2.0.56
2
+ python-multipart==0.0.9
3
+ fastapi
4
+ uvicorn
templates/dynamic.html ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Remove Image Background</title>
5
+ </head>
6
+ <style>
7
+ hr {
8
+ border: none;
9
+ border-top: 3px double #333;
10
+ color: #333;
11
+ overflow: visible;
12
+ text-align: center;
13
+ height: 5px;
14
+ }
15
+
16
+ hr::after {
17
+ background: #fff;
18
+ content: "📷";
19
+ padding: 0 4px;
20
+ position: relative;
21
+ top: -13px;
22
+ }
23
+
24
+ form {
25
+ padding: 10px;
26
+ border: 1px solid #ddd;
27
+ margin-bottom: 20px;
28
+ }
29
+
30
+ input[type="submit"] {
31
+ background-color: #333;
32
+ color: #fff;
33
+ padding: 5px 10px;
34
+ border: none;
35
+ border-radius: 3px;
36
+ cursor: pointer;
37
+ }
38
+
39
+ .drop-zone {
40
+ border: 2px dashed #ccc;
41
+ padding: 20px;
42
+ display: flex;
43
+ flex-direction: column;
44
+ align-items: center;
45
+ justify-content: center;
46
+ cursor: pointer;
47
+ }
48
+
49
+ .drop-zone.dragover {
50
+ background-color: #f0f0f0;
51
+ }
52
+ </style>
53
+
54
+ <body>
55
+ <div id="dropZone" class="drop-zone">
56
+ <h1>Remove Background from Images!</h1>
57
+ <form
58
+ id="upload_form"
59
+ action="/remove"
60
+ enctype="multipart/form-data"
61
+ method="POST"
62
+ >
63
+ <input id="input_image" name="file" type="file" accept="image/*" />
64
+
65
+ <label for="mask_only">Mask Only</label>
66
+ <input
67
+ type="checkbox"
68
+ id="mask_only"
69
+ name="mask_only"
70
+ type="checkbox"
71
+ />
72
+
73
+ <label for="name_of_model">Choose a model:</label>
74
+
75
+ <select name="name_of_model" id="name_of_model">
76
+ <option value="u2net">U2Net</option>
77
+ <option value="u2net_human_seg">U2Net Human Seg</option>
78
+ <option value="u2net_cloth_seg">U2Net Cloth Seg</option>
79
+ <option value="isnet-general-use">ISNET</option>
80
+ </select>
81
+
82
+ <input type="submit" />
83
+ </form>
84
+
85
+ <hr />
86
+
87
+ {% if image %}
88
+ <h1>Input Image</h1>
89
+ <img src="data:image/jpeg;base64,{{ image }}" height="200" />
90
+ {% else %} No Image Selected {% endif %} {% if output_img %}
91
+ <h1>Output Image</h1>
92
+ <img src="data:image/jpeg;base64,{{ output_img }}" height="200" />
93
+ {% endif %}
94
+
95
+ <h4>The available models are:</h4>
96
+ <ul>
97
+ <li><b>u2net:</b> A pre-trained model for general use cases.</li>
98
+ <li>
99
+ <b>u2net_human_seg:</b> A pre-trained model for human segmentation.
100
+ </li>
101
+ <li>
102
+ <b>u2net_cloth_seg:</b> A pre-trained model for Cloths Parsing from
103
+ human portrait. Here clothes are parsed into 3 category: Upper body,
104
+ Lower body and Full body. isnet-general-use: A new pre-trained model
105
+ for general use cases.
106
+ </li>
107
+ <li>
108
+ <a
109
+ href="https://github.com/danielgatis/rembg?tab=readme-ov-file#models"
110
+ >Learn More</a
111
+ >
112
+ </li>
113
+ </ul>
114
+ </div>
115
+ </body>
116
+ </html>
templates/error.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Remove Image Background</title>
5
+ </head>
6
+ <body>
7
+ <h1>Error!!!!</h1>
8
+ {% if error_msg %}
9
+ <h3>{{error_msg}}</h3>
10
+ {% endif %}
11
+ <a href="/">Home</a>
12
+ </body>
13
+ </html>