Spaces:
Build error
Build error
neverix
commited on
Commit
•
fbf122b
1
Parent(s):
8bf2254
For when the situation calms down
Browse files- app.py +6 -2
- pulsar_clip.py +4 -0
- utils.py +81 -0
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from pulsar_clip import PulsarCLIP, CONFIG_SPEC
|
2 |
from datetime import datetime
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
|
6 |
def generate(*args):
|
@@ -14,7 +15,8 @@ def generate(*args):
|
|
14 |
from tqdm.auto import tqdm
|
15 |
from subprocess import Popen, PIPE
|
16 |
fps = 30
|
17 |
-
|
|
|
18 |
if frames:
|
19 |
p = Popen((f"ffmpeg -y -f image2pipe -vcodec png -r {fps} -i - -vcodec libx264 -r {fps} "
|
20 |
f"-pix_fmt yuv420p -crf 17 -preset fast ").split() + [str(video_path)], stdin=PIPE)
|
@@ -22,7 +24,9 @@ def generate(*args):
|
|
22 |
im.save(p.stdin, "PNG")
|
23 |
p.stdin.close()
|
24 |
p.wait()
|
25 |
-
model_path =
|
|
|
|
|
26 |
return [video_path, model_path]
|
27 |
|
28 |
|
|
|
1 |
from pulsar_clip import PulsarCLIP, CONFIG_SPEC
|
2 |
from datetime import datetime
|
3 |
import gradio as gr
|
4 |
+
import utils
|
5 |
|
6 |
|
7 |
def generate(*args):
|
|
|
15 |
from tqdm.auto import tqdm
|
16 |
from subprocess import Popen, PIPE
|
17 |
fps = 30
|
18 |
+
filename = datetime.strftime(datetime.now(), "%Y-%m-%d-%H-%M-%S")
|
19 |
+
video_path = f"{filename}.mp4"
|
20 |
if frames:
|
21 |
p = Popen((f"ffmpeg -y -f image2pipe -vcodec png -r {fps} -i - -vcodec libx264 -r {fps} "
|
22 |
f"-pix_fmt yuv420p -crf 17 -preset fast ").split() + [str(video_path)], stdin=PIPE)
|
|
|
24 |
im.save(p.stdin, "PNG")
|
25 |
p.stdin.close()
|
26 |
p.wait()
|
27 |
+
model_path = f"{filename}.ply"
|
28 |
+
model.save_ply(model_path)
|
29 |
+
# model_path = None # TODO
|
30 |
return [video_path, model_path]
|
31 |
|
32 |
|
pulsar_clip.py
CHANGED
@@ -224,6 +224,10 @@ class PulsarCLIP(object):
|
|
224 |
#yield img
|
225 |
except KeyboardInterrupt:
|
226 |
pass
|
|
|
|
|
|
|
|
|
227 |
|
228 |
|
229 |
class DotDict(dict):
|
|
|
224 |
#yield img
|
225 |
except KeyboardInterrupt:
|
226 |
pass
|
227 |
+
|
228 |
+
|
229 |
+
def save_ply(self, fn):
|
230 |
+
utils.save_ply(self.get_points(), fn)
|
231 |
|
232 |
|
233 |
class DotDict(dict):
|
utils.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import random
|
2 |
import torch
|
3 |
import math
|
@@ -69,3 +70,83 @@ def load_clip(model_name="ViT-B/16", device="cuda:0" if torch.cuda.is_available(
|
|
69 |
if len(preprocess.transforms) > 4:
|
70 |
preprocess.transforms = preprocess.transforms[-1:]
|
71 |
return model, preprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
import random
|
3 |
import torch
|
4 |
import math
|
|
|
70 |
if len(preprocess.transforms) > 4:
|
71 |
preprocess.transforms = preprocess.transforms[-1:]
|
72 |
return model, preprocess
|
73 |
+
|
74 |
+
|
75 |
+
# http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html
|
76 |
+
def ico():
|
77 |
+
phi = (1 + 5 ** 0.5) / 2
|
78 |
+
return (
|
79 |
+
np.array([
|
80 |
+
[-1, phi, 0],
|
81 |
+
[1, phi, 0],
|
82 |
+
[-1, -phi, 0],
|
83 |
+
[1, -phi, 0],
|
84 |
+
[0, -1, phi],
|
85 |
+
[0, 1, phi],
|
86 |
+
[0, -1, -phi],
|
87 |
+
[0, 1, -phi],
|
88 |
+
[phi, 0, -1],
|
89 |
+
[phi, 0, 1],
|
90 |
+
[-phi, 0, -1],
|
91 |
+
[-phi, 0, 1]
|
92 |
+
]) / phi,
|
93 |
+
[
|
94 |
+
[0, 11, 5],
|
95 |
+
[0, 5, 1],
|
96 |
+
[0, 1, 7],
|
97 |
+
[0, 7, 10],
|
98 |
+
[0, 10, 11],
|
99 |
+
[1, 5, 9],
|
100 |
+
[5, 11, 4],
|
101 |
+
[11, 10, 2],
|
102 |
+
[10, 7, 6],
|
103 |
+
[7, 1, 8],
|
104 |
+
[3, 9, 4],
|
105 |
+
[3, 4, 2],
|
106 |
+
[3, 2, 6],
|
107 |
+
[3, 6, 8],
|
108 |
+
[3, 8, 9],
|
109 |
+
[4, 9, 5],
|
110 |
+
[2, 4, 11],
|
111 |
+
[6, 2, 10],
|
112 |
+
[8, 6, 7],
|
113 |
+
[9, 8, 1]
|
114 |
+
]
|
115 |
+
)
|
116 |
+
|
117 |
+
|
118 |
+
def ico_at(xyz=np.array([0, 0, 0]), radius=1.0, i=0):
|
119 |
+
vert, idx = ico()
|
120 |
+
return vert * radius + xyz, [[y + i for y in x] for x in idx]
|
121 |
+
|
122 |
+
|
123 |
+
def save_ply(points, out_path):
|
124 |
+
with torch.inference_mode():
|
125 |
+
vert_pos, vert_col, vert_rad, vert_opa = (x.detach().cpu().numpy() for x in points)
|
126 |
+
|
127 |
+
verts = []
|
128 |
+
faces = []
|
129 |
+
for xyz, radius, (r, g, b), a in zip(vert_pos, vert_rad, vert_col, vert_opa):
|
130 |
+
v, i = ico_at(xyz, radius, len(verts))
|
131 |
+
for x, y, z in v:
|
132 |
+
verts.append((x, y, z, int(r * 255), int(g * 255), int(b * 255), int(a * 255)))
|
133 |
+
faces += i
|
134 |
+
|
135 |
+
with open(out_path, "w") as out_file:
|
136 |
+
out_file.write("ply\n")
|
137 |
+
out_file.write("format ascii 1.0\n")
|
138 |
+
out_file.write(f"element vertex {len(verts)}\n")
|
139 |
+
out_file.write("property float x\n")
|
140 |
+
out_file.write("property float y\n")
|
141 |
+
out_file.write("property float z\n")
|
142 |
+
out_file.write("property uchar red\n")
|
143 |
+
out_file.write("property uchar green\n")
|
144 |
+
out_file.write("property uchar blue\n")
|
145 |
+
out_file.write("property uchar alpha\n")
|
146 |
+
out_file.write(f"element face {len(faces)}\n")
|
147 |
+
out_file.write("property list uchar int vertex_index\n")
|
148 |
+
out_file.write("end_header\n")
|
149 |
+
for v in verts:
|
150 |
+
out_file.write(" ".join(map(str, v)) + "\n")
|
151 |
+
for f in faces:
|
152 |
+
out_file.write(" ".join(map(str, [len(f)] + f)) + "\n")
|