Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,5 @@
|
|
1 |
-
|
2 |
-
from flask_cors import CORS
|
3 |
-
from rembg import remove
|
4 |
-
from PIL import Image
|
5 |
-
import io
|
6 |
-
|
7 |
-
app = Flask(__name__)
|
8 |
-
CORS(app) # Enable CORS for all routes
|
9 |
-
|
10 |
-
@app.route('/remove_background', methods=['POST'])
|
11 |
-
def remove_background():
|
12 |
-
if 'image' not in request.files:
|
13 |
-
return jsonify({"error": "No image provided"}), 400
|
14 |
-
|
15 |
-
input_image = request.files['image'].read() # Read the image file
|
16 |
-
|
17 |
-
# Apply background removal using rembg
|
18 |
-
output_bytes = remove(input_image)
|
19 |
-
|
20 |
-
# Convert the output bytes back into a PIL image
|
21 |
-
output_image = Image.open(io.BytesIO(output_bytes))
|
22 |
-
|
23 |
-
# Save the processed image to a BytesIO object
|
24 |
-
img_byte_arr = io.BytesIO()
|
25 |
-
output_image.save(img_byte_arr, format='PNG')
|
26 |
-
img_byte_arr.seek(0)
|
27 |
-
|
28 |
-
# Return the image as a response
|
29 |
-
return send_file(img_byte_arr, mimetype='image/png')
|
30 |
|
31 |
if __name__ == "__main__":
|
32 |
-
app
|
|
|
|
1 |
+
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
if __name__ == "__main__":
|
4 |
+
# Run the Flask app using Gunicorn
|
5 |
+
os.system("gunicorn -w 4 -b 0.0.0.0:7860 myapp:myapp") # 4 worker processes
|