52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342 | @define
class DepthScene(ShaderScene):
state: DepthState = Factory(DepthState)
class Config(ShaderScene.Config):
image: Iterable[PydanticImage] = DEFAULT_IMAGE
depth: Iterable[PydanticImage] = None
estimator: DepthEstimator = Field(default_factory=DepthAnythingV2)
animation: DepthAnimation = Field(default_factory=DepthAnimation)
upscaler: BrokenUpscaler = Field(default_factory=NoUpscaler)
# -------------------------------------------------------------------------------------------- #
# Command line interface
def commands(self):
self.cli.description = DEPTHFLOW_ABOUT
with self.cli.panel(self.scene_panel):
self.cli.command(self.input)
with self.cli.panel("🔧 Preloading"):
self.cli.command(self.load_estimator, hidden=True)
self.cli.command(self.load_upscaler, hidden=True)
with self.cli.panel("🌊 Depth estimator"):
self.cli.command(DepthAnythingV1, post=self.set_estimator, name="any1")
self.cli.command(DepthAnythingV2, post=self.set_estimator, name="any2")
self.cli.command(DepthPro, post=self.set_estimator)
self.cli.command(ZoeDepth, post=self.set_estimator)
self.cli.command(Marigold, post=self.set_estimator)
with self.cli.panel("⭐️ Upscaler"):
self.cli.command(Realesr, post=self.set_upscaler)
self.cli.command(Upscayl, post=self.set_upscaler)
self.cli.command(Waifu2x, post=self.set_upscaler)
with self.cli.panel("🚀 Animation components"):
_hidden = Environment.flag("ADVANCED", 0)
for animation in Actions.members():
if issubclass(animation, ComponentBase):
self.cli.command(animation, post=self.config.animation.add, hidden=_hidden)
with self.cli.panel("🔮 Animation presets"):
for preset in Actions.members():
if issubclass(preset, PresetBase):
self.cli.command(preset, post=self.config.animation.add)
with self.cli.panel("🎨 Post-processing"):
for post in Actions.members():
if issubclass(post, FilterBase):
self.cli.command(post, post=self.config.animation.add)
def input(self,
image: Annotated[list[str], Option("--image", "-i",
help="[bold green](🟢 Basic)[/] Background Image [green](Path, URL, NumPy, PIL)[/]"
)],
depth: Annotated[list[str], Option("--depth", "-d",
help="[bold green](🟢 Basic)[/] Depthmap of the Image [medium_purple3](None to estimate)[/]"
)]=None,
) -> None:
"""Input images from Path, URL, Directories and its estimated Depthmap (Lazy load)"""
self.config.image = image
self.config.depth = depth
# -------------------------------------------------------------------------------------------- #
# Module implementation
def build(self) -> None:
self.image = ShaderTexture(scene=self, name="image").repeat(False)
self.depth = ShaderTexture(scene=self, name="depth").repeat(False)
self.shader.fragment = DEPTH_SHADER
self.base_duration = 5.0
self.subsample = 2
self.ssaa = 1.2
def setup(self) -> None:
if (not self.config.animation):
self.config.animation.add(Actions.Orbital())
self._load_inputs()
def update(self) -> None:
self.config.animation.apply(self)
def handle(self, message: ShaderMessage) -> None:
ShaderScene.handle(self, message)
if isinstance(message, ShaderMessage.Window.FileDrop):
self.input(image=message.first, depth=message.second)
self._load_inputs()
def pipeline(self) -> Iterable[ShaderVariable]:
yield from ShaderScene.pipeline(self)
yield from self.state.pipeline()
# -------------------------------------------------------------------------------------------- #
# Proxy methods
# # Upscalers
def set_upscaler(self, upscaler: Optional[BrokenUpscaler]=None) -> BrokenUpscaler:
self.config.upscaler = (upscaler or NoUpscaler())
return self.config.upscaler
def clear_upscaler(self) -> None:
self.config.upscaler = NoUpscaler()
def load_upscaler(self) -> None:
self.config.upscaler.download()
def realesr(self, **options) -> Realesr:
return self.set_upscaler(Realesr(**options))
def upscayl(self, **options) -> Upscayl:
return self.set_upscaler(Upscayl(**options))
def waifu2x(self, **options) -> Waifu2x:
return self.set_upscaler(Waifu2x(**options))
# # Estimators
def set_estimator(self, estimator: BaseEstimator) -> BaseEstimator:
self.config.estimator = estimator
return self.config.estimator
def load_estimator(self) -> None:
self.config.estimator.load_model()
def depth_anything1(self, **options) -> DepthAnythingV1:
return self.set_estimator(DepthAnythingV1(**options))
def depth_anything2(self, **options) -> DepthAnythingV2:
return self.set_estimator(DepthAnythingV2(**options))
def depth_pro(self, **options) -> DepthPro:
return self.set_estimator(DepthPro(**options))
def zoe_depth(self, **options) -> ZoeDepth:
return self.set_estimator(ZoeDepth(**options))
def marigold(self, **options) -> Marigold:
return self.set_estimator(Marigold(**options))
# # Animations
# Constant
def set(self, **options) -> Actions.Set:
return self.config.animation.add(Actions.Set(**options))
def add(self, **options) -> Actions.Add:
return self.config.animation.add(Actions.Add(**options))
# Basic
def linear(self, **options) -> Actions.Linear:
return self.config.animation.add(Actions.Linear(**options))
def sine(self, **options) -> Actions.Sine:
return self.config.animation.add(Actions.Sine(**options))
def cosine(self, **options) -> Actions.Cosine:
return self.config.animation.add(Actions.Cosine(**options))
def triangle(self, **options) -> Actions.Triangle:
return self.config.animation.add(Actions.Triangle(**options))
# Presets
def vertical(self, **options) -> Actions.Vertical:
return self.config.animation.add(Actions.Vertical(**options))
def horizontal(self, **options) -> Actions.Horizontal:
return self.config.animation.add(Actions.Horizontal(**options))
def zoom(self, **options) -> Actions.Zoom:
return self.config.animation.add(Actions.Zoom(**options))
def circle(self, **options) -> Actions.Circle:
return self.config.animation.add(Actions.Circle(**options))
def dolly(self, **options) -> Actions.Dolly:
return self.config.animation.add(Actions.Dolly(**options))
def orbital(self, **options) -> Actions.Orbital:
return self.config.animation.add(Actions.Orbital(**options))
# Post-processing
def vignette(self, **options) -> Actions.Vignette:
return self.config.animation.add(Actions.Vignette(**options))
def blur(self, **options) -> Actions.Blur:
return self.config.animation.add(Actions.Blur(**options))
def inpaint(self, **options) -> Actions.Inpaint:
return self.config.animation.add(Actions.Inpaint(**options))
def colors(self, **options) -> Actions.Colors:
return self.config.animation.add(Actions.Colors(**options))
# -------------------------------------------------------------------------------------------- #
# Internal batch exporting
def _load_inputs(self) -> None:
"""Load inputs: single or batch exporting"""
# Batch exporting implementation
image = self._get_batch_input(self.config.image)
depth = self._get_batch_input(self.config.depth)
if (image is None):
raise ShaderBatchStop()
self.log_info(f"Loading image: {image}")
self.log_info(f"Loading depth: {depth or 'Estimating from image'}")
# Load, estimate, upscale input image
image = self.config.upscaler.upscale(LoadImage(image))
depth = LoadImage(depth) or self.config.estimator.estimate(image)
# Match rendering resolution to image
self.resolution = (image.width,image.height)
self.aspect_ratio = (image.width/image.height)
self.image.from_image(image)
self.depth.from_image(depth)
# Default to 1920x1080 on base image
if (self.config.image is DEFAULT_IMAGE):
self.resolution = (1920, 1080)
self.aspect_ratio = (16/9)
def export_name(self, path: Path) -> Path:
"""Modifies the output path if on batch exporting mode"""
options = list(self._iter_batch_input(self.config.image))
# Single file mode, return as-is
if (len(options) == 1):
return path
# Assume it's a local path
image = Path(options[self.index])
original = image.stem
# Use the URL filename as base
if validators.url(image):
original = BrokenPath.url_filename(image)
# Build the batch filename: 'file' + -'custom stem'
return path.with_stem(original + "-" + path.stem)
def _iter_batch_input(self, item: Optional[LoadableImage]) -> Iterable[LoadableImage]:
if (item is None):
return None
# Recurse on multiple inputs
if isinstance(item, (list, tuple, set)):
for part in item:
yield from self._iter_batch_input(part)
# Return known valid inputs as is
elif isinstance(item, (bytes, Image, numpy.ndarray)):
yield item
elif validators.url(item):
yield item
# Valid directory on disk
elif (path := BrokenPath.get(item, exists=True)):
if (path.is_dir()):
files = (path.glob("*" + x) for x in FileExtensions.Image)
yield from sorted(flatten(files))
else:
yield path
# Interpret as a glob pattern
elif ("*" in str(item)):
yield from sorted(path.parent.glob(path.name))
else:
self.log_minor(f"Assuming {item} is an iterable, could go wrong..")
yield from item
def _get_batch_input(self, item: LoadableImage) -> Optional[LoadableImage]:
return list_get(list(self._iter_batch_input(item)), self.index)
def ui(self) -> None:
if (state := imgui.slider_float("Height", self.state.height, 0, 1, "%.2f"))[0]:
self.state.height = max(0, state[1])
if (state := imgui.slider_float("Steady", self.state.steady, 0, 1, "%.2f"))[0]:
self.state.steady = max(0, state[1])
if (state := imgui.slider_float("Focus", self.state.focus, 0, 1, "%.2f"))[0]:
self.state.focus = max(0, state[1])
if (state := imgui.slider_float("Invert", self.state.invert, 0, 1, "%.2f"))[0]:
self.state.invert = max(0, state[1])
if (state := imgui.slider_float("Zoom", self.state.zoom, 0, 2, "%.2f"))[0]:
self.state.zoom = max(0, state[1])
if (state := imgui.slider_float("Isometric", self.state.isometric, 0, 1, "%.2f"))[0]:
self.state.isometric = max(0, state[1])
if (state := imgui.slider_float("Dolly", self.state.dolly, 0, 5, "%.2f"))[0]:
self.state.dolly = max(0, state[1])
imgui.text("- True camera position")
if (state := imgui.slider_float("Center X", self.state.center_x, -self.aspect_ratio, self.aspect_ratio, "%.2f"))[0]:
self.state.center_x = state[1]
if (state := imgui.slider_float("Center Y", self.state.center_y, -1, 1, "%.2f"))[0]:
self.state.center_y = state[1]
imgui.text("- Fixed point at height changes")
if (state := imgui.slider_float("Origin X", self.state.origin_x, -self.aspect_ratio, self.aspect_ratio, "%.2f"))[0]:
self.state.origin_x = state[1]
if (state := imgui.slider_float("Origin Y", self.state.origin_y, -1, 1, "%.2f"))[0]:
self.state.origin_y = state[1]
imgui.text("- Parallax offset")
if (state := imgui.slider_float("Offset X", self.state.offset_x, -2, 2, "%.2f"))[0]:
self.state.offset_x = state[1]
if (state := imgui.slider_float("Offset Y", self.state.offset_y, -2, 2, "%.2f"))[0]:
self.state.offset_y = state[1]
|