perisoelusive/Patterns
0
1import tensorflow as tf2import gradio as gr3from tensorflow.keras.models import load_model4from PIL import Image5import numpy as np6 7# Load your model8model = load_model("Colors.keras")9 10# Infer input size from model11ish = model.input_shape12if isinstance(ish, (list, tuple)) and isinstance(ish[0], (list, tuple)):13 ish = ish[0] # first input if multi-input14H, W = int(ish[1]), int(ish[2])15 16def process_image(image):17 # Preprocess: RGB, resize, normalize [-1,1]18 image = image.convert("RGB").resize((W, H))19 arr = np.asarray(image, np.float32)20 arr = (arr / 127.5) - 1.021 arr = arr[None, ...] # [1,H,W,3]22 23 # Run model24 out = model.predict(arr, verbose=0)25 26 # Postprocess back to [0,255]27 out = (out[0] * 0.5 + 0.5) * 255.028 out = np.clip(out, 0, 255).astype(np.uint8)29 return Image.fromarray(out)30 31demo = gr.Interface(32 fn=process_image,33 inputs=gr.Image(type="pil"),34 outputs=gr.Image(type="pil"),35 title="Colors.keras Demo (Image-to-Image)",36 description="Upload an image and see the transformed output from the model."37)38 39demo.launch()40 