Ankan Ghosh
commited on
Upload 3 files
Browse files- .gitattributes +1 -0
- app.py +112 -0
- requirements.txt +3 -0
- sample/car.mp4 +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ 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
|
app.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# input_video = 'car.mp4'
|
| 7 |
+
|
| 8 |
+
# video Inference
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def vid_inf(vid_path):
|
| 12 |
+
# Create a VideoCapture object
|
| 13 |
+
cap = cv2.VideoCapture(vid_path)
|
| 14 |
+
|
| 15 |
+
# get the video frames' width and height for proper saving of videos
|
| 16 |
+
frame_width = int(cap.get(3))
|
| 17 |
+
frame_height = int(cap.get(4))
|
| 18 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 19 |
+
frame_size = (frame_width, frame_height)
|
| 20 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 21 |
+
output_video = "output_recorded.mp4"
|
| 22 |
+
|
| 23 |
+
# create the `VideoWriter()` object
|
| 24 |
+
out = cv2.VideoWriter(output_video, fourcc, fps, frame_size)
|
| 25 |
+
|
| 26 |
+
# Create Background Subtractor MOG2 object
|
| 27 |
+
backSub = cv2.createBackgroundSubtractorMOG2()
|
| 28 |
+
|
| 29 |
+
# Check if camera opened successfully
|
| 30 |
+
if not cap.isOpened():
|
| 31 |
+
print("Error opening video file")
|
| 32 |
+
count = 0
|
| 33 |
+
# Read until video is completed
|
| 34 |
+
while cap.isOpened():
|
| 35 |
+
# Capture frame-by-frame
|
| 36 |
+
ret, frame = cap.read()
|
| 37 |
+
# print(frame.shape)
|
| 38 |
+
if ret:
|
| 39 |
+
# Apply background subtraction
|
| 40 |
+
fg_mask = backSub.apply(frame)
|
| 41 |
+
# print(fg_mask.shape)
|
| 42 |
+
# cv2.imshow('Frame_bg', fg_mask)
|
| 43 |
+
|
| 44 |
+
# apply global threshol to remove shadows
|
| 45 |
+
retval, mask_thresh = cv2.threshold(
|
| 46 |
+
fg_mask, 180, 255, cv2.THRESH_BINARY)
|
| 47 |
+
# cv2.imshow('frame_thresh', mask_thresh)
|
| 48 |
+
|
| 49 |
+
# set the kernal
|
| 50 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
| 51 |
+
# Apply erosion
|
| 52 |
+
mask_eroded = cv2.morphologyEx(mask_thresh, cv2.MORPH_OPEN, kernel)
|
| 53 |
+
# cv2.imshow('frame_erode', mask_eroded)
|
| 54 |
+
|
| 55 |
+
# Find contours
|
| 56 |
+
contours, hierarchy = cv2.findContours(
|
| 57 |
+
mask_eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 58 |
+
# print(contours)
|
| 59 |
+
|
| 60 |
+
min_contour_area = 1000 # Define your minimum area threshold
|
| 61 |
+
large_contours = [
|
| 62 |
+
cnt for cnt in contours if cv2.contourArea(cnt) > min_contour_area]
|
| 63 |
+
# frame_ct = cv2.drawContours(frame, large_contours, -1, (0, 255, 0), 2)
|
| 64 |
+
frame_out = frame.copy()
|
| 65 |
+
for cnt in large_contours:
|
| 66 |
+
# print(cnt.shape)
|
| 67 |
+
x, y, w, h = cv2.boundingRect(cnt)
|
| 68 |
+
frame_out = cv2.rectangle(
|
| 69 |
+
frame, (x, y), (x+w, y+h), (0, 0, 200), 3)
|
| 70 |
+
frame_out_display = cv2.cvtColor(frame_out, cv2.COLOR_BGR2RGB)
|
| 71 |
+
vid = out.write(frame_out)
|
| 72 |
+
|
| 73 |
+
# Display the resulting frame
|
| 74 |
+
# cv2.imshow('Frame_final', frame_out)
|
| 75 |
+
|
| 76 |
+
# update the count every frame and display every 12th frame
|
| 77 |
+
if not count % 12:
|
| 78 |
+
yield frame_out_display, None
|
| 79 |
+
count += 1
|
| 80 |
+
|
| 81 |
+
# Press Q on keyboard to exit
|
| 82 |
+
if cv2.waitKey(25) & 0xFF == ord('q'):
|
| 83 |
+
break
|
| 84 |
+
else:
|
| 85 |
+
break
|
| 86 |
+
|
| 87 |
+
# When everything done, release the video capture and writer object
|
| 88 |
+
cap.release()
|
| 89 |
+
out.release()
|
| 90 |
+
# Closes all the frames
|
| 91 |
+
cv2.destroyAllWindows()
|
| 92 |
+
yield None, output_video
|
| 93 |
+
|
| 94 |
+
# vid_inf(input_video)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# gradio interface
|
| 98 |
+
input_video = gr.Video(label="Input Video")
|
| 99 |
+
output_frames = gr.Image(label="Output Frames")
|
| 100 |
+
output_video_file = gr.Video(label="Output video")
|
| 101 |
+
# sample_video=r'sample/car.mp4'
|
| 102 |
+
|
| 103 |
+
app = gr.Interface(
|
| 104 |
+
fn=vid_inf,
|
| 105 |
+
inputs=[input_video],
|
| 106 |
+
outputs=[output_frames, output_video_file],
|
| 107 |
+
title=f"MotionScope",
|
| 108 |
+
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.',
|
| 109 |
+
allow_flagging="never",
|
| 110 |
+
examples=[["sample/car.mp4"]],
|
| 111 |
+
)
|
| 112 |
+
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
|