mguven61 commited on
Commit
aa38b0a
·
verified ·
1 Parent(s): 94576f5

Update app.py

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