Abubakar740 commited on
Commit
a81edd6
·
1 Parent(s): abd4c19

update app

Browse files
Files changed (1) hide show
  1. app.py +269 -88
app.py CHANGED
@@ -1,119 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import cv2
3
  import torch
4
  import numpy as np
5
  import uuid
6
- import threading
7
  import gradio as gr
8
- from fastapi import FastAPI, UploadFile, File, BackgroundTasks, HTTPException
9
- from fastapi.responses import FileResponse
10
  from collections import deque
11
  from pytorchvideo.models.hub import slowfast_r50
12
  from ultralytics import YOLO
13
- import torch.nn as nn
14
 
15
- # --- SETUP & DIRECTORIES ---
16
- UPLOAD_DIR = "uploads"
17
- OUTPUT_DIR = "outputs"
18
  MODEL_PATH = "models/best_slowfast_theft.pth"
19
- os.makedirs(UPLOAD_DIR, exist_ok=True)
20
- os.makedirs(OUTPUT_DIR, exist_ok=True)
 
21
 
22
- JOBS = {} # Track progress
 
 
 
 
 
23
 
24
- # --- MODEL LOADING ---
25
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  yolo = YOLO("yolov8n.pt")
 
 
27
 
28
- def load_slowfast():
29
- model = slowfast_r50(pretrained=False)
30
- in_features = model.blocks[-1].proj.in_features
31
- model.blocks[-1].proj = nn.Sequential(
32
- nn.Dropout(p=0.5),
33
- nn.Linear(in_features, 2)
34
- )
35
- if os.path.exists(MODEL_PATH):
36
- ckpt = torch.load(MODEL_PATH, map_location=DEVICE)
37
- model.load_state_dict(ckpt["model"] if "model" in ckpt else ckpt)
38
- model.to(DEVICE).eval()
39
- return model
40
-
41
- detector_model = load_slowfast()
42
-
43
- # --- DETECTION LOGIC ---
44
- def process_video_logic(job_id, input_path, output_path):
45
- cap = cv2.VideoCapture(input_path)
46
- total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
47
- fps = int(cap.get(cv2.CAP_PROP_FPS))
48
- w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
49
 
50
- out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
51
- frame_buffer = deque(maxlen=32)
 
 
 
 
 
52
 
53
- curr = 0
 
 
 
 
 
 
 
 
 
 
 
 
54
  while cap.isOpened():
55
  ret, frame = cap.read()
56
  if not ret: break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- curr += 1
59
- JOBS[job_id]["progress"] = int((curr/total_frames)*100)
60
-
61
- # Basic YOLO logic (Simplified for speed)
62
- results = yolo(frame, verbose=False)
63
- for r in results:
64
- for box in r.boxes:
65
- if int(box.cls[0]) == 0:
66
- x1, y1, x2, y2 = map(int, box.xyxy[0])
67
- cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
68
 
69
  out.write(frame)
70
-
71
  cap.release()
72
  out.release()
73
- JOBS[job_id]["status"] = "completed"
74
-
75
- # --- FASTAPI ENDPOINTS ---
76
- app = FastAPI()
77
 
