SAM3_Cloud/prod_server/client_segment_text.py
2026-07-08 20:48:01 +08:00

75 lines
3.6 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""开放词表分割(texts → mask + box)—— 走 prod_server HTTP 服务(/api/v1/segment,texts 分支)。
结构镜像 demos/00_text_grounding.py:给一段/多段开放词表文本,检测并分割图中**所有匹配实例**,
输出每个实例的 mask、包围框、置信度分数。唯一差别——推理不调本地 `proc.set_text_prompt`,
而是 POST 给服务;服务按短语循环 grounding,返回跨短语实例(每条带 `text/box/score`)。
渲染/IO/路径全部复用 demos/_common(与 demos 同源,mask+框+`#编号`+分数同款);HTTP 走 _client_common。
`texts` 是与 `points`/`bboxes` 平级的第三个 prompt 字段(list)。在 sam3 conda env 下运行(同 demos)。
python3 prod_server/client_segment_text.py
python3 prod_server/client_segment_text.py --input assets/inputs/groceries.jpg --texts "a plastic bag" "a bottle" --threshold 0.5
"""
import argparse
import os
import sys
import numpy as np
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "demos")) # demos/_common
sys.path.insert(0, HERE) # _client_common
from _common import load_image, overlay_masks, save_image, in_path, out_path
from _client_common import file_to_data_uri, post, polygons_to_mask
def run(input_path, texts, output_path, threshold=0.5):
img = load_image(input_path)
# 1) 推理走服务:POST /api/v1/segment(texts 分支,吃开放词表短语列表)
payload = {
"image": file_to_data_uri(input_path),
"model": "SAM-3",
"texts": list(texts),
"threshold": threshold,
}
resp = post("/api/v1/segment", payload)
print(f"[texts] status={resp['status']} message={resp['message']!r} texts={texts} threshold={threshold}")
data = resp.get("data") or []
if not data:
print("[texts] ⚠ 服务未返回实例(短语可能未命中 / threshold 过高)")
save_image(img, output_path)
return
# 2) 还原掩码并叠加框+分数(demos 同款 overlay_masks;boxes 用 xyxy)
w, h = img.size
masks, boxes, scores = [], [], []
per_phrase = {}
for item in data:
polys = item.get("polygons") or []
x, y, bw, bh = item["box"] # xywh
masks.append(polygons_to_mask(polys, w, h))
boxes.append([x, y, x + bw, y + bh]) # xywh -> xyxy
scores.append(float(item["score"]))
per_phrase[item["text"]] = per_phrase.get(item["text"], 0) + 1
masks = np.stack(masks) # (N,H,W)
boxes = np.array(boxes, dtype=float) # (N,4) xyxy
scores = np.array(scores, dtype=float) # (N,)
print(f"[texts] 共 {len(data)} 个实例:" + ", ".join(f"{k!r}->{v}" for k, v in per_phrase.items()))
save_image(overlay_masks(img, masks, boxes=boxes, scores=scores), output_path)
print(f"[texts] 结果图: {output_path}")
if __name__ == "__main__":
ap = argparse.ArgumentParser(description="开放词表分割(走 prod_server 服务;镜像 demos 00)")
ap.add_argument("--input", default=in_path("truck.jpg"), help="输入图(默认 demos 同图)")
ap.add_argument("--texts", nargs="+", default=["a truck"], metavar="PHRASE",
help="开放词表短语列表(与 points/bboxes 平级;默认 ['a truck'])")
ap.add_argument("--threshold", type=float, default=0.5, help="置信度阈值(默认 0.5;越低实例越多)")
ap.add_argument("--output", default=out_path("server_00_truck.jpg"), help="结果图输出")
args = ap.parse_args()
run(args.input, args.texts, args.output, args.threshold)