Spaces:
Runtime error
Runtime error
Upload 12 files
Browse files- 0.png +0 -0
- app.py +112 -6
- generated.png +0 -0
- image_dataset.py +1 -1
- models/player_model.glb +0 -0
- models/temp_model.glb +0 -0
- requirements.txt +2 -0
0.png
ADDED
app.py
CHANGED
@@ -6,7 +6,13 @@ from os.path import exists
|
|
6 |
from PIL import Image, ImageEnhance
|
7 |
import numpy as np
|
8 |
import random
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
print(gr.__version__)
|
11 |
def init():
|
12 |
generated_samples_folder = "."
|
@@ -24,6 +30,7 @@ def init():
|
|
24 |
)
|
25 |
crop()
|
26 |
|
|
|
27 |
def crop():
|
28 |
|
29 |
import generator
|
@@ -37,7 +44,7 @@ def crop():
|
|
37 |
|
38 |
top = 2
|
39 |
bottom = 2
|
40 |
-
for i in range(
|
41 |
left = (res+2)*i +2
|
42 |
right = width-(res+2)*i
|
43 |
imgcrop = img.crop((left,top,left+res,res+2))
|
@@ -57,35 +64,134 @@ def gen(seed=None):
|
|
57 |
init() # Initialize and generate samples
|
58 |
crop() # Crop the generated images
|
59 |
imgArr = []
|
60 |
-
for i in range(
|
61 |
img = Image.open(f"{i}.png").convert('RGBA')
|
62 |
img = img.resize((64, 64), Image.NEAREST)
|
63 |
imgArr.append(img)
|
64 |
return imgArr, seed # Return both images and the seed used
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
|
|
|
|
|
|
|
|
|
|
|
67 |
# Function to handle the input from the interface
|
68 |
def handle_input(seed_text, surprise_me):
|
69 |
if surprise_me:
|
70 |
images, used_seed = gen("Surprise Me")
|
71 |
else:
|
72 |
images, used_seed = gen(seed_text)
|
73 |
-
|
|
|
|
|
74 |
|
75 |
|
76 |
|
77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
iface = gr.Interface(
|
79 |
fn=handle_input,
|
80 |
inputs=[
|
81 |
gr.Text(label="Seed", placeholder="Enter a seed"), # Text input for seed
|
82 |
gr.Checkbox(label="Surprise Me", value=False), # Checkbox for 'Surprise Me' functionality
|
|
|
83 |
],
|
84 |
outputs=[
|
85 |
-
gr.Gallery(label="Generated Skins"),
|
86 |
-
gr.Textbox(label="Used Seed") # Display the seed used to generate the images
|
|
|
|
|
|
|
|
|
|
|
87 |
],
|
88 |
title = "Minecraft Skin Generator <style>img{image-rendering: pixelated;}</style>", #<-- EWW GROSS IK IK IM SORRY, BUT THAT'S THE ONLY WAY I FOUND IT TO WORK
|
|
|
|
|
|
|
|
|
|
|
89 |
)
|
90 |
|
91 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
6 |
from PIL import Image, ImageEnhance
|
7 |
import numpy as np
|
8 |
import random
|
9 |
+
from pygltflib import GLTF2
|
10 |
+
from pygltflib.utils import ImageFormat, Texture, Material, Image as GLTFImage
|
11 |
+
import time
|
12 |
+
import threading
|
13 |
|
14 |
+
|
15 |
+
clicked_image_index = 0
|
16 |
print(gr.__version__)
|
17 |
def init():
|
18 |
generated_samples_folder = "."
|
|
|
30 |
)
|
31 |
crop()
|
32 |
|
33 |
+
|
34 |
def crop():
|
35 |
|
36 |
import generator
|
|
|
44 |
|
45 |
top = 2
|
46 |
bottom = 2
|
47 |
+
for i in range(1):
|
48 |
left = (res+2)*i +2
|
49 |
right = width-(res+2)*i
|
50 |
imgcrop = img.crop((left,top,left+res,res+2))
|
|
|
64 |
init() # Initialize and generate samples
|
65 |
crop() # Crop the generated images
|
66 |
imgArr = []
|
67 |
+
for i in range(1):
|
68 |
img = Image.open(f"{i}.png").convert('RGBA')
|
69 |
img = img.resize((64, 64), Image.NEAREST)
|
70 |
imgArr.append(img)
|
71 |
return imgArr, seed # Return both images and the seed used
|
72 |
|
73 |
+
def update_model(index):
|
74 |
+
filename = "models/player_model.glb"
|
75 |
+
gltf = GLTF2().load(filename)
|
76 |
+
|
77 |
+
# Step 1: Find the index of the existing texture you want to replace
|
78 |
+
# Let's assume the texture you want to replace is at index 1 (you need to replace 1 with the actual index)
|
79 |
+
existing_texture_index = 0
|
80 |
+
|
81 |
+
# Check if the existing_texture_index is valid
|
82 |
+
if existing_texture_index < len(gltf.textures):
|
83 |
+
# Step 2: Remove the old texture and its associated image from the GLB
|
84 |
+
# Remove the texture
|
85 |
+
gltf.textures.pop(existing_texture_index)
|
86 |
+
|
87 |
+
# Remove the image associated with the texture
|
88 |
+
existing_image_index = gltf.materials[0].pbrMetallicRoughness.baseColorTexture.index
|
89 |
+
gltf.images.pop(existing_image_index)
|
90 |
+
|
91 |
+
|
92 |
+
# Step 3: Add the new image and texture to the GLB
|
93 |
+
# Create and add a new image to the glTF (same as before)
|
94 |
+
new_image = GLTFImage()
|
95 |
+
new_image.uri = os.path.join(os.path.dirname(__file__), f"{index}.png")
|
96 |
+
gltf.images.append(new_image)
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
# Create a new texture and associate it with the added image
|
101 |
+
new_texture = Texture()
|
102 |
+
new_texture.source = len(gltf.images) - 1 # Index of the newly added image
|
103 |
+
new_texture.sampler = 0
|
104 |
+
# set to nearest neighbor
|
105 |
+
|
106 |
+
gltf.textures.append(new_texture)
|
107 |
+
|
108 |
+
# Step 4: Assign the new texture to the appropriate material(s) or mesh(es)
|
109 |
+
# Assuming you have a mesh/primitive that was using the old texture and you want to apply the new texture to it, you need to set the material index for that mesh/primitive.
|
110 |
+
# Replace 0 with the actual index of the mesh/primitive you want to update.
|
111 |
+
gltf.meshes[0].primitives[0].material = len(gltf.materials) - 1
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
# Now you can convert the images to data URIs and save the updated GLB
|
118 |
+
gltf.convert_images(ImageFormat.DATAURI)
|
119 |
+
|
120 |
+
filename2 = "models/temp_model.glb"
|
121 |
+
gltf.save(filename2)
|
122 |
+
else:
|
123 |
+
print("Invalid existing_texture_index")
|
124 |
|
125 |
+
|
126 |
+
running = False
|
127 |
+
time_since_last_run = 0
|
128 |
+
|
129 |
+
|
130 |
# Function to handle the input from the interface
|
131 |
def handle_input(seed_text, surprise_me):
|
132 |
if surprise_me:
|
133 |
images, used_seed = gen("Surprise Me")
|
134 |
else:
|
135 |
images, used_seed = gen(seed_text)
|
136 |
+
|
137 |
+
|
138 |
+
loadMinecraftSkinViewer(0)
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
return images, f"Seed Used: {used_seed}", os.path.join(os.path.dirname(__file__), "models/temp_model.glb")
|
148 |
+
|
149 |
+
# Function to load the Minecraft skin viewer
|
150 |
+
def loadMinecraftSkinViewer(index):
|
151 |
+
update_model(index)
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
head = f"""
|
159 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
|
160 |
+
<script>
|
161 |
+
function loadMinecraftSkinViewer(index) {{
|
162 |
+
gr.interface.trigger("generate", index);
|
163 |
+
}}
|
164 |
+
</script>
|
165 |
+
"""
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
iface = gr.Interface(
|
170 |
fn=handle_input,
|
171 |
inputs=[
|
172 |
gr.Text(label="Seed", placeholder="Enter a seed"), # Text input for seed
|
173 |
gr.Checkbox(label="Surprise Me", value=False), # Checkbox for 'Surprise Me' functionality
|
174 |
+
|
175 |
],
|
176 |
outputs=[
|
177 |
+
gr.Gallery(label="Generated Skins"),
|
178 |
+
gr.Textbox(label="Used Seed"), # Display the seed used to generate the images
|
179 |
+
gr.Model3D(
|
180 |
+
clear_color=[0.0, 0.0, 0.0, 0.0], label="3D Model"),
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
],
|
185 |
title = "Minecraft Skin Generator <style>img{image-rendering: pixelated;}</style>", #<-- EWW GROSS IK IK IM SORRY, BUT THAT'S THE ONLY WAY I FOUND IT TO WORK
|
186 |
+
head=head, # Add the Babylon.js library to the interface
|
187 |
+
allow_flagging=False,
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
)
|
192 |
|
193 |
+
iface.launch()
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
|
generated.png
ADDED
image_dataset.py
CHANGED
@@ -21,4 +21,4 @@ class ImageDataset(Dataset):
|
|
21 |
return self.transform(Image.open(self.files[index % len(self.files)]))
|
22 |
|
23 |
def __len__(self):
|
24 |
-
return len(self.files)
|
|
|
21 |
return self.transform(Image.open(self.files[index % len(self.files)]))
|
22 |
|
23 |
def __len__(self):
|
24 |
+
return len(self.files)
|
models/player_model.glb
ADDED
Binary file (61.5 kB). View file
|
|
models/temp_model.glb
ADDED
Binary file (77.3 kB). View file
|
|
requirements.txt
CHANGED
@@ -5,3 +5,5 @@ Pillow===9.0.1
|
|
5 |
numpy==1.21.4
|
6 |
Flask==2.0.2
|
7 |
waitress==2.1.1
|
|
|
|
|
|
5 |
numpy==1.21.4
|
6 |
Flask==2.0.2
|
7 |
waitress==2.1.1
|
8 |
+
pygltflib
|
9 |
+
|