""" One-shot refinement of temporal_profile + query anchors/texts for residual gaps. Does NOT touch Roboflow COCO. Only scene track JSON + *_queries.json. """ from __future__ import annotations import json from copy import deepcopy from pathlib import Path ROOT = Path(__file__).resolve().parents[1] SCENES = ROOT / "data" / "scenes" def load(p: Path): return json.loads(p.read_text(encoding="utf-8")) def save(p: Path, obj) -> None: p.write_text(json.dumps(obj, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") def set_profile(tracks: list, tid: str, profile: dict) -> None: for t in tracks: if t["track_id"] == tid: t["temporal_profile"] = profile return raise KeyError(tid) def patch_query(queries: list, qid: str, **fields) -> None: for q in queries: if q["query_id"] == qid: q.update(fields) return raise KeyError(qid) def as_queries(raw): if isinstance(raw, dict) and "queries" in raw: return raw, raw["queries"] if isinstance(raw, list): return raw, raw raise TypeError(type(raw)) def main() -> None: # ---------- 1) High-priority pairs ---------- # americano: while darkening vs after fully darkened am = load(SCENES / "americano" / "americano.json") set_profile( am["tracks"], "americano_2", { "static": [[0, 524]], "light_liquid": [[0, 140]], "darkening": [[140, 400]], "fully_darkened": [[400, 524]], }, ) save(SCENES / "americano" / "americano.json", am) raw, qs = as_queries(load(SCENES / "americano" / "americano_queries.json")) patch_query( qs, "americano_q1", expected_temporal_anchor="darkening", temporal_relation="while", text_en="The glass cup while the liquid inside is darkening.", text_zh="内部液体正在变深的玻璃杯。", ) patch_query( qs, "americano_q10", expected_temporal_anchor="fully_darkened", temporal_relation="after", text_en="The glass cup after the liquid inside has fully darkened.", text_zh="杯中液体完全变深之后的玻璃杯。", note="No separate liquid instance; referent is the glass cup track. " "Mask positives use fully_darkened [400,524], distinct from while-darkening.", ) save(SCENES / "americano" / "americano_queries.json", raw) # torchchocolate: solid before melt vs melting tc = load(SCENES / "torchchocolate" / "torchchocolate.json") set_profile( tc["tracks"], "torchchocolate_3", { "solid": [[0, 100]], "melting": [[100, 690]], }, ) save(SCENES / "torchchocolate" / "torchchocolate.json", tc) raw, qs = as_queries(load(SCENES / "torchchocolate" / "torchchocolate_queries.json")) patch_query( qs, "torchchocolate_q7", expected_temporal_anchor="solid", temporal_relation="before", text_en="The solid chocolate before any melting begins.", text_zh="尚未开始融化的固态巧克力。", ) patch_query( qs, "torchchocolate_q1", expected_temporal_anchor="melting", temporal_relation="while", text_en="The chocolate while it is melting under the flame.", text_zh="在火焰下正在融化的巧克力。", ) save(SCENES / "torchchocolate" / "torchchocolate_queries.json", raw) # split_cookie: intact / breaking / broken_pieces sc = load(SCENES / "split_cookie" / "split_cookie.json") set_profile( sc["tracks"], "cookie_3", { "intact": [[0, 220]], "breaking": [[220, 320]], "broken_pieces": [[320, 537]], }, ) save(SCENES / "split_cookie" / "split_cookie.json", sc) raw, qs = as_queries(load(SCENES / "split_cookie" / "split_cookie_queries.json")) patch_query(qs, "split_cookie_q1", expected_temporal_anchor="intact", temporal_relation="before") patch_query( qs, "split_cookie_q6", expected_temporal_anchor="breaking", temporal_relation="while", text_en="The cookie while it is being actively broken.", text_zh="正在被掰断的曲奇饼干。", ) patch_query( qs, "split_cookie_q2", expected_temporal_anchor="broken_pieces", temporal_relation="after", text_en="The cookie after it has been split into pieces.", text_zh="被掰成碎片后的曲奇饼干。", ) save(SCENES / "split_cookie" / "split_cookie_queries.json", raw) # ---------- 2) Mid-priority: shrink continuous-action profiles ---------- # cook_spinach: stirring not the whole clip margins cs = load(SCENES / "cook_spinach" / "cook-spinach.json") set_profile(cs["tracks"], "cook-spinach_8", {"stirring_spinach": [[40, 280]]}) set_profile(cs["tracks"], "cook-spinach_5", {"holding_and_stirring": [[40, 280]]}) set_profile(cs["tracks"], "cook-spinach_2", {"being_tossed": [[40, 280]]}) set_profile(cs["tracks"], "cook-spinach_4", {"operating_tongs": [[40, 280]]}) save(SCENES / "cook_spinach" / "cook-spinach.json", cs) # flame_salmon: torch/hand active after raw phase fs = load(SCENES / "flame_salmon" / "flame_salmon.json") set_profile(fs["tracks"], "flame_salmon_7", {"emitting_flame_and_sweeping": [[50, 300]]}) set_profile(fs["tracks"], "flame_salmon_4", {"holding_torch_and_sweeping": [[50, 300]]}) set_profile(fs["tracks"], "flame_salmon_6", {"operating_torch": [[50, 300]]}) # keep raw / being_seared on pot as-is save(SCENES / "flame_salmon" / "flame_salmon.json", fs) # flame_steak: torch activity after brief start ft = load(SCENES / "flame_steak" / "flame_steak.json") set_profile(ft["tracks"], "flame_steak_8", {"emitting_flame_and_roaming": [[40, 300]]}) set_profile(ft["tracks"], "flame_steak_4", {"holding_torch_and_roaming": [[40, 300]]}) set_profile(ft["tracks"], "flame_steak_2", {"operating_torch": [[40, 300]]}) save(SCENES / "flame_steak" / "flame_steak.json", ft) # cut_roasted_beef: cutting after intact cr = load(SCENES / "cut_roasted_beef" / "cut_roasted_beef.json") set_profile(cr["tracks"], "cut_roasted_beef_8", {"cutting": [[50, 300]]}) set_profile(cr["tracks"], "cut_roasted_beef_6", {"holding_knife_and_cutting": [[50, 300]]}) set_profile(cr["tracks"], "cut_roasted_beef_7", {"holding_tongs_and_pressing": [[50, 300]]}) set_profile(cr["tracks"], "cut_roasted_beef_10", {"pressing": [[50, 300]]}) set_profile(cr["tracks"], "cut_roasted_beef_3", {"handling_steak": [[50, 300]]}) save(SCENES / "cut_roasted_beef" / "cut_roasted_beef.json", cr) # keyboard: typing not literally every frame of the clip kb = load(SCENES / "keyboard" / "keyboard.json") set_profile(kb["tracks"], "keyboard_2", {"typing": [[40, 560]]}) set_profile(kb["tracks"], "keyboard_3", {"typing": [[40, 560]]}) save(SCENES / "keyboard" / "keyboard.json", kb) # ---------- 3) sear_steak wording / idle window ---------- ss = load(SCENES / "sear_steak" / "sear_steak.json") # align idle hand with text frames 0–80 (exclude frame 100) t5 = next(t for t in ss["tracks"] if t["track_id"] == "sear_steak_5") t5["temporal_profile"] = { "holding_tongs_idle": [[0, 80]], "repositioning_with_tongs": [[100, 180]], "lifting_steak_with_tongs": [[180, 299]], } save(SCENES / "sear_steak" / "sear_steak.json", ss) raw, qs = as_queries(load(SCENES / "sear_steak" / "sear_steak_queries.json")) patch_query( qs, "sear_steak_q2", expected_temporal_anchor="lifted_and_upright", text_en=( "The pot with beef steak in the final frames (280–299) when it is held upright by the tongs." ), text_zh="在最后阶段(约280–299帧)被夹子夹起并竖起的装有牛排的锅。", note="COCO category is 'Pot with beef steak' (pot and steak are one instance). " "Positive masks = lifted_and_upright [280,299].", ) patch_query( qs, "sear_steak_q3", expected_temporal_anchor="holding_tongs_idle", text_en=( "The hand on the left side of the image before it makes any contact with the steak " "(frames 0–80)." ), text_zh="在与牛排接触前(0–80帧)、位于图像左侧的手。", note="Left/right means image/viewer coordinates (not anatomical).", ) save(SCENES / "sear_steak" / "sear_steak_queries.json", raw) # ---------- 4) Weak motion wording without anchors ---------- raw, qs = as_queries(load(SCENES / "cut_lemon" / "cut_lemon_queries.json")) patch_query( qs, "cut_lemon_q5", expected_temporal_anchor="cutting", temporal_relation="while", requires_motion_understanding=True, text_en="The hands and the knife that are moving while cutting the lemon.", text_zh="正在切割柠檬、处于运动中的手和刀。", note="Positives use cutting [590,1770] on hand+knife tracks.", ) save(SCENES / "cut_lemon" / "cut_lemon_queries.json", raw) # Ensure cut_lemon hand/knife cutting intervals exist (already do) cl = load(SCENES / "cut_lemon" / "cut_lemon.json") # keep cutting windows; ensure knife has cutting key for multi-track gate save(SCENES / "cut_lemon" / "cut_lemon.json", cl) raw, qs = as_queries(load(SCENES / "keyboard" / "keyboard_queries.json")) patch_query( qs, "keyboard_q2", expected_temporal_anchor="typing", temporal_relation="while", requires_motion_understanding=True, text_en="Both hands that are actively typing.", text_zh="正在键盘上打字的两只手。", ) save(SCENES / "keyboard" / "keyboard_queries.json", raw) print("Refined temporal profiles + query anchors/texts.") if __name__ == "__main__": main()