File size: 1,563 Bytes
a3c63f1
0e8bb83
a3c63f1
 
 
 
 
a080c54
 
 
0e8bb83
a3c63f1
 
 
 
 
 
 
 
 
 
0e8bb83
a3c63f1
 
 
 
 
 
 
d8cece4
 
 
 
 
 
 
 
 
a3c63f1
d8cece4
 
a1c8ed3
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
from flask import Flask, render_template, request, send_file
from rembg import remove, new_session
from PIL import Image
from io import BytesIO

app = Flask(__name__)

# Use birefnet-general for state-of-the-art quality (requires latest rembg)
# This model provides significantly better edge detection and detail preservation
session = new_session("birefnet-general")

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        if 'file' not in request.files:
            return 'No file uploaded', 400
        file = request.files['file']
        if file.filename == '':
            return 'No file selected', 400
        if file:
            input_image = Image.open(file.stream)
            output_image = remove(input_image, session=session, post_process_mask=True)
            img_io = BytesIO()
            output_image.save(img_io, 'PNG')
            img_io.seek(0)
            # return send_file(img_io, mimetype='image/png')  # Change download in separatre browser tab
            return send_file(img_io, mimetype='image/png', as_attachment=True, download_name='_rmbg.png')
    return render_template('index.html')

import psutil

# ... (existing imports)

@app.route('/cpu', methods=['GET'])
def get_cpu():
    # interval=None gets the cpu usage since the last call, non-blocking
    return jsonify({'cpu_percent': psutil.cpu_percent(interval=None)})

if __name__ == '__main__':
    # Initial call to set the blocking point for psutil
    psutil.cpu_percent()
    app.run(host='0.0.0.0', debug=True, port=7860)