Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
def preprocess_image(image):
|
| 8 |
+
# Convert to numpy array
|
| 9 |
+
image = np.array(image)
|
| 10 |
+
|
| 11 |
+
# Paint a black rectangle on the image
|
| 12 |
+
h, w, _ = image.shape
|
| 13 |
+
x1, y1, x2, y2 = w//4, h//4, 3*w//4, 3*h//4 # Example position
|
| 14 |
+
image[y1:y2, x1:x2] = (0, 0, 0)
|
| 15 |
+
|
| 16 |
+
# Resize to 256x256
|
| 17 |
+
image = cv2.resize(image, (256, 256))
|
| 18 |
+
|
| 19 |
+
# Normalize to 0-1
|
| 20 |
+
image = image / 255.0
|
| 21 |
+
|
| 22 |
+
return image
|
| 23 |
+
|
| 24 |
+
def predict(image):
|
| 25 |
+
# Load model
|
| 26 |
+
model = tf.keras.models.load_model("initial_generator_model.h5")
|
| 27 |
+
|
| 28 |
+
# Preprocess image
|
| 29 |
+
processed_image = preprocess_image(image)
|
| 30 |
+
|
| 31 |
+
# Expand dimensions to match model input shape
|
| 32 |
+
input_image = np.expand_dims(processed_image, axis=0)
|
| 33 |
+
|
| 34 |
+
# Run model prediction
|
| 35 |
+
output_image = model.predict(input_image)[0]
|
| 36 |
+
|
| 37 |
+
# Convert output back to image format (denormalize)
|
| 38 |
+
output_image = (output_image * 255).astype(np.uint8)
|
| 39 |
+
|
| 40 |
+
return Image.fromarray(output_image)
|
| 41 |
+
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=predict,
|
| 44 |
+
inputs=gr.Image(type="pil"),
|
| 45 |
+
outputs=gr.Image(type="pil"),
|
| 46 |
+
title="Image Inpainting Model",
|
| 47 |
+
description="Upload an image. A black rectangle will be drawn on it, and the model will attempt to fill it in."
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
demo.launch()
|