Stage 3 (Video Output Version)
#10
by
nishanth-saka
- opened
app.py
CHANGED
|
@@ -1,165 +1,159 @@
|
|
| 1 |
# ============================================================
|
| 2 |
-
# ๐ฆ Stage 3 โ Wrong-Direction Detection
|
| 3 |
-
# (Angle + Temporal + Zone-Aware + Entry Gating + Confidence)
|
| 4 |
# ============================================================
|
| 5 |
|
| 6 |
import gradio as gr
|
| 7 |
import numpy as np, cv2, json, os, tempfile
|
| 8 |
from collections import defaultdict
|
|
|
|
| 9 |
|
| 10 |
# ------------------------------------------------------------
|
| 11 |
# โ๏ธ CONFIG
|
| 12 |
# ------------------------------------------------------------
|
| 13 |
-
ANGLE_THRESHOLD = 60 #
|
| 14 |
-
SMOOTH_FRAMES
|
| 15 |
-
ENTRY_ZONE_RATIO = 0.15 # top 15%
|
| 16 |
CONF_MIN, CONF_MAX = 0, 100
|
|
|
|
| 17 |
|
| 18 |
# ------------------------------------------------------------
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
# ------------------------------------------------------------
|
| 21 |
def load_flow_model(flow_model_json):
|
| 22 |
-
model =
|
| 23 |
centers = [np.array(z) for z in model["zone_flow_centers"]]
|
| 24 |
return centers
|
| 25 |
|
| 26 |
# ------------------------------------------------------------
|
| 27 |
-
#
|
| 28 |
# ------------------------------------------------------------
|
| 29 |
def extract_trajectories(json_file):
|
| 30 |
-
data =
|
| 31 |
tracks = {tid: np.array(pts) for tid, pts in data.items() if len(pts) > 2}
|
| 32 |
return tracks
|
| 33 |
|
| 34 |
# ------------------------------------------------------------
|
| 35 |
-
#
|
| 36 |
# ------------------------------------------------------------
|
| 37 |
def smooth_direction(pts, window=SMOOTH_FRAMES):
|
| 38 |
-
if len(pts) < 2:
|
| 39 |
-
return np.array([0, 0])
|
| 40 |
diffs = np.diff(pts[-window:], axis=0)
|
| 41 |
v = np.mean(diffs, axis=0)
|
| 42 |
-
|
| 43 |
-
return v / (n + 1e-6)
|
| 44 |
|
| 45 |
-
# ------------------------------------------------------------
|
| 46 |
-
# 4๏ธโฃ Compute angular difference (deg)
|
| 47 |
-
# ------------------------------------------------------------
|
| 48 |
def angle_between(v1, v2):
|
| 49 |
-
v1 = v1 / (np.linalg.norm(v1)
|
| 50 |
-
v2 = v2 / (np.linalg.norm(v2)
|
| 51 |
-
cosang = np.clip(np.dot(v1,
|
| 52 |
return np.degrees(np.arccos(cosang))
|
| 53 |
|
| 54 |
-
# ------------------------------------------------------------
|
| 55 |
-
# 5๏ธโฃ Determine zone index for y
|
| 56 |
-
# ------------------------------------------------------------
|
| 57 |
-
def get_zone_idx(y, frame_h, n_zones):
|
| 58 |
-
zone_height = frame_h / n_zones
|
| 59 |
-
return int(np.clip(y // zone_height, 0, n_zones - 1))
|
| 60 |
-
|
| 61 |
-
# ------------------------------------------------------------
|
| 62 |
-
# 6๏ธโฃ Confidence mapping
|
| 63 |
-
# ------------------------------------------------------------
|
| 64 |
def angle_to_confidence(angle):
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
return CONF_MIN
|
| 74 |
-
# linear mapping: smaller angle = higher confidence
|
| 75 |
-
conf = max(CONF_MIN, CONF_MAX - (angle / 180) * 100)
|
| 76 |
-
return round(conf, 1)
|
| 77 |
|
| 78 |
# ------------------------------------------------------------
|
| 79 |
-
#
|
| 80 |
# ------------------------------------------------------------
|
| 81 |
-
def
|
| 82 |
tracks = extract_trajectories(traj_json)
|
| 83 |
centers_by_zone = load_flow_model(flow_model_json)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
else:
|
| 88 |
-
bg = np.ones((600,
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
for
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
cv2.
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
results.append({
|
| 128 |
-
"id": tid,
|
| 129 |
-
"zone": int(zone_idx),
|
| 130 |
-
"angle": round(best_angle, 1),
|
| 131 |
-
"confidence": conf,
|
| 132 |
-
"label": label
|
| 133 |
-
})
|
| 134 |
-
|
| 135 |
-
combined = cv2.addWeighted(bg, 0.6, overlay, 0.4, 0)
|
| 136 |
-
out_path = tempfile.NamedTemporaryFile(suffix=".jpg", delete=False).name
|
| 137 |
-
cv2.imwrite(out_path, combined)
|
| 138 |
-
return out_path, results
|
| 139 |
|
| 140 |
# ------------------------------------------------------------
|
| 141 |
# ๐ฅ๏ธ Gradio Interface
|
| 142 |
# ------------------------------------------------------------
|
| 143 |
description_text = """
|
| 144 |
-
### ๐ฆ Wrong-Direction Detection (
|
| 145 |
-
|
| 146 |
-
-
|
| 147 |
-
-
|
| 148 |
-
-
|
| 149 |
"""
|
| 150 |
|
| 151 |
demo = gr.Interface(
|
| 152 |
-
fn=
|
| 153 |
inputs=[
|
| 154 |
gr.File(label="Trajectories JSON (Stage 1)"),
|
| 155 |
gr.File(label="Flow Model JSON (Stage 2)"),
|
| 156 |
-
gr.File(label="Optional background frame (.jpg)")
|
| 157 |
-
],
|
| 158 |
-
outputs=[
|
| 159 |
-
gr.Image(label="Annotated Output"),
|
| 160 |
-
gr.JSON(label="Per-Vehicle Results")
|
| 161 |
],
|
| 162 |
-
|
|
|
|
| 163 |
description=description_text
|
| 164 |
)
|
| 165 |
|
|
|
|
| 1 |
# ============================================================
|
| 2 |
+
# ๐ฆ Stage 3 โ Wrong-Direction Detection (Video Output Version)
|
|
|
|
| 3 |
# ============================================================
|
| 4 |
|
| 5 |
import gradio as gr
|
| 6 |
import numpy as np, cv2, json, os, tempfile
|
| 7 |
from collections import defaultdict
|
| 8 |
+
import math
|
| 9 |
|
| 10 |
# ------------------------------------------------------------
|
| 11 |
# โ๏ธ CONFIG
|
| 12 |
# ------------------------------------------------------------
|
| 13 |
+
ANGLE_THRESHOLD = 60 # deg โ above = WRONG
|
| 14 |
+
SMOOTH_FRAMES = 5 # temporal smoothing
|
| 15 |
+
ENTRY_ZONE_RATIO = 0.15 # skip top 15 %
|
| 16 |
CONF_MIN, CONF_MAX = 0, 100
|
| 17 |
+
FPS = 25 # output video fps
|
| 18 |
|
| 19 |
# ------------------------------------------------------------
|
| 20 |
+
# ๐ง Helper โ universal loader for Gradio inputs
|
| 21 |
+
# ------------------------------------------------------------
|
| 22 |
+
def load_json_input(file_obj):
|
| 23 |
+
if file_obj is None:
|
| 24 |
+
raise ValueError("No file provided.")
|
| 25 |
+
if isinstance(file_obj, dict) and "name" in file_obj:
|
| 26 |
+
path = file_obj["name"]
|
| 27 |
+
return json.load(open(path))
|
| 28 |
+
elif hasattr(file_obj, "name"):
|
| 29 |
+
return json.load(open(file_obj.name))
|
| 30 |
+
elif isinstance(file_obj, str):
|
| 31 |
+
return json.load(open(file_obj))
|
| 32 |
+
else:
|
| 33 |
+
raise ValueError("Unsupported file input type.")
|
| 34 |
+
|
| 35 |
+
# ------------------------------------------------------------
|
| 36 |
+
# ๐งฉ Load Stage 2 flow model
|
| 37 |
# ------------------------------------------------------------
|
| 38 |
def load_flow_model(flow_model_json):
|
| 39 |
+
model = load_json_input(flow_model_json)
|
| 40 |
centers = [np.array(z) for z in model["zone_flow_centers"]]
|
| 41 |
return centers
|
| 42 |
|
| 43 |
# ------------------------------------------------------------
|
| 44 |
+
# ๐งฉ Extract trajectories (Stage 1)
|
| 45 |
# ------------------------------------------------------------
|
| 46 |
def extract_trajectories(json_file):
|
| 47 |
+
data = load_json_input(json_file)
|
| 48 |
tracks = {tid: np.array(pts) for tid, pts in data.items() if len(pts) > 2}
|
| 49 |
return tracks
|
| 50 |
|
| 51 |
# ------------------------------------------------------------
|
| 52 |
+
# ๐งฎ Direction + Angle + Confidence Helpers
|
| 53 |
# ------------------------------------------------------------
|
| 54 |
def smooth_direction(pts, window=SMOOTH_FRAMES):
|
| 55 |
+
if len(pts) < 2: return np.array([0,0])
|
|
|
|
| 56 |
diffs = np.diff(pts[-window:], axis=0)
|
| 57 |
v = np.mean(diffs, axis=0)
|
| 58 |
+
return v / (np.linalg.norm(v)+1e-6)
|
|
|
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
def angle_between(v1, v2):
|
| 61 |
+
v1 = v1 / (np.linalg.norm(v1)+1e-6)
|
| 62 |
+
v2 = v2 / (np.linalg.norm(v2)+1e-6)
|
| 63 |
+
cosang = np.clip(np.dot(v1,v2), -1,1)
|
| 64 |
return np.degrees(np.arccos(cosang))
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
def angle_to_confidence(angle):
|
| 67 |
+
if angle<0: return CONF_MIN
|
| 68 |
+
if angle>=180: return CONF_MIN
|
| 69 |
+
conf = max(CONF_MIN, CONF_MAX - (angle/180)*100)
|
| 70 |
+
return round(conf,1)
|
| 71 |
+
|
| 72 |
+
def get_zone_idx(y, frame_h, n_zones):
|
| 73 |
+
zone_h = frame_h/n_zones
|
| 74 |
+
return int(np.clip(y//zone_h, 0, n_zones-1))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# ------------------------------------------------------------
|
| 77 |
+
# ๐ฅ Main logic โ annotated video
|
| 78 |
# ------------------------------------------------------------
|
| 79 |
+
def classify_wrong_direction_video(traj_json, flow_model_json, bg_img=None):
|
| 80 |
tracks = extract_trajectories(traj_json)
|
| 81 |
centers_by_zone = load_flow_model(flow_model_json)
|
| 82 |
|
| 83 |
+
# background size
|
| 84 |
+
if bg_img:
|
| 85 |
+
if isinstance(bg_img, dict) and "name" in bg_img:
|
| 86 |
+
bg_path = bg_img["name"]
|
| 87 |
+
elif hasattr(bg_img,"name"):
|
| 88 |
+
bg_path = bg_img.name
|
| 89 |
+
else:
|
| 90 |
+
bg_path = bg_img
|
| 91 |
+
bg = cv2.imread(bg_path)
|
| 92 |
else:
|
| 93 |
+
bg = np.ones((600,900,3),dtype=np.uint8)*40
|
| 94 |
+
if bg is None: bg = np.ones((600,900,3),dtype=np.uint8)*40
|
| 95 |
+
h,w = bg.shape[:2]
|
| 96 |
+
|
| 97 |
+
# infer video length from longest track
|
| 98 |
+
max_len = max(len(p) for p in tracks.values())
|
| 99 |
+
out_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 100 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 101 |
+
writer = cv2.VideoWriter(out_path, fourcc, FPS, (w,h))
|
| 102 |
font = cv2.FONT_HERSHEY_SIMPLEX
|
| 103 |
+
|
| 104 |
+
# render frame-by-frame
|
| 105 |
+
for fi in range(max_len):
|
| 106 |
+
frame = bg.copy()
|
| 107 |
+
for tid, pts in tracks.items():
|
| 108 |
+
if fi >= len(pts): continue
|
| 109 |
+
cur_pt = pts[fi]
|
| 110 |
+
y = cur_pt[1]
|
| 111 |
+
zone_idx = get_zone_idx(y, h, len(centers_by_zone))
|
| 112 |
+
if y < h*ENTRY_ZONE_RATIO: continue
|
| 113 |
+
|
| 114 |
+
# smooth direction using past window
|
| 115 |
+
win = pts[max(0,fi-SMOOTH_FRAMES):fi+1]
|
| 116 |
+
v = smooth_direction(win)
|
| 117 |
+
centers = centers_by_zone[zone_idx]
|
| 118 |
+
angles = [angle_between(v,c) for c in centers]
|
| 119 |
+
best_angle = min(angles)
|
| 120 |
+
conf = angle_to_confidence(best_angle)
|
| 121 |
+
label = "OK" if best_angle < ANGLE_THRESHOLD else "WRONG"
|
| 122 |
+
color = (0,255,0) if label=="OK" else (0,0,255)
|
| 123 |
+
|
| 124 |
+
# draw trajectory so far
|
| 125 |
+
for p1,p2 in zip(pts[:fi], pts[1:fi+1]):
|
| 126 |
+
cv2.line(frame, tuple(p1.astype(int)), tuple(p2.astype(int)), color, 2)
|
| 127 |
+
cv2.circle(frame, tuple(cur_pt.astype(int)), 5, color, -1)
|
| 128 |
+
cv2.putText(frame, f"ID:{tid} {label} ({conf}%)",
|
| 129 |
+
(int(cur_pt[0])+5, int(cur_pt[1])-5),
|
| 130 |
+
font, 0.55, color, 2)
|
| 131 |
+
|
| 132 |
+
writer.write(frame)
|
| 133 |
+
|
| 134 |
+
writer.release()
|
| 135 |
+
return out_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
# ------------------------------------------------------------
|
| 138 |
# ๐ฅ๏ธ Gradio Interface
|
| 139 |
# ------------------------------------------------------------
|
| 140 |
description_text = """
|
| 141 |
+
### ๐ฆ Stage 3 โ Wrong-Direction Detection (Video Output)
|
| 142 |
+
Uses **trajectories (Stage 1)** + **flow model (Stage 2)** to create an annotated MP4:
|
| 143 |
+
- Angle-based + temporal smoothing + zone awareness
|
| 144 |
+
- Entry-zone gating
|
| 145 |
+
- Confidence (%) per vehicle
|
| 146 |
"""
|
| 147 |
|
| 148 |
demo = gr.Interface(
|
| 149 |
+
fn=classify_wrong_direction_video,
|
| 150 |
inputs=[
|
| 151 |
gr.File(label="Trajectories JSON (Stage 1)"),
|
| 152 |
gr.File(label="Flow Model JSON (Stage 2)"),
|
| 153 |
+
gr.File(label="Optional background frame (.jpg/.png)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
],
|
| 155 |
+
outputs=gr.Video(label="Annotated Video Output"),
|
| 156 |
+
title="๐ Stage 3 โ Wrong-Direction Detection (Video Output)",
|
| 157 |
description=description_text
|
| 158 |
)
|
| 159 |
|