Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
import cv2
|
| 4 |
+
import PIL
|
| 5 |
+
|
| 6 |
+
def get_frame_id_from_pct(v, pct):
|
| 7 |
+
return int(v.get(cv2.CAP_PROP_FRAME_COUNT) * pct/100)
|
| 8 |
+
|
| 9 |
+
def frame2pil(frame):
|
| 10 |
+
return PIL.Image.fromarray(frame, mode="RGB")
|
| 11 |
+
|
| 12 |
+
def extract_frame(pct, video):
|
| 13 |
+
v = cv2.VideoCapture(video)
|
| 14 |
+
frame_at = get_frame_id_from_pct(v, pct)
|
| 15 |
+
v.set(cv2.CAP_PROP_POS_FRAMES, frame_at)
|
| 16 |
+
ret, frame = v.read()
|
| 17 |
+
return frame2pil(frame)
|
| 18 |
+
|
| 19 |
+
def process_video(pct, video):
|
| 20 |
+
|
| 21 |
+
return extract_frame(pct, video)
|
| 22 |
+
|
| 23 |
+
app = gr.Interface(fn=process_video,
|
| 24 |
+
inputs=[gr.Number(label="PCT", ) ,gr.PlayableVideo()]
|
| 25 |
+
, outputs='image')
|
| 26 |
+
app.launch()
|