Saad0KH's picture
Update app.py
f3b4b4b verified
raw
history blame
1.23 kB
from flask import Flask, request, jsonify
from PIL import Image
import base64
import io
from SegCloth import segment_clothing
# Créer une instance FastAPI
app = Flask(__name__)
def segment_image(img, clothes):
img = decode_image_from_base64(img)
return segment_clothing(img, clothes)
# Fonction pour décoder une image encodée en base64 en objet PIL.Image.Image
def decode_image_from_base64(image_data):
image_data = base64.b64decode(image_data)
image = Image.open(io.BytesIO(image_data))
return image
def encode_image_to_base64(image):
buffered = BytesIO()
image.save(buffered, format="PNG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')
@app.get("/")
def root():
return "Welcome to Fashion Clothing Detectiony API!"
# Route pour l'API REST
@app.route('/api/classify', methods=['POST'])
def classify():
data = request.json
print(data)
clothes = ["Hat", "Upper-clothes", "Skirt", "Pants", "Dress", "Belt", "Left-shoe", "Right-shoe", "Scarf"]
image = data['image']
result = segment_image(clothes, image)
return jsonify({'result': encode_image_to_base64(result)})
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=7860)