Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,207 +1,212 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import cv2
|
| 3 |
-
import torch
|
| 4 |
-
import numpy as np
|
| 5 |
-
import time
|
| 6 |
-
from datetime import datetime
|
| 7 |
-
import threading
|
| 8 |
-
import base64
|
| 9 |
-
from werkzeug.utils import secure_filename
|
| 10 |
-
|
| 11 |
-
from flask import Flask, render_template, Response, request, jsonify
|
| 12 |
-
from flask_socketio import SocketIO
|
| 13 |
-
|
| 14 |
-
#
|
| 15 |
-
from
|
| 16 |
-
from utils
|
| 17 |
-
from
|
| 18 |
-
from yolo_detection import analyze_video_with_yolo
|
| 19 |
-
|
| 20 |
-
# ---- App Setup ----
|
| 21 |
-
|
| 22 |
-
app.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
FEATURE_EXTRACTOR_PATH =
|
| 35 |
-
AD_MODEL_PATH =
|
| 36 |
-
YOLO_MODEL_PATH =
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
socketio.emit('
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
if
|
| 146 |
-
return jsonify({'error': 'No video file
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
return "
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
if
|
| 206 |
-
|
| 207 |
-
socketio.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
import time
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import threading
|
| 8 |
+
import base64
|
| 9 |
+
from werkzeug.utils import secure_filename
|
| 10 |
+
|
| 11 |
+
from flask import Flask, render_template, Response, request, jsonify
|
| 12 |
+
from flask_socketio import SocketIO
|
| 13 |
+
|
| 14 |
+
# CHANGED: Imports are now flat
|
| 15 |
+
from load_model import load_models
|
| 16 |
+
from utils import build_transforms
|
| 17 |
+
from TorchUtils import get_torch_device
|
| 18 |
+
from yolo_detection import analyze_video_with_yolo
|
| 19 |
+
|
| 20 |
+
# ---- App Setup ----
|
| 21 |
+
# CHANGED: Told Flask to look for templates/static files in the current folder ('.')
|
| 22 |
+
app = Flask(__name__, template_folder='.', static_folder='.')
|
| 23 |
+
app.config['SECRET_KEY'] = 'your_secret_key!'
|
| 24 |
+
|
| 25 |
+
# --- File Paths (CHANGED to relative paths) ---
|
| 26 |
+
UPLOAD_FOLDER = 'uploads'
|
| 27 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
| 28 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 29 |
+
|
| 30 |
+
SAVE_DIR = "outputs/anomaly_frames"
|
| 31 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 32 |
+
|
| 33 |
+
# CHANGED: All paths are now relative to this app.py file
|
| 34 |
+
FEATURE_EXTRACTOR_PATH = "models/c3d.pickle"
|
| 35 |
+
AD_MODEL_PATH = "models/epoch_8000.pt"
|
| 36 |
+
YOLO_MODEL_PATH = "models/yolo_my_model.pt"
|
| 37 |
+
|
| 38 |
+
# CHANGED: All video paths are now relative
|
| 39 |
+
VIDEO_PATHS = {
|
| 40 |
+
"Abuse": "demo_videos/Abuse.mp4",
|
| 41 |
+
"Arrest": "demo_videos/Arrest.mp4",
|
| 42 |
+
"Arson": "demo_videos/Arson.mp4",
|
| 43 |
+
"Assault": "demo_videos/Assault.mp4",
|
| 44 |
+
"Burglary": "demo_videos/Burglary.mp4",
|
| 45 |
+
"Explosion": "demo_videos/Explosion.mp4",
|
| 46 |
+
"Fighting": "demo_videos/Fighting.mp4",
|
| 47 |
+
"RoadAccidents": "demo_videos/RoadAccidents.mp4",
|
| 48 |
+
"Robbery": "demo_videos/Robbery.mp4",
|
| 49 |
+
"Shooting": "demo_videos/Shooting.mp4",
|
| 50 |
+
"Shoplifting": "demo_videos/Shoplifting.mp4",
|
| 51 |
+
"Stealing": "demo_videos/Stealing.mp4",
|
| 52 |
+
"Vandalism": "demo_videos/Vandalism.mp4",
|
| 53 |
+
"Normal": "demo_videos/Normal.mp4"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
# ---- Global Config & Model Loading ----
|
| 57 |
+
print("[INFO] Loading models...")
|
| 58 |
+
DEVICE = get_torch_device()
|
| 59 |
+
anomaly_detector, feature_extractor = load_models(
|
| 60 |
+
FEATURE_EXTRACTOR_PATH, AD_MODEL_PATH, features_method="c3d", device=DEVICE
|
| 61 |
+
)
|
| 62 |
+
feature_extractor.eval()
|
| 63 |
+
anomaly_detector.eval()
|
| 64 |
+
TRANSFORMS = build_transforms(mode="c3d")
|
| 65 |
+
ANOMALY_THRESHOLD = 0.5
|
| 66 |
+
print("[INFO] Models loaded successfully.")
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# --- Threading control ---
|
| 70 |
+
thread = None
|
| 71 |
+
thread_lock = threading.Lock()
|
| 72 |
+
stop_event = threading.Event()
|
| 73 |
+
|
| 74 |
+
# (The `smooth_score` and `video_processing_task` functions remain unchanged)
|
| 75 |
+
def smooth_score(scores, new_score, window=5):
|
| 76 |
+
scores.append(new_score)
|
| 77 |
+
if len(scores) > window:
|
| 78 |
+
scores.pop(0)
|
| 79 |
+
return float(np.mean(scores))
|
| 80 |
+
|
| 81 |
+
def video_processing_task(video_path):
|
| 82 |
+
global thread
|
| 83 |
+
try:
|
| 84 |
+
cap = cv2.VideoCapture(video_path)
|
| 85 |
+
if not cap.isOpened():
|
| 86 |
+
socketio.emit('processing_error', {'error': f'Could not open video file.'})
|
| 87 |
+
return
|
| 88 |
+
frame_buffer = []
|
| 89 |
+
last_save_time = 0
|
| 90 |
+
recent_scores = []
|
| 91 |
+
FRAME_SKIP = 4
|
| 92 |
+
frame_count = 0
|
| 93 |
+
while cap.isOpened() and not stop_event.is_set():
|
| 94 |
+
socketio.sleep(0.001)
|
| 95 |
+
ret, frame = cap.read()
|
| 96 |
+
if not ret: break
|
| 97 |
+
frame_count += 1
|
| 98 |
+
if frame_count % (FRAME_SKIP + 1) != 0: continue
|
| 99 |
+
frame_buffer.append(frame.copy())
|
| 100 |
+
if len(frame_buffer) == 16:
|
| 101 |
+
frames_resized = [cv2.resize(f, (112, 112)) for f in frame_buffer]
|
| 102 |
+
clip_np = np.array(frames_resized, dtype=np.uint8)
|
| 103 |
+
clip_torch = torch.from_numpy(clip_np)
|
| 104 |
+
clip_torch = TRANSFORMS(clip_torch)
|
| 105 |
+
clip_torch = clip_torch.unsqueeze(0).to(DEVICE)
|
| 106 |
+
with torch.no_grad():
|
| 107 |
+
features = feature_extractor(clip_torch).detach()
|
| 108 |
+
score_tensor = anomaly_detector(features).detach()
|
| 109 |
+
score = float(score_tensor.view(-1)[0].item())
|
| 110 |
+
score = smooth_score(recent_scores, score)
|
| 111 |
+
score = float(np.clip(score, 0, 1))
|
| 112 |
+
socketio.emit('update_graph', {'score': score})
|
| 113 |
+
if score > ANOMALY_THRESHOLD and (time.time() - last_save_time) >= 30:
|
| 114 |
+
last_save_time = time.time()
|
| 115 |
+
socketio.emit('update_status', {'status': 'Anomaly detected! Saving clip...'})
|
| 116 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 117 |
+
clip_dir = os.path.join(SAVE_DIR, f"anomaly_{timestamp}")
|
| 118 |
+
os.makedirs(clip_dir, exist_ok=True)
|
| 119 |
+
first_frame_path = os.path.join(clip_dir, "anomaly_frame.jpg")
|
| 120 |
+
cv2.imwrite(first_frame_path, frame_buffer[0])
|
| 121 |
+
try:
|
| 122 |
+
yolo_result = analyze_video_with_yolo(first_frame_path, model_path=YOLO_MODEL_PATH, return_class=True)
|
| 123 |
+
socketio.emit('update_yolo_text', {'text': f"YOLO Class: {yolo_result}"})
|
| 124 |
+
_, buffer = cv2.imencode('.jpg', frame_buffer[0])
|
| 125 |
+
b64_str = base64.b64encode(buffer).decode('utf-8')
|
| 126 |
+
socketio.emit('update_yolo_image', {'image_data': b64_str})
|
| 127 |
+
except Exception as e:
|
| 128 |
+
socketio.emit('update_yolo_text', {'text': f'YOLO Error: {e}'})
|
| 129 |
+
frame_buffer.clear()
|
| 130 |
+
cap.release()
|
| 131 |
+
if not stop_event.is_set():
|
| 132 |
+
socketio.emit('processing_finished', {'message': 'Video finished.'})
|
| 133 |
+
finally:
|
| 134 |
+
with thread_lock:
|
| 135 |
+
thread = None
|
| 136 |
+
stop_event.clear()
|
| 137 |
+
|
| 138 |
+
@app.route('/')
|
| 139 |
+
def index():
|
| 140 |
+
# This will now look for 'index.html' in the same folder
|
| 141 |
+
return render_template('index.html', anomaly_names=VIDEO_PATHS.keys())
|
| 142 |
+
|
| 143 |
+
@app.route('/upload', methods=['POST'])
|
| 144 |
+
def upload_file():
|
| 145 |
+
if 'video' not in request.files:
|
| 146 |
+
return jsonify({'error': 'No video file found'}), 400
|
| 147 |
+
file = request.files['video']
|
| 148 |
+
if file.filename == '':
|
| 149 |
+
return jsonify({'error': 'No video file selected'}), 400
|
| 150 |
+
if file:
|
| 151 |
+
filename = secure_filename(file.filename)
|
| 152 |
+
unique_filename = f"{datetime.now().strftime('%Y%m%d%HM%S')}_{filename}"
|
| 153 |
+
save_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
| 154 |
+
file.save(save_path)
|
| 155 |
+
return jsonify({'success': True, 'filename': unique_filename})
|
| 156 |
+
return jsonify({'error': 'File upload failed'}), 500
|
| 157 |
+
|
| 158 |
+
@app.route('/video_stream/<source>/<filename>')
|
| 159 |
+
def video_stream(source, filename):
|
| 160 |
+
if source == 'demo':
|
| 161 |
+
path = VIDEO_PATHS.get(filename)
|
| 162 |
+
elif source == 'upload':
|
| 163 |
+
# Need to URL-decode the filename
|
| 164 |
+
safe_filename = secure_filename(filename)
|
| 165 |
+
path = os.path.join(app.config['UPLOAD_FOLDER'], safe_filename)
|
| 166 |
+
else:
|
| 167 |
+
return "Invalid source", 404
|
| 168 |
+
|
| 169 |
+
if not path or not os.path.exists(path):
|
| 170 |
+
return "Video not found", 404
|
| 171 |
+
|
| 172 |
+
def generate():
|
| 173 |
+
with open(path, "rb") as f:
|
| 174 |
+
while chunk := f.read(1024 * 1024):
|
| 175 |
+
yield chunk
|
| 176 |
+
|
| 177 |
+
return Response(generate(), mimetype="video/mp4")
|
| 178 |
+
|
| 179 |
+
@socketio.on('start_processing')
|
| 180 |
+
def handle_start_processing(data):
|
| 181 |
+
global thread
|
| 182 |
+
with thread_lock:
|
| 183 |
+
if thread is None:
|
| 184 |
+
stop_event.clear()
|
| 185 |
+
source = data.get('source')
|
| 186 |
+
filename = data.get('filename')
|
| 187 |
+
video_path = None
|
| 188 |
+
|
| 189 |
+
if source == 'demo':
|
| 190 |
+
video_path = VIDEO_PATHS.get(filename)
|
| 191 |
+
elif source == 'upload':
|
| 192 |
+
safe_filename = secure_filename(filename)
|
| 193 |
+
video_path = os.path.join(app.config['UPLOAD_FOLDER'], safe_filename)
|
| 194 |
+
|
| 195 |
+
if video_path and os.path.exists(video_path):
|
| 196 |
+
print(f"[INFO] Starting processing for {filename} from {source}")
|
| 197 |
+
thread = socketio.start_background_task(target=video_processing_task, video_path=video_path)
|
| 198 |
+
else:
|
| 199 |
+
socketio.emit('processing_error', {'error': f'Video file not found!'})
|
| 200 |
+
|
| 201 |
+
@socketio.on('reset_system')
|
| 202 |
+
def handle_reset():
|
| 203 |
+
global thread
|
| 204 |
+
with thread_lock:
|
| 205 |
+
if thread is not None:
|
| 206 |
+
stop_event.set()
|
| 207 |
+
socketio.emit('system_reset_confirm')
|
| 208 |
+
|
| 209 |
+
if __name__ == '__main__':
|
| 210 |
+
print("[INFO] Starting Flask server...")
|
| 211 |
+
# Using 0.0.0.0 is still correct for services like Hugging Face
|
| 212 |
+
socketio.run(app, debug=True, host='0.0.0.0')
|