Upload folder using huggingface_hub
Browse files- scripts/image_to_ply.py +15 -0
- scripts/run_image.py +116 -0
scripts/image_to_ply.py
CHANGED
|
@@ -28,6 +28,9 @@ def main():
|
|
| 28 |
ap.add_argument("--mode", choices=["synthetic", "real"], default="synthetic")
|
| 29 |
ap.add_argument("--seed", type=int, default=42)
|
| 30 |
ap.add_argument("--dtype", default="bf16", choices=["bf16", "fp16"])
|
|
|
|
|
|
|
|
|
|
| 31 |
args = ap.parse_args()
|
| 32 |
|
| 33 |
paths = ([os.path.join(args.images, n) for n in sorted(os.listdir(args.images))
|
|
@@ -36,6 +39,18 @@ def main():
|
|
| 36 |
os.makedirs(args.out, exist_ok=True)
|
| 37 |
|
| 38 |
pipe = load_pipeline(args.weights, dtype=args.dtype, optimize=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
log = []
|
| 40 |
for i, path in enumerate(paths):
|
| 41 |
name = os.path.splitext(os.path.basename(path))[0]
|
|
|
|
| 28 |
ap.add_argument("--mode", choices=["synthetic", "real"], default="synthetic")
|
| 29 |
ap.add_argument("--seed", type=int, default=42)
|
| 30 |
ap.add_argument("--dtype", default="bf16", choices=["bf16", "fp16"])
|
| 31 |
+
ap.add_argument("--batch-cfg", action="store_true", default=False,
|
| 32 |
+
help="run guided and unguided passes as one batch (same math; measured no gain here)")
|
| 33 |
+
ap.add_argument("--no-batch-cfg", dest="batch_cfg", action="store_false")
|
| 34 |
args = ap.parse_args()
|
| 35 |
|
| 36 |
paths = ([os.path.join(args.images, n) for n in sorted(os.listdir(args.images))
|
|
|
|
| 39 |
os.makedirs(args.out, exist_ok=True)
|
| 40 |
|
| 41 |
pipe = load_pipeline(args.weights, dtype=args.dtype, optimize=False)
|
| 42 |
+
if args.batch_cfg:
|
| 43 |
+
from contextlib import contextmanager
|
| 44 |
+
|
| 45 |
+
@contextmanager
|
| 46 |
+
def _no_settings(self):
|
| 47 |
+
yield
|
| 48 |
+
|
| 49 |
+
# batch_cfg batches the guided and unguided passes; identical math, ~1.5x on the denoiser.
|
| 50 |
+
# We skip the rest of upstream's optimize bundle (cuDNN attention / TF32), which changes
|
| 51 |
+
# numerics and hangs on some sm_120 drivers.
|
| 52 |
+
pipe.optimize = True
|
| 53 |
+
pipe._sampling_settings = _no_settings.__get__(pipe, type(pipe))
|
| 54 |
log = []
|
| 55 |
for i, path in enumerate(paths):
|
| 56 |
name = os.path.splitext(os.path.basename(path))[0]
|
scripts/run_image.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Upload any image to ComfyUI, run a workflow that ends in Idol, print the resulting .ply path.
|
| 2 |
+
|
| 3 |
+
Usage:
|
| 4 |
+
python run_image.py --image photo.jpg --workflow workflows/image_to_3d_api.json
|
| 5 |
+
python run_image.py --image photo.jpg --workflow workflows/image_to_transparent_3d_api.json \
|
| 6 |
+
--prompt "This is an RGBA format image with transparency. ..."
|
| 7 |
+
"""
|
| 8 |
+
import argparse
|
| 9 |
+
import json
|
| 10 |
+
import os
|
| 11 |
+
import time
|
| 12 |
+
import urllib.parse
|
| 13 |
+
import urllib.request
|
| 14 |
+
import uuid
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def upload_image(server, path):
|
| 18 |
+
boundary = uuid.uuid4().hex
|
| 19 |
+
name = os.path.basename(path)
|
| 20 |
+
with open(path, "rb") as f:
|
| 21 |
+
data = f.read()
|
| 22 |
+
body = b"".join([
|
| 23 |
+
f"--{boundary}\r\nContent-Disposition: form-data; name=\"image\"; filename=\"{name}\"\r\n"
|
| 24 |
+
f"Content-Type: application/octet-stream\r\n\r\n".encode(),
|
| 25 |
+
data, b"\r\n",
|
| 26 |
+
f"--{boundary}\r\nContent-Disposition: form-data; name=\"overwrite\"\r\n\r\ntrue\r\n".encode(),
|
| 27 |
+
f"--{boundary}--\r\n".encode(),
|
| 28 |
+
])
|
| 29 |
+
req = urllib.request.Request(
|
| 30 |
+
server + "/upload/image", data=body,
|
| 31 |
+
headers={"Content-Type": f"multipart/form-data; boundary={boundary}"})
|
| 32 |
+
with urllib.request.urlopen(req, timeout=120) as r:
|
| 33 |
+
return json.loads(r.read())
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def post_json(server, path, payload):
|
| 37 |
+
req = urllib.request.Request(server + path, data=json.dumps(payload).encode(),
|
| 38 |
+
headers={"Content-Type": "application/json"})
|
| 39 |
+
with urllib.request.urlopen(req, timeout=60) as r:
|
| 40 |
+
return json.loads(r.read())
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def get_json(server, path):
|
| 44 |
+
with urllib.request.urlopen(server + path, timeout=60) as r:
|
| 45 |
+
return json.loads(r.read())
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def wait(server, prompt_id, timeout):
|
| 49 |
+
t0 = time.time()
|
| 50 |
+
while time.time() - t0 < timeout:
|
| 51 |
+
hist = get_json(server, f"/history/{prompt_id}")
|
| 52 |
+
if prompt_id in hist:
|
| 53 |
+
entry = hist[prompt_id]
|
| 54 |
+
status = entry.get("status", {})
|
| 55 |
+
if status.get("status_str") == "error":
|
| 56 |
+
raise RuntimeError(json.dumps(status.get("messages", status))[:1200])
|
| 57 |
+
if status.get("completed") or entry.get("outputs"):
|
| 58 |
+
return entry
|
| 59 |
+
time.sleep(2)
|
| 60 |
+
raise TimeoutError(f"{timeout}s")
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
ap = argparse.ArgumentParser()
|
| 65 |
+
ap.add_argument("--server", default="http://127.0.0.1:8188")
|
| 66 |
+
ap.add_argument("--image", required=True)
|
| 67 |
+
ap.add_argument("--workflow", required=True)
|
| 68 |
+
ap.add_argument("--prompt", default=None, help="override the text prompt (edit workflow)")
|
| 69 |
+
ap.add_argument("--seed", type=int, default=42)
|
| 70 |
+
ap.add_argument("--steps", type=int, default=50)
|
| 71 |
+
ap.add_argument("--gaussians", type=int, default=262144)
|
| 72 |
+
ap.add_argument("--mode", default=None, choices=["synthetic", "real"])
|
| 73 |
+
ap.add_argument("--timeout", type=int, default=3600)
|
| 74 |
+
ap.add_argument("--out", default=None, help="download returned images here")
|
| 75 |
+
args = ap.parse_args()
|
| 76 |
+
|
| 77 |
+
wf = json.load(open(args.workflow, encoding="utf-8"))
|
| 78 |
+
ids = wf.pop("_node_ids")
|
| 79 |
+
|
| 80 |
+
up = upload_image(args.server, args.image)
|
| 81 |
+
img_name = up["name"] if not up.get("subfolder") else f'{up["subfolder"]}/{up["name"]}'
|
| 82 |
+
wf[ids["load"]]["inputs"]["image"] = img_name
|
| 83 |
+
|
| 84 |
+
if "text" in ids:
|
| 85 |
+
wf[ids["text"]]["inputs"]["prompt"] = args.prompt if args.prompt else wf[ids["text"]]["inputs"]["prompt"]
|
| 86 |
+
if "noise" in ids:
|
| 87 |
+
wf[ids["noise"]]["inputs"]["noise_seed"] = args.seed
|
| 88 |
+
if "idol" in ids and ids["idol"] in wf:
|
| 89 |
+
idol = wf[ids["idol"]]["inputs"]
|
| 90 |
+
idol["seed"] = args.seed
|
| 91 |
+
idol["steps"] = args.steps
|
| 92 |
+
idol["num_gaussians"] = args.gaussians
|
| 93 |
+
if args.mode:
|
| 94 |
+
idol["mode"] = args.mode
|
| 95 |
+
|
| 96 |
+
resp = post_json(args.server, "/prompt", {"prompt": wf, "client_id": str(uuid.uuid4())})
|
| 97 |
+
entry = wait(args.server, resp["prompt_id"], args.timeout)
|
| 98 |
+
|
| 99 |
+
for node_id, out in entry.get("outputs", {}).items():
|
| 100 |
+
for line in out.get("text", []):
|
| 101 |
+
print(f"node {node_id}: {line}")
|
| 102 |
+
for img in out.get("images", []):
|
| 103 |
+
print(f"node {node_id}: image {img.get('subfolder','')}/{img['filename']}")
|
| 104 |
+
if args.out:
|
| 105 |
+
os.makedirs(args.out, exist_ok=True)
|
| 106 |
+
q = urllib.parse.urlencode({"filename": img["filename"],
|
| 107 |
+
"subfolder": img.get("subfolder", ""),
|
| 108 |
+
"type": img.get("type", "output")})
|
| 109 |
+
with urllib.request.urlopen(args.server + "/view?" + q, timeout=120) as r:
|
| 110 |
+
data = r.read()
|
| 111 |
+
with open(os.path.join(args.out, img["filename"]), "wb") as f:
|
| 112 |
+
f.write(data)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == "__main__":
|
| 116 |
+
main()
|