Improve seeing people: clothes, hair, glasses and posture
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+861
-63
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,158 @@
|
||||
"""Self-check: questions about a person's looks (glasses, hat, mask, bag, hair, the kind and pattern of clothes, what
|
||||
they do, who is closest, "what about her?") answered at once from the looks (seeing.norm_looks / scene_looks) and
|
||||
the measured colours, EN and KO; "really?" asks for a new look; the VLM's JSON normalised; questions about Lumi left
|
||||
alone. Fake people, no robot, no VLM. python tests/test_seeing_people.py
|
||||
"""
|
||||
import re
|
||||
|
||||
from assistant import seeing
|
||||
from vision import scene
|
||||
|
||||
ME = {"top": "red", "bottom": "black", "top_type": "hoodie", "bottom_type": "jeans", "hair": "long brown",
|
||||
"glasses": True, "hat": "black cap", "mask": False, "bag": "blue backpack", "action": "looking at a phone",
|
||||
"pattern": "striped"}
|
||||
HER = {"top": "blue", "hair": "short black", "glasses": False, "hat": None, "action": "waving"}
|
||||
|
||||
|
||||
def person(bearing, dist, clothes, looks):
|
||||
return {"camera": "head", "box": [0, 0, 1, 1], "distance_m": dist, "bearing_deg": bearing, "position_mm": None,
|
||||
"facing": True, "raised": [], "pointing": None, "gesture": None, "posture": "standing",
|
||||
"clothes": clothes, "hands": {"left": {"object": None, "distance_m": dist},
|
||||
"right": {"object": None, "distance_m": dist}},
|
||||
"seen": 17, "where": scene.where(bearing), "looks": looks}
|
||||
|
||||
|
||||
world = {"people": []}
|
||||
scene._persons = lambda frames=None: world["people"]
|
||||
scene.people = lambda frames=None: [{"distance_m": p["distance_m"], "bearing_deg": p["bearing_deg"], "camera": "head"}
|
||||
for p in sorted(world["people"], key=lambda p: p["distance_m"])]
|
||||
scene.things = lambda frames=None: []
|
||||
scene.ask_vlm = lambda *a, **k: None
|
||||
scene.ask_vlm_person = lambda *a, **k: (_ for _ in ()).throw(AssertionError("the VLM asked in a test"))
|
||||
FRAMES = [{"camera": "head"}]
|
||||
|
||||
|
||||
def setup(me=ME, her=HER, her_nearer=False):
|
||||
world["people"] = [person(0.0, 1.0, {"upper": me.get("top"), "lower": me.get("bottom"), "feet": None},
|
||||
seeing.scene_looks(me))]
|
||||
if her:
|
||||
world["people"].append(person(30.0, 0.8 if her_nearer else 2.0, {"upper": her.get("top"), "lower": None,
|
||||
"feet": None}, seeing.scene_looks(her)))
|
||||
|
||||
|
||||
def ask(text):
|
||||
lang = "ko" if re.search(r"[가-힣]", text) else "en"
|
||||
return seeing.answer(text, lang, FRAMES)
|
||||
|
||||
|
||||
CASES = [ # (question, regex the answer must match; None: not a seeing question)
|
||||
("am I wearing glasses?", r"^Yes, you're wearing glasses"),
|
||||
("do I have sunglasses on?", r"glasses, not sunglasses"),
|
||||
("Lumi, am I wearing a hat?", r"^Yes.*black cap"),
|
||||
("what colour is my cap?", r"black cap"),
|
||||
("do I have a bag?", r"^Yes.*blue backpack"),
|
||||
("am I wearing a mask?", r"^No, I don't see a mask"),
|
||||
("what colour is my hair?", r"hair looks brown"),
|
||||
("is my hair short?", r"^No, your hair looks long"),
|
||||
("is my hair long?", r"^Yes, your hair looks long"),
|
||||
("describe me", r"red hoodie.*black jeans.*long brown hair.*glasses"),
|
||||
("what do I look like?", r"red hoodie"),
|
||||
("who is closest to you?", r"^You are, about 1 metre"),
|
||||
("am I wearing a jacket?", r"^No, you're wearing a red hoodie"),
|
||||
("am I wearing a hoodie?", r"^Yes, you're wearing a red hoodie"),
|
||||
("am I wearing jeans?", r"^Yes, you're wearing black jeans"),
|
||||
("am I wearing a blue hoodie?", r"^No, you're wearing a red hoodie"),
|
||||
("is my shirt striped?", r"hoodie looks striped"),
|
||||
("what am I wearing?", r"red hoodie and black jeans"),
|
||||
("what am I doing?", r"looking at a phone"),
|
||||
("is she wearing glasses?", r"^No, they're not wearing glasses"),
|
||||
("what colour is her hair?", r"^Their hair looks black"),
|
||||
("안경 썼어?", r"^네, 안경을 쓰고"),
|
||||
("안경 쓰셨나요?", r"^네, 안경을"),
|
||||
("모자 쓰고 있어요?", r"검은색 모자를 쓰고"),
|
||||
("가방 메고 있습니까?", r"파란색 백팩을 메고"),
|
||||
("마스크 썼어?", r"^아니요, 마스크는 안 보여요"),
|
||||
("내 머리 무슨 색이야?", r"갈색 머리"),
|
||||
("머리 길어?", r"^네, 머리가 길어"),
|
||||
("제 머리 짧아요?", r"^아니요, 머리가 길어"),
|
||||
("나 어떻게 생겼어?", r"빨간색 후드티.*갈색 머리"),
|
||||
("누가 제일 가까워?", r"손님이 제일 가까워요"),
|
||||
("청바지 입었어?", r"^네, 검은색 청바지를 입고"),
|
||||
("자켓 입고 있어요?", r"^아니요, 빨간색 후드티를 입고"),
|
||||
("내가 뭐 입었어?", r"빨간색 후드티에 검은색 청바지"),
|
||||
("내가 지금 뭐 하고 있어?", r"휴대폰을 보고 계세요"),
|
||||
("그분 머리 무슨 색이에요?", r"검은 머리"),
|
||||
("셔츠에 무늬 있어?", r"줄무늬"),
|
||||
("루미 머리 무슨 색이야?", None),
|
||||
("are you wearing glasses?", None),
|
||||
("what colour is your hair?", None),
|
||||
("머리 돌려", None),
|
||||
("I need a new hat", None),
|
||||
]
|
||||
|
||||
setup()
|
||||
bad = 0
|
||||
for q, want in CASES:
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
got = ask(q)
|
||||
ok = got is None if want is None else bool(got and re.search(want, got))
|
||||
if not ok:
|
||||
bad += 1
|
||||
print(f"FAIL {q!r} -> {got!r} (want {want!r})")
|
||||
|
||||
# follow-ups: "what about her?" / "그 사람은?" ask the same about the other person; "and me?" back
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"^Yes, you're wearing glasses", ask("am I wearing glasses?"))
|
||||
assert re.search(r"^No, they're not wearing glasses", ask("what about her?")), seeing._last
|
||||
assert re.search(r"^Yes, you're wearing glasses", ask("and me?"))
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"갈색 머리", ask("내 머리 무슨 색이야?"))
|
||||
assert re.search(r"^그분은 검은 머리", ask("그 사람은?"))
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"looking at a phone", ask("what am I doing?"))
|
||||
assert re.search(r"^They're standing.*waving", ask("what about her?"))
|
||||
|
||||
# someone else nearer than the speaker
|
||||
setup(her_nearer=True)
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"person on my left in a blue top is closest", ask("who is closest?"))
|
||||
|
||||
# "really?" after a looks answer: a new look (the scene changed: the glasses are off now)
|
||||
setup()
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert ask("am I wearing glasses?").startswith("Yes")
|
||||
setup(me={**ME, "glasses": False})
|
||||
assert re.search(r"I looked again\. No, you're not wearing glasses", ask("are you sure?"))
|
||||
|
||||
# a face not in view: no guess; a scene's hat / bag None means none (tests/blind/r6 format)
|
||||
setup(me={**ME, "glasses": None, "hat": None, "bag": {"kind": "handbag", "colour": "red"}})
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"can't see your face", ask("am I wearing glasses?"))
|
||||
assert re.search(r"^No, I don't see a hat", ask("am I wearing a hat?"))
|
||||
assert re.search(r"red handbag", ask("do I have a bag?"))
|
||||
assert re.search(r"빨간색 핸드백", ask("가방 메고 있어요?"))
|
||||
|
||||
# no looks (an older test scene): plain "can't tell", never a guess; the clothes colours still answered
|
||||
world["people"] = [person(0.0, 1.0, {"upper": "white", "lower": None, "feet": None}, None)]
|
||||
for p in world["people"]:
|
||||
del p["looks"]
|
||||
seeing._last.update(kind=None, asked=None)
|
||||
assert re.search(r"can't see", ask("am I wearing a hat?"))
|
||||
assert re.search(r"can't see your hair", ask("what colour is my hair?"))
|
||||
assert re.search(r"top looks white, but I can't tell if it's a jacket", ask("am I wearing a jacket?"))
|
||||
|
||||
# the VLM's JSON in its several shapes
|
||||
n = seeing.norm_looks({"top": {"type": "T-shirt", "color": "Gray", "pattern": "plain"}, "bottom": "dark blue jeans",
|
||||
"hair": {"colour": "black", "length": "short"}, "glasses": False, "sunglasses": True,
|
||||
"hat": "null", "mask": "none", "bag": {"object": "backpack", "colour": "black"},
|
||||
"left_hand": {"object": "cup", "colour": "yellow"}, "right_hand": None, "posture": "standing",
|
||||
"action": "Waving"})
|
||||
assert n["top"] == {"type": "t-shirt", "colour": "grey", "pattern": "plain"}, n["top"]
|
||||
assert n["bottom"] == {"type": "jeans", "colour": "dark blue", "pattern": None}, n["bottom"]
|
||||
assert n["glasses"] == "sunglasses" and n["hat"] is None and n["mask"] is False # null: not in view; "none": none
|
||||
assert n["bag"] == "black backpack" and n["left_hand"] == "yellow cup" and n["action"] == "waving"
|
||||
assert seeing.norm_looks("not json") is None
|
||||
assert seeing.scene_looks({"id": "A", "top": "red"}) is None # an older scene: no looks fields
|
||||
|
||||
assert not bad, f"{bad} failed"
|
||||
print(f"seeing people ok ({len(CASES)} questions)")
|
||||
+59
-11
@@ -11,6 +11,8 @@
|
||||
ask_vlm(question, lang="en") -> one spoken sentence from gemma4 on the picture of the person, or None (slow: 3-20 s)
|
||||
ask_vlm_hand(evidence, lang="en") -> the same about what the person holds, from the picture around the hands and
|
||||
the measured facts; "UNSURE" when it can't tell (slow: 1-30 s)
|
||||
ask_vlm_person(frames) -> {top, bottom, feet, hair, glasses, hat, mask, bag, left_hand, right_hand, posture,
|
||||
action} of the person in front from one VLM pass on their picture, or None (slow: 2-15 s)
|
||||
|
||||
python vision/scene.py # print all of it for the cameras now
|
||||
python vision/scene.py --save tests/data/x # save the frames now as a test fixture (x_head.jpg/.npz/.json, ...)
|
||||
@@ -117,6 +119,23 @@ VLM_HAND_PROMPT = ("You are the eyes of a friendly service robot. The picture sh
|
||||
"shape if that helps, beginning \"It looks like\" unless it is plain to see. Trust the picture over the "
|
||||
"measurements. If the hand is clearly empty, say so. If you can't tell what it is, answer exactly "
|
||||
"UNSURE.")
|
||||
VLM_PERSON_MODEL = VLM_HAND_MODEL # one look at the whole person: e4b 7-8 s, gemma4:26b 17 s, qwen2.5vl:7b 10 s on the
|
||||
# same picture with the GPU busy (measured 2026-10-23 23:00); e4b is kept loaded anyway
|
||||
VLM_PERSON_WIDTH = 320 # px; the person picture sent (448 was not better, only slower)
|
||||
VLM_PERSON_TIMEOUT_S = 15.0
|
||||
VLM_PERSON_PROMPT = (
|
||||
"You are the eyes of a service robot. Describe the person nearest the camera as JSON with exactly these keys: "
|
||||
'"top": {"type": "t-shirt" / "shirt" / "jacket" / "hoodie" / "sweater" / "coat" / "blouse" / "dress" / "vest", '
|
||||
'"colour": one colour word, "pattern": "plain" / "striped" / "checked" / "printed" / "dotted"}, '
|
||||
'"bottom": {"type": "trousers" / "jeans" / "shorts" / "skirt", "colour": one colour word}, '
|
||||
'"feet": {"type": "sneakers" / "shoes" / "boots" / "sandals" / "slippers", "colour": one colour word}, '
|
||||
'"hair": {"colour": "black" / "brown" / "blond" / "grey" / "white" / "red" / "dyed", "length": "short" / "medium" / '
|
||||
'"long" / "bald"}, "glasses": "none" or "glasses" or "sunglasses", "hat": "none" or what it is with its colour '
|
||||
'("black cap"), "mask": "none" or the mask with its colour, "bag": "none" or the bag with its colour ("brown '
|
||||
'backpack"); for these four use null only when that part of the person is not in the picture, '
|
||||
'"left_hand": null or the thing in their own left hand with its colour, "right_hand": the same for their right '
|
||||
'hand, "posture": "standing" / "sitting" / "crouching", "action": what they are doing in a few words ("looking at '
|
||||
'a phone", "waving", "carrying a jacket"), "action_ko": the same in polite Korean ("휴대폰을 보고 계세요"). Use null for a part that is not visible. Only what is clearly visible.')
|
||||
|
||||
_det_lock = threading.RLock()
|
||||
_det = {"net": None}
|
||||
@@ -668,13 +687,17 @@ def _front(frames):
|
||||
def other_person(frames=None):
|
||||
"""The nearest person in view other than the one in front (another bearing), described as person_in_front, or
|
||||
None: "what is my friend wearing?"."""
|
||||
frames = snapshot() if frames is None else frames
|
||||
p = _other(snapshot() if frames is None else frames)
|
||||
return _public(p) if p else None
|
||||
|
||||
|
||||
def _other(frames):
|
||||
me = _front(frames)
|
||||
if me is None:
|
||||
return None
|
||||
for p in _persons(frames):
|
||||
if abs(p["bearing_deg"] - me["bearing_deg"]) > _same_deg(me["distance_m"]):
|
||||
return _public(p)
|
||||
return p
|
||||
return None
|
||||
|
||||
|
||||
@@ -694,7 +717,7 @@ def object_in_hand(frames=None):
|
||||
if h and h["object"]:
|
||||
o = h["object"]
|
||||
return {"name": o["name"], "colour": o["colour"], "hand": s, "position_mm": o["position_mm"],
|
||||
"distance_m": o["distance_m"], "width_mm": o["width_mm"], "score": o["score"]}
|
||||
"distance_m": o["distance_m"], "width_mm": o["width_mm"], "score": o.get("score")}
|
||||
return None
|
||||
|
||||
|
||||
@@ -749,23 +772,48 @@ def ask_vlm(question, lang="en", frames=None):
|
||||
frames = snapshot() if frames is None else frames
|
||||
if not frames:
|
||||
return None
|
||||
p = _front(frames)
|
||||
return _vlm(_person_img(frames), VLM_PROMPT.format(lang="Korean" if lang == "ko" else "English", q=question))
|
||||
|
||||
|
||||
def _person_img(frames, p=None):
|
||||
"""The picture of person p (default: the one in front) with a margin, or the whole head view when nobody is there."""
|
||||
p = p or _front(frames)
|
||||
if p is not None:
|
||||
f, (x1, y1, x2, y2) = p["_f"], p["box"]
|
||||
mx, my = (x2 - x1) // 6, (y2 - y1) // 10
|
||||
img = f["color"][max(0, y1 - my):y2 + my, max(0, x1 - mx):x2 + mx]
|
||||
else:
|
||||
img = next((f for f in frames if f["camera"] == "head"), frames[0])["color"]
|
||||
return _vlm(img, VLM_PROMPT.format(lang="Korean" if lang == "ko" else "English", q=question))
|
||||
return f["color"][max(0, y1 - my):y2 + my, max(0, x1 - mx):x2 + mx]
|
||||
return next((f for f in frames if f["camera"] == "head"), frames[0])["color"]
|
||||
|
||||
|
||||
def _vlm(img, content, width=VLM_WIDTH, timeout=VLM_TIMEOUT_S, model=VLM_MODEL):
|
||||
"""gemma4's words about a picture, or None on a timeout / error."""
|
||||
def ask_vlm_person(frames=None, other=False, bearing=None):
|
||||
"""What the person in front (other: the nearest one besides them; bearing: the one seen nearest that bearing) looks
|
||||
like, from one VLM pass on their picture (VLM_PERSON_PROMPT's keys; None for what it can't see), or None: nobody
|
||||
there, a timeout or no JSON. Slow: 2-15 s."""
|
||||
frames = snapshot() if frames is None else frames
|
||||
p = (_other if other else _front)(frames) if frames else None
|
||||
if p is not None and bearing is not None:
|
||||
p = min(_persons(frames), key=lambda q: abs(q["bearing_deg"] - bearing))
|
||||
if p is None:
|
||||
return None
|
||||
said = _vlm(_person_img(frames, p), VLM_PERSON_PROMPT, width=VLM_PERSON_WIDTH, timeout=VLM_PERSON_TIMEOUT_S,
|
||||
model=VLM_PERSON_MODEL, fmt="json", tokens=200)
|
||||
try:
|
||||
got = json.loads(said or "")
|
||||
except ValueError:
|
||||
print(f"scene: VLM person look gave no JSON ({said!r})", flush=True)
|
||||
return None
|
||||
return got if isinstance(got, dict) else None
|
||||
|
||||
|
||||
def _vlm(img, content, width=VLM_WIDTH, timeout=VLM_TIMEOUT_S, model=VLM_MODEL, fmt=None, tokens=60):
|
||||
"""gemma4's words about a picture, or None on a timeout / error. fmt "json": the model must answer JSON."""
|
||||
img = cv2.resize(img, (width, max(1, int(img.shape[0] * width / img.shape[1]))))
|
||||
b64 = base64.b64encode(cv2.imencode(".jpg", img, [cv2.IMWRITE_JPEG_QUALITY, 85])[1]).decode()
|
||||
body = {"model": model, "stream": False, "think": False, "keep_alive": -1,
|
||||
"options": {"temperature": 0, "num_predict": 60},
|
||||
"options": {"temperature": 0, "num_predict": tokens},
|
||||
"messages": [{"role": "user", "images": [b64], "content": content}]}
|
||||
if fmt:
|
||||
body["format"] = fmt
|
||||
try:
|
||||
req = urllib.request.Request(VLM_URL, json.dumps(body).encode(), {"Content-Type": "application/json"})
|
||||
with urllib.request.urlopen(req, timeout=timeout) as r:
|
||||
|
||||
Reference in New Issue
Block a user