Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,17 +7,6 @@ import numpy as np
|
|
7 |
import gradio as gr
|
8 |
from paddleocr import PaddleOCR
|
9 |
|
10 |
-
from PIL import Image
|
11 |
-
|
12 |
-
def is_valid_image(path):
|
13 |
-
try:
|
14 |
-
img = Image.open(path)
|
15 |
-
img.verify()
|
16 |
-
return True
|
17 |
-
except:
|
18 |
-
return False
|
19 |
-
|
20 |
-
|
21 |
ocr = PaddleOCR(use_angle_cls=True, lang='en', det_model_dir='models/det', rec_model_dir='models/rec', cls_model_dir='models/cls')
|
22 |
|
23 |
def classify_background_color(avg_color, white_thresh=230, black_thresh=50, yellow_thresh=100):
|
@@ -50,48 +39,20 @@ def sample_border_color(image, box, padding=2):
|
|
50 |
median_color = np.median(border_pixels, axis=0)
|
51 |
return tuple(map(int, median_color))
|
52 |
|
53 |
-
def detect_text_boxes(image
|
54 |
-
|
55 |
-
|
56 |
-
if image is None or not hasattr(image, 'shape'):
|
57 |
-
print("Invalid image. Skipping...")
|
58 |
-
return []
|
59 |
-
|
60 |
-
# Resize large images to reduce memory load
|
61 |
-
height, width = image.shape[:2]
|
62 |
-
if max(height, width) > max_dim:
|
63 |
-
scale = max_dim / float(max(height, width))
|
64 |
-
image = cv2.resize(image, (int(width * scale), int(height * scale)))
|
65 |
-
|
66 |
-
# Ensure image is in RGB
|
67 |
-
if image.shape[2] == 1:
|
68 |
-
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
69 |
-
elif image.shape[2] == 3:
|
70 |
-
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
71 |
-
|
72 |
-
# Call PaddleOCR correctly
|
73 |
-
results = ocr.ocr(image, cls=True)
|
74 |
-
|
75 |
-
if results is None or not results[0]:
|
76 |
-
print("No OCR results found or OCR returned None.")
|
77 |
-
return []
|
78 |
-
|
79 |
-
boxes = []
|
80 |
-
for line in results[0]:
|
81 |
-
box, (text, confidence) = line
|
82 |
-
if text.strip():
|
83 |
-
x_min = int(min(pt[0] for pt in box))
|
84 |
-
x_max = int(max(pt[0] for pt in box))
|
85 |
-
y_min = int(min(pt[1] for pt in box))
|
86 |
-
y_max = int(max(pt[1] for pt in box))
|
87 |
-
boxes.append(((x_min, y_min, x_max, y_max), text, confidence))
|
88 |
-
return boxes
|
89 |
-
|
90 |
-
except Exception as e:
|
91 |
-
print(f"OCR failed on image: {e}")
|
92 |
return []
|
93 |
-
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
def remove_text_dynamic_fill(img_path, output_path):
|
97 |
image = cv2.imread(img_path)
|
@@ -138,36 +99,16 @@ def remove_text_dynamic_fill(img_path, output_path):
|
|
138 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
139 |
cv2.imwrite(output_path, image)
|
140 |
|
141 |
-
import uuid
|
142 |
-
|
143 |
def process_folder(input_files):
|
144 |
temp_output = tempfile.mkdtemp()
|
145 |
-
|
146 |
for file in input_files:
|
147 |
filename = os.path.basename(file.name)
|
148 |
output_path = os.path.join(temp_output, filename)
|
149 |
remove_text_dynamic_fill(file.name, output_path)
|
150 |
|
151 |
-
|
152 |
-
zip_path = os.path.join("/tmp", f"cleaned_output_{unique_name}.zip")
|
153 |
-
shutil.make_archive(zip_path.replace(".zip", ""), 'zip', temp_output)
|
154 |
-
|
155 |
-
delayed_cleanup(zip_path)
|
156 |
return zip_path
|
157 |
|
158 |
-
|
159 |
-
import threading
|
160 |
-
import time
|
161 |
-
|
162 |
-
def delayed_cleanup(path, delay=30):
|
163 |
-
def cleanup():
|
164 |
-
time.sleep(delay)
|
165 |
-
if os.path.exists(path):
|
166 |
-
os.remove(path)
|
167 |
-
threading.Thread(target=cleanup).start()
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
demo = gr.Interface(
|
172 |
fn=process_folder,
|
173 |
inputs=gr.File(file_types=[".jpg", ".jpeg", ".png"], file_count="multiple", label="Upload Comic Images"),
|
@@ -176,4 +117,4 @@ demo = gr.Interface(
|
|
176 |
description="Upload comic images and get a zip of cleaned versions (text removed). Uses PaddleOCR for detection."
|
177 |
)
|
178 |
|
179 |
-
demo.launch()
|
|
|
7 |
import gradio as gr
|
8 |
from paddleocr import PaddleOCR
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
ocr = PaddleOCR(use_angle_cls=True, lang='en', det_model_dir='models/det', rec_model_dir='models/rec', cls_model_dir='models/cls')
|
11 |
|
12 |
def classify_background_color(avg_color, white_thresh=230, black_thresh=50, yellow_thresh=100):
|
|
|
39 |
median_color = np.median(border_pixels, axis=0)
|
40 |
return tuple(map(int, median_color))
|
41 |
|
42 |
+
def detect_text_boxes(image):
|
43 |
+
results = ocr.ocr(image, cls=True)
|
44 |
+
if not results or not results[0]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
return []
|
46 |
+
boxes = []
|
47 |
+
for line in results[0]:
|
48 |
+
box, (text, confidence) = line
|
49 |
+
if text.strip():
|
50 |
+
x_min = int(min(pt[0] for pt in box))
|
51 |
+
x_max = int(max(pt[0] for pt in box))
|
52 |
+
y_min = int(min(pt[1] for pt in box))
|
53 |
+
y_max = int(max(pt[1] for pt in box))
|
54 |
+
boxes.append(((x_min, y_min, x_max, y_max), text, confidence))
|
55 |
+
return boxes
|
56 |
|
57 |
def remove_text_dynamic_fill(img_path, output_path):
|
58 |
image = cv2.imread(img_path)
|
|
|
99 |
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
100 |
cv2.imwrite(output_path, image)
|
101 |
|
|
|
|
|
102 |
def process_folder(input_files):
|
103 |
temp_output = tempfile.mkdtemp()
|
|
|
104 |
for file in input_files:
|
105 |
filename = os.path.basename(file.name)
|
106 |
output_path = os.path.join(temp_output, filename)
|
107 |
remove_text_dynamic_fill(file.name, output_path)
|
108 |
|
109 |
+
zip_path = shutil.make_archive(temp_output, 'zip', temp_output)
|
|
|
|
|
|
|
|
|
110 |
return zip_path
|
111 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
demo = gr.Interface(
|
113 |
fn=process_folder,
|
114 |
inputs=gr.File(file_types=[".jpg", ".jpeg", ".png"], file_count="multiple", label="Upload Comic Images"),
|
|
|
117 |
description="Upload comic images and get a zip of cleaned versions (text removed). Uses PaddleOCR for detection."
|
118 |
)
|
119 |
|
120 |
+
demo.launch()
|