Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,144 +1,54 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
import
|
| 5 |
import os
|
| 6 |
-
|
| 7 |
|
| 8 |
-
def
|
| 9 |
-
""
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
end_frame = int(end_time * fps)
|
| 21 |
-
|
| 22 |
-
# စာတန်းတွေသိမ်းမယ့် list
|
| 23 |
-
subtitles = []
|
| 24 |
-
last_text = ""
|
| 25 |
-
current_start = start_time
|
| 26 |
-
|
| 27 |
-
# Frame အလိုက်ဖတ်မယ် (1 FPS)
|
| 28 |
-
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
|
| 29 |
-
|
| 30 |
-
for frame_num in range(start_frame, end_frame, int(fps)):
|
| 31 |
-
ret, frame = cap.read()
|
| 32 |
-
if not ret:
|
| 33 |
-
break
|
| 34 |
-
|
| 35 |
-
# ရွေးထားတဲ့နေရာကိုဖြတ်မယ်
|
| 36 |
-
h, w = frame.shape[:2]
|
| 37 |
-
x1 = int((box_x / 100) * w)
|
| 38 |
-
y1 = int((box_y / 100) * h)
|
| 39 |
-
x2 = int(((box_x + box_w) / 100) * w)
|
| 40 |
-
y2 = int(((box_y + box_h) / 100) * h)
|
| 41 |
-
|
| 42 |
-
crop = frame[y1:y2, x1:x2]
|
| 43 |
-
|
| 44 |
-
# ဒီနေရာမှာ OCR လုပ်ဖို့လိုမယ် (Tesseract/EasyOCR/PaddleOCR)
|
| 45 |
-
# ယခု ဥပမာအနေနဲ့ စာသားအတုပဲထုတ်ထားတယ်
|
| 46 |
-
text = f"Sample OCR text at {frame_num/fps:.1f}s"
|
| 47 |
-
|
| 48 |
-
# စာသားတူရင် merge လုပ်မယ်
|
| 49 |
-
if text and text != last_text:
|
| 50 |
-
if last_text:
|
| 51 |
-
subtitles.append({
|
| 52 |
-
'start': current_start,
|
| 53 |
-
'end': frame_num / fps,
|
| 54 |
-
'text': last_text
|
| 55 |
-
})
|
| 56 |
-
current_start = frame_num / fps
|
| 57 |
-
last_text = text
|
| 58 |
-
|
| 59 |
-
# နောက်ဆုံးစာတန်းကိုထည့်မယ်
|
| 60 |
-
if last_text:
|
| 61 |
-
subtitles.append({
|
| 62 |
-
'start': current_start,
|
| 63 |
-
'end': end_time,
|
| 64 |
-
'text': last_text
|
| 65 |
-
})
|
| 66 |
-
|
| 67 |
-
cap.release()
|
| 68 |
-
|
| 69 |
-
# SRT format ပြောင်းမယ်
|
| 70 |
srt_content = ""
|
| 71 |
for i, sub in enumerate(subtitles, 1):
|
| 72 |
-
|
| 73 |
-
end = format_time(sub['end'])
|
| 74 |
-
srt_content += f"{i}\n{start} --> {end}\n{sub['text']}\n\n"
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
srt_path = "
|
| 78 |
-
with open(srt_path, "w"
|
| 79 |
f.write(srt_content)
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
|
| 84 |
-
return srt_path,
|
| 85 |
-
|
| 86 |
-
def format_time(seconds):
|
| 87 |
-
"""အချိန်ကို SRT format (HH:MM:SS,mmm) ပြောင်းမယ်"""
|
| 88 |
-
h = int(seconds // 3600)
|
| 89 |
-
m = int((seconds % 3600) // 60)
|
| 90 |
-
s = int(seconds % 60)
|
| 91 |
-
ms = int((seconds - int(seconds)) * 1000)
|
| 92 |
-
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
with gr.Blocks(
|
| 96 |
-
gr.Markdown("#
|
| 97 |
-
gr.Markdown("Video ထဲက hardcoded စာတန်းတွေကို ထုတ်ယူပါ။")
|
| 98 |
|
| 99 |
with gr.Row():
|
| 100 |
-
with gr.Column(
|
| 101 |
-
# Video Input
|
| 102 |
video_input = gr.Video(label="Upload Video")
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
language = gr.Dropdown(
|
| 106 |
-
choices=["English", "Thai", "Japanese", "Chinese (Simplified)", "Chinese (Traditional)"],
|
| 107 |
-
label="Language",
|
| 108 |
-
value="English"
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
# Time Range
|
| 112 |
-
with gr.Row():
|
| 113 |
-
start_time = gr.Number(label="Start Time (s)", value=0)
|
| 114 |
-
end_time = gr.Number(label="End Time (s)", value=10)
|
| 115 |
-
|
| 116 |
-
# Subtitle Area (Box)
|
| 117 |
-
with gr.Accordion("Subtitle Area Adjustment", open=True):
|
| 118 |
-
gr.Markdown("စာတန်းရှိတဲ့နေရာကို ချိန်ညှိပါ")
|
| 119 |
-
box_x = gr.Slider(0, 100, value=10, label="X Position (%)")
|
| 120 |
-
box_y = gr.Slider(0, 100, value=80, label="Y Position (%)")
|
| 121 |
-
box_w = gr.Slider(0, 100, value=80, label="Width (%)")
|
| 122 |
-
box_h = gr.Slider(0, 100, value=15, label="Height (%)")
|
| 123 |
-
|
| 124 |
-
process_btn = gr.Button("Process Video", variant="primary")
|
| 125 |
|
| 126 |
-
with gr.Column(
|
| 127 |
-
|
| 128 |
-
video_output = gr.Video(label="Video Preview")
|
| 129 |
-
subtitle_output = gr.Dataframe(
|
| 130 |
-
headers=["Start", "End", "Text"],
|
| 131 |
-
label="Extracted Subtitles",
|
| 132 |
-
wrap=True
|
| 133 |
-
)
|
| 134 |
file_output = gr.File(label="Download SRT")
|
| 135 |
|
| 136 |
-
# Process function
|
| 137 |
process_btn.click(
|
| 138 |
fn=extract_subtitles,
|
| 139 |
-
inputs=[video_input, language,
|
| 140 |
outputs=[file_output, subtitle_output]
|
| 141 |
)
|
| 142 |
|
| 143 |
-
# Launch
|
| 144 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
import json
|
| 5 |
import os
|
| 6 |
+
import tempfile
|
| 7 |
|
| 8 |
+
def test_function(video):
|
| 9 |
+
return "Video uploaded: " + str(video)
|
| 10 |
+
|
| 11 |
+
def extract_subtitles(video, language, start, end, x, y, w, h):
|
| 12 |
+
# Create sample subtitles
|
| 13 |
+
subtitles = [
|
| 14 |
+
{"start": 0, "end": 2, "text": "Hello"},
|
| 15 |
+
{"start": 2, "end": 4, "text": "Welcome to video OCR"},
|
| 16 |
+
{"start": 4, "end": 6, "text": "This is a test"}
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
# Generate SRT
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
srt_content = ""
|
| 21 |
for i, sub in enumerate(subtitles, 1):
|
| 22 |
+
srt_content += f"{i}\n00:00:{sub['start']:02d},000 --> 00:00:{sub['end']:02d},000\n{sub['text']}\n\n"
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# Save files
|
| 25 |
+
srt_path = "subtitles.srt"
|
| 26 |
+
with open(srt_path, "w") as f:
|
| 27 |
f.write(srt_content)
|
| 28 |
|
| 29 |
+
# Display list
|
| 30 |
+
display_list = [[f"00:00:{s['start']:02d}", f"00:00:{s['end']:02d}", s['text']] for s in subtitles]
|
| 31 |
|
| 32 |
+
return srt_path, display_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Simple interface
|
| 35 |
+
with gr.Blocks(title="Video OCR") as demo:
|
| 36 |
+
gr.Markdown("# Video OCR Extractor")
|
|
|
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
+
with gr.Column():
|
|
|
|
| 40 |
video_input = gr.Video(label="Upload Video")
|
| 41 |
+
language = gr.Dropdown(["English", "Thai", "Japanese", "Chinese"], label="Language")
|
| 42 |
+
process_btn = gr.Button("Process", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
with gr.Column():
|
| 45 |
+
subtitle_output = gr.Dataframe(headers=["Start", "End", "Text"], label="Subtitles")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
file_output = gr.File(label="Download SRT")
|
| 47 |
|
|
|
|
| 48 |
process_btn.click(
|
| 49 |
fn=extract_subtitles,
|
| 50 |
+
inputs=[video_input, language, gr.State(0), gr.State(10), gr.State(10), gr.State(80), gr.State(80), gr.State(15)],
|
| 51 |
outputs=[file_output, subtitle_output]
|
| 52 |
)
|
| 53 |
|
|
|
|
| 54 |
demo.launch()
|