Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
def show_preds_video():
|
6 |
+
|
7 |
+
|
8 |
+
background = cv2.imread("background2.png")
|
9 |
+
background = cv2.cvtColor(background,cv2.COLOR_BGR2GRAY)
|
10 |
+
background = cv2.GaussianBlur(background,(21,21),0)
|
11 |
+
|
12 |
+
|
13 |
+
# def detect_motion(thres_input):
|
14 |
+
cap = cv2.VideoCapture('CCTV.avi')
|
15 |
+
|
16 |
+
# Initialize video writer for processed video
|
17 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
18 |
+
out = cv2.VideoWriter("processed_video.mp4", fourcc, cap.get(cv2.CAP_PROP_FPS), (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
|
19 |
+
|
20 |
+
while True:
|
21 |
+
# Capture current frame
|
22 |
+
ret, frame = cap.read()
|
23 |
+
|
24 |
+
# If end of video, break loop
|
25 |
+
if not ret:
|
26 |
+
break
|
27 |
+
|
28 |
+
# Convert current frame to grayscale
|
29 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
30 |
+
gray = cv2.GaussianBlur(gray,(21,21), 0)
|
31 |
+
|
32 |
+
|
33 |
+
# reaize background to match current frame
|
34 |
+
background = cv2.resize(background, (gray.shape[1], gray.shape[0]))
|
35 |
+
|
36 |
+
# Calculate absolute difference between current frame and background
|
37 |
+
diff = cv2.absdiff(background,gray)
|
38 |
+
thresh = cv2.threshold(diff,30,255,cv2.THRESH_BINARY)[1]
|
39 |
+
thresh = cv2.dilate(thresh, None, iterations = 2)
|
40 |
+
|
41 |
+
cnts,res = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
42 |
+
|
43 |
+
for contour in cnts:
|
44 |
+
if cv2.contourArea(contour) < 10000 :
|
45 |
+
continue
|
46 |
+
(x,y,w,h) = cv2.boundingRect(contour)
|
47 |
+
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0), 3)
|
48 |
+
|
49 |
+
# # Check if any contours were found
|
50 |
+
if len(contour) > 0:
|
51 |
+
# Motion detected, trigger alarm
|
52 |
+
message = "Motion detected !!!"
|
53 |
+
|
54 |
+
else:
|
55 |
+
# No motion detected
|
56 |
+
message = "No motion detected"
|
57 |
+
|
58 |
+
# Display current frame and processed frames
|
59 |
+
cv2.putText(frame, message, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
60 |
+
|
61 |
+
# Write processed frame to output video
|
62 |
+
out.write(frame)
|
63 |
+
|
64 |
+
|
65 |
+
return "processed_video.mp4"
|
66 |
+
|
67 |
+
outputs_video = [
|
68 |
+
gr.outputs.Video(label="Processed Video"),
|
69 |
+
]
|
70 |
+
|
71 |
+
inputs_video = [ #gr.components.Video(type="filepath", label="Input Video", visible =False),
|
72 |
+
]
|
73 |
+
|
74 |
+
interface_video = gr.Interface(
|
75 |
+
fn=show_preds_video,
|
76 |
+
inputs=inputs_video,
|
77 |
+
outputs=outputs_video,
|
78 |
+
title="Security - Trespasser monitoring ",
|
79 |
+
cache_examples=False,
|
80 |
+
allow_flagging=False,
|
81 |
+
capture_session=True,
|
82 |
+
cache=True
|
83 |
+
|
84 |
+
).queue().launch()
|
85 |
+
|