Upload 8 files
Browse files- README.md +2 -11
- __pycache__/app.cpython-311.pyc +0 -0
- app.py +47 -0
- static/uploads/WIN_20230809_20_30_26_Pro.jpg +0 -0
- static/uploads/WIN_20230809_20_30_26_Pro_rembg.png +0 -0
- static/uploads/WIN_20230830_11_09_25_Pro.jpg +0 -0
- static/uploads/WIN_20230830_11_09_25_Pro_rembg.png +0 -0
- templates/home.html +88 -0
README.md
CHANGED
|
@@ -1,12 +1,3 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Bgrem
|
| 3 |
-
emoji: 🔥
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: indigo
|
| 6 |
-
sdk: streamlit
|
| 7 |
-
sdk_version: 1.28.2
|
| 8 |
-
app_file: app.py
|
| 9 |
-
pinned: false
|
| 10 |
-
---
|
| 11 |
|
| 12 |
-
|
|
|
|
| 1 |
+
# Background-Remover-Flask-App-using-Python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
Blog Link - https://machinelearningprojects.net/background-remover-flask-app/
|
__pycache__/app.cpython-311.pyc
ADDED
|
Binary file (3.12 kB). View file
|
|
|
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 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')
|
| 13 |
+
|
| 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 |
+
input = Image.open(input_path)
|
| 27 |
+
output = remove(input)
|
| 28 |
+
output.save(output_path)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
@app.route('/')
|
| 32 |
+
def home():
|
| 33 |
+
return render_template('home.html')
|
| 34 |
+
|
| 35 |
+
@app.route('/remback',methods=['POST'])
|
| 36 |
+
def remback():
|
| 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 |
+
app.run(debug=True)
|
static/uploads/WIN_20230809_20_30_26_Pro.jpg
ADDED
|
static/uploads/WIN_20230809_20_30_26_Pro_rembg.png
ADDED
|
static/uploads/WIN_20230830_11_09_25_Pro.jpg
ADDED
|
static/uploads/WIN_20230830_11_09_25_Pro_rembg.png
ADDED
|
templates/home.html
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
|
| 4 |
+
<style type='text/css'>
|
| 5 |
+
body {
|
| 6 |
+
background-color: black !important;
|
| 7 |
+
font-family: sans-serif;
|
| 8 |
+
margin-top: 40px;
|
| 9 |
+
margin: 0;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
.regform {
|
| 13 |
+
width: 800px;
|
| 14 |
+
background-color: #fb2056;
|
| 15 |
+
margin: auto;
|
| 16 |
+
color: #FFFFFF;
|
| 17 |
+
padding: 10px 0px 10px 0px;
|
| 18 |
+
text-align: center;
|
| 19 |
+
border-radius: 15px 15px 0px 0px;
|
| 20 |
+
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
.main-form {
|
| 24 |
+
width: 800px;
|
| 25 |
+
margin: auto;
|
| 26 |
+
background-color: #fc86a463;
|
| 27 |
+
padding-left: 50px;
|
| 28 |
+
padding-right: 50px;
|
| 29 |
+
padding-bottom: 20px;
|
| 30 |
+
color: #FFFFFF;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
img {
|
| 34 |
+
max-height: 400px;
|
| 35 |
+
max-width: 500px;
|
| 36 |
+
height: auto;
|
| 37 |
+
width: auto;
|
| 38 |
+
}
|
| 39 |
+
</style>
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
<head>
|
| 43 |
+
<!-- Required meta tags -->
|
| 44 |
+
<meta charset="utf-8">
|
| 45 |
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 46 |
+
|
| 47 |
+
<!-- Bootstrap CSS -->
|
| 48 |
+
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet"
|
| 49 |
+
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
|
| 50 |
+
|
| 51 |
+
<title>Remove Background</title>
|
| 52 |
+
</head>
|
| 53 |
+
|
| 54 |
+
<body>
|
| 55 |
+
|
| 56 |
+
<div class='regform mt-3'>
|
| 57 |
+
<h1>Remove Background</h1>
|
| 58 |
+
</div>
|
| 59 |
+
|
| 60 |
+
<form action='/remback' class='main-form' method="POST" enctype="multipart/form-data">
|
| 61 |
+
|
| 62 |
+
<div class='text-center'>
|
| 63 |
+
<input type="file" id="file" name='file' style="margin-top:10px;margin-bottom:10px;">
|
| 64 |
+
<button type='submit' class='btn btn-success'> Remove Background
|
| 65 |
+
</button>
|
| 66 |
+
</div>
|
| 67 |
+
|
| 68 |
+
</form>
|
| 69 |
+
|
| 70 |
+
{% if rembg_img_name %}
|
| 71 |
+
<div class="row" style="margin-top:10px;margin-bottom:10px;">
|
| 72 |
+
|
| 73 |
+
<div class="col text-center" style="background-color: #fb2056ba;margin: 50px;border-radius: 20px;padding: 20px;">
|
| 74 |
+
<h2 style="color: #FFFFFF;">Original Image</h2><img src='../static/uploads/{{ org_img_name }}'
|
| 75 |
+
style="display: block;margin-left: auto;margin-right: auto;">
|
| 76 |
+
</div>
|
| 77 |
+
|
| 78 |
+
<div class="col text-center" style="background-color: #fb2056ba;margin: 50px;border-radius: 20px;padding: 20px;">
|
| 79 |
+
<h2 style="color: #FFFFFF;">Image with Background Removed</h2><img src='../static/uploads/{{ rembg_img_name }}'
|
| 80 |
+
style="display: block;margin-left: auto;margin-right: auto;">
|
| 81 |
+
</div>
|
| 82 |
+
|
| 83 |
+
</div>
|
| 84 |
+
{% endif %}
|
| 85 |
+
|
| 86 |
+
</body>
|
| 87 |
+
|
| 88 |
+
</html>
|