Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,76 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
from controlnet_aux.open_pose import OpenposeDetector # NEW
|
| 4 |
-
from controlnet_aux.depth_anything import DepthAnythingDetector
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def process(image: Image.Image):
|
| 11 |
-
image = image.convert("RGB")
|
| 12 |
-
result_openpose = openpose(image)
|
| 13 |
-
result_depth = depth(image)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
if not isinstance(result_depth, Image.Image):
|
| 18 |
-
result_depth = Image.fromarray(result_depth)
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
demo = gr.Interface(
|
| 23 |
-
fn=
|
| 24 |
-
inputs=
|
| 25 |
-
|
| 26 |
-
gr.
|
| 27 |
-
gr.Image(type="pil", label="Depth Result"),
|
| 28 |
],
|
| 29 |
-
|
| 30 |
-
|
| 31 |
)
|
| 32 |
|
| 33 |
if __name__ == "__main__":
|
| 34 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
from PIL import Image
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Gradio for UI
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# ControlNet aux for OpenPose
|
| 11 |
+
try:
|
| 12 |
+
from controlnet_aux.open_pose import OpenposeDetector
|
| 13 |
+
except ImportError:
|
| 14 |
+
from controlnet_aux.openpose import OpenposeDetector # fallback (old versions)
|
| 15 |
+
|
| 16 |
+
# DepthAnything now comes as its own package
|
| 17 |
+
from depth_anything.dpt import DepthAnything
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# --------- Wrappers ---------
|
| 21 |
+
class DepthAnythingDetector:
|
| 22 |
+
def __init__(self, model_type="vitl", device="cpu"):
|
| 23 |
+
self.device = device
|
| 24 |
+
self.model = DepthAnything.from_pretrained(model_type).to(device)
|
| 25 |
+
|
| 26 |
+
def __call__(self, image: Image.Image):
|
| 27 |
+
arr = np.array(image.convert("RGB"))
|
| 28 |
+
tensor = torch.from_numpy(arr).permute(2, 0, 1).float().unsqueeze(0).to(self.device)
|
| 29 |
+
|
| 30 |
+
with torch.no_grad():
|
| 31 |
+
depth = self.model(tensor)[0]
|
| 32 |
+
|
| 33 |
+
depth = depth.cpu().numpy()
|
| 34 |
+
depth = (depth - depth.min()) / (depth.max() - depth.min()) * 255
|
| 35 |
+
depth = depth.astype(np.uint8)
|
| 36 |
+
return Image.fromarray(depth)
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# --------- Predictor Class ---------
|
| 40 |
+
class Predictor:
|
| 41 |
+
def __init__(self):
|
| 42 |
+
self.device = "cpu"
|
| 43 |
+
self.openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 44 |
+
self.depth = DepthAnythingDetector(model_type="vitl", device=self.device)
|
| 45 |
+
|
| 46 |
+
def run(self, image: Image.Image, use_openpose: bool = True) -> Image.Image:
|
| 47 |
+
if use_openpose:
|
| 48 |
+
result = self.openpose(image)
|
| 49 |
+
else:
|
| 50 |
+
result = self.depth(image)
|
| 51 |
+
|
| 52 |
+
if isinstance(result, Image.Image):
|
| 53 |
+
return result
|
| 54 |
+
else:
|
| 55 |
+
return Image.fromarray(result)
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# --------- Gradio UI ---------
|
| 59 |
+
predictor = Predictor()
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
def infer(image, method):
|
| 62 |
+
use_openpose = method == "OpenPose"
|
| 63 |
+
return predictor.run(image, use_openpose=use_openpose)
|
| 64 |
|
| 65 |
demo = gr.Interface(
|
| 66 |
+
fn=infer,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 69 |
+
gr.Radio(["OpenPose", "DepthAnything"], value="OpenPose", label="Method"),
|
|
|
|
| 70 |
],
|
| 71 |
+
outputs=gr.Image(type="pil", label="Result"),
|
| 72 |
+
title="OpenPose + DepthAnything Demo",
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
| 76 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|