File size: 2,192 Bytes
6e6426e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from celery import Celery
from flask import current_app
from app2 import app, sse  # Import your Flask app and SSE
import uuid
import os

# import threading
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)



# OUTPUT_FOLDER = 'path/to/output/folder'
# image_results = {}
# image_results_lock = threading.Lock()


# def create_progress_callback(session_id):
#     def progress_callback(step, total_steps):
#         progress = int((step + 1) / total_steps * 100)
#         print(f"Publishing progress {progress} for session {session_id}")
#         sse.publish({"progress": progress}, type='progress', channel=session_id)
#     return progress_callback

@celery.task(bind=True)
def process_image(self, session_id, garm_path, vton_path, category):
    try:
        print(f"Starting process_image task for session {session_id}")

        progress_callback = create_progress_callback(session_id)

        output_images = process_dc(garm_img=garm_path,
                                   vton_img=vton_path,
                                   category=category,
                                   progress_callback=progress_callback)

        if not output_images:
            sse.publish({"error": "No output image generated"}, type='error', channel=session_id)
            return None

        output_image = output_images[0]
        
        # Generate a UUID for the output image
        image_uuid = str(uuid.uuid4())
        
        # Create the output filename with the UUID
        output_filename = f"{image_uuid}.png"
        output_path = os.path.join(OUTPUT_FOLDER, output_filename)
        
        # Save the output image
        output_image.save(output_path, format='PNG')
        
        # Add the UUID and path to the image_results map
        with image_results_lock:
            image_results[image_uuid] = output_path

        sse.publish({"message": "Processing complete", "uuid": image_uuid}, type='complete', channel=session_id)
        
        return image_uuid

    except Exception as e:
        sse.publish({"error": str(e)}, type='error', channel=session_id)
        return print(f"panic in process_image: {str(e)}")