Offex commited on
Commit
61fb20e
·
verified ·
1 Parent(s): fc2c793

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -28
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 jo English text padhega
7
  reader = easyocr.Reader(['en'], gpu=False)
8
 
9
- def remove_text_from_image(img):
10
- # Photo ko array me convert karna
11
- img = np.array(img)
 
 
 
 
 
12
 
13
- # Photo me text kahan hai, wo dhundhna
14
- results = reader.readtext(img)
 
15
 
16
- # Ek khali (black) mask banana
17
- mask = np.zeros(img.shape[:2], dtype=np.uint8)
18
 
19
- # Jahan text mila hai, wahan mask par safed (white) box banana
20
- for (bbox, text, prob) in results:
21
- (tl, tr, br, bl) = bbox
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
- # Asli magic: Text ko hata kar background fill karna (Inpainting)
31
- result = cv2.inpaint(img, mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- return result
34
 
35
- # Gradio ka User Interface (UI) banana
36
  interface = gr.Interface(
37
- fn=remove_text_from_image,
38
- inputs=gr.Image(type="numpy", label="Upload Image (Text wali photo daalein)"),
39
- outputs=gr.Image(type="numpy", label="Result (Bina text wali photo)"),
40
- title="Hyco AI Text Remover",
41
- description="Is tool me photo upload karein, aur AI automatically usme se captions aur text hata dega."
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()