Ankan Ghosh
commited on
Commit
•
43e9b6c
1
Parent(s):
29159ad
Upload 5 files
Browse files- .gitattributes +3 -0
- app.py +114 -0
- requirements.txt +3 -0
- sample/car.mp4 +3 -0
- sample/home.mp4 +3 -0
- sample/motion_test.mp4 +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
sample/car.mp4 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
sample/home.mp4 filter=lfs diff=lfs merge=lfs -text
|
38 |
+
sample/motion_test.mp4 filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
|
6 |
+
# input_video = './sample/car.mp4'
|
7 |
+
|
8 |
+
# video Inference
|
9 |
+
def vid_inf(vid_path, contour_thresh):
|
10 |
+
# Create a VideoCapture object
|
11 |
+
cap = cv2.VideoCapture(vid_path)
|
12 |
+
|
13 |
+
# get the video frames' width and height for proper saving of videos
|
14 |
+
frame_width = int(cap.get(3))
|
15 |
+
frame_height = int(cap.get(4))
|
16 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
17 |
+
frame_size = (frame_width, frame_height)
|
18 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
19 |
+
output_video = "output_recorded.mp4"
|
20 |
+
|
21 |
+
# create the `VideoWriter()` object
|
22 |
+
out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
|
23 |
+
|
24 |
+
# Create Background Subtractor MOG2 object
|
25 |
+
backSub = cv2.createBackgroundSubtractorMOG2(history=200, varThreshold=25, detectShadows=True)
|
26 |
+
# print(dir(backSub))
|
27 |
+
|
28 |
+
# Check if camera opened successfully
|
29 |
+
if not cap.isOpened():
|
30 |
+
print("Error opening video file")
|
31 |
+
count = 0
|
32 |
+
# Read until video is completed
|
33 |
+
while cap.isOpened():
|
34 |
+
# Capture frame-by-frame
|
35 |
+
ret, frame = cap.read()
|
36 |
+
# print(frame.shape)
|
37 |
+
if ret:
|
38 |
+
# Apply background subtraction
|
39 |
+
fg_mask = backSub.apply(frame)
|
40 |
+
# print(fg_mask.shape)
|
41 |
+
# fg_mask = cv2.resize(fg_mask, (640,480))
|
42 |
+
# print(fg_mask.shape)
|
43 |
+
# cv2.imshow('Frame_bg', fg_mask)
|
44 |
+
|
45 |
+
# apply global threshol to remove shadows
|
46 |
+
retval, mask_thresh = cv2.threshold(
|
47 |
+
fg_mask, 200, 255, cv2.THRESH_BINARY)
|
48 |
+
# cv2.imshow('frame_thresh', mask_thresh)
|
49 |
+
|
50 |
+
# set the kernal
|
51 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
52 |
+
# Apply erosion
|
53 |
+
mask_eroded = cv2.morphologyEx(mask_thresh, cv2.MORPH_OPEN, kernel)
|
54 |
+
# mask_eroded = cv2.resize(mask_eroded, (640,480))
|
55 |
+
# cv2.imshow('frame_erode', mask_eroded)
|
56 |
+
|
57 |
+
# Find contours
|
58 |
+
contours, hierarchy = cv2.findContours(
|
59 |
+
mask_eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
60 |
+
# print(contours)
|
61 |
+
|
62 |
+
min_contour_area = contour_thresh # Define your minimum area threshold
|
63 |
+
large_contours = [
|
64 |
+
cnt for cnt in contours if cv2.contourArea(cnt) > min_contour_area]
|
65 |
+
# frame_ct = cv2.drawContours(frame, large_contours, -1, (0, 255, 0), 2)
|
66 |
+
frame_out = frame.copy()
|
67 |
+
for cnt in large_contours:
|
68 |
+
# print(cnt.shape)
|
69 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
70 |
+
frame_out = cv2.rectangle(frame_out, (x, y), (x+w, y+h), (0, 0, 200), 3)
|
71 |
+
frame_out_final = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)
|
72 |
+
vid = out.write(frame_out)
|
73 |
+
|
74 |
+
# Display the resulting frame
|
75 |
+
# resized_frame = cv2.resize(frame_out, (640,480))
|
76 |
+
# cv2.imshow('Frame_final', frame_out)
|
77 |
+
|
78 |
+
# update the count every frame and display every 12th frame
|
79 |
+
if not count % 12:
|
80 |
+
yield frame_out_final, None
|
81 |
+
count += 1
|
82 |
+
|
83 |
+
# Press Q on keyboard to exit
|
84 |
+
if cv2.waitKey(25) & 0xFF == ord('q'):
|
85 |
+
break
|
86 |
+
else:
|
87 |
+
break
|
88 |
+
|
89 |
+
# When everything done, release the video capture and writer object
|
90 |
+
cap.release()
|
91 |
+
out.release()
|
92 |
+
# Closes all the frames
|
93 |
+
cv2.destroyAllWindows()
|
94 |
+
yield None, output_video
|
95 |
+
|
96 |
+
# vid_inf(input_video)
|
97 |
+
|
98 |
+
|
99 |
+
# gradio interface
|
100 |
+
input_video = gr.Video(label="Input Video")
|
101 |
+
contour_thresh = gr.Slider(0, 10000, value=4, label="Contour Threshold", info="Adjust the Countour Threshold according to the object size that you want to detect.")
|
102 |
+
output_frames = gr.Image(label="Output Frames")
|
103 |
+
output_video_file = gr.Video(label="Output video")
|
104 |
+
|
105 |
+
app = gr.Interface(
|
106 |
+
fn=vid_inf,
|
107 |
+
inputs=[input_video, contour_thresh],
|
108 |
+
outputs=[output_frames, output_video_file],
|
109 |
+
title=f"Motion Detection using OpenCV",
|
110 |
+
description=f'A gradio app for dynamic video analysis tool that leverages advanced background subtraction and contour detection techniques to identify and track moving objects in real-time.',
|
111 |
+
allow_flagging="never",
|
112 |
+
examples=[["./sample/car.mp4"], ["./sample/motion_test.mp4"], ["./sample/home.mp4"]],
|
113 |
+
)
|
114 |
+
app.queue().launch()
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
opencv-contrib-python
|
2 |
+
numpy
|
3 |
+
gradio
|
sample/car.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c7f7287faa231cf287c433ef8b2f38949e214cac9c473aea286d75f2bdea2330
|
3 |
+
size 4708643
|
sample/home.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f6f269d372f84dcc2bb8317f7be2db05173bcc42a8433e28ede51ffca7f8265d
|
3 |
+
size 2853355
|
sample/motion_test.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:20090f4160519b776e1fb855617d2268745e826e07d81f1eab4a588df43628cf
|
3 |
+
size 17887070
|