Spaces:
No application file
No application file
Update Background-Remover-Flask-App-using-Python-main/app.py
Browse files
Background-Remover-Flask-App-using-Python-main/app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
-
import
|
| 2 |
import os
|
| 3 |
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
from werkzeug.utils import secure_filename
|
| 6 |
-
from flask import Flask,request,render_template
|
| 7 |
|
| 8 |
UPLOAD_FOLDER = 'static/uploads'
|
| 9 |
-
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg','webp'])
|
| 10 |
|
| 11 |
if 'static' not in os.listdir('.'):
|
| 12 |
os.mkdir('static')
|
|
@@ -14,34 +13,30 @@ if 'static' not in os.listdir('.'):
|
|
| 14 |
if 'uploads' not in os.listdir('static/'):
|
| 15 |
os.mkdir('static/uploads')
|
| 16 |
|
| 17 |
-
app = Flask(__name__)
|
| 18 |
-
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
|
| 19 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 20 |
-
app.secret_key = "secret key"
|
| 21 |
-
|
| 22 |
def allowed_file(filename):
|
| 23 |
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
|
| 24 |
|
| 25 |
-
def remove_background(input_path,output_path):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
file = request.files['file']
|
| 38 |
-
if file and allowed_file(file.filename):
|
| 39 |
-
filename = secure_filename(file.filename)
|
| 40 |
-
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
| 41 |
-
rembg_img_name = filename.split('.')[0]+"_rembg.png"
|
| 42 |
-
remove_background(UPLOAD_FOLDER+'/'+filename,UPLOAD_FOLDER+'/'+rembg_img_name)
|
| 43 |
-
return render_template('home.html',org_img_name=filename,rembg_img_name=rembg_img_name)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
if __name__ == '__main__':
|
| 47 |
-
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
import os
|
| 3 |
from rembg import remove
|
| 4 |
from PIL import Image
|
| 5 |
from werkzeug.utils import secure_filename
|
|
|
|
| 6 |
|
| 7 |
UPLOAD_FOLDER = 'static/uploads'
|
| 8 |
+
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'webp'])
|
| 9 |
|
| 10 |
if 'static' not in os.listdir('.'):
|
| 11 |
os.mkdir('static')
|
|
|
|
| 13 |
if 'uploads' not in os.listdir('static/'):
|
| 14 |
os.mkdir('static/uploads')
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def allowed_file(filename):
|
| 17 |
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
|
| 18 |
|
| 19 |
+
def remove_background(input_path, output_path):
|
| 20 |
+
input_image = Image.open(input_path)
|
| 21 |
+
output_image = remove(input_image)
|
| 22 |
+
output_image.save(output_path)
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
st.title("Remove Background")
|
| 26 |
|
| 27 |
+
uploaded_file = st.file_uploader("Choose a file", type=['png', 'jpg', 'jpeg', 'webp'])
|
| 28 |
|
| 29 |
+
if uploaded_file is not None and allowed_file(uploaded_file.name):
|
| 30 |
+
filename = secure_filename(uploaded_file.name)
|
| 31 |
+
file_path = os.path.join(UPLOAD_FOLDER, filename)
|
| 32 |
+
uploaded_file.save(file_path)
|
| 33 |
|
| 34 |
+
rembg_img_name = filename.split('.')[0] + "_rembg.png"
|
| 35 |
+
remove_background(file_path, os.path.join(UPLOAD_FOLDER, rembg_img_name))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
st.image(Image.open(file_path), caption='Original Image', use_column_width=True)
|
| 38 |
+
st.image(Image.open(os.path.join(UPLOAD_FOLDER, rembg_img_name)),
|
| 39 |
+
caption='Image with Background Removed', use_column_width=True)
|
| 40 |
|
| 41 |
if __name__ == '__main__':
|
| 42 |
+
main()
|