File size: 2,273 Bytes
9b4f4f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
import gradio as gr
import numpy as np
from preprocess import preprocess_image_from_array


def process_image(image):
    """
    Process an uploaded image and return the preprocessed version.
    
    Args:
        image: PIL Image or numpy array from Gradio
        
    Returns:
        Preprocessed image as numpy array
    """
    if image is None:
        return None
    
    try:
        # Convert PIL Image to numpy array if needed
        if hasattr(image, 'mode'):
            # PIL Image
            img_array = np.array(image)
        else:
            # Already a numpy array
            img_array = image
        
        # Ensure RGB format
        if len(img_array.shape) == 2:
            # Grayscale, convert to RGB
            img_array = np.stack([img_array] * 3, axis=-1)
        elif img_array.shape[2] == 4:
            # RGBA, convert to RGB
            img_array = img_array[:, :, :3]
        
        # Process the image
        processed = preprocess_image_from_array(img_array)
        
        return processed
    except Exception as e:
        print(f"Erreur lors du traitement: {e}")
        import traceback
        traceback.print_exc()
        return None


# Create Gradio interface
with gr.Blocks(title="Preprocessing d'Échiquier") as demo:
    gr.Markdown("""
    # Preprocessing d'Images d'Échiquier
    
    Cette application applique le preprocessing d'images d'échiquier pour convertir 
    une photo prise sous un angle arbitraire en une projection 2D.
    
    Uploadez une image d'échiquier et obtenez la version préprocessée.
    """)
    
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(label="Image d'entrée", type="numpy")
            process_btn = gr.Button("Traiter l'image", variant="primary")
        
        with gr.Column():
            output_image = gr.Image(label="Image préprocessée", type="numpy")
    
    process_btn.click(
        fn=process_image,
        inputs=[input_image],
        outputs=[output_image]
    )
    
    # Auto-process when image is uploaded
    input_image.change(
        fn=process_image,
        inputs=[input_image],
        outputs=[output_image]
    )


if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)