Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,8 @@ Abnormal-Behaviour Detector (VideoMAE)
|
|
| 4 |
|
| 5 |
Tabs
|
| 6 |
ββββ
|
| 7 |
-
1.
|
| 8 |
-
2.
|
| 9 |
-
Sensitivity slider now goes from 0.0 β 1.0, default 0.0.
|
| 10 |
"""
|
| 11 |
|
| 12 |
import os, cv2, tempfile, numpy as np, matplotlib
|
|
@@ -26,12 +25,12 @@ processor = AutoImageProcessor.from_pretrained(MODEL_DIR, use_fast=False)
|
|
| 26 |
model.eval()
|
| 27 |
|
| 28 |
# ββ Constants βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 29 |
-
CLIP = 16 # frames per
|
| 30 |
-
STRIDE = 1 # evaluate
|
| 31 |
-
STREAM_FPS = 15 #
|
| 32 |
-
HIST_LEN = 90 #
|
| 33 |
|
| 34 |
-
# ββ
|
| 35 |
def sliding_window(path, clip_len=CLIP, stride=STRIDE):
|
| 36 |
cap, frames = cv2.VideoCapture(path), []
|
| 37 |
fps = cap.get(cv2.CAP_PROP_FPS) or 25
|
|
@@ -44,40 +43,35 @@ def sliding_window(path, clip_len=CLIP, stride=STRIDE):
|
|
| 44 |
|
| 45 |
probs = []
|
| 46 |
for s in range(0, len(frames) - clip_len + 1, stride):
|
| 47 |
-
clip = [cv2.resize(frames[i], (224,
|
| 48 |
-
|
| 49 |
-
inp = processor(clip, return_tensors="pt").to(DEVICE)
|
| 50 |
with torch.no_grad():
|
| 51 |
-
|
| 52 |
-
probs.append(p)
|
| 53 |
return frames, probs, fps
|
| 54 |
|
|
|
|
| 55 |
def make_outputs(frames, probs, fps, thresh):
|
| 56 |
-
# annotate
|
| 57 |
h, w = frames[0].shape[:2]
|
| 58 |
for i,p in enumerate(probs):
|
| 59 |
if p > thresh:
|
| 60 |
for f in range(i, min(i+CLIP, len(frames))):
|
| 61 |
cv2.rectangle(frames[f], (0,0),(w-1,h-1),(255,0,0),6)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
vw = cv2.VideoWriter(vid, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w,h))
|
| 66 |
for f in frames:
|
| 67 |
vw.write(cv2.cvtColor(f, cv2.COLOR_RGB2BGR))
|
| 68 |
vw.release()
|
| 69 |
|
| 70 |
-
# static plot
|
| 71 |
t = np.arange(len(probs)) / fps
|
| 72 |
fig, ax = plt.subplots(figsize=(5,2))
|
| 73 |
colors = ["red" if p>thresh else "blue" for p in probs]
|
| 74 |
ax.bar(t, probs, color=colors, width=1/fps)
|
| 75 |
ax.set_ylim(0,1); ax.set_xlabel("sec"); ax.set_ylabel("fight prob")
|
| 76 |
fig.tight_layout()
|
| 77 |
-
|
| 78 |
-
fig.savefig(
|
| 79 |
|
| 80 |
-
# beep
|
| 81 |
alarm = None
|
| 82 |
if any(p>thresh for p in probs):
|
| 83 |
sr = 16000
|
|
@@ -85,7 +79,8 @@ def make_outputs(frames, probs, fps, thresh):
|
|
| 85 |
alarm = tempfile.NamedTemporaryFile(suffix=".wav",delete=False).name
|
| 86 |
sf.write(alarm, 0.2*np.sin(2*np.pi*440*t_beep), sr)
|
| 87 |
|
| 88 |
-
return
|
|
|
|
| 89 |
|
| 90 |
def run_pipeline(file_path, thresh):
|
| 91 |
if not file_path or not os.path.exists(file_path):
|
|
@@ -93,91 +88,55 @@ def run_pipeline(file_path, thresh):
|
|
| 93 |
frames, probs, fps = sliding_window(file_path)
|
| 94 |
return make_outputs(frames, probs, fps, thresh)
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def plot_history(history, thresh):
|
| 99 |
fig, ax = plt.subplots(figsize=(5,2))
|
| 100 |
-
colors = ["red" if p>thresh else "blue" for p in
|
| 101 |
-
ax.bar(np.arange(len(
|
| 102 |
ax.set_ylim(0,1); ax.set_xlabel("recent clips"); ax.set_ylabel("fight prob")
|
| 103 |
-
fig.tight_layout()
|
| 104 |
-
|
| 105 |
|
| 106 |
def live_handler(frame, state, history, thresh):
|
| 107 |
state.append(frame)
|
| 108 |
if len(state) < CLIP:
|
| 109 |
return state, frame, history, plot_history(history, thresh), 0.0
|
| 110 |
|
| 111 |
-
# infer every frame
|
| 112 |
clip = [cv2.resize(f, (224,224)) for f in list(state)]
|
| 113 |
inp = processor(clip, return_tensors="pt").to(DEVICE)
|
| 114 |
-
with torch.no_grad():
|
| 115 |
-
p = torch.softmax(model(**inp).logits, dim=1)[0,1].item()
|
| 116 |
-
|
| 117 |
history.append(p)
|
| 118 |
-
if len(history) > HIST_LEN:
|
| 119 |
-
history.pop(0)
|
| 120 |
-
|
| 121 |
-
# annotate & overlay
|
| 122 |
-
disp = frame.copy()
|
| 123 |
-
h, w = disp.shape[:2]
|
| 124 |
-
if p > thresh:
|
| 125 |
-
cv2.rectangle(disp, (0,0),(w-1,h-1),(255,0,0),6)
|
| 126 |
-
cv2.putText(disp, f"fight prob: {p:.2f}",
|
| 127 |
-
(10,h-15), cv2.FONT_HERSHEY_SIMPLEX, 0.7,
|
| 128 |
-
(255,255,255), 2, cv2.LINE_AA)
|
| 129 |
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
| 131 |
|
|
|
|
| 132 |
|
| 133 |
-
# ββ
|
| 134 |
with gr.Blocks(title="Abnormal Behaviour Detection") as demo:
|
|
|
|
|
|
|
| 135 |
|
| 136 |
-
# Sensitivity slider now 0.0 β 1.0, default 0.0
|
| 137 |
-
thresh_slider = gr.Slider(
|
| 138 |
-
minimum=0.0, maximum=1.0, step=0.01, value=0.0,
|
| 139 |
-
label="Sensitivity threshold (lower β more sensitive)"
|
| 140 |
-
)
|
| 141 |
-
|
| 142 |
-
# β Tab 1: Video file βββββββββββββββββββββββββββββββββββββββββββββ
|
| 143 |
with gr.Tab("Video file"):
|
| 144 |
-
gr.Markdown(
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
vid_out = gr.Video(label="Annotated video")
|
| 151 |
-
plot_out = gr.Image(label="Fight probability curve")
|
| 152 |
-
beep_out = gr.Audio(label="Alarm")
|
| 153 |
-
|
| 154 |
-
gr.Button("Analyze").click(
|
| 155 |
-
fn=run_pipeline,
|
| 156 |
-
inputs=[file_in, thresh_slider],
|
| 157 |
-
outputs=[vid_out, plot_out, beep_out],
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
# β Tab 2: Live stream ββββββββββββββββββββββββββββββββββββββββββββ
|
| 161 |
-
with gr.Tab("Live stream"):
|
| 162 |
-
gr.Markdown(
|
| 163 |
-
"Live webcam detection. Border & barβplot turn red whenever "
|
| 164 |
-
"`prob > Sensitivity`. See the **Current prob** below in real time."
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
cam_in = gr.Image(sources="webcam", streaming=True,
|
| 168 |
-
type="numpy", label="Webcam")
|
| 169 |
-
cam_out = gr.Image(streaming=True, label="Live annotated")
|
| 170 |
-
plot_live = gr.Plot(label="Fight probability (rolling)")
|
| 171 |
-
num_prob = gr.Number(label="Current prob", precision=2)
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
frame_state = gr.State(value=deque(maxlen=CLIP))
|
| 174 |
prob_state = gr.State(value=[])
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
outputs=[frame_state, cam_out, prob_state, plot_live, num_prob],
|
| 180 |
-
stream_every=1/STREAM_FPS,
|
| 181 |
-
)
|
| 182 |
|
| 183 |
demo.queue().launch()
|
|
|
|
| 4 |
|
| 5 |
Tabs
|
| 6 |
ββββ
|
| 7 |
+
1. Video file β upload / record β annotated video, static plot, beep.
|
| 8 |
+
2. Live stream β webcam feed + rolling barβplot + numeric prob; sensitivity slider defaults to 0.0.
|
|
|
|
| 9 |
"""
|
| 10 |
|
| 11 |
import os, cv2, tempfile, numpy as np, matplotlib
|
|
|
|
| 25 |
model.eval()
|
| 26 |
|
| 27 |
# ββ Constants βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
CLIP = 16 # frames per clip
|
| 29 |
+
STRIDE = 1 # evaluate each frame
|
| 30 |
+
STREAM_FPS = 15 # ~15 updates/sec
|
| 31 |
+
HIST_LEN = 90 # history length for rolling plot
|
| 32 |
|
| 33 |
+
# ββ Video-file pipeline (unchanged) ββββββββββββββββββββββββββββββββββ
|
| 34 |
def sliding_window(path, clip_len=CLIP, stride=STRIDE):
|
| 35 |
cap, frames = cv2.VideoCapture(path), []
|
| 36 |
fps = cap.get(cv2.CAP_PROP_FPS) or 25
|
|
|
|
| 43 |
|
| 44 |
probs = []
|
| 45 |
for s in range(0, len(frames) - clip_len + 1, stride):
|
| 46 |
+
clip = [cv2.resize(frames[i], (224,224)) for i in range(s, s+clip_len)]
|
| 47 |
+
inp = processor(clip, return_tensors="pt").to(DEVICE)
|
|
|
|
| 48 |
with torch.no_grad():
|
| 49 |
+
probs.append(torch.softmax(model(**inp).logits, dim=1)[0,1].item())
|
|
|
|
| 50 |
return frames, probs, fps
|
| 51 |
|
| 52 |
+
|
| 53 |
def make_outputs(frames, probs, fps, thresh):
|
|
|
|
| 54 |
h, w = frames[0].shape[:2]
|
| 55 |
for i,p in enumerate(probs):
|
| 56 |
if p > thresh:
|
| 57 |
for f in range(i, min(i+CLIP, len(frames))):
|
| 58 |
cv2.rectangle(frames[f], (0,0),(w-1,h-1),(255,0,0),6)
|
| 59 |
|
| 60 |
+
vid_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
|
| 61 |
+
vw = cv2.VideoWriter(vid_out, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w,h))
|
|
|
|
| 62 |
for f in frames:
|
| 63 |
vw.write(cv2.cvtColor(f, cv2.COLOR_RGB2BGR))
|
| 64 |
vw.release()
|
| 65 |
|
|
|
|
| 66 |
t = np.arange(len(probs)) / fps
|
| 67 |
fig, ax = plt.subplots(figsize=(5,2))
|
| 68 |
colors = ["red" if p>thresh else "blue" for p in probs]
|
| 69 |
ax.bar(t, probs, color=colors, width=1/fps)
|
| 70 |
ax.set_ylim(0,1); ax.set_xlabel("sec"); ax.set_ylabel("fight prob")
|
| 71 |
fig.tight_layout()
|
| 72 |
+
plot_out = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name
|
| 73 |
+
fig.savefig(plot_out, dpi=120); plt.close(fig)
|
| 74 |
|
|
|
|
| 75 |
alarm = None
|
| 76 |
if any(p>thresh for p in probs):
|
| 77 |
sr = 16000
|
|
|
|
| 79 |
alarm = tempfile.NamedTemporaryFile(suffix=".wav",delete=False).name
|
| 80 |
sf.write(alarm, 0.2*np.sin(2*np.pi*440*t_beep), sr)
|
| 81 |
|
| 82 |
+
return vid_out, plot_out, alarm
|
| 83 |
+
|
| 84 |
|
| 85 |
def run_pipeline(file_path, thresh):
|
| 86 |
if not file_path or not os.path.exists(file_path):
|
|
|
|
| 88 |
frames, probs, fps = sliding_window(file_path)
|
| 89 |
return make_outputs(frames, probs, fps, thresh)
|
| 90 |
|
| 91 |
+
# ββ Live-stream helpers βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 92 |
+
def plot_history(hist, thresh):
|
|
|
|
| 93 |
fig, ax = plt.subplots(figsize=(5,2))
|
| 94 |
+
colors = ["red" if p>thresh else "blue" for p in hist]
|
| 95 |
+
ax.bar(np.arange(len(hist)), hist, color=colors)
|
| 96 |
ax.set_ylim(0,1); ax.set_xlabel("recent clips"); ax.set_ylabel("fight prob")
|
| 97 |
+
fig.tight_layout(); return fig
|
| 98 |
+
|
| 99 |
|
| 100 |
def live_handler(frame, state, history, thresh):
|
| 101 |
state.append(frame)
|
| 102 |
if len(state) < CLIP:
|
| 103 |
return state, frame, history, plot_history(history, thresh), 0.0
|
| 104 |
|
|
|
|
| 105 |
clip = [cv2.resize(f, (224,224)) for f in list(state)]
|
| 106 |
inp = processor(clip, return_tensors="pt").to(DEVICE)
|
| 107 |
+
with torch.no_grad(): p = torch.softmax(model(**inp).logits, dim=1)[0,1].item()
|
|
|
|
|
|
|
| 108 |
history.append(p)
|
| 109 |
+
if len(history) > HIST_LEN: history.pop(0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
+
disp = frame.copy(); h, w = disp.shape[:2]
|
| 112 |
+
if p > thresh: cv2.rectangle(disp, (0,0),(w-1,h-1),(255,0,0),6)
|
| 113 |
+
cv2.putText(disp, f"fight prob: {p:.2f}", (10,h-15), cv2.FONT_HERSHEY_SIMPLEX,
|
| 114 |
+
0.7, (255,255,255),2,cv2.LINE_AA)
|
| 115 |
|
| 116 |
+
return state, disp, history, plot_history(history, thresh), p
|
| 117 |
|
| 118 |
+
# ββ UI setup ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 119 |
with gr.Blocks(title="Abnormal Behaviour Detection") as demo:
|
| 120 |
+
thresh_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.0,
|
| 121 |
+
label="Sensitivity threshold (lower β more sensitive)")
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
with gr.Tab("Video file"):
|
| 124 |
+
gr.Markdown("Upload a video or record one, adjust Sensitivity, then Analyze.")
|
| 125 |
+
file_in = gr.Video(format="mp4", webcam_options=gr.WebcamOptions())
|
| 126 |
+
vid_out = gr.Video(); plot_out = gr.Image(); beep_out = gr.Audio()
|
| 127 |
+
gr.Button("Analyze").click(run_pipeline,
|
| 128 |
+
inputs=[file_in, thresh_slider],
|
| 129 |
+
outputs=[vid_out, plot_out, beep_out])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
with gr.Tab("Live stream"):
|
| 132 |
+
gr.Markdown("Live webcam detection. Border+plot turn red when p>threshold.")
|
| 133 |
+
cam_in = gr.Image(sources="webcam", streaming=True, type="numpy")
|
| 134 |
+
cam_out = gr.Image(streaming=True); plot_live = gr.Plot(); num_p = gr.Number()
|
| 135 |
frame_state = gr.State(value=deque(maxlen=CLIP))
|
| 136 |
prob_state = gr.State(value=[])
|
| 137 |
+
cam_in.stream(live_handler,
|
| 138 |
+
inputs=[cam_in, frame_state, prob_state, thresh_slider],
|
| 139 |
+
outputs=[frame_state, cam_out, prob_state, plot_live, num_p],
|
| 140 |
+
stream_every=1/STREAM_FPS)
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
demo.queue().launch()
|