AndreySokolov01 commited on
Commit
ec2de63
·
verified ·
1 Parent(s): f9f8c19

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -33
app.py CHANGED
@@ -1,41 +1,24 @@
1
- pip install opencv-python
2
- import cv2
3
- import os
4
  import gradio as gr
5
 
6
- def extract_clip(video_path, clip_start, clip_duration):
7
- video = cv2.VideoCapture(video_path)
8
-
9
- success, frame = video.read()
10
- count = 0
11
- frames = []
12
- while success:
13
- current_time = video.get(cv2.CAP_PROP_POS_MSEC) / 1000
14
-
15
- # Seek to the desired starting point
16
- if current_time < clip_start:
17
- video.set(cv2.CAP_PROP_POS_MSEC, clip_start * 1000)
18
- success, frame = video.read()
19
- continue
20
-
21
- # Check duration after reaching the clip_start timestamp
22
- elif current_time - clip_start <= clip_duration:
23
- frames.append(frame)
24
-
25
- success, frame = video.read()
26
- count += 1
27
-
28
- else:
29
- break
30
 
31
- video.release()
32
- return frames
33
 
34
  def display_frames(frames):
35
- num_cols = 4 # Number of columns in the image grid
36
- num_rows = (len(frames) + num_cols - 1) // num_cols
37
- grid_image = cv2.hconcat([cv2.hconcat(frames[i:i+num_cols]) for i in range(0, len(frames), num_cols)])
38
- return grid_image
39
 
40
  # Create Gradio interface
41
  video_input = gr.inputs.Video(type="file")
 
1
+ import subprocess
2
+ import sys
 
3
  import gradio as gr
4
 
5
+ def install_opencv():
6
+ try:
7
+ import cv2
8
+ print("OpenCV is already installed.")
9
+ except ImportError:
10
+ print("Installing OpenCV...")
11
+ subprocess.check_call([sys.executable, "-m", "pip", "install", "opencv-python"])
12
+ print("OpenCV installation completed.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ def extract_clip(video_path, clip_start, clip_duration):
15
+ # ... (rest of your extract_clip function)
16
 
17
  def display_frames(frames):
18
+ # ... (rest of your display_frames function)
19
+
20
+ # Install OpenCV if not already installed
21
+ install_opencv()
22
 
23
  # Create Gradio interface
24
  video_input = gr.inputs.Video(type="file")