Spaces:
Sleeping
Sleeping
Commit
·
ac73e0b
1
Parent(s):
512231d
Init commit
Browse files- .gitignore +2 -0
- app.py +78 -0
- pca_texture_model.npy +3 -0
- requirements.txt +4 -0
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
.venv/*
|
2 |
+
.idea/*
|
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import gradio as gr
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load the PCA model
|
7 |
+
PCA_MODEL_PATH = "pca_texture_model.npy"
|
8 |
+
pca = np.load(PCA_MODEL_PATH, allow_pickle=True).item()
|
9 |
+
|
10 |
+
# PCA attributes
|
11 |
+
mean_texture = pca.mean_
|
12 |
+
components = pca.components_
|
13 |
+
n_components = components.shape[0]
|
14 |
+
TEXTURE_SIZE = np.sqrt(mean_texture.shape[0] // 3).astype(int)
|
15 |
+
|
16 |
+
# Slider ranges based on PCA variance
|
17 |
+
slider_ranges = [3 * np.sqrt(var) for var in pca.explained_variance_]
|
18 |
+
|
19 |
+
# Function to reconstruct texture from PCA components
|
20 |
+
def reconstruct_texture(component_values):
|
21 |
+
# Calculate the texture using PCA components
|
22 |
+
new_texture = mean_texture + np.dot(component_values, components)
|
23 |
+
new_texture = np.clip(new_texture, 0, 255).astype(np.uint8) # Ensure valid pixel range
|
24 |
+
new_texture = new_texture.reshape((TEXTURE_SIZE, TEXTURE_SIZE, 3)) # Reshape back to image
|
25 |
+
new_texture = cv2.cvtColor(new_texture, cv2.COLOR_BGR2RGB) # Convert to RGB for display
|
26 |
+
return Image.fromarray(new_texture)
|
27 |
+
|
28 |
+
# Function to handle slider values and generate the texture
|
29 |
+
def generate_texture(*component_values):
|
30 |
+
component_values = np.array(component_values)
|
31 |
+
return reconstruct_texture(component_values)
|
32 |
+
|
33 |
+
# Function to generate random textures
|
34 |
+
def random_texture():
|
35 |
+
component_values = np.random.normal(0, 1, n_components)
|
36 |
+
return reconstruct_texture(component_values), list(component_values)
|
37 |
+
|
38 |
+
# Gradio interface
|
39 |
+
def create_app():
|
40 |
+
sliders = [
|
41 |
+
gr.Slider(
|
42 |
+
minimum=-slider_ranges[i],
|
43 |
+
maximum=slider_ranges[i],
|
44 |
+
step=0.1,
|
45 |
+
label=f"Component {i + 1}",
|
46 |
+
value=0,
|
47 |
+
)
|
48 |
+
for i in range(n_components)
|
49 |
+
]
|
50 |
+
|
51 |
+
# Define interface elements
|
52 |
+
with gr.Blocks() as app:
|
53 |
+
with gr.Row():
|
54 |
+
with gr.Column():
|
55 |
+
gr.Markdown("### PCA Texture Generator")
|
56 |
+
sliders_container = gr.Group(sliders)
|
57 |
+
generate_button = gr.Button("Generate")
|
58 |
+
random_button = gr.Button("Random")
|
59 |
+
|
60 |
+
with gr.Column():
|
61 |
+
preview = gr.Image(shape=(TEXTURE_SIZE, TEXTURE_SIZE), label="Preview")
|
62 |
+
|
63 |
+
# Link sliders and preview
|
64 |
+
generate_button.click(
|
65 |
+
generate_texture, inputs=sliders, outputs=preview
|
66 |
+
)
|
67 |
+
|
68 |
+
random_button.click(
|
69 |
+
random_texture, inputs=None, outputs=[preview] + sliders
|
70 |
+
)
|
71 |
+
|
72 |
+
return app
|
73 |
+
|
74 |
+
|
75 |
+
if __name__ == "__main__":
|
76 |
+
# Create and launch the app
|
77 |
+
app = create_app()
|
78 |
+
app.launch()
|
pca_texture_model.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2e1dbed56f7de2021a5304440e9f21ae3d87fcdd65f72703a871da8a4244a02c
|
3 |
+
size 19662889
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
numpy
|
3 |
+
opencv-python
|
4 |
+
pillow
|