AI-Naga's picture
Upload app.py
398805b
raw
history blame contribute delete
No virus
2.57 kB
import cv2
import numpy as np
import gradio as gr
def show_preds_video():
background = cv2.imread("background2.png")
background = cv2.cvtColor(background,cv2.COLOR_BGR2GRAY)
background = cv2.GaussianBlur(background,(21,21),0)
# def detect_motion(thres_input):
cap = cv2.VideoCapture('CCTV.avi')
# Initialize video writer for processed video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
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))))
while True:
# Capture current frame
ret, frame = cap.read()
# If end of video, break loop
if not ret:
break
# Convert current frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(21,21), 0)
# reaize background to match current frame
background = cv2.resize(background, (gray.shape[1], gray.shape[0]))
# Calculate absolute difference between current frame and background
diff = cv2.absdiff(background,gray)
thresh = cv2.threshold(diff,30,255,cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations = 2)
cnts,res = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in cnts:
if cv2.contourArea(contour) < 10000 :
continue
(x,y,w,h) = cv2.boundingRect(contour)
cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0), 3)
# # Check if any contours were found
if len(contour) > 0:
# Motion detected, trigger alarm
message = "Motion detected !!!"
else:
# No motion detected
message = "No motion detected"
# Display current frame and processed frames
cv2.putText(frame, message, (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
# Write processed frame to output video
out.write(frame)
return "processed_video.mp4"
outputs_video = [
gr.outputs.Video(label="Processed Video"),
]
inputs_video = [ #gr.components.Video(type="filepath", label="Input Video", visible =False),
]
interface_video = gr.Interface(
fn=show_preds_video,
inputs=inputs_video,
outputs=outputs_video,
title="Security - Trespasser monitoring ",
cache_examples=False,
allow_flagging=False,
capture_session=True,
cache=True
).queue().launch()