mguven61 commited on
Commit
a1827e4
·
verified ·
1 Parent(s): 8e8c17d

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +97 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from paddleocr import PaddleOCR
4
+ from langdetect import detect
5
+ from googletrans import Translator
6
+ from PIL import ImageFont, ImageDraw, Image
7
+ import numpy as np
8
+ import os
9
+
10
+ ocr = PaddleOCR(use_angle_cls=True, lang='en')
11
+ translator = Translator()
12
+
13
+ def process_video(video_file):
14
+ input_path = video_file
15
+ output_path = "output_with_subs.mp4"
16
+
17
+ cap = cv2.VideoCapture(input_path)
18
+ if not cap.isOpened():
19
+ return "Video could not be opened!", None
20
+
21
+ width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
22
+ height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
23
+ fps = cap.get(cv2.CAP_PROP_FPS)
24
+
25
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
26
+ out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
27
+ if not out.isOpened():
28
+ return "VideoWriter could not be started!", None
29
+
30
+ frame_count = 0
31
+ lang_printed = False
32
+ all_translations = []
33
+ detected_lang = None
34
+
35
+ while cap.isOpened():
36
+ ret, frame = cap.read()
37
+ if not ret:
38
+ break
39
+
40
+ frame_count += 1
41
+ if frame_count % 30 == 0:
42
+ img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
43
+ result = ocr.ocr(img, cls=True)
44
+
45
+ text = ''
46
+ for line in result:
47
+ for word in line:
48
+ text += word[1][0] + ' '
49
+
50
+ if text.strip():
51
+ try:
52
+ if len(text.strip()) < 10:
53
+ detected_lang = 'en'
54
+ else:
55
+ detected_lang = detect(text)
56
+ lang_printed = True
57
+ except Exception as e:
58
+ continue
59
+
60
+ try:
61
+ translated = translator.translate(text, src=detected_lang, dest='tr')
62
+ all_translations.append(translated.text)
63
+ except Exception as e:
64
+ pass
65
+
66
+ subtitle = ' '.join(all_translations)
67
+ if subtitle:
68
+ max_len = 120
69
+ if len(subtitle) > max_len:
70
+ subtitle = subtitle[-max_len:]
71
+ frame_pil = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
72
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
73
+ if not os.path.exists(font_path):
74
+ font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"
75
+ font = ImageFont.truetype(font_path, 32)
76
+ draw = ImageDraw.Draw(frame_pil)
77
+ draw.text((30, height - 60), subtitle, font=font, fill=(0, 255, 0))
78
+ frame = cv2.cvtColor(np.array(frame_pil), cv2.COLOR_RGB2BGR)
79
+ if frame.shape[1] != width or frame.shape[0] != height:
80
+ frame = cv2.resize(frame, (width, height))
81
+ out.write(frame)
82
+
83
+ cap.release()
84
+ out.release()
85
+ cv2.destroyAllWindows()
86
+ return "Success!", output_path
87
+
88
+ demo = gr.Interface(
89
+ fn=process_video,
90
+ inputs=gr.Video(type="filepath", label="Upload a video"),
91
+ outputs=[gr.Textbox(label="Status"), gr.File(label="Download video with subtitles")],
92
+ title="Video OCR & Translation Subtitle Generator",
93
+ description="Upload a video. The app will extract text, translate it to Turkish, and add it as subtitles."
94
+ )
95
+
96
+ if __name__ == "__main__":
97
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio
2
+ opencv-python
3
+ paddleocr
4
+ paddlepaddle
5
+ langdetect
6
+ googletrans==4.0.0rc1
7
+ pillow