Spaces:
Running
Running
Update utils/bubble_utils.py
Browse files- utils/bubble_utils.py +0 -49
utils/bubble_utils.py
CHANGED
|
@@ -1,11 +1,6 @@
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from PIL import ImageDraw
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
-
FONT_PATH = os.path.abspath(
|
| 7 |
-
os.path.join(os.path.dirname(__file__), "..", "NotoSansSC-Regular.ttf")
|
| 8 |
-
)
|
| 9 |
|
| 10 |
def detect_speech_bubbles(pil_img):
|
| 11 |
"""
|
|
@@ -43,47 +38,3 @@ def detect_speech_bubbles(pil_img):
|
|
| 43 |
bubbles.append(polygon)
|
| 44 |
|
| 45 |
return bubbles
|
| 46 |
-
|
| 47 |
-
def bubble_pipeline_single(file_obj):
|
| 48 |
-
filename, image, chunks = load_and_split_image(file_obj, num_chunks=1)
|
| 49 |
-
chunk = chunks[0]
|
| 50 |
-
|
| 51 |
-
# 1. Detect bubbles, not OCR boxes
|
| 52 |
-
bubble_polygons = detect_speech_bubbles(chunk)
|
| 53 |
-
|
| 54 |
-
# 2. OCR for text only (no polygon)
|
| 55 |
-
translations = extract_and_translate_chunk(chunk)
|
| 56 |
-
|
| 57 |
-
translated_image = chunk.copy()
|
| 58 |
-
|
| 59 |
-
# 3. Assign each OCR text item to nearest bubble
|
| 60 |
-
for t in translations:
|
| 61 |
-
text = t["translated"]
|
| 62 |
-
text_center = np.mean(t["polygon"], axis=0)
|
| 63 |
-
|
| 64 |
-
# Find nearest bubble
|
| 65 |
-
dists = [np.linalg.norm(text_center - np.mean(b, axis=0)) for b in bubble_polygons]
|
| 66 |
-
bubble = bubble_polygons[int(np.argmin(dists))]
|
| 67 |
-
|
| 68 |
-
# Slightly shrink inside the bubble
|
| 69 |
-
bubble_inner = shrink_or_expand_polygon(bubble, 0.9)
|
| 70 |
-
|
| 71 |
-
# Fill bubble area
|
| 72 |
-
draw = ImageDraw.Draw(translated_image)
|
| 73 |
-
draw.polygon(bubble_inner, fill=(255,255,255))
|
| 74 |
-
|
| 75 |
-
# Draw translated text
|
| 76 |
-
draw_text_center(
|
| 77 |
-
translated_image,
|
| 78 |
-
bubble_inner,
|
| 79 |
-
text,
|
| 80 |
-
font_path=FONT_PATH
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
original_html = encode_image_to_html(chunk)
|
| 84 |
-
translated_html = encode_image_to_html(translated_image)
|
| 85 |
-
|
| 86 |
-
# Optional table
|
| 87 |
-
table_data = [[t["original"], t["translated"]] for t in translations]
|
| 88 |
-
|
| 89 |
-
return filename, original_html, translated_html, table_data, [translations]
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
from PIL import ImageDraw
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def detect_speech_bubbles(pil_img):
|
| 6 |
"""
|
|
|
|
| 38 |
bubbles.append(polygon)
|
| 39 |
|
| 40 |
return bubbles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|