update
Browse files
app.py
CHANGED
|
@@ -13,26 +13,16 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 13 |
print(f"Using device: {DEVICE}")
|
| 14 |
|
| 15 |
# Model cache
|
| 16 |
-
|
| 17 |
-
_dwpose_detector = None
|
| 18 |
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
"""Get or create
|
| 22 |
-
global
|
| 23 |
-
if
|
| 24 |
-
from controlnet_aux import
|
| 25 |
-
|
| 26 |
-
return
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def get_dwpose_detector():
|
| 30 |
-
"""Get or create DWPose detector."""
|
| 31 |
-
global _dwpose_detector
|
| 32 |
-
if _dwpose_detector is None:
|
| 33 |
-
from controlnet_aux import DWposeDetector
|
| 34 |
-
_dwpose_detector = DWposeDetector.from_pretrained("yolox_l.onnx", "dw-ll_ucoco_384.onnx")
|
| 35 |
-
return _dwpose_detector
|
| 36 |
|
| 37 |
|
| 38 |
def detect_pose(image, model_type, detect_hand, detect_face, detect_resolution):
|
|
@@ -49,50 +39,46 @@ def detect_pose(image, model_type, detect_hand, detect_face, detect_resolution):
|
|
| 49 |
if image.mode != "RGB":
|
| 50 |
image = image.convert("RGB")
|
| 51 |
|
| 52 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
if model_type == "DWPose":
|
| 54 |
-
|
| 55 |
-
result = detector(
|
| 56 |
-
image,
|
| 57 |
-
detect_resolution=detect_resolution,
|
| 58 |
-
image_resolution=detect_resolution,
|
| 59 |
-
include_hand=detect_hand,
|
| 60 |
-
include_face=detect_face,
|
| 61 |
-
include_body=True,
|
| 62 |
-
output_type="pil"
|
| 63 |
-
)
|
| 64 |
elif model_type == "OpenPose (Full)":
|
| 65 |
-
|
| 66 |
-
result = detector(
|
| 67 |
-
image,
|
| 68 |
-
detect_resolution=detect_resolution,
|
| 69 |
-
hand_and_face=True,
|
| 70 |
-
output_type="pil"
|
| 71 |
-
)
|
| 72 |
elif model_type == "OpenPose (Face Only)":
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
detect_resolution=detect_resolution,
|
| 77 |
-
include_body=False,
|
| 78 |
-
include_hand=False,
|
| 79 |
-
include_face=True,
|
| 80 |
-
output_type="pil"
|
| 81 |
-
)
|
| 82 |
else:
|
| 83 |
-
# Basic OpenPose
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
return result
|
| 93 |
|
| 94 |
except Exception as e:
|
| 95 |
print(f"Error during processing: {str(e)}")
|
|
|
|
|
|
|
| 96 |
return None
|
| 97 |
|
| 98 |
|
|
@@ -118,14 +104,14 @@ with gr.Blocks(
|
|
| 118 |
|
| 119 |
model_type = gr.Dropdown(
|
| 120 |
label="π€ Model",
|
| 121 |
-
choices=["DWPose", "OpenPose", "OpenPose (Full)", "OpenPose (Face Only)"],
|
| 122 |
-
value="
|
| 123 |
-
info="
|
| 124 |
)
|
| 125 |
|
| 126 |
with gr.Row():
|
| 127 |
-
detect_hand = gr.Checkbox(label="π Detect Hands", value=True)
|
| 128 |
-
detect_face = gr.Checkbox(label="π Detect Face", value=True)
|
| 129 |
|
| 130 |
detect_resolution = gr.Slider(
|
| 131 |
label="π Detection Resolution",
|
|
@@ -144,10 +130,14 @@ with gr.Blocks(
|
|
| 144 |
gr.Markdown(
|
| 145 |
"""
|
| 146 |
### π Tips
|
| 147 |
-
- **
|
| 148 |
-
- **
|
| 149 |
- Higher **Detection Resolution** improves accuracy but increases processing time
|
| 150 |
- The output image can be directly used with ControlNet OpenPose models
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
"""
|
| 152 |
)
|
| 153 |
|
|
|
|
| 13 |
print(f"Using device: {DEVICE}")
|
| 14 |
|
| 15 |
# Model cache
|
| 16 |
+
_processors = {}
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
+
def get_processor(processor_id):
|
| 20 |
+
"""Get or create a processor by ID."""
|
| 21 |
+
global _processors
|
| 22 |
+
if processor_id not in _processors:
|
| 23 |
+
from controlnet_aux.processor import Processor
|
| 24 |
+
_processors[processor_id] = Processor(processor_id)
|
| 25 |
+
return _processors[processor_id]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
def detect_pose(image, model_type, detect_hand, detect_face, detect_resolution):
|
|
|
|
| 39 |
if image.mode != "RGB":
|
| 40 |
image = image.convert("RGB")
|
| 41 |
|
| 42 |
+
# Resize to detect_resolution while maintaining aspect ratio
|
| 43 |
+
original_size = image.size
|
| 44 |
+
ratio = detect_resolution / max(original_size)
|
| 45 |
+
new_size = (int(original_size[0] * ratio), int(original_size[1] * ratio))
|
| 46 |
+
image_resized = image.resize(new_size, Image.Resampling.LANCZOS)
|
| 47 |
+
|
| 48 |
+
# Map model type to processor ID
|
| 49 |
if model_type == "DWPose":
|
| 50 |
+
processor_id = "dwpose"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
elif model_type == "OpenPose (Full)":
|
| 52 |
+
processor_id = "openpose_full"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
elif model_type == "OpenPose (Face Only)":
|
| 54 |
+
processor_id = "openpose_faceonly"
|
| 55 |
+
elif model_type == "OpenPose (Hand)":
|
| 56 |
+
processor_id = "openpose_hand"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
else:
|
| 58 |
+
# Basic OpenPose with options
|
| 59 |
+
if detect_hand and detect_face:
|
| 60 |
+
processor_id = "openpose_full"
|
| 61 |
+
elif detect_face:
|
| 62 |
+
processor_id = "openpose_face"
|
| 63 |
+
elif detect_hand:
|
| 64 |
+
processor_id = "openpose_hand"
|
| 65 |
+
else:
|
| 66 |
+
processor_id = "openpose"
|
| 67 |
+
|
| 68 |
+
# Get processor and process
|
| 69 |
+
processor = get_processor(processor_id)
|
| 70 |
+
result = processor(image_resized, to_pil=True)
|
| 71 |
+
|
| 72 |
+
# Resize result back to original size if needed
|
| 73 |
+
if result is not None and result.size != original_size:
|
| 74 |
+
result = result.resize(original_size, Image.Resampling.LANCZOS)
|
| 75 |
|
| 76 |
return result
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
print(f"Error during processing: {str(e)}")
|
| 80 |
+
import traceback
|
| 81 |
+
traceback.print_exc()
|
| 82 |
return None
|
| 83 |
|
| 84 |
|
|
|
|
| 104 |
|
| 105 |
model_type = gr.Dropdown(
|
| 106 |
label="π€ Model",
|
| 107 |
+
choices=["DWPose", "OpenPose", "OpenPose (Full)", "OpenPose (Face Only)", "OpenPose (Hand)"],
|
| 108 |
+
value="OpenPose (Full)",
|
| 109 |
+
info="OpenPose (Full) is recommended for body+face+hand detection"
|
| 110 |
)
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
+
detect_hand = gr.Checkbox(label="π Detect Hands", value=True, info="For basic OpenPose mode")
|
| 114 |
+
detect_face = gr.Checkbox(label="π Detect Face", value=True, info="For basic OpenPose mode")
|
| 115 |
|
| 116 |
detect_resolution = gr.Slider(
|
| 117 |
label="π Detection Resolution",
|
|
|
|
| 130 |
gr.Markdown(
|
| 131 |
"""
|
| 132 |
### π Tips
|
| 133 |
+
- **OpenPose (Full)** detects body, face, and hands together - recommended for most use cases
|
| 134 |
+
- **DWPose** requires additional dependencies and may not work in all environments
|
| 135 |
- Higher **Detection Resolution** improves accuracy but increases processing time
|
| 136 |
- The output image can be directly used with ControlNet OpenPose models
|
| 137 |
+
|
| 138 |
+
### β οΈ Note
|
| 139 |
+
- Hand/Face checkboxes only apply when using basic "OpenPose" mode
|
| 140 |
+
- For full functionality, use "OpenPose (Full)" which includes everything
|
| 141 |
"""
|
| 142 |
)
|
| 143 |
|