Ketengan-Diffusion-Lab commited on
Commit
a38a1e2
·
verified ·
1 Parent(s): f9aaf13

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -0
app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from videocr import save_subtitles_to_file
4
+
5
+ # Define OCR function
6
+ def run_video_ocr(input_video, output_file_name, language_code, use_gpu, start_time, end_time, confidence_threshold, similarity_threshold, frames_to_skip, crop_x, crop_y, crop_width, crop_height):
7
+ try:
8
+ # Ensure the output directory exists
9
+ persistent_dir = '/persistent'
10
+ if not os.path.exists(persistent_dir):
11
+ os.makedirs(persistent_dir)
12
+
13
+ # Define full path for the output file
14
+ output_path = os.path.join(persistent_dir, output_file_name)
15
+
16
+ # Save the subtitles to file
17
+ save_subtitles_to_file(
18
+ input_video.name, # The uploaded video file
19
+ output_path, # Output .srt file path
20
+ lang=language_code,
21
+ use_gpu=use_gpu,
22
+ time_start=start_time,
23
+ time_end=end_time,
24
+ conf_threshold=confidence_threshold,
25
+ sim_threshold=similarity_threshold,
26
+ frames_to_skip=frames_to_skip,
27
+ crop_x=crop_x,
28
+ crop_y=crop_y,
29
+ crop_width=crop_width,
30
+ crop_height=crop_height
31
+ )
32
+
33
+ return f"Subtitle extraction completed! File saved to {output_path}"
34
+ except Exception as e:
35
+ return f"Error: {str(e)}"
36
+
37
+ # Define Gradio interface
38
+ def video_ocr_interface():
39
+ with gr.Blocks() as demo:
40
+ with gr.Row():
41
+ input_video = gr.File(label="Upload Video", type="file")
42
+ output_file_name = gr.Textbox(label="Output File Name (.srt)", value="subtitle.srt")
43
+ language_code = gr.Textbox(label="Language Code", value="ch")
44
+ use_gpu = gr.Checkbox(label="Use GPU", value=True)
45
+
46
+ with gr.Row():
47
+ start_time = gr.Textbox(label="Start Time (HH:MM:SS)", value="00:00:00")
48
+ end_time = gr.Textbox(label="End Time (HH:MM:SS)", value="")
49
+
50
+ with gr.Row():
51
+ confidence_threshold = gr.Slider(label="Confidence Threshold", minimum=0, maximum=100, value=75)
52
+ similarity_threshold = gr.Slider(label="Similarity Threshold", minimum=0, maximum=100, value=80)
53
+
54
+ with gr.Row():
55
+ frames_to_skip = gr.Slider(label="Frames to Skip", minimum=0, maximum=10, value=0)
56
+ crop_x = gr.Number(label="Crop X", value=0)
57
+ crop_y = gr.Number(label="Crop Y", value=0)
58
+ crop_width = gr.Number(label="Crop Width", value=0)
59
+ crop_height = gr.Number(label="Crop Height", value=0)
60
+
61
+ submit_btn = gr.Button("Start OCR")
62
+
63
+ output_label = gr.Textbox(label="Status", interactive=False)
64
+
65
+ # Define what happens when the button is clicked
66
+ submit_btn.click(
67
+ fn=run_video_ocr,
68
+ inputs=[input_video, output_file_name, language_code, use_gpu, start_time, end_time, confidence_threshold, similarity_threshold, frames_to_skip, crop_x, crop_y, crop_width, crop_height],
69
+ outputs=output_label
70
+ )
71
+
72
+ return demo
73
+
74
+ # Launch the Gradio interface
75
+ demo = video_ocr_interface()
76
+ demo.launch()