File size: 2,387 Bytes
b05de99
1e330bd
67b8057
554a6f2
9abe70d
 
487c322
cc3c167
9abe70d
 
bed9a40
 
a212e09
bed9a40
554a6f2
 
 
 
cc3c167
bed9a40
9abe70d
cc3c167
 
 
 
 
 
 
a3d4937
 
 
cc3c167
 
a3d4937
cc3c167
 
 
 
 
 
 
 
 
 
 
 
a3d4937
 
9abe70d
487c322
 
9abe70d
 
 
 
 
 
 
 
 
896542a
9abe70d
cc3c167
9abe70d
a3d4937
cb14b48
 
 
e2edca9
 
cb14b48
e2edca9
9abe70d
 
bed9a40
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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)