File size: 2,566 Bytes
398805b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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()