leetuan023 commited on
Commit
a23edc5
·
verified ·
1 Parent(s): ffe7bd3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import pytesseract
3
+ import gradio as gr
4
+ from datetime import timedelta
5
+ import os
6
+ import tempfile
7
+
8
+ # Đặt đường dẫn Tesseract nếu cần thiết (cho Windows)
9
+ # pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
10
+
11
+ # Hàm nhận diện văn bản từ khung hình với Tesseract OCR
12
+ def tesseract_ocr(frame):
13
+ """
14
+ Sử dụng Tesseract OCR để nhận diện văn bản từ hình ảnh (frame).
15
+ """
16
+ # Chuyển đổi frame sang ảnh đen trắng để tăng độ chính xác
17
+ gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
18
+ # Nhận diện văn bản từ khung hình
19
+ text = pytesseract.image_to_string(gray_frame, lang='vie')
20
+ return text.strip()
21
+
22
+ def extract_frames(video_path, interval=1):
23
+ """
24
+ Tách khung hình từ video theo khoảng thời gian (mỗi giây 1 frame).
25
+ """
26
+ video_capture = cv2.VideoCapture(video_path)
27
+ fps = video_capture.get(cv2.CAP_PROP_FPS) # Lấy số khung hình trên giây
28
+ frames = []
29
+ count = 0
30
+
31
+ while True:
32
+ success, frame = video_capture.read()
33
+ if not success:
34
+ break
35
+
36
+ if count % int(fps * interval) == 0:
37
+ frames.append((frame, count / fps)) # frame và thời gian hiện tại trong video
38
+
39
+ count += 1
40
+
41
+ video_capture.release()
42
+ return frames
43
+
44
+ # Hàm chuyển đổi thời gian chính xác
45
+ def convert_time(seconds):
46
+ """
47
+ Chuyển đổi giây thành định dạng giờ:phút:giây,millisecond cho file .srt.
48
+ """
49
+ milliseconds = int((seconds - int(seconds)) * 1000)
50
+ delta = timedelta(seconds=int(seconds))
51
+ return f"{str(delta)}.{milliseconds:03d}".replace('.', ',')
52
+
53
+ def create_srt(transcriptions, srt_file_path):
54
+ """
55
+ Tạo file SRT từ danh sách các đoạn text và thời gian.
56
+ """
57
+ with open(srt_file_path, 'w', encoding='utf-8') as srt_file:
58
+ for idx, (text, start_time, end_time) in enumerate(transcriptions, 1):
59
+ srt_file.write(f"{idx}\n")
60
+ srt_file.write(f"{convert_time(start_time)} --> {convert_time(end_time)}\n")
61
+ srt_file.write(f"{text}\n\n")
62
+
63
+ def process_video(video_path, frame_interval=1):
64
+ """
65
+ Xử lý video và tạo file SRT.
66
+ """
67
+ frames = extract_frames(video_path, interval=frame_interval)
68
+ transcriptions = []
69
+ last_text = None
70
+ last_timestamp = None
71
+
72
+ for idx, (frame, timestamp) in enumerate(frames):
73
+ # Nhận diện văn bản từ khung hình với Tesseract OCR
74
+ current_text = tesseract_ocr(frame)
75
+
76
+ # Loại bỏ dòng trùng lặp
77
+ if current_text != last_text:
78
+ if last_text is not None:
79
+ # Cập nhật end_time cho đoạn trước đó
80
+ transcriptions[-1] = (last_text, last_timestamp, timestamp)
81
+
82
+ # Ghi lại đoạn mới với start_time
83
+ transcriptions.append((current_text, timestamp, timestamp + frame_interval))
84
+ last_text = current_text
85
+ last_timestamp = timestamp
86
+
87
+ # Cập nhật thời gian kết thúc cho đoạn cuối cùng
88
+ if last_text is not None:
89
+ transcriptions[-1] = (last_text, last_timestamp, timestamp)
90
+
91
+ # Tạo file SRT tạm thời và lưu kết quả
92
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".srt") as temp_srt_file:
93
+ srt_output_path = temp_srt_file.name
94
+ create_srt(transcriptions, srt_output_path)
95
+
96
+ return srt_output_path
97
+
98
+ # Tạo giao diện với Gradio
99
+ interface = gr.Interface(
100
+ fn=process_video, # Hàm xử lý video và trả về SRT
101
+ inputs=gr.Video(), # Video input từ người dùng
102
+ outputs=gr.File(label="Download .srt file"), # File .srt xuất ra
103
+ title="Video to SRT with Tesseract OCR", # Tiêu đề ứng dụng
104
+ description="Tải video lên để chuyển thành file SRT sử dụng Tesseract OCR."
105
+ )
106
+
107
+ # Chạy ứng dụng
108
+ interface.launch()