Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,44 +2,66 @@ import gradio as gr
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import easyocr
|
|
|
|
| 5 |
|
| 6 |
-
# AI model load ho raha hai
|
| 7 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
tl = (int(tl[0]), int(tl[1]))
|
| 23 |
-
br = (int(br[0]), int(br[1]))
|
| 24 |
-
cv2.rectangle(mask, tl, br, 255, -1)
|
| 25 |
-
|
| 26 |
-
# Text ke kinaro ko thoda bada karna taaki pura text cover ho jaye
|
| 27 |
-
kernel = np.ones((5,5), np.uint8)
|
| 28 |
-
mask = cv2.dilate(mask, kernel, iterations=1)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
return
|
| 34 |
|
| 35 |
-
# Gradio
|
| 36 |
interface = gr.Interface(
|
| 37 |
-
fn=
|
| 38 |
-
inputs=gr.
|
| 39 |
-
outputs=gr.
|
| 40 |
-
title="Hyco
|
| 41 |
-
description="
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# App ko start karna
|
| 45 |
interface.launch()
|
|
|
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
import easyocr
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
+
# AI model load ho raha hai
|
| 8 |
reader = easyocr.Reader(['en'], gpu=False)
|
| 9 |
|
| 10 |
+
def remove_text_from_video(video_path):
|
| 11 |
+
if video_path is None:
|
| 12 |
+
return None
|
| 13 |
+
|
| 14 |
+
cap = cv2.VideoCapture(video_path)
|
| 15 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 16 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 17 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 18 |
|
| 19 |
+
# Processed video save karne ke liye temporary file
|
| 20 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
|
| 21 |
+
output_path = temp_file.name
|
| 22 |
|
| 23 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 24 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
|
| 25 |
|
| 26 |
+
# Hugging Face free tier ko crash hone se bachane ke liye 150 frames (approx 5 sec) ki limit
|
| 27 |
+
max_frames = 150
|
| 28 |
+
frame_count = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
while cap.isOpened():
|
| 31 |
+
ret, frame = cap.read()
|
| 32 |
+
if not ret or frame_count >= max_frames:
|
| 33 |
+
break
|
| 34 |
+
|
| 35 |
+
# Text dhundhna aur mask banana
|
| 36 |
+
results = reader.readtext(frame)
|
| 37 |
+
mask = np.zeros(frame.shape[:2], dtype=np.uint8)
|
| 38 |
+
|
| 39 |
+
for (bbox, text, prob) in results:
|
| 40 |
+
(tl, tr, br, bl) = bbox
|
| 41 |
+
cv2.rectangle(mask, (int(tl[0]), int(tl[1])), (int(br[0]), int(br[1])), 255, -1)
|
| 42 |
+
|
| 43 |
+
# Mask ko thoda bada karna taaki text puri tarah cover ho
|
| 44 |
+
kernel = np.ones((5,5), np.uint8)
|
| 45 |
+
mask = cv2.dilate(mask, kernel, iterations=1)
|
| 46 |
+
|
| 47 |
+
# Frame se text hata kar background fill karna (Inpainting)
|
| 48 |
+
result_frame = cv2.inpaint(frame, mask, 3, cv2.INPAINT_TELEA)
|
| 49 |
+
|
| 50 |
+
out.write(result_frame)
|
| 51 |
+
frame_count += 1
|
| 52 |
+
|
| 53 |
+
cap.release()
|
| 54 |
+
out.release()
|
| 55 |
|
| 56 |
+
return output_path
|
| 57 |
|
| 58 |
+
# Gradio Video UI
|
| 59 |
interface = gr.Interface(
|
| 60 |
+
fn=remove_text_from_video,
|
| 61 |
+
inputs=gr.Video(label="Upload Video (Test karne ke liye 2-5 sec ki clip daalein)"),
|
| 62 |
+
outputs=gr.Video(label="Result Video"),
|
| 63 |
+
title="Hyco Video Text Remover",
|
| 64 |
+
description="Isme apni clip upload karein. AI har frame se captions dhundhega aur unhe automatically hata dega."
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
interface.launch()
|