sammyview80 commited on
Commit
1bc5390
1 Parent(s): 1b667e3

Update flask_app.py

Browse files
Files changed (1) hide show
  1. flask_app.py +9 -8
flask_app.py CHANGED
@@ -5,7 +5,7 @@ from flask_session import Session
5
  from werkzeug.utils import secure_filename
6
  from example_inference import example_inference
7
  from flask import send_from_directory
8
-
9
 
10
  UPLOAD_FOLDER = 'images'
11
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
@@ -23,9 +23,7 @@ def allowed_file(filename):
23
 
24
  @app.route('/rm-bg/<transparent>', methods=['GET', 'POST'])
25
  def upload_file(transparent):
26
- print(transparent)
27
  transparent = True if transparent == "true" else False
28
- print(transparent)
29
  if request.method == 'POST':
30
  if 'file' not in request.files:
31
  flash('No file part')
@@ -35,11 +33,14 @@ def upload_file(transparent):
35
  flash('No selected file')
36
  return {"status": "Failed", "message": "Filename Not Found."}
37
  if file and allowed_file(file.filename):
38
- filename = secure_filename('normal_image.png')
39
- file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
40
- im_path = f"{os.path.dirname(os.path.abspath(__file__))}/images/{filename}"
41
- rm_image_path = example_inference(im_path, transparent)
42
- return send_from_directory(app.config["UPLOAD_FOLDER"], rm_image_path)
 
 
 
43
  return {
44
  "message": "Get Request not allowed"
45
  }
 
5
  from werkzeug.utils import secure_filename
6
  from example_inference import example_inference
7
  from flask import send_from_directory
8
+ import base64
9
 
10
  UPLOAD_FOLDER = 'images'
11
  ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
 
23
 
24
  @app.route('/rm-bg/<transparent>', methods=['GET', 'POST'])
25
  def upload_file(transparent):
 
26
  transparent = True if transparent == "true" else False
 
27
  if request.method == 'POST':
28
  if 'file' not in request.files:
29
  flash('No file part')
 
33
  flash('No selected file')
34
  return {"status": "Failed", "message": "Filename Not Found."}
35
  if file and allowed_file(file.filename):
36
+ img_data = file.read()
37
+ img_base64 = base64.b64encode(img_data)
38
+ if isinstance(img_base64, bytes):
39
+ img_base64 = img_base64.decode('utf-8')
40
+ print('yes')
41
+ image_bytes = base64.b64decode(img_base64)
42
+ rm_image_path = example_inference(image_bytes, transparent)
43
+ return 'data:image/png;base64,' + rm_image_path
44
  return {
45
  "message": "Get Request not allowed"
46
  }