zoheb commited on
Commit
203a497
1 Parent(s): 68ac5b3

first commit

Browse files
Files changed (4) hide show
  1. .gitignore +3 -0
  2. .streamlit/config.toml +2 -0
  3. app.py +46 -18
  4. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ *.mp4
2
+ extracted_images/
3
+ modified_images/
.streamlit/config.toml ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ [server]
2
+ maxUploadSize = 50
app.py CHANGED
@@ -7,6 +7,7 @@ import torch
7
  import matplotlib.pyplot as plt
8
  from stqdm import stqdm
9
  from pathlib import Path
 
10
 
11
 
12
  # Load the model
@@ -19,6 +20,21 @@ model_pt = AutoModelForObjectDetection.from_pretrained(best_model_path).to(devic
19
  COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
20
  [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Convert Video to Frames
23
  def video_to_frames(video, dir):
24
  cap = cv2.VideoCapture(str(video))
@@ -35,7 +51,6 @@ def video_to_frames(video, dir):
35
  cap.release()
36
  #print (f"No. of frames {frame_count}")
37
 
38
-
39
  # for output bounding box post-processing
40
  def box_cxcywh_to_xyxy(x):
41
  x_c, y_c, w, h = x.unbind(1)
@@ -115,12 +130,18 @@ def frames_to_video(dir, path, fps=5):
115
  #print(filename)
116
  #inserting the frames into an image array
117
  frame_array.append(img)
118
- out = cv2.VideoWriter(str(path), cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
119
  for item in frame_array:
120
  # writing to a image array
121
  out.write(item)
122
  out.release()
123
 
 
 
 
 
 
 
124
 
125
  # Main
126
  if __name__=='__main__':
@@ -140,10 +161,11 @@ if __name__=='__main__':
140
  shutil.rmtree(MOD_DIR)
141
  MOD_DIR.mkdir(parents=True, exist_ok=True)
142
 
143
- generated_video = Path(BASE_DIR, "final_video.mp4")
 
144
 
145
  # Upload the video
146
- uploaded_file = st.file_uploader("Upload a small video containing Balloons", type=["mp4"])
147
  if uploaded_file is not None:
148
  st.video(uploaded_file)
149
  vid = uploaded_file.name
@@ -152,19 +174,25 @@ if __name__=='__main__':
152
  f.write(uploaded_file.read())
153
  uploaded_video = Path(BASE_DIR, vid)
154
 
155
- # Detect balloon in the frames and generate video
156
- try:
157
- video_to_frames(uploaded_video, FRAMES_DIR)
158
- predict_on_frames(FRAMES_DIR, MOD_DIR)
159
- frames_to_video(MOD_DIR, generated_video)
160
- st.success("Successfully Generated!!")
161
-
162
- # Video file Generated
163
- video_file = open(str(generated_video), 'rb')
164
- video_bytes = video_file.read()
165
- st.video(video_bytes)
166
- st.download_button('Download the Video', video_bytes, file_name=generated_video.name)
167
- except Exception as e:
168
- st.error(f"Could not convert the file due to {e}")
 
 
 
 
 
 
169
  else:
170
  st.info('File Not Uploaded Yet!!!')
 
7
  import matplotlib.pyplot as plt
8
  from stqdm import stqdm
9
  from pathlib import Path
10
+ from moviepy.editor import VideoFileClip
11
 
12
 
13
  # Load the model
 
20
  COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
21
  [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
22
 
23
+
24
+ # Edit video
25
+ def cut_video(clip=None):
26
+ with st.form("edit"):
27
+ duration = int(clip.duration)
28
+ st.write("Edit a small part of video")
29
+ start = st.sidebar.number_input('Start time (seconds):',max_value=duration)
30
+ end = st.sidebar.number_input('End time (seconds):',min_value=start+3,max_value=duration)
31
+ submitted = st.form_submit_button("Edit Out")
32
+ if submitted:
33
+ clip = clip.subclip(start, end)
34
+ clip.write_videofile("edit.mp4")
35
+ return clip
36
+
37
+
38
  # Convert Video to Frames
39
  def video_to_frames(video, dir):
40
  cap = cv2.VideoCapture(str(video))
 
51
  cap.release()
52
  #print (f"No. of frames {frame_count}")
53
 
 
54
  # for output bounding box post-processing
55
  def box_cxcywh_to_xyxy(x):
56
  x_c, y_c, w, h = x.unbind(1)
 
130
  #print(filename)
131
  #inserting the frames into an image array
132
  frame_array.append(img)
133
+ out = cv2.VideoWriter(str(path), cv2.VideoWriter_fourcc(*'mp4v'), fps, size)
134
  for item in frame_array:
135
  # writing to a image array
136
  out.write(item)
137
  out.release()
138
 
139
+ # Display video
140
+ def display(path):
141
+ video_file = open(str(path), 'rb')
142
+ video_bytes = video_file.read()
143
+ st.video(video_bytes)
144
+
145
 
146
  # Main
147
  if __name__=='__main__':
 
161
  shutil.rmtree(MOD_DIR)
162
  MOD_DIR.mkdir(parents=True, exist_ok=True)
163
 
164
+ edited_video = Path(BASE_DIR, "edit.mp4")
165
+ generated_video = Path(BASE_DIR, "balloons.mp4")
166
 
167
  # Upload the video
168
+ uploaded_file = st.sidebar.file_uploader("Upload a small video containing Balloons", type=["mp4", "mpeg"])
169
  if uploaded_file is not None:
170
  st.video(uploaded_file)
171
  vid = uploaded_file.name
 
174
  f.write(uploaded_file.read())
175
  uploaded_video = Path(BASE_DIR, vid)
176
 
177
+ clip = VideoFileClip(vid)
178
+ clip = cut_video(clip)
179
+
180
+ if clip is not None:
181
+ # Detect balloon in the frames and generate video
182
+ try:
183
+ st.info('View Edited Clip')
184
+ display(edited_video)
185
+ video_to_frames(edited_video, FRAMES_DIR)
186
+ predict_on_frames(FRAMES_DIR, MOD_DIR)
187
+ frames_to_video(MOD_DIR, generated_video)
188
+ st.success("Successfully Generated!!")
189
+
190
+ # Video file Generated
191
+ display(generated_video)
192
+ st.download_button('Download the Video', open(str(generated_video), 'rb').read(), file_name=generated_video.name)
193
+ except Exception as e:
194
+ st.error(f"Could not convert the file due to {e}")
195
+ else:
196
+ st.error("Please submit an edited clip.")
197
  else:
198
  st.info('File Not Uploaded Yet!!!')
requirements.txt CHANGED
@@ -5,3 +5,4 @@ matplotlib==3.6.1
5
  torch==1.12.1
6
  transformers==4.22.2
7
  stqdm==0.0.4
 
 
5
  torch==1.12.1
6
  transformers==4.22.2
7
  stqdm==0.0.4
8
+ moviepy==1.0.3