giorgio-caparvi
sending image and json with captions
cc3c167
raw
history blame
2.39 kB
from flask import Flask, render_template, send_from_directory, send_file, request
from flask_cors import CORS
import subprocess
import os
import io
from model.src.utils.arg_parser import eval_parse_args # Nuovo import corretto
import sys
from PIL import Image
from model.src import eval
app = Flask(__name__)
CORS(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/generate-design', methods=['GET','POST'])
def generate_design():
try:
# Getting json
json_data_from_req = request.get_json()
if not json_data_from_req:
return "Invalid or missing JSON data", 400
print(json_data_from_req)
# Getting Image
if 'image' not in request.files:
return "No image file in request", 400
image_file = request.files['image']
try:
image = Image.open(image_file)
except Exception as e:
return f"Failed to open the image: {str(e)}", 400
# Create an in-memory buffer to store the image (instead of saving to disk)
img_sketch_buffer = io.BytesIO()
# Save the image to the buffer in JPEG format
image.save(img_sketch_buffer, format='JPEG')
# Rewind the buffer's position to the beginning
img_sketch_buffer.seek(0)
# Creiamo una lista di argomenti come quelli che passeresti via CLI
sys.argv = [
'eval.py',
'--dataset_path', '/api/model/assets/data/vitonhd',
'--batch_size', '1',
'--mixed_precision', 'fp16',
'--num_workers_test', '4',
'--sketch_cond_rate', '0.2',
'--dataset', 'vitonhd',
'--start_cond_rate', '0.0',
'--test_order', 'paired'
]
# Esegui la funzione `main()` di eval.py passando gli argomenti
final_image = eval.main(img_sketch_buffer, json_data_from_req)
# Save the image to a BytesIO buffer to return via HTTP
img_io = io.BytesIO()
final_image.save(img_io, 'JPEG')
img_io.seek(0)
# Return the image as a file download
return send_file(img_io, mimetype='image/jpeg', as_attachment=True, download_name='in memory_image.jpg')
except Exception as e:
return str(e), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)