Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from PIL import Image
|
3 |
+
import base64
|
4 |
+
import io
|
5 |
+
import numpy as np
|
6 |
+
import torch
|
7 |
+
import torch.nn.functional as F
|
8 |
+
from torchvision.transforms.functional import normalize
|
9 |
+
|
10 |
+
|
11 |
+
# Initialize Flask app
|
12 |
+
app = Flask(__name__)
|
13 |
+
|
14 |
+
# Function to decode a base64 encoded image to a PIL image
|
15 |
+
def decode_image_from_base64(image_data):
|
16 |
+
encoded_image = image_data.split(",")[1]
|
17 |
+
decoded_image = base64.b64decode(encoded_image)
|
18 |
+
image = Image.open(io.BytesIO(decoded_image))
|
19 |
+
return image
|
20 |
+
|
21 |
+
# Function to encode a PIL image to base64
|
22 |
+
def encode_image_to_base64(image):
|
23 |
+
buffered = io.BytesIO()
|
24 |
+
image.save(buffered, format="PNG")
|
25 |
+
encoded_image = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
26 |
+
return "data:image/png;base64," + encoded_image
|
27 |
+
|
28 |
+
# Function to process the image
|
29 |
+
def process(image_data):
|
30 |
+
image = decode_image_from_base64(image_data)
|
31 |
+
|
32 |
+
|
33 |
+
return image
|
34 |
+
|
35 |
+
@app.route("/")
|
36 |
+
def root():
|
37 |
+
return "Welcome to StyleSync Outfit Backround API!"
|
38 |
+
|
39 |
+
# Route for the REST API
|
40 |
+
@app.route('/api/tryon', methods=['POST'])
|
41 |
+
def classify():
|
42 |
+
data = request.json
|
43 |
+
print(data)
|
44 |
+
image_data = data['image']
|
45 |
+
result_image = process(image_data)
|
46 |
+
result_base64 = encode_image_to_base64(result_image)
|
47 |
+
return jsonify({'result': result_base64})
|
48 |
+
|
49 |
+
if __name__ == "__main__":
|
50 |
+
app.run(host="0.0.0.0", port=7860)
|
51 |
+
|