Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,30 @@
|
|
| 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 |
-
#
|
| 11 |
try:
|
| 12 |
from controlnet_aux.open_pose import OpenposeDetector
|
| 13 |
except ImportError:
|
| 14 |
-
from controlnet_aux.openpose import OpenposeDetector # fallback
|
| 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 |
-
#
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
|
|
|
| 65 |
demo = gr.Interface(
|
| 66 |
-
fn=
|
| 67 |
-
inputs=
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
outputs=gr.Image(type="pil", label="Result"),
|
| 72 |
-
title="OpenPose + DepthAnything Demo",
|
| 73 |
)
|
| 74 |
|
| 75 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from PIL import Image
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
+
# Import OpenPose from controlnet-aux
|
| 5 |
try:
|
| 6 |
from controlnet_aux.open_pose import OpenposeDetector
|
| 7 |
except ImportError:
|
| 8 |
+
from controlnet_aux.openpose import OpenposeDetector # fallback for older versions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
# Load the model once
|
| 11 |
+
openpose = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
|
| 12 |
|
| 13 |
+
# Gradio inference function
|
| 14 |
+
def generate_pose(image: Image.Image):
|
| 15 |
+
image = image.convert("RGB")
|
| 16 |
+
result = openpose(image)
|
| 17 |
+
if not isinstance(result, Image.Image):
|
| 18 |
+
result = Image.fromarray(result)
|
| 19 |
+
return result
|
| 20 |
|
| 21 |
+
# Gradio UI
|
| 22 |
demo = gr.Interface(
|
| 23 |
+
fn=generate_pose,
|
| 24 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 25 |
+
outputs=gr.Image(type="pil", label="Pose Output"),
|
| 26 |
+
title="OpenPose Pose Generator",
|
| 27 |
+
description="Upload an image and generate a pose map using OpenPose."
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
if __name__ == "__main__":
|