78
- @app.post("/api/detect")
79
- async def api_detect(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
80
- job_id = str(uuid.uuid4())
81
- in_p = os.path.join(UPLOAD_DIR, f"{job_id}.mp4")
82
- out_p = os.path.join(OUTPUT_DIR, f"{job_id}.mp4")
83
-
84
- with open(in_p, "wb") as f: f.write(await file.read())
85
-
86
- JOBS[job_id] = {"progress": 0, "status": "processing", "file": out_p}
87
- background_tasks.add_task(process_video_logic, job_id, in_p, out_p)
88
- return {"job_id": job_id}
89
-
90
- @app.get("/api/progress/{job_id}")
91
- async def api_progress(job_id: str):
92
- return JOBS.get(job_id, {"error": "not found"})
93
-
94
- # --- GRADIO FRONTEND ---
95
- def web_ui_process(video_input):
96
- if video_input is None: return None
97
- job_id = str(uuid.uuid4())
98
- out_p = os.path.join(OUTPUT_DIR, f"{job_id}.mp4")
99
 
100
- # Run the processing (Sync for Gradio UI to show progress)
101
- JOBS[job_id] = {"progress": 0, "status": "processing"}
102
- process_video_logic(job_id, video_input, out_p)
103
- return out_p
104
-
105
- with gr.Blocks(title="Theft Detection System") as demo:
106
- gr.Markdown("# 🛡️ AI Theft Detection System")
107
  with gr.Row():
108
- video_in = gr.Video(label="Upload Video")
109
- video_out = gr.Video(label="Processed Result")
110
- btn = gr.Button("Detect Theft")
111
- btn.click(web_ui_process, inputs=video_in, outputs=video_out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
- # --- MOUNT FASTAPI TO GRADIO ---
114
- # This allows both to run on the same port on Hugging Face
115
- app = gr.mount_gradio_app(app, demo, path="/")
 
 
116
 
117
  if __name__ == "__main__":
118
- import uvicorn
119
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ # import os
2
+ # import cv2
3
+ # import torch
4
+ # import numpy as np
5
+ # import uuid
6
+ # import threading
7
+ # import gradio as gr
8
+ # from fastapi import FastAPI, UploadFile, File, BackgroundTasks, HTTPException
9
+ # from fastapi.responses import FileResponse
10
+ # from collections import deque
11
+ # from pytorchvideo.models.hub import slowfast_r50
12
+ # from ultralytics import YOLO
13
+ # import torch.nn as nn
14
+
15
+ # # --- SETUP & DIRECTORIES ---
16
+ # UPLOAD_DIR = "uploads"
17
+ # OUTPUT_DIR = "outputs"
18
+ # MODEL_PATH = "models/best_slowfast_theft.pth"
19
+ # os.makedirs(UPLOAD_DIR, exist_ok=True)
20
+ # os.makedirs(OUTPUT_DIR, exist_ok=True)
21
+
22
+ # JOBS = {} # Track progress
23
+
24
+ # # --- MODEL LOADING ---
25
+ # DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
26
+ # yolo = YOLO("yolov8n.pt")
27
+
28
+ # def load_slowfast():
29
+ # model = slowfast_r50(pretrained=False)
30
+ # in_features = model.blocks[-1].proj.in_features
31
+ # model.blocks[-1].proj = nn.Sequential(
32
+ # nn.Dropout(p=0.5),
33
+ # nn.Linear(in_features, 2)
34
+ # )
35
+ # if os.path.exists(MODEL_PATH):
36
+ # ckpt = torch.load(MODEL_PATH, map_location=DEVICE)
37
+ # model.load_state_dict(ckpt["model"] if "model" in ckpt else ckpt)
38
+ # model.to(DEVICE).eval()
39
+ # return model
40
+
41
+ # detector_model = load_slowfast()
42
+
43
+ # # --- DETECTION LOGIC ---
44
+ # def process_video_logic(job_id, input_path, output_path):
45
+ # cap = cv2.VideoCapture(input_path)
46
+ # total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
47
+ # fps = int(cap.get(cv2.CAP_PROP_FPS))
48
+ # w, h = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
49
+
50
+ # out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
51
+ # frame_buffer = deque(maxlen=32)
52
+
53
+ # curr = 0
54
+ # while cap.isOpened():
55
+ # ret, frame = cap.read()
56
+ # if not ret: break
57
+
58
+ # curr += 1
59
+ # JOBS[job_id]["progress"] = int((curr/total_frames)*100)
60
+
61
+ # # Basic YOLO logic (Simplified for speed)
62
+ # results = yolo(frame, verbose=False)
63
+ # for r in results:
64
+ # for box in r.boxes:
65
+ # if int(box.cls[0]) == 0:
66
+ # x1, y1, x2, y2 = map(int, box.xyxy[0])
67
+ # cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
68
+
69
+ # out.write(frame)
70
+
71
+ # cap.release()
72
+ # out.release()
73
+ # JOBS[job_id]["status"] = "completed"
74
+
75
+ # # --- FASTAPI ENDPOINTS ---
76
+ # app = FastAPI()
77
+
78
+ # @app.post("/api/detect")
79
+ # async def api_detect(background_tasks: BackgroundTasks, file: UploadFile = File(...)):
80
+ # job_id = str(uuid.uuid4())
81
+ # in_p = os.path.join(UPLOAD_DIR, f"{job_id}.mp4")
82
+ # out_p = os.path.join(OUTPUT_DIR, f"{job_id}.mp4")
83
+
84
+ # with open(in_p, "wb") as f: f.write(await file.read())
85
+
86
+ # JOBS[job_id] = {"progress": 0, "status": "processing", "file": out_p}
87
+ # background_tasks.add_task(process_video_logic, job_id, in_p, out_p)
88
+ # return {"job_id": job_id}
89
+
90
+ # @app.get("/api/progress/{job_id}")
91
+ # async def api_progress(job_id: str):
92
+ # return JOBS.get(job_id, {"error": "not found"})
93
+
94
+ # # --- GRADIO FRONTEND ---
95
+ # def web_ui_process(video_input):
96
+ # if video_input is None: return None
97
+ # job_id = str(uuid.uuid4())
98
+ # out_p = os.path.join(OUTPUT_DIR, f"{job_id}.mp4")
99
+
100
+ # # Run the processing (Sync for Gradio UI to show progress)
101
+ # JOBS[job_id] = {"progress": 0, "status": "processing"}
102
+ # process_video_logic(job_id, video_input, out_p)
103
+ # return out_p
104
+
105
+ # with gr.Blocks(title="Theft Detection System") as demo:
106
+ # gr.Markdown("# 🛡️ AI Theft Detection System")
107
+ # with gr.Row():
108
+ # video_in = gr.Video(label="Upload Video")
109
+ # video_out = gr.Video(label="Processed Result")
110
+ # btn = gr.Button("Detect Theft")
111
+ # btn.click(web_ui_process, inputs=video_in, outputs=video_out)
112
+
113
+ # # --- MOUNT FASTAPI TO GRADIO ---
114
+ # # This allows both to run on the same port on Hugging Face
115
+ # app = gr.mount_gradio_app(app, demo, path="/")
116
+
117
+ # if __name__ == "__main__":
118
+ # import uvicorn
119
+ # uvicorn.run(app, host="0.0.0.0", port=7860)
120
+
121
+
122
+
123
+
124
+
125
  import os
126
  import cv2
127
  import torch
128
  import numpy as np
129
  import uuid
130
+ import torch.nn as nn
131
  import gradio as gr
 
 
132
  from collections import deque
133
  from pytorchvideo.models.hub import slowfast_r50
134
  from ultralytics import YOLO
 
135
 
136
+ # --- CONFIG & DIRECTORIES ---
137
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
138
  MODEL_PATH = "models/best_slowfast_theft.pth"
139
+ os.makedirs("uploads", exist_ok=True)
140
+ os.makedirs("outputs", exist_ok=True)
141
+ os.makedirs("models", exist_ok=True)
142
 
143
+ # --- HEATMAP CLASS ---
144
+ class Heatmap:
145
+ def __init__(self, h, w, decay=0.92):
146
+ self.m = np.zeros((h, w), np.float32)
147
+ self.decay = decay
148
+ self.h, self.w = h, w
149
 
150
+ def add(self, bbox, intensity=1.0, poly_mask=None):
151
+ x1, y1, x2, y2 = [int(v) for v in bbox]
152
+ x1, y1 = max(0, x1), max(0, y1)
153
+ x2, y2 = min(self.w-1, x2), min(self.h-1, y2)
154
+ if x2 <= x1 or y2 <= y1: return
155
+ cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
156
+ rx, ry = max(1, (x2 - x1) // 2), max(1, (y2 - y1) // 2)
157
+ blob = np.zeros((self.h, self.w), np.float32)
158
+ cv2.ellipse(blob, (cx, cy), (rx, ry), 0, 0, 360, intensity, -1)
159
+ blob = cv2.GaussianBlur(blob, (0, 0), rx * 0.6, sigmaY=ry * 0.6)
160
+ if poly_mask is not None: blob *= poly_mask
161
+ self.m = np.clip(self.m + blob, 0, 10.0)
162
+
163
+ def step(self): self.m *= self.decay
164
+
165
+ def overlay(self, frame, alpha=0.45, poly_mask=None):
166
+ norm = np.clip(self.m / 10.0, 0, 1)
167
+ coloured = cv2.applyColorMap((norm * 255).astype(np.uint8), cv2.COLORMAP_JET)
168
+ mask3 = np.stack([(norm > 0.05).astype(np.float32)] * 3, -1)
169
+ if poly_mask is not None: mask3 *= np.stack([poly_mask] * 3, -1)
170
+ return (coloured * mask3 * alpha + frame * (1 - mask3 * alpha)).astype(np.uint8)
171
+
172
+ # --- LOAD MODELS ---
173
+ print("Loading Models...")
174
  yolo = YOLO("yolov8n.pt")
175
+ sf_model = slowfast_r50(pretrained=False)
176
+ sf_model.blocks[-1].proj = nn.Sequential(nn.Dropout(p=0.5), nn.Linear(sf_model.blocks[-1].proj.in_features, 2))
177
 
178
+ if os.path.exists(MODEL_PATH):
179
+ ckpt = torch.load(MODEL_PATH, map_location=DEVICE)
180
+ sf_model.load_state_dict(ckpt["model"] if "model" in ckpt else ckpt)
181
+ sf_model.to(DEVICE).eval()
182
+
183
+ # --- CORE LOGIC ---
184
+ def process_video(video_path, roi_image):
185
+ if not video_path: return None
 
 
 
 
 
 
 
 
 
 
 
 
 
186
 
187
+ cap = cv2.VideoCapture(video_path)
188
+ w, h = int(cap.get(3)), int(cap.get(4))
189
+ fps = int(cap.get(5))
190
+ output_path = f"outputs/out_{uuid.uuid4()}.mp4"
191
+ out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
192
+
193
+ heatmap = Heatmap(h, w)
194
 
195
+ # Process ROI Mask from Sketch
196
+ poly_mask = None
197
+ if roi_image is not None and 'layers' in roi_image:
198
+ # Use the sketch layer to create a mask
199
+ mask_layer = roi_image['layers'][0]
200
+ mask_layer = cv2.resize(mask_layer, (w, h))
201
+ gray = cv2.cvtColor(mask_layer, cv2.COLOR_BGR2GRAY)
202
+ _, poly_mask = cv2.threshold(gray, 10, 1.0, cv2.THRESH_BINARY)
203
+ poly_mask = poly_mask.astype(np.float32)
204
+
205
+ person_buffers = {}
206
+ prediction_buffers = {}
207
+
208
  while cap.isOpened():
209
  ret, frame = cap.read()
210
  if not ret: break
211
+
212
+ heatmap.step()
213
+ results = yolo.track(frame, persist=True, verbose=False, classes=[0])
214
+ global_theft = False
215
+
216
+ if results[0].boxes.id is not None:
217
+ boxes = results[0].boxes.xyxy.cpu().numpy()
218
+ ids = results[0].boxes.id.cpu().numpy().astype(int)
219
+
220
+ for box, track_id in zip(boxes, ids):
221
+ x1, y1, x2, y2 = map(int, box)
222
+ cx, cy = (x1 + x2) // 2, (y1 + y2) // 2
223
+
224
+ # ROI Check
225
+ if poly_mask is not None and poly_mask[cy, cx] == 0: continue
226
+
227
+ heatmap.add(box, poly_mask=poly_mask)
228
+
229
+ if track_id not in person_buffers:
230
+ person_buffers[track_id] = deque(maxlen=32)
231
+ prediction_buffers[track_id] = deque(maxlen=10)
232
+
233
+ crop = frame[y1:y2, x1:x2]
234
+ if crop.size > 0: person_buffers[track_id].append(crop)
235
+
236
+ current_score = 0.0
237
+ if len(person_buffers[track_id]) == 32:
238
+ processed = [cv2.resize(f, (224, 224))[:,:,::-1]/255.0 for f in person_buffers[track_id]]
239
+ clip = torch.tensor(np.transpose(np.array(processed), (3,0,1,2))).float().unsqueeze(0).to(DEVICE)
240
+ with torch.no_grad():
241
+ out_sf = sf_model([clip[:, :, ::4, :, :], clip])
242
+ current_score = torch.softmax(out_sf, dim=1)[0][1].item()
243
+ prediction_buffers[track_id].append(current_score)
244
+ current_score = np.mean(prediction_buffers[track_id])
245
+
246
+ if current_score > 0.6: global_theft = True
247
+ color = (0, 0, 255) if current_score > 0.6 else (0, 255, 0)
248
+ cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
249
+ cv2.putText(frame, f"ID:{track_id} {current_score:.2f}", (x1, y1-10), 0, 0.5, color, 2)
250
+
251
+ # Fancy Overlays
252
+ frame = heatmap.overlay(frame, poly_mask=poly_mask)
253
+ overlay = frame.copy()
254
+ cv2.rectangle(overlay, (0,0), (w, 80), (0,0,0), -1)
255
+ cv2.addWeighted(overlay, 0.5, frame, 0.5, 0, frame)
256
 
257
+ status = "!!! THEFT DETECTED !!!" if global_theft else "Monitoring Area..."
258
+ scolor = (0,0,255) if global_theft else (0,255,0)
259
+ cv2.putText(frame, status, (20, 50), 0, 1.0, scolor, 3)
 
 
 
 
 
 
 
260
 
261
  out.write(frame)
262
+
263
  cap.release()
264
  out.release()
265
+ return output_path
 
 
 
266
 
267
+ # --- GRADIO UI ---
268
+ with gr.Blocks(theme=gr.themes.Soft(), title="Theft Detection Pro") as demo:
269
+ gr.Markdown("# 🛡️ AI Theft Detection & Heatmap System")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
270
 
 
 
 
 
 
 
 
271
  with gr.Row():
272
+ with gr.Column(scale=1):
273
+ video_input = gr.Video(label="1. Upload Video", height=400)
274
+ gr.Markdown("### 2. Draw ROI (Optional)\nDraw on the image below to monitor a specific area.")
275
+ # This handles getting the first frame automatically
276
+ roi_input = gr.ImageMask(label="Draw Region of Interest", height=400)
277
+
278
+ def get_first_frame(vid):
279
+ if vid is None: return None
280
+ cap = cv2.VideoCapture(vid)
281
+ ret, frame = cap.read()
282
+ cap.release()
283
+ if ret: return cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
284
+ return None
285
+
286
+ video_input.change(get_first_frame, inputs=video_input, outputs=roi_input)
287
+
288
+ submit_btn = gr.Button("🚀 Start Processing", variant="primary")
289
+
290
+ with gr.Column(scale=1):
291
+ video_output = gr.Video(label="3. Detection Result", height=800)
292
 
293
+ submit_btn.click(
294
+ fn=process_video,
295
+ inputs=[video_input, roi_input],
296
+ outputs=video_output
297
+ )
298
 
299
  if __name__ == "__main__":
300
+ demo.launch()