Spaces:
Sleeping
Sleeping
switch to VideoMAE archit11 model
Browse files
app.py
CHANGED
|
@@ -18,15 +18,16 @@ import cv2
|
|
| 18 |
import gradio as gr
|
| 19 |
import requests
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
-
from transformers import
|
| 22 |
|
| 23 |
load_dotenv()
|
| 24 |
|
| 25 |
-
MODEL_ID =
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
DB_PATH = Path("events.db")
|
| 31 |
ALERT_COOLDOWN_SEC = 10
|
| 32 |
|
|
@@ -34,8 +35,23 @@ TELEGRAM_TOKEN = os.environ.get("TELEGRAM_TOKEN")
|
|
| 34 |
TELEGRAM_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")
|
| 35 |
|
| 36 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
-
processor =
|
| 38 |
-
model =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
_last_alert_ts = 0.0
|
| 41 |
_alert_lock = threading.Lock()
|
|
@@ -109,16 +125,11 @@ def should_alert(confidence):
|
|
| 109 |
|
| 110 |
|
| 111 |
def run_inference(frames):
|
| 112 |
-
inputs = processor(
|
| 113 |
-
text=LABELS,
|
| 114 |
-
videos=list(frames),
|
| 115 |
-
return_tensors="pt",
|
| 116 |
-
padding=True,
|
| 117 |
-
).to(device)
|
| 118 |
with torch.no_grad():
|
| 119 |
outputs = model(**inputs)
|
| 120 |
-
probs = outputs.
|
| 121 |
-
return {
|
| 122 |
|
| 123 |
|
| 124 |
def trigger_alert(confidence, source):
|
|
|
|
| 18 |
import gradio as gr
|
| 19 |
import requests
|
| 20 |
from dotenv import load_dotenv
|
| 21 |
+
from transformers import VideoMAEImageProcessor, VideoMAEForVideoClassification
|
| 22 |
|
| 23 |
load_dotenv()
|
| 24 |
|
| 25 |
+
MODEL_ID = os.environ.get(
|
| 26 |
+
"MODEL_ID",
|
| 27 |
+
"archit11/videomae-base-finetuned-fight-nofight-subset2",
|
| 28 |
+
)
|
| 29 |
+
ALERT_THRESHOLD = float(os.environ.get("ALERT_THRESHOLD", "0.60"))
|
| 30 |
+
NUM_FRAMES = 16
|
| 31 |
DB_PATH = Path("events.db")
|
| 32 |
ALERT_COOLDOWN_SEC = 10
|
| 33 |
|
|
|
|
| 35 |
TELEGRAM_CHAT_ID = os.environ.get("TELEGRAM_CHAT_ID")
|
| 36 |
|
| 37 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 38 |
+
processor = VideoMAEImageProcessor.from_pretrained(MODEL_ID)
|
| 39 |
+
model = VideoMAEForVideoClassification.from_pretrained(MODEL_ID).to(device).eval()
|
| 40 |
+
|
| 41 |
+
ID2LABEL = {int(k): v for k, v in model.config.id2label.items()}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _find_alert_index():
|
| 45 |
+
for idx, label in ID2LABEL.items():
|
| 46 |
+
norm = label.lower().replace(" ", "").replace("_", "").replace("-", "")
|
| 47 |
+
if norm in ("fight", "violence"):
|
| 48 |
+
return idx
|
| 49 |
+
return 0
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
ALERT_IDX = _find_alert_index()
|
| 53 |
+
ALERT_LABEL = ID2LABEL[ALERT_IDX]
|
| 54 |
+
print(f"[config] model={MODEL_ID} labels={ID2LABEL} alert_on={ALERT_LABEL}")
|
| 55 |
|
| 56 |
_last_alert_ts = 0.0
|
| 57 |
_alert_lock = threading.Lock()
|
|
|
|
| 125 |
|
| 126 |
|
| 127 |
def run_inference(frames):
|
| 128 |
+
inputs = processor(list(frames), return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
with torch.no_grad():
|
| 130 |
outputs = model(**inputs)
|
| 131 |
+
probs = outputs.logits.softmax(dim=-1)[0].cpu().numpy()
|
| 132 |
+
return {ID2LABEL[i]: float(p) for i, p in enumerate(probs)}
|
| 133 |
|
| 134 |
|
| 135 |
def trigger_alert(confidence, source):
|