EthanStanks commited on
Commit
7137256
·
1 Parent(s): 3bab9e9

Removed YouTube Downloader

Browse files
Files changed (3) hide show
  1. Dockerfile +0 -2
  2. app.py +30 -9
  3. app_backend.py +40 -64
Dockerfile CHANGED
@@ -21,8 +21,6 @@ RUN chmod -R a+r /app
21
  COPY . /app
22
 
23
  RUN pip install gradio
24
- RUN pip install yt-dlp
25
- RUN pip install pytube
26
 
27
  EXPOSE 7860
28
  ENV PYTHONUNBUFFERED=1 \
 
21
  COPY . /app
22
 
23
  RUN pip install gradio
 
 
24
 
25
  EXPOSE 7860
26
  ENV PYTHONUNBUFFERED=1 \
app.py CHANGED
@@ -3,8 +3,8 @@ import app_backend
3
 
4
  gr.set_static_paths(['.'])
5
 
6
- def detect_crashes(name, sim_paths, real_path, frame_out, text_output, file_out, video_out):
7
- for outputs in app_backend.backend(name, sim_paths, real_path, frame_out, text_output, file_out, video_out):
8
  yield outputs
9
 
10
  with gr.Blocks(title="Drone Crash Detection") as demo:
@@ -13,18 +13,39 @@ with gr.Blocks(title="Drone Crash Detection") as demo:
13
  video_output = gr.Gallery(interactive=False)
14
  file_output = gr.File(interactive=False)
15
 
16
- label = gr.Label("Drone Crash Classification Using Yolov8.", scale=0,show_label=False)
17
 
