RCaz commited on
Commit
e8d1963
·
1 Parent(s): 7bd4c94

added check of temp files

Browse files
Files changed (3) hide show
  1. app.py +7 -3
  2. check_image_extraction.py +42 -0
  3. utils.py +6 -8
app.py CHANGED
@@ -1,14 +1,18 @@
1
  import gradio as gr
2
  from textblob import TextBlob
3
- import utils
4
  import tempfile
5
 
6
 
7
  def answer_video_question(query : str, url : str, file : bytes) -> dict:
8
  # Either `file` or `url` must be provided
 
 
 
 
9
  if file is not None:
10
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_vid:
11
- temp_vid.write(file.read())
12
  temp_video_path = temp_vid.name
13
 
14
  # Output frame folder
@@ -31,7 +35,7 @@ demo = gr.Interface(
31
  inputs=[
32
  gr.Textbox(placeholder="Enter Query about the movie", label="Query"),
33
  gr.Textbox(placeholder="Paste the URL of the movie", label="Movie URL (optional)"),
34
- gr.File(label="Upload Movie File (optional)")
35
  ],
36
  outputs=gr.JSON(),
37
  title="Text Sentiment Analysis",
 
1
  import gradio as gr
2
  from textblob import TextBlob
3
+ from utils import *
4
  import tempfile
5
 
6
 
7
  def answer_video_question(query : str, url : str, file : bytes) -> dict:
8
  # Either `file` or `url` must be provided
9
+
10
+ output_path='/tmp/video'
11
+ if not os.path.exists(output_path):
12
+ os.mkdir(output_path)
13
  if file is not None:
14
  with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_vid:
15
+ temp_vid.write(file)
16
  temp_video_path = temp_vid.name
17
 
18
  # Output frame folder
 
35
  inputs=[
36
  gr.Textbox(placeholder="Enter Query about the movie", label="Query"),
37
  gr.Textbox(placeholder="Paste the URL of the movie", label="Movie URL (optional)"),
38
+ gr.File(label="Upload Movie File (optional)",type='binary')
39
  ],
40
  outputs=gr.JSON(),
41
  title="Text Sentiment Analysis",
check_image_extraction.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+
4
+ # Get all images in the folder, sorted by filename
5
+ image_dir = "/tmp/video/frames"
6
+ image_paths = sorted([
7
+ os.path.join(image_dir, f)
8
+ for f in os.listdir(image_dir)
9
+ if f.endswith(".jpg")
10
+ ])
11
+
12
+ def get_image(index):
13
+ # Make sure the index is within bounds
14
+ if 0 <= index < len(image_paths):
15
+ return image_paths[index], index
16
+ return None, index # No change if out of bounds
17
+
18
+ def next_image(current_index):
19
+ next_index = current_index + 1
20
+ return get_image(next_index)
21
+
22
+ def previous_image(current_index):
23
+ prev_index = current_index - 1
24
+ return get_image(prev_index)
25
+
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown("### Browse Keyframes")
28
+
29
+ image = gr.Image()
30
+ index_state = gr.State(0) # Track current index
31
+
32
+ with gr.Row():
33
+ prev_btn = gr.Button("Previous")
34
+ next_btn = gr.Button("Next")
35
+
36
+ next_btn.click(fn=next_image, inputs=index_state, outputs=[image, index_state])
37
+ prev_btn.click(fn=previous_image, inputs=index_state, outputs=[image, index_state])
38
+
39
+ # Initialize with first image
40
+ demo.load(fn=get_image, inputs=index_state, outputs=[image, index_state])
41
+
42
+ demo.launch()
utils.py CHANGED
@@ -14,12 +14,12 @@ def download_video(url):
14
  """
15
 
16
  # instanciate output path
17
- output_path='./cache'
18
  if not os.path.exists(output_path):
19
  os.mkdir(output_path)
20
 
21
  # get cookies
22
- export_cookies_path = "./cache/exported_cookies.txt"
23
  os.makedirs(os.path.dirname(export_cookies_path), exist_ok=True)
24
  try:
25
  ydl_opts_export_cookies = {
@@ -72,7 +72,7 @@ def download_video(url):
72
 
73
 
74
 
75
- def is_significantly_different(img1, img2, threshold=0.4):
76
  """Check if two images are significantly different using SSIM.
77
  Args:
78
  img1 (numpy.ndarray): First image.
@@ -104,7 +104,7 @@ def extract_keyframes(video_path, diff_threshold=0.4):
104
  print("Failed to read video.")
105
  return
106
 
107
- output_path='./cache/video/frames'
108
  if not os.path.exists(output_path):
109
  os.mkdir(output_path)
110
 
@@ -115,14 +115,12 @@ def extract_keyframes(video_path, diff_threshold=0.4):
115
  frame_id += 1
116
 
117
  if is_significantly_different(prev_frame, frame, threshold=diff_threshold):
118
- filename = os.path.join("./cache/video/frames/",f"keyframe_{saved_id:04d}.jpg")
119
  cv2.imwrite(filename, frame)
120
  prev_frame = frame
121
  saved_id += 1
 
122
 
123
  cap.release()
124
  print(f"Extracted {saved_id} key frames.")
125
  return "success"
126
-
127
- # Example usage
128
- extract_keyframes(video_path)
 
14
  """
15
 
16
  # instanciate output path
17
+ output_path='/tmp'
18
  if not os.path.exists(output_path):
19
  os.mkdir(output_path)
20
 
21
  # get cookies
22
+ export_cookies_path = "/tmp/exported_cookies.txt"
23
  os.makedirs(os.path.dirname(export_cookies_path), exist_ok=True)
24
  try:
25
  ydl_opts_export_cookies = {
 
72
 
73
 
74
 
75
+ def is_significantly_different(img1, img2, threshold=0.1):
76
  """Check if two images are significantly different using SSIM.
77
  Args:
78
  img1 (numpy.ndarray): First image.
 
104
  print("Failed to read video.")
105
  return
106
 
107
+ output_path='/tmp/video/frames'
108
  if not os.path.exists(output_path):
109
  os.mkdir(output_path)
110
 
 
115
  frame_id += 1
116
 
117
  if is_significantly_different(prev_frame, frame, threshold=diff_threshold):
118
+ filename = os.path.join("/tmp/video/frames/",f"keyframe_{saved_id:04d}.jpg")
119
  cv2.imwrite(filename, frame)
120
  prev_frame = frame
121
  saved_id += 1
122
+ print(f"frame{saved_id} saved")
123
 
124
  cap.release()
125
  print(f"Extracted {saved_id} key frames.")
126
  return "success"