#!/usr/bin/env python3 """依頼312(paths272.py の写し。中身は同じ。依頼311 の確かめの回から作る)/依頼272:清掃の撮る回のカメラの道(-c272CamPath の CSV)を、依頼269 の同じ条件の回(main269_r2_split_s0・main269_r2_shared_s0。 撮る回は記録が同じになる)の robot_track・calls・snags・faceoffs・stalls から作る。作りは病院の paths141.py・paths241.py・win243.py と同じ。 night(1晩の早回し):0.05 秒から、ふだん 1.2 秒ごと(25 コマ/秒で施設の時刻の30倍)で引き。 引き=左30°・75°見下ろし・縦の画角 40°。範囲は 72×36 m(病院の「西半分」と同じ考え方:フロアの一部)の決まった9か所(ANCHORS)から選ぶ。 選び方:窓の台(下)が入る所のうち、動いている台(充電・待機(終わり)でない台)がいちばん多く入る所。同じなら窓の台が真ん中に近い所。 今の所に窓の台が 4 m 内側に入っている間は変えない。変えるときは 25 コマ(動画 1 秒)でなめらかに動かす 全長(フロア全体 200×90 m が入る引き):ごくたまに= 0:00・3:00 と、最後の 90 秒(それぞれ 75 コマ=動画 3 秒。前後 25 コマで切り替え) 寄り=問題が起きている間だけ(0.2 秒ごと=施設の時刻の5倍):左30°・75°見下ろし・距離 27.9 m(病院の寄りと同じ)。注視点=問題の台の位置を σ1.5 秒でならした点 (向かい合って止まった・立ち往生で相手の台が 8 m 以内なら2台の真ん中)。最初と最後の 25 コマで引き⇄寄り(smoothstep。距離は対数・注視点は線で混ぜる) 問題(区間):引っかかった(snags:t_s 〜 t_s+resolve_s)・人を呼んだ(calls:t_call_s 〜 t_done_s。水・引っかかり・立ち往生)・ 向かい合って止まった(faceoffs:t_s 〜 t_s+2 秒)・30秒の立ち往生(stalls:t_start_s 〜 t_start_s+30 秒)。前 3 秒・後 3 秒を足し、20 秒より近い区間はつなぐ win(拡大の窓・病院の依頼243):night の 30倍のコマ(全長を含む)と同じ時刻に、注視点=窓の台の位置(robot_track を線でつないだ点)・距離 6/tan20°(横 12 m)・744×744 窓の台=そのコマの前後 120 秒の中に問題があればその問題の台(いちばん近い問題)、なければ1号機 静止画:stills.csv(名前・時刻・カメラ・注視点・距離・何を撮ったか) python3 paths272.py <依頼269 の回のフォルダ> <出力の頭(…/camspath272_split)> [--stills split|shared] """ import csv, json, math, os, sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from camfit139 import fit DT = 0.05 FOV, W, H = 40, 2720, 2160 NEAR_DIST = 27.9 SIGMA = 1.5 TRANS = 25 WIDE_STEP, NEAR_STEP = 1.2, 0.2 PAD_S, JOIN_S = 3.0, 20.0 WIN_W_M = 12.0 WIN_DIST = WIN_W_M / 2.0 / math.tan(math.radians(FOV / 2.0)) WIN_PX = 744 WIN_NEAR_S = 120.0 HOLD_MARGIN = 4.0 ALL_AT = [7200.0, 18000.0] # 0:00・3:00 ALL_N = 75 RECT_W, RECT_D = 72.0, 36.0 XC = [36.0, 100.0, 164.0] ZC = [18.0, 45.0, 72.0] IDLE = ("充電", "待機(終わり)") def g(t): return round(round(t / DT) * DT, 2) def smooth(a): a = max(0.0, min(1.0, a)) return a * a * (3 - 2 * a) def anchors(): out = [] for zc in ZC: for xc in XC: x0, x1, z0, z1 = xc - RECT_W / 2, xc + RECT_W / 2, zc - RECT_D / 2, zc + RECT_D / 2 tx, tz, d = fit(30.0, 75.0, x0, x1, z0, z1) out.append(dict(name="x%d_z%d" % (xc, zc), rect=(x0, x1, z0, z1), tx=tx, tz=tz, dist=d)) return out def all_view(): tx, tz, d = fit(30.0, 75.0, -0.5, 200.5, -0.5, 90.5) return dict(name="全長", tx=tx, tz=tz, dist=d) class Track: def __init__(self, run): self.T, self.P, self.S = {}, {}, {} for r in csv.DictReader(open(os.path.join(run, "robot_track.csv"), encoding="utf-8")): k = int(r["robot"]) self.T.setdefault(k, []).append(float(r["t_s"])) self.P.setdefault(k, []).append((float(r["x"]), float(r["z"]))) self.S.setdefault(k, []).append(r["state"]) self.ids = sorted(self.T) self.end = max(self.T[k][-1] for k in self.ids) def _i(self, k, t): T = self.T[k] if t <= T[0]: return 0, 0.0 if t >= T[-1]: return len(T) - 2, 1.0 i = int((t - T[0]) / (T[1] - T[0])) i = max(0, min(len(T) - 2, i)) while T[i + 1] < t: i += 1 while T[i] > t: i -= 1 return i, (t - T[i]) / (T[i + 1] - T[i]) def raw(self, k, t): i, a = self._i(k, t) (x0, z0), (x1, z1) = self.P[k][i], self.P[k][i + 1] return x0 + a * (x1 - x0), z0 + a * (z1 - z0) def state(self, k, t): i, a = self._i(k, t) return self.S[k][i + 1 if a > 0.5 else i] def at(self, k, t): sx = sz = sw = 0.0 q = -3.0 * SIGMA while q <= 3.0 * SIGMA + 1e-9: w = math.exp(-0.5 * (q / SIGMA) ** 2) x, z = self.raw(k, t + q) sx += w * x; sz += w * z; sw += w q += 0.25 return sx / sw, sz / sw def problems(run): """問題の区間(始め・終わり・台・相手の台・何が起きたか)""" P = [] for s in csv.DictReader(open(os.path.join(run, "snags.csv"), encoding="utf-8")): a = float(s["t_s"]); b = a + float(s["resolve_s"]) obj = s["label"].split("(")[0] lab = "%s号機が%sに引っかかって止まった" % (s["robot"], s["snag_kind"] if s["snag_kind"].startswith(obj) else obj + "の" + s["snag_kind"]) if s["staff_called"] == "1": lab += "・清掃員を呼んだ" P.append(dict(a=a, b=b, robot=int(s["robot"]), other=None, kind="引っかかり", what=lab)) for c in csv.DictReader(open(os.path.join(run, "calls.csv"), encoding="utf-8")): a, b = float(c["t_call_s"]), float(c["t_done_s"]) if c["kind"] == "水": lab = "%s号機が水の入れ替えで清掃員を呼んだ(控室の前)" % c["robot"] elif c["kind"] == "引っかかり": continue # snags と同じ区間(呼んだことは snags の名前に入れた) else: lab = "%s号機が%s(%s)" % (c["robot"], "30秒の立ち往生で清掃員を呼んだ", c["outcome"]) other = int(c["partner"].replace("ロボット", "").replace("号機", "")) if c["partner"].startswith("ロボット") else None P.append(dict(a=a, b=b, robot=int(c["robot"]), other=other, kind=c["kind"], what=lab)) for f in csv.DictReader(open(os.path.join(run, "faceoffs.csv"), encoding="utf-8")): a = float(f["t_s"]) P.append(dict(a=a, b=a + 2.0, robot=int(f["robot_a"]), other=int(f["robot_b"]), kind="向かい合い", what="%s号機と%s号機が向かい合って止まった" % (f["robot_a"], f["robot_b"]))) for s in csv.DictReader(open(os.path.join(run, "stalls.csv"), encoding="utf-8")): a = float(s["t_start_s"]) other = int(s["partner"].replace("ロボット", "").replace("号機", "")) if s["partner"].startswith("ロボット") else None P.append(dict(a=a, b=a + 30.0, robot=int(s["robot"]), other=other, kind="立ち往生", what="%s号機が止まったまま30秒(立ち往生)" % s["robot"])) P.sort(key=lambda p: (p["a"], p["b"])) return P def windows(P, end): """寄りの区間(前後に PAD_S を足し、JOIN_S より近いものはつなぐ)。区間ごとに中の問題の一覧""" wins = [] for p in P: a, b = max(0.05, p["a"] - PAD_S), min(end, p["b"] + PAD_S) if wins and a - wins[-1]["b"] < JOIN_S: wins[-1]["b"] = max(wins[-1]["b"], b); wins[-1]["ps"].append(p) else: wins.append(dict(a=a, b=b, ps=[p])) return wins def focus(tr, w, t): """寄りの注視点:t に起きている問題(なければ区間の最初の問題)の台。相手の台が 8 m 以内なら2台の真ん中""" cur = [p for p in w["ps"] if p["a"] - PAD_S <= t <= p["b"] + PAD_S] or [w["ps"][0]] p = cur[-1] x, z = tr.at(p["robot"], t) if p["other"] is not None and p["other"] in tr.ids: x2, z2 = tr.at(p["other"], t) if math.hypot(x2 - x, z2 - z) < 8.0: x, z = (x + x2) / 2, (z + z2) / 2 return x, z, p def main(): run, head = sys.argv[1], sys.argv[2] tr = Track(run) P = problems(run) end = tr.end wins = windows(P, end) AN = anchors() AV = all_view() def win_robot(t): best = None for p in P: d = 0.0 if p["a"] <= t <= p["b"] else min(abs(t - p["a"]), abs(t - p["b"])) if d <= WIN_NEAR_S and (best is None or d < best[0]): best = (d, p["robot"]) return best[1] if best else 1 def inside(an, x, z, m=0.0): x0, x1, z0, z1 = an["rect"] return x0 + m <= x <= x1 - m and z0 + m <= z <= z1 - m def pick(t, cur): k = win_robot(t) x, z = tr.raw(k, t) if cur is not None and inside(cur, x, z, HOLD_MARGIN): return cur act = [j for j in tr.ids if tr.state(j, t) not in IDLE] cand = [an for an in AN if inside(an, x, z)] if not cand: cand = [min(AN, key=lambda an: math.hypot(an["tx"] - x, an["tz"] - z))] def key(an): n = sum(1 for j in act if inside(an, *tr.raw(j, t))) x0, x1, z0, z1 = an["rect"] c = math.hypot((x - (x0 + x1) / 2) / RECT_W, (z - (z0 + z1) / 2) / RECT_D) return (-n, c) return min(cand, key=key) rows = [] # (t, tx, tz, dist, speed, near_a, view, robot, what) cur = None pan = None # (from, to, k) t, wi = 0.05, 0 all_q = list(ALL_AT) + [max(0.05, end - ALL_N * WIDE_STEP - 2 * TRANS * WIDE_STEP)] all_q = [g(a) for a in all_q] allrun = None # [残りコマ, 合計] def blend(a, A, B): tx = A["tx"] + a * (B["tx"] - A["tx"]); tz = A["tz"] + a * (B["tz"] - A["tz"]) d = math.exp(math.log(A["dist"]) + a * (math.log(B["dist"]) - math.log(A["dist"]))) return tx, tz, d while t <= end + 1e-6: if wi < len(wins) and t >= g(wins[wi]["a"]) - 1e-6: w = wins[wi] s0, e0 = g(max(t, w["a"])), w["b"] n = int(math.ceil((g(e0) - s0) / NEAR_STEP - 1e-6)) ts = [g(s0 + NEAR_STEP * k) for k in range(n + 1)] # 寄りの始めは、いま映している所(前のコマのカメラ)から寄る w_in = dict(tx=rows[-1][1], tz=rows[-1][2], dist=rows[-1][3]) if rows else pick(s0, cur) w_out = pick(ts[-1], None) fs = [focus(tr, w, tt) for tt in ts] # 問題が変わって注視点が 3 m より飛ぶときは、25 コマで前の問題の点から次の問題の点へなめらかに移す mv = None for k in range(1, len(ts)): if mv is None and fs[k][2] is not fs[k - 1][2] and math.hypot(fs[k][0] - fs[k - 1][0], fs[k][1] - fs[k - 1][1]) > 3.0: mv = [fs[k - 1][2], 0] if mv is not None: mv[1] += 1 po = mv[0] ox, oz = tr.at(po["robot"], ts[k]) b = smooth(mv[1] / TRANS) fs[k] = (ox + b * (fs[k][0] - ox), oz + b * (fs[k][1] - oz), fs[k][2]) if mv[1] >= TRANS: mv = None for k, tt in enumerate(ts): a = min(smooth((k + 1) / TRANS), smooth((len(ts) - k) / TRANS)) fx, fz, p = fs[k] wide = w_in if k < len(ts) / 2 else w_out # 寄りの終わりは、そのとき選ぶ引きの所へ引く tx, tz, d = blend(a, wide, dict(tx=fx, tz=fz, dist=NEAR_DIST)) rows.append((tt, tx, tz, d, "5倍", a, "寄り", p["robot"], p["what"])) cur = w_out; pan = None t = g(ts[-1] + WIDE_STEP) wi += 1 while all_q and all_q[0] < t: all_q.pop(0) continue # 全長(ごくたまに) if allrun is None and all_q and t >= all_q[0] - 1e-6: all_q.pop(0) allrun = [ALL_N + 2 * TRANS, ALL_N + 2 * TRANS] k = win_robot(t) nxt = pick(t, cur) if cur is None: cur = nxt if nxt is not cur and pan is None: pan = [cur, nxt, 0] if pan is not None: pan[2] += 1 a = smooth(pan[2] / TRANS) base = blend(a, pan[0], pan[1]) view = pan[1]["name"] if pan[2] >= TRANS: cur = pan[1]; pan = None else: base = (cur["tx"], cur["tz"], cur["dist"]); view = cur["name"] if allrun is not None: i = allrun[1] - allrun[0] a = min(smooth((i + 1) / TRANS), smooth(allrun[0] / TRANS)) tx, tz, d = blend(a, dict(tx=base[0], tz=base[1], dist=base[2]), AV) allrun[0] -= 1 if allrun[0] <= 0: allrun = None rows.append((t, tx, tz, d, "30倍", 0.0, "全長" if a > 0.5 else view, k, "")) else: rows.append((t, base[0], base[1], base[2], "30倍", 0.0, view, k, "")) nt = g(t + WIDE_STEP) if wi < len(wins) and nt > g(wins[wi]["a"]): nt = max(g(wins[wi]["a"]), g(t + DT)) t = nt # 窓(30倍のコマ) wrows = [] for r in rows: if r[4] != "30倍": continue x, z = tr.raw(r[7], r[0]) wrows.append((r[0], x, z)) with open(head + ".csv", "w", newline="") as f: w = csv.writer(f) w.writerow(["cam", "t_s", "tx", "tz", "dist"]) for r in rows: w.writerow(["night", "%.2f" % r[0], "%.4f" % r[1], "%.4f" % r[2], "%.4f" % r[3]]) for r in wrows: w.writerow(["win", "%.2f" % r[0], "%.4f" % r[1], "%.4f" % r[2], "%.4f" % WIN_DIST]) with open(head + "_meta.csv", "w", newline="") as f: w = csv.writer(f) w.writerow(["cam", "t_s", "speed", "near_a", "view", "robot", "what"]) for r in rows: w.writerow(["night", "%.2f" % r[0], r[4], "%.4f" % r[5], r[6], r[7], r[8]]) with open(head + "_cams.txt", "w") as f: f.write("night,30,75,%.4f,%.4f,%.4f,%d,%d,%d\n" % (AN[4]["tx"], AN[4]["tz"], AN[4]["dist"], FOV, W, H)) f.write("win,30,75,%.4f,%.4f,%.4f,%d,%d,%d\n" % (wrows[0][1], wrows[0][2], WIN_DIST, FOV, WIN_PX, WIN_PX)) with open(head + "_wins.csv", "w", newline="") as f: w = csv.writer(f) w.writerow(["no", "a_s", "b_s", "frames", "video_s", "robot", "what"]) for i, x in enumerate(wins): nfr = sum(1 for r in rows if r[4] == "5倍" and x["a"] - 1e-6 <= r[0] <= x["b"] + NEAR_STEP) w.writerow([i + 1, "%.2f" % x["a"], "%.2f" % x["b"], nfr, "%.1f" % (nfr / 25.0), x["ps"][0]["robot"], " / ".join(dict.fromkeys(p["what"] for p in x["ps"]))]) n5 = sum(1 for r in rows if r[4] == "5倍") nall = sum(1 for r in rows if r[6] == "全長") print("night %d コマ(%.1f 秒=%.1f 分。5倍 %d・30倍 %d(うち全長 %d))〜%.2f/寄り %d 区間・問題 %d・win %d" % (len(rows), len(rows) / 25, len(rows) / 25 / 60, n5, len(rows) - n5, nall, rows[-1][0], len(wins), len(P), len(wrows))) views = {} for r in rows: if r[4] == "30倍": views[r[6]] = views.get(r[6], 0) + 1 print("引きの所ごとのコマ:", views) if __name__ == "__main__": main()