File size: 1,792 Bytes
6f70afa
 
 
 
 
 
 
1bc5390
6f70afa
 
 
 
 
 
851394e
6f70afa
 
 
 
 
 
 
 
aea461d
51c3821
6f70afa
 
 
 
 
 
 
 
 
 
1bc5390
 
 
 
 
 
aea461d
2c011da
6f70afa
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   
import os
from flask import Flask, flash, request, redirect, url_for, session
from flask_session import Session
from werkzeug.utils import secure_filename
from example_inference import example_inference
from flask import send_from_directory
import base64

UPLOAD_FOLDER = 'images'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

Session(app)
 
def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/rm-bg/<transparent>', methods=['GET', 'POST'])
def upload_file(transparent):
    transparent = True if transparent == "true" else False
    if request.method == 'POST':
        if 'file' not in request.files:
            flash('No file part')
            return {"status": "Failed", "message": "Please Provide file name(file)."}
        file = request.files['file']
        if file.filename == '':
            flash('No selected file')
            return  {"status": "Failed", "message": "Filename Not Found."}
        if file and allowed_file(file.filename):
            img_data = file.read()
            img_base64 = base64.b64encode(img_data)
            if isinstance(img_base64, bytes):
                img_base64 = img_base64.decode('utf-8')
                print('yes')
            image_bytes = base64.b64decode(img_base64)
            rm_image_path = example_inference(image_bytes, transparent)
            return rm_image_path
    return {
        "message": "Get Request not allowed"
    }

if __name__ == '__main__':
    # http_server = WSGIServer(('', 8000), app)
    # http_server.serve_forever()
    app.debug = True
    app.run(port=8000)