-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdemo.py
More file actions
190 lines (170 loc) · 5.72 KB
/
demo.py
File metadata and controls
190 lines (170 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
# Copyright (c) Meta Platforms, Inc. and affiliates.
import argparse
import os
from glob import glob
import pyrootutils
root = pyrootutils.setup_root(
search_from=__file__,
indicator=[".git", "pyproject.toml", ".sl"],
pythonpath=True,
dotenv=True,
)
import cv2
import numpy as np
import torch
from sam_3d_body import load_sam_3d_body, SAM3DBodyEstimator
from tools.vis_utils import visualize_sample, visualize_sample_together
from tqdm import tqdm
def main(args):
if args.output_folder == "":
output_folder = os.path.join("./output", os.path.basename(args.image_folder))
else:
output_folder = args.output_folder
os.makedirs(output_folder, exist_ok=True)
# Use command-line args or environment variables
mhr_path = args.mhr_path or os.environ.get("SAM3D_MHR_PATH", "")
detector_path = args.detector_path or os.environ.get("SAM3D_DETECTOR_PATH", "")
segmentor_path = args.segmentor_path or os.environ.get("SAM3D_SEGMENTOR_PATH", "")
fov_path = args.fov_path or os.environ.get("SAM3D_FOV_PATH", "")
# Initialize sam-3d-body model and other optional modules
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model, model_cfg = load_sam_3d_body(
args.checkpoint_path, device=device, mhr_path=mhr_path
)
human_detector, human_segmentor, fov_estimator = None, None, None
if args.detector_name:
from tools.build_detector import HumanDetector
human_detector = HumanDetector(
name=args.detector_name, device=device, path=detector_path
)
if len(segmentor_path):
from tools.build_sam import HumanSegmentor
human_segmentor = HumanSegmentor(
name=args.segmentor_name, device=device, path=segmentor_path
)
if args.fov_name:
from tools.build_fov_estimator import FOVEstimator
fov_estimator = FOVEstimator(name=args.fov_name, device=device, path=fov_path)
estimator = SAM3DBodyEstimator(
sam_3d_body_model=model,
model_cfg=model_cfg,
human_detector=human_detector,
human_segmentor=human_segmentor,
fov_estimator=fov_estimator,
)
image_extensions = [
"*.jpg",
"*.jpeg",
"*.png",
"*.gif",
"*.bmp",
"*.tiff",
"*.webp",
]
images_list = sorted(
[
image
for ext in image_extensions
for image in glob(os.path.join(args.image_folder, ext))
]
)
for image_path in tqdm(images_list):
outputs = estimator.process_one_image(
image_path,
bbox_thr=args.bbox_thresh,
use_mask=args.use_mask,
)
img = cv2.imread(image_path)
rend_img = visualize_sample_together(img, outputs, estimator.faces)
cv2.imwrite(
f"{output_folder}/{os.path.basename(image_path)[:-4]}.jpg",
rend_img.astype(np.uint8),
)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="SAM 3D Body Demo - Single Image Human Mesh Recovery",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python demo.py --image_folder ./images --checkpoint_path ./checkpoints/model.ckpt
Environment Variables:
SAM3D_MHR_PATH: Path to MHR asset
SAM3D_DETECTOR_PATH: Path to human detection model folder
SAM3D_SEGMENTOR_PATH: Path to human segmentation model folder
SAM3D_FOV_PATH: Path to fov estimation model folder
""",
)
parser.add_argument(
"--image_folder",
required=True,
type=str,
help="Path to folder containing input images",
)
parser.add_argument(
"--output_folder",
default="",
type=str,
help="Path to output folder (default: ./output/<image_folder_name>)",
)
parser.add_argument(
"--checkpoint_path",
required=True,
type=str,
help="Path to SAM 3D Body model checkpoint",
)
parser.add_argument(
"--detector_name",
default="vitdet",
type=str,
help="Human detection model for demo (Default `vitdet`, add your favorite detector if needed).",
)
parser.add_argument(
"--segmentor_name",
default="sam2",
type=str,
help="Human segmentation model for demo (Default `sam2`, add your favorite segmentor if needed).",
)
parser.add_argument(
"--fov_name",
default="moge2",
type=str,
help="FOV estimation model for demo (Default `moge2`, add your favorite fov estimator if needed).",
)
parser.add_argument(
"--detector_path",
default="",
type=str,
help="Path to human detection model folder (or set SAM3D_DETECTOR_PATH)",
)
parser.add_argument(
"--segmentor_path",
default="",
type=str,
help="Path to human segmentation model folder (or set SAM3D_SEGMENTOR_PATH)",
)
parser.add_argument(
"--fov_path",
default="",
type=str,
help="Path to fov estimation model folder (or set SAM3D_FOV_PATH)",
)
parser.add_argument(
"--mhr_path",
default="",
type=str,
help="Path to MoHR/assets folder (or set SAM3D_mhr_path)",
)
parser.add_argument(
"--bbox_thresh",
default=0.8,
type=float,
help="Bounding box detection threshold",
)
parser.add_argument(
"--use_mask",
action="store_true",
default=False,
help="Use mask-conditioned prediction (segmentation mask is automatically generated from bbox)",
)
args = parser.parse_args()
main(args)