3v324v23 commited on
Commit
efd9723
Β·
1 Parent(s): 32d1b11
Files changed (1) hide show
  1. app.py +50 -60
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
- _openpose_detector = None
17
- _dwpose_detector = None
18
 
19
 
20
- def get_openpose_detector():
21
- """Get or create OpenPose detector."""
22
- global _openpose_detector
23
- if _openpose_detector is None:
24
- from controlnet_aux import OpenposeDetector
25
- _openpose_detector = OpenposeDetector.from_pretrained("lllyasviel/Annotators")
26
- return _openpose_detector
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
- # Process based on model type
 
 
 
 
 
 
53
  if model_type == "DWPose":
54
- detector = get_dwpose_detector()
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
- detector = get_openpose_detector()
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
- detector = get_openpose_detector()
74
- result = detector(
75
- image,
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
- detector = get_openpose_detector()
85
- result = detector(
86
- image,
87
- detect_resolution=detect_resolution,
88
- hand_and_face=detect_hand and detect_face,
89
- output_type="pil"
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="DWPose",
123
- info="DWPose is recommended for better accuracy"
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
- - **DWPose** is recommended for best accuracy, especially for hands
148
- - **OpenPose (Full)** detects body, face, and hands together
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