Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
# Placeholder for the actual model loading and processing logic
|
| 9 |
+
# Replace this with the actual code from generate.py
|
| 10 |
+
def load_model(model_path, use_cpu=False):
|
| 11 |
+
# Example: Load your ESRGAN model here
|
| 12 |
+
# This is a placeholder; adapt it based on the actual model loading in generate.py
|
| 13 |
+
model = torch.load(model_path)
|
| 14 |
+
if not use_cpu and torch.cuda.is_available():
|
| 15 |
+
model = model.cuda()
|
| 16 |
+
model.eval()
|
| 17 |
+
return model
|
| 18 |
+
|
| 19 |
+
def process_image(input_image, tile_size=512, seamless=False, use_cpu=False):
|
| 20 |
+
# Convert Gradio input (PIL image) to OpenCV format
|
| 21 |
+
img = np.array(input_image)
|
| 22 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 23 |
+
|
| 24 |
+
# Load models (adjust paths to match your uploaded model files)
|
| 25 |
+
normal_model = load_model("models/normal_model.pth", use_cpu)
|
| 26 |
+
disp_model = load_model("models/displacement_model.pth", use_cpu)
|
| 27 |
+
rough_model = load_model("models/roughness_model.pth", use_cpu)
|
| 28 |
+
|
| 29 |
+
# Placeholder processing logic (replace with actual generate.py logic)
|
| 30 |
+
# For example, apply the model to the image
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
# Convert image to tensor
|
| 33 |
+
img_tensor = torch.from_numpy(img.transpose(2, 0, 1)).float() / 255.0
|
| 34 |
+
if not use_cpu and torch.cuda.is_available():
|
| 35 |
+
img_tensor = img_tensor.cuda()
|
| 36 |
+
|
| 37 |
+
# Generate maps (simplified example)
|
| 38 |
+
normal_map = normal_model(img_tensor.unsqueeze(0)).cpu().numpy().squeeze().transpose(1, 2, 0)
|
| 39 |
+
disp_map = disp_model(img_tensor.unsqueeze(0)).cpu().numpy().squeeze().transpose(1, 2, 0)
|
| 40 |
+
rough_map = rough_model(img_tensor.unsqueeze(0)).cpu().numpy().squeeze().transpose(1, 2, 0)
|
| 41 |
+
|
| 42 |
+
# Convert back to uint8 for display
|
| 43 |
+
normal_map = (normal_map * 255).astype(np.uint8)
|
| 44 |
+
disp_map = (disp_map * 255).astype(np.uint8)
|
| 45 |
+
rough_map = (rough_map * 255).astype(np.uint8)
|
| 46 |
+
|
| 47 |
+
# Convert to RGB for Gradio output
|
| 48 |
+
normal_map = cv2.cvtColor(normal_map, cv2.COLOR_BGR2RGB)
|
| 49 |
+
disp_map = cv2.cvtColor(disp_map, cv2.COLOR_BGR2RGB)
|
| 50 |
+
rough_map = cv2.cvtColor(rough_map, cv2.COLOR_BGR2RGB)
|
| 51 |
+
|
| 52 |
+
return normal_map, disp_map, rough_map
|
| 53 |
+
|
| 54 |
+
# Gradio interface
|
| 55 |
+
def generate_maps(input_image, tile_size, seamless, use_cpu):
|
| 56 |
+
normal_map, disp_map, rough_map = process_image(input_image, tile_size, seamless, use_cpu)
|
| 57 |
+
return input_image, normal_map, disp_map, rough_map
|
| 58 |
+
|
| 59 |
+
interface = gr.Interface(
|
| 60 |
+
fn=generate_maps,
|
| 61 |
+
inputs=[
|
| 62 |
+
gr.Image(type="pil", label="Diffuse Texture"),
|
| 63 |
+
gr.Slider(minimum=256, maximum=1024, step=64, value=512, label="Tile Size"),
|
| 64 |
+
gr.Checkbox(label="Seamless", value=False),
|
| 65 |
+
gr.Checkbox(label="Use CPU", value=False),
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Image(type="numpy", label="Input Diffuse Texture"),
|
| 69 |
+
gr.Image(type="numpy", label="Normal Map"),
|
| 70 |
+
gr.Image(type="numpy", label="Displacement Map"),
|
| 71 |
+
gr.Image(type="numpy", label="Roughness Map"),
|
| 72 |
+
],
|
| 73 |
+
title="Material Map Generator",
|
| 74 |
+
description="Upload a diffuse texture to generate AI-generated Normal, Displacement, and Roughness maps."
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
if __name__ == "__main__":
|
| 78 |
+
interface.launch()
|