Medha Sawhney
commited on
Commit
·
cc2adeb
1
Parent(s):
2244fb0
video playback check
Browse files- .gitignore +8 -0
- app.py +196 -0
- requirements.txt +5 -0
.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.ipynb
|
2 |
+
ipynb_checkpoints/
|
3 |
+
*.mp4
|
4 |
+
*.avi
|
5 |
+
flagged/
|
6 |
+
frames/
|
7 |
+
diff_frames/
|
8 |
+
*.wmv
|
app.py
ADDED
@@ -0,0 +1,196 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import shutil
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
# def process_video(input_video):
|
9 |
+
# # Create a directory to store frames
|
10 |
+
# frames_dir = Path("frames")
|
11 |
+
# frames_dir.mkdir(exist_ok=True)
|
12 |
+
|
13 |
+
# # Read the video
|
14 |
+
# cap = cv2.VideoCapture(input_video)
|
15 |
+
# count = 0
|
16 |
+
# frames = []
|
17 |
+
|
18 |
+
# while True:
|
19 |
+
# ret, frame = cap.read()
|
20 |
+
# if not ret:
|
21 |
+
# break
|
22 |
+
# frame_path = str(frames_dir / f"frame_{count}.jpg")
|
23 |
+
# cv2.imwrite(frame_path, frame)
|
24 |
+
# frames.append(frame)
|
25 |
+
# count += 1
|
26 |
+
|
27 |
+
# # Close video file
|
28 |
+
# cap.release()
|
29 |
+
|
30 |
+
# # Process the first and the last frame (simple example: convert to grayscale)
|
31 |
+
# if frames:
|
32 |
+
# frames[0] = cv2.cvtColor(frames[0], cv2.COLOR_BGR2GRAY)
|
33 |
+
# frames[-1] = cv2.cvtColor(frames[-1], cv2.COLOR_BGR2GRAY)
|
34 |
+
|
35 |
+
# # Create a processed video
|
36 |
+
# output_video_path = "processed_video.mp4"
|
37 |
+
# height, width = frames[0].shape[:2]
|
38 |
+
# fourcc = cv2.VideoWriter_fourcc(*'mp4v') # or use 'XVID' if mp4 does not work
|
39 |
+
# out = cv2.VideoWriter(output_video_path, fourcc, 60.0, (width, height), isColor=len(frames[0].shape) > 2)
|
40 |
+
|
41 |
+
# for frame in frames:
|
42 |
+
# out.write(frame)
|
43 |
+
|
44 |
+
# out.release()
|
45 |
+
# return output_video_path
|
46 |
+
|
47 |
+
|
48 |
+
def process_video(input_video):
|
49 |
+
# Create a directory to store difference frames
|
50 |
+
diff_frames_dir = Path("diff_frames")
|
51 |
+
if os.path.exists(diff_frames_dir):
|
52 |
+
shutil.rmtree(diff_frames_dir)
|
53 |
+
diff_frames_dir.mkdir(exist_ok=True)
|
54 |
+
|
55 |
+
frames_dir = Path("frames")
|
56 |
+
if os.path.exists(frames_dir):
|
57 |
+
shutil.rmtree(frames_dir)
|
58 |
+
frames_dir.mkdir(exist_ok=True)
|
59 |
+
|
60 |
+
# Read the video
|
61 |
+
cap = cv2.VideoCapture(input_video)
|
62 |
+
count = 0
|
63 |
+
diff_frames = []
|
64 |
+
prev_frame = None
|
65 |
+
|
66 |
+
while True:
|
67 |
+
ret, frame = cap.read()
|
68 |
+
if not ret:
|
69 |
+
break
|
70 |
+
|
71 |
+
# Convert frame to grayscale
|
72 |
+
gray_frame = frame #cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
73 |
+
|
74 |
+
frame_path = str(frames_dir / f"frame_{count}.jpg")
|
75 |
+
cv2.imwrite(frame_path, frame)
|
76 |
+
|
77 |
+
if prev_frame is not None:
|
78 |
+
# Calculate difference with the previous frame
|
79 |
+
diff = cv2.absdiff(prev_frame, gray_frame)
|
80 |
+
diff_frames.append(gray_frame)
|
81 |
+
|
82 |
+
# Save difference frame
|
83 |
+
diff_frame_path = str(diff_frames_dir / f"diff_frame_{count}.jpg")
|
84 |
+
cv2.imwrite(diff_frame_path, diff)
|
85 |
+
else:
|
86 |
+
# For the first frame, there is no previous frame to compare
|
87 |
+
diff_frames.append(gray_frame)
|
88 |
+
|
89 |
+
prev_frame = gray_frame
|
90 |
+
count += 1
|
91 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
92 |
+
print(fps)
|
93 |
+
size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
|
94 |
+
print(size)
|
95 |
+
# Close video file
|
96 |
+
cap.release()
|
97 |
+
|
98 |
+
# Create a video from the difference frames
|
99 |
+
output_video_path = "diff_video.mp4"
|
100 |
+
if diff_frames:
|
101 |
+
print("Here")
|
102 |
+
height, width = diff_frames[0].shape[:2]
|
103 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Use 'mp4v' for MP4 or 'XVID' for AVI
|
104 |
+
out = cv2.VideoWriter(output_video_path, fourcc, fps, size, isColor=True)
|
105 |
+
|
106 |
+
for diff_frame in diff_frames:
|
107 |
+
# Need to convert single channel image to three channels to write with VideoWriter
|
108 |
+
# three_channel_frame = cv2.cvtColor(diff_frame, cv2.COLOR_GRAY2BGR)
|
109 |
+
out.write(diff_frame)
|
110 |
+
|
111 |
+
out.release()
|
112 |
+
|
113 |
+
return output_video_path
|
114 |
+
|
115 |
+
def show_preds_video(input_video):
|
116 |
+
cap = cv2.VideoCapture(input_video)
|
117 |
+
while(cap.isOpened()):
|
118 |
+
ret, frame = cap.read()
|
119 |
+
if ret:
|
120 |
+
frame_copy = frame.copy()
|
121 |
+
|
122 |
+
yield frame_copy
|
123 |
+
|
124 |
+
|
125 |
+
# # Define Gradio Interface
|
126 |
+
# iface = gr.Interface(
|
127 |
+
# fn=process_video,
|
128 |
+
# inputs=gr.Video(label="Upload a Video"),
|
129 |
+
# outputs=gr.Video(label="Processed Video"),
|
130 |
+
# title="Video Frame Processor",
|
131 |
+
# description="Upload a video to split it into frames, process the first and last frame, and return the video."
|
132 |
+
# )
|
133 |
+
|
134 |
+
# # Define Gradio Interface
|
135 |
+
# iface = gr.Interface(
|
136 |
+
# fn=show_preds_video,
|
137 |
+
# inputs=gr.Video(label="Upload a Video"),
|
138 |
+
# outputs=[gr.components.Image(type="numpy", label="Output Image")],
|
139 |
+
# cache_examples=False,
|
140 |
+
# title="Video Frame Processor",
|
141 |
+
# description="Upload a video to split it into frames, process the first and last frame, and return the video."
|
142 |
+
# )
|
143 |
+
|
144 |
+
|
145 |
+
# if __name__ == "__main__":
|
146 |
+
# iface.launch(share=True)
|
147 |
+
|
148 |
+
|
149 |
+
# # import spaces
|
150 |
+
import gradio as gr
|
151 |
+
import cv2
|
152 |
+
import numpy as np
|
153 |
+
import time
|
154 |
+
import random
|
155 |
+
from PIL import Image
|
156 |
+
# from transparent_background import Remover
|
157 |
+
|
158 |
+
# @spaces.GPU()
|
159 |
+
def doo(video, mode, progress=gr.Progress()):
|
160 |
+
cap = cv2.VideoCapture(video)
|
161 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # Get total frames
|
162 |
+
writer = None
|
163 |
+
tmpname = random.randint(111111111, 999999999)
|
164 |
+
processed_frames = 0
|
165 |
+
|
166 |
+
while cap.isOpened():
|
167 |
+
ret, frame = cap.read()
|
168 |
+
|
169 |
+
if ret is False:
|
170 |
+
break
|
171 |
+
|
172 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
173 |
+
img = Image.fromarray(frame).convert('RGB')
|
174 |
+
|
175 |
+
if writer is None:
|
176 |
+
writer = cv2.VideoWriter(str(tmpname) + '.mp4', cv2.VideoWriter_fourcc(*'mp4v'), cap.get(cv2.CAP_PROP_FPS), img.size)
|
177 |
+
|
178 |
+
processed_frames += 1
|
179 |
+
print(f"Processing frame {processed_frames}")
|
180 |
+
progress(processed_frames / total_frames, desc=f"Processing frame {processed_frames}/{total_frames}")
|
181 |
+
out = img
|
182 |
+
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
183 |
+
|
184 |
+
cap.release()
|
185 |
+
writer.release()
|
186 |
+
return str(tmpname) + '.mp4'
|
187 |
+
|
188 |
+
title = "🎞️ Video Tool 🎥"
|
189 |
+
|
190 |
+
iface = gr.Interface(
|
191 |
+
fn=doo,
|
192 |
+
inputs="video",
|
193 |
+
outputs="video",
|
194 |
+
title=title
|
195 |
+
)
|
196 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
torchvision
|
4 |
+
opencv-python
|
5 |
+
tqdm
|