Spaces:
Running
Running
Upload 2 files (#1)
Browse files- Upload 2 files (fe737d48cd1b75bfc59c968ea83caee0b4744260)
- ToomBComicCleanerGradio.py +120 -0
- requirement.txt +5 -0
ToomBComicCleanerGradio.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
import os
|
3 |
+
import shutil
|
4 |
+
import tempfile
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
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):
|
13 |
+
r, g, b = avg_color
|
14 |
+
if r >= white_thresh and g >= white_thresh and b >= white_thresh:
|
15 |
+
return (255, 255, 255)
|
16 |
+
if r <= black_thresh and g <= black_thresh and b <= black_thresh:
|
17 |
+
return (0, 0, 0)
|
18 |
+
if r >= yellow_thresh and g >= yellow_thresh and b < yellow_thresh:
|
19 |
+
return (255, 255, 0)
|
20 |
+
return None
|
21 |
+
|
22 |
+
def sample_border_color(image, box, padding=2):
|
23 |
+
h, w = image.shape[:2]
|
24 |
+
x_min, y_min, x_max, y_max = box
|
25 |
+
x_min = max(0, x_min - padding)
|
26 |
+
x_max = min(w-1, x_max + padding)
|
27 |
+
y_min = max(0, y_min - padding)
|
28 |
+
y_max = min(h-1, y_max + padding)
|
29 |
+
|
30 |
+
top = image[y_min:y_min+padding, x_min:x_max]
|
31 |
+
bottom = image[y_max-padding:y_max, x_min:x_max]
|
32 |
+
left = image[y_min:y_max, x_min:x_min+padding]
|
33 |
+
right = image[y_min:y_max, x_max-padding:x_max]
|
34 |
+
|
35 |
+
border_pixels = np.vstack((top.reshape(-1, 3), bottom.reshape(-1, 3),
|
36 |
+
left.reshape(-1, 3), right.reshape(-1, 3)))
|
37 |
+
if border_pixels.size == 0:
|
38 |
+
return (255, 255, 255)
|
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)
|
59 |
+
if image is None:
|
60 |
+
return
|
61 |
+
|
62 |
+
if len(image.shape) == 2:
|
63 |
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
64 |
+
elif image.shape[2] == 1:
|
65 |
+
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
|
66 |
+
else:
|
67 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
68 |
+
|
69 |
+
boxes = detect_text_boxes(image)
|
70 |
+
|
71 |
+
for (bbox, text, confidence) in boxes:
|
72 |
+
if confidence < 0.4 or not text.strip():
|
73 |
+
continue
|
74 |
+
|
75 |
+
x_min, y_min, x_max, y_max = bbox
|
76 |
+
height = y_max - y_min
|
77 |
+
|
78 |
+
if height <= 30:
|
79 |
+
padding = 2
|
80 |
+
elif height <= 60:
|
81 |
+
padding = 4
|
82 |
+
else:
|
83 |
+
padding = 6
|
84 |
+
|
85 |
+
x_min_p = max(0, x_min - padding)
|
86 |
+
y_min_p = max(0, y_min - padding)
|
87 |
+
x_max_p = min(image.shape[1]-1, x_max + padding)
|
88 |
+
y_max_p = min(image.shape[0]-1, y_max + padding)
|
89 |
+
|
90 |
+
sample_crop = image[y_min_p:y_max_p, x_min_p:x_max_p]
|
91 |
+
avg_color = np.mean(sample_crop.reshape(-1, 3), axis=0)
|
92 |
+
|
93 |
+
fill_color = classify_background_color(avg_color)
|
94 |
+
if fill_color is None:
|
95 |
+
fill_color = sample_border_color(image, (x_min, y_min, x_max, y_max))
|
96 |
+
|
97 |
+
cv2.rectangle(image, (x_min_p, y_min_p), (x_max_p, y_max_p), fill_color, -1)
|
98 |
+
|
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"),
|
115 |
+
outputs=gr.File(label="Download Cleaned Zip"),
|
116 |
+
title="Comic Text Cleaner",
|
117 |
+
description="Upload comic images and get a zip of cleaned versions (text removed). Uses PaddleOCR for detection."
|
118 |
+
)
|
119 |
+
|
120 |
+
demo.launch()
|
requirement.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
opencv-python
|
3 |
+
paddlepaddle
|
4 |
+
paddleocr
|
5 |
+
gradio
|