18
- student_name_tb = gr.Textbox(lines=1, placeholder="Enter student's name. Ex: \'First Last\'", visible=True, show_label=False)
19
- simulation_videos_tb = gr.Textbox(lines=1, placeholder="Enter simulation flying videos. Ex: \'https://youtu.be/Gd12g9XqSo4,https://youtu.be/Vzj78FsgQZg,...,https://youtu.be/gf7cp6_XFvQ\'", visible=True, show_label=False)
20
- real_videos_tb = gr.Textbox(lines=1, placeholder="Enter real flying videos. Ex: \'https://youtu.be/Gd12g9XqSo4,https://youtu.be/Vzj78FsgQZg,...,https://youtu.be/gf7cp6_XFvQ\'", visible=True, show_label=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  with gr.Row():
22
  btn_prompt = gr.Button("Detect Crashes")
23
  btn_prompt.click(
24
  fn=detect_crashes,
25
- inputs=[student_name_tb, simulation_videos_tb, real_videos_tb, frame_output, text_output, file_output, video_output],
 
 
 
 
 
 
 
 
26
  outputs=[frame_output, text_output, file_output, video_output]
27
  )
28
 
29
-
30
- demo.launch()
 
3
 
4
  gr.set_static_paths(['.'])
5
 
6
+ def detect_crashes(name, sim_files, real_files, frame_out, text_output, file_out, video_out):
7
+ for outputs in app_backend.backend(name, sim_files, real_files, frame_out, text_output, file_out, video_out):
8
  yield outputs
9
 
10
  with gr.Blocks(title="Drone Crash Detection") as demo:
 
13
  video_output = gr.Gallery(interactive=False)
14
  file_output = gr.File(interactive=False)
15
 
16
+ label = gr.Label("Drone Crash Classification Using Yolov8.", scale=0, show_label=False)
17
 
18
+ student_name_tb = gr.Textbox(
19
+ lines=1,
20
+ placeholder="Enter student's name. Ex: 'First Last'",
21
+ visible=True,
22
+ show_label=True,
23
+ label="Student's Name"
24
+ )
25
+ simulation_videos_upload = gr.File(
26
+ label="Upload Simulation Drone Videos",
27
+ type="file",
28
+ file_count="multiple"
29
+ )
30
+ real_videos_upload = gr.File(
31
+ label="Upload Real Drone Videos",
32
+ type="file",
33
+ file_count="multiple"
34
+ )
35
  with gr.Row():
36
  btn_prompt = gr.Button("Detect Crashes")
37
  btn_prompt.click(
38
  fn=detect_crashes,
39
+ inputs=[
40
+ student_name_tb,
41
+ simulation_videos_upload,
42
+ real_videos_upload,
43
+ frame_output,
44
+ text_output,
45
+ file_output,
46
+ video_output
47
+ ],
48
  outputs=[frame_output, text_output, file_output, video_output]
49
  )
50
 
51
+ demo.launch()
 
app_backend.py CHANGED
@@ -1,9 +1,7 @@
1
- import model.video_classifier as classifier
2
  import os
3
  import csv
4
- from yt_dlp import YoutubeDL
5
  from dataclasses import dataclass
6
- import pytube
7
 
8
  device = 'cpu'
9
  model_path = './model/best.pt'
@@ -17,57 +15,15 @@ class VideoResult:
17
  video_path: str
18
  label_counts: dict
19
  crash_events: list
20
- video_link: str
21
  is_simulation: int
22
 
23
- def parse_inputs(student_name_tb, simulation_videos_tb, real_videos_tb):
24
- simulation_video_list = [url.strip() for url in simulation_videos_tb.split(',') if url.strip()]
25
- real_video_list = [url.strip() for url in real_videos_tb.split(',') if url.strip()]
26
- return student_name_tb.strip(), simulation_video_list, real_video_list
27
-
28
- def download_youtube_video(youtube_url,rename):
29
- try:
30
- yt = pytube.YouTube(youtube_url)
31
- video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
32
- video.download(f'{video_folder}')
33
- original_filename = video.default_filename
34
- os.rename(original_filename, rename)
35
- return new_filename
36
-
37
- except Exception as e:
38
- print("An error occurred:", e)
39
- return None
40
-
41
-
42
- def download_videos(simulation_links, real_links):
43
- simulation_paths = []
44
- real_paths = []
45
- for i, url in enumerate(simulation_links, start=1):
46
- #ydl_opts = {
47
- # 'format': 'best',
48
- # 'outtmpl': f'{video_folder}/sim_video{i}.%(ext)s'
49
- #}
50
- #with YoutubeDL(ydl_opts) as ydl:
51
- # ydl.download([url])
52
- download_youtube_video(url,f"sim_video{i}.mp4")
53
- simulation_paths.append(f'{video_folder}/sim_video{i}.mp4')
54
- for i, url in enumerate(real_links, start=1):
55
- #ydl_opts = {
56
- # 'format': 'best',
57
- # 'outtmpl': f'{video_folder}/real_video{i}.%(ext)s'
58
- #}
59
- #with YoutubeDL(ydl_opts) as ydl:
60
- # ydl.download([url])
61
- download_youtube_video(url,f"real_video{i}.mp4")
62
- real_paths.append(f'{video_folder}/real_video{i}.mp4')
63
- return simulation_paths, real_paths
64
-
65
  def result_csv(student, simulation_results, real_results):
66
  results = simulation_results + real_results
67
  file_name = 'crash_results.csv'
68
  csv_file = os.path.join(output_folder, file_name)
69
  csv_columns = [
70
- 'Student', 'YouTubeLink', 'IsSimulation', 'UniqueCrashes', 'CrashFrames', 'FlightFrames',
71
  'NoDroneFrames', 'NoSignalFrames', 'NoStartedFrames', 'StartedFrames', 'UnstableFrames',
72
  'LandingFrames', 'CrashTimes', 'CrashDurations'
73
  ]
@@ -94,7 +50,7 @@ def result_csv(student, simulation_results, real_results):
94
 
95
  writer.writerow({
96
  'Student': student,
97
- 'YouTubeLink': result.video_link,
98
  'IsSimulation': result.is_simulation,
99
  'UniqueCrashes': unique_crashes,
100
  'CrashFrames': result.label_counts.get("Crash", 0),
@@ -110,12 +66,6 @@ def result_csv(student, simulation_results, real_results):
110
  })
111
  return csv_file
112
 
113
- def delete_youtube_videos(simulation_paths, real_paths):
114
- all_video_paths = simulation_paths + real_paths
115
- for video_file in all_video_paths:
116
- if os.path.exists(video_file):
117
- os.remove(video_file)
118
-
119
  def remove_videos_and_csv(folder_path):
120
  video_extensions = {'.mp4'}
121
  csv_extension = '.csv'
@@ -126,20 +76,45 @@ def remove_videos_and_csv(folder_path):
126
  if extension.lower() in video_extensions or extension.lower() == csv_extension:
127
  os.remove(file_path)
128
 
129
- def backend(student_name_tb, simulation_videos_tb, real_videos_tb, frame_output, text_output, file_output, video_output):
 
 
 
 
 
 
130
  yield frame_output, "Starting process...", file_output, video_output
131
  remove_videos_and_csv(output_folder)
132
- yield frame_output, "Parsing Input...", file_output, video_output
133
- name, simulation_links, real_links = parse_inputs(student_name_tb, simulation_videos_tb, real_videos_tb)
134
- yield frame_output, "Downloading YouTube Videos...", file_output, video_output
135
- simulation_paths, real_paths = download_videos(simulation_links, real_links)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  simulation_results = []
138
  real_results = []
139
  output_video_paths = []
140
 
141
- for i, path in enumerate(simulation_paths, start=1): # for each sim video
142
- # setup output paths
 
143
  crash_video_filename = f'Simulation_Video_{i}_Crashes.mp4'
144
  crash_video_path = os.path.join(output_folder, crash_video_filename)
145
  labeled_video_filename = f'Simulation_Video_{i}_Labeled.mp4'
@@ -163,7 +138,7 @@ def backend(student_name_tb, simulation_videos_tb, real_videos_tb, frame_output,
163
  video_path=path,
164
  label_counts=label_counts,
165
  crash_events=crash_events,
166
- video_link=simulation_links[i - 1],
167
  is_simulation=1))
168
  break
169
 
@@ -192,12 +167,13 @@ def backend(student_name_tb, simulation_videos_tb, real_videos_tb, frame_output,
192
  video_path=path,
193
  label_counts=label_counts,
194
  crash_events=crash_events,
195
- video_link=real_links[i - 1],
196
  is_simulation=0))
197
  break
198
 
199
  yield frame_output, "Outputting Results to CSV...", file_output, video_output
200
  csv_file = result_csv(name, simulation_results, real_results)
201
  yield frame_output, "Finishing process...", file_output, video_output
202
- delete_youtube_videos(simulation_paths, real_paths)
 
203
  yield frame_output, "Download Ready.", csv_file, output_video_paths
 
 
1
  import os
2
  import csv
 
3
  from dataclasses import dataclass
4
+ import model.video_classifier as classifier
5
 
6
  device = 'cpu'
7
  model_path = './model/best.pt'
 
15
  video_path: str
16
  label_counts: dict
17
  crash_events: list
18
+ video_name: str
19
  is_simulation: int
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def result_csv(student, simulation_results, real_results):
22
  results = simulation_results + real_results
23
  file_name = 'crash_results.csv'
24
  csv_file = os.path.join(output_folder, file_name)
25
  csv_columns = [
26
+ 'Student', 'VideoFileName', 'IsSimulation', 'UniqueCrashes', 'CrashFrames', 'FlightFrames',
27
  'NoDroneFrames', 'NoSignalFrames', 'NoStartedFrames', 'StartedFrames', 'UnstableFrames',
28
  'LandingFrames', 'CrashTimes', 'CrashDurations'
29
  ]
 
50
 
51
  writer.writerow({
52
  'Student': student,
53
+ 'VideoFileName': result.video_name,
54
  'IsSimulation': result.is_simulation,
55
  'UniqueCrashes': unique_crashes,
56
  'CrashFrames': result.label_counts.get("Crash", 0),
 
66
  })
67
  return csv_file
68
 
 
 
 
 
 
 
69
  def remove_videos_and_csv(folder_path):
70
  video_extensions = {'.mp4'}
71
  csv_extension = '.csv'
 
76
  if extension.lower() in video_extensions or extension.lower() == csv_extension:
77
  os.remove(file_path)
78
 
79
+ def delete_uploaded_videos(simulation_paths, real_paths):
80
+ all_video_paths = simulation_paths + real_paths
81
+ for video_file in all_video_paths:
82
+ if os.path.exists(video_file):
83
+ os.remove(video_file)
84
+
85
+ def backend(student_name_tb, simulation_files, real_files, frame_output, text_output, file_output, video_output):
86
  yield frame_output, "Starting process...", file_output, video_output
87
  remove_videos_and_csv(output_folder)
88
+ yield frame_output, "Processing Input...", file_output, video_output
89
+ name = student_name_tb.strip()
90
+ simulation_paths = []
91
+ real_paths = []
92
+
93
+ # Save the uploaded simulation videos to disk
94
+ for i, file in enumerate(simulation_files, start=1):
95
+ sim_video_path = os.path.join(video_folder, f'sim_video{i}.mp4')
96
+ with open(sim_video_path, 'wb') as f:
97
+ f.write(file.read())
98
+ simulation_paths.append(sim_video_path)
99
+
100
+ # Save the uploaded real videos to disk
101
+ for i, file in enumerate(real_files, start=1):
102
+ real_video_path = os.path.join(video_folder, f'real_video{i}.mp4')
103
+ with open(real_video_path, 'wb') as f:
104
+ f.write(file.read())
105
+ real_paths.append(real_video_path)
106
+
107
+ # Store original filenames
108
+ simulation_filenames = [file.name for file in simulation_files]
109
+ real_filenames = [file.name for file in real_files]
110
 
111
  simulation_results = []
112
  real_results = []
113
  output_video_paths = []
114
 
115
+ # Process simulation videos
116
+ for i, path in enumerate(simulation_paths, start=1):
117
+ # Setup output paths
118
  crash_video_filename = f'Simulation_Video_{i}_Crashes.mp4'
119
  crash_video_path = os.path.join(output_folder, crash_video_filename)
120
  labeled_video_filename = f'Simulation_Video_{i}_Labeled.mp4'
 
138
  video_path=path,
139
  label_counts=label_counts,
140
  crash_events=crash_events,
141
+ video_name=simulation_filenames[i - 1],
142
  is_simulation=1))
143
  break
144
 
 
167
  video_path=path,
168
  label_counts=label_counts,
169
  crash_events=crash_events,
170
+ video_name=real_filenames[i - 1],
171
  is_simulation=0))
172
  break
173
 
174
  yield frame_output, "Outputting Results to CSV...", file_output, video_output
175
  csv_file = result_csv(name, simulation_results, real_results)
176
  yield frame_output, "Finishing process...", file_output, video_output
177
+ # Delete the uploaded video files from the server
178
+ delete_uploaded_videos(simulation_paths, real_paths)
179
  yield frame_output, "Download Ready.", csv_file, output_video_paths