Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
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 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
frame_count
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
subtitle
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
draw =
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
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()
|