File size: 7,451 Bytes
b2c5857
 
28470ee
 
 
 
 
 
 
b2c5857
6bccba5
fe44f24
 
b2c5857
 
 
fe44f24
b2c5857
28470ee
 
 
 
 
 
6bccba5
 
 
 
28470ee
 
fe44f24
 
28470ee
fe44f24
 
 
 
 
 
6bccba5
fe44f24
 
 
 
 
 
 
 
 
b2c5857
fe44f24
28470ee
 
 
 
fe44f24
28470ee
b2c5857
 
 
fe44f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6bccba5
fe44f24
5ff8f69
 
 
 
fe44f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ff8f69
 
 
 
 
 
fe44f24
 
 
 
28470ee
fe44f24
 
28470ee
 
 
fe44f24
 
 
 
 
 
 
28470ee
 
fe44f24
 
 
 
28470ee
 
 
 
 
 
 
 
 
fe44f24
28470ee
 
 
 
fe44f24
28470ee
 
 
 
 
 
fe44f24
28470ee
 
 
fe44f24
 
28470ee
 
fe44f24
 
 
 
 
b2c5857
fe44f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ff8f69
fe44f24
 
 
 
b2c5857
 
 
 
fe44f24
b2c5857
 
 
 
 
 
 
 
fe44f24
b2c5857
fe44f24
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import os
import json
import base64
import io
import cv2
import torch
import numpy as np
from PIL import Image

from grounding import load_grounding_model, detect_objects
from text_extractor import extract_text_layers
from proposal_engine import run_proposal_engine, compute_groups, reject_text_inside_objects
from logo_decomposer import decompose_logo, get_text_inside_logo

BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))

LOGO_AREA_THRESHOLD = 0.20

def pil_to_base64(img: Image.Image, fmt: str = "PNG") -> str:
    buffer = io.BytesIO()
    img.save(buffer, format=fmt)
    return base64.b64encode(buffer.getvalue()).decode("utf-8")


# def crop_text_layer(image_path, x, y, w, h):
#     img  = Image.open(image_path).convert("RGBA")
#     crop = img.crop((x, y, x + w, y + h))
#     return pil_to_base64(crop, "PNG")


def classify_image_type(detections, img_w, img_h):
    image_area = img_w * img_h

    best_det      = None
    best_fraction = 0.0

    for d in detections:
        label    = d["label"].strip().lower()
        fraction = (d["x2"] - d["x1"]) * (d["y2"] - d["y1"]) / image_area
        if label.startswith("logo") and fraction > LOGO_AREA_THRESHOLD and fraction > best_fraction:
            best_fraction = fraction
            best_det      = d

    if best_det:
        print(f"[LAYERS] Image classified as: logo (logo covers {best_fraction:.1%})")
        return "logo", best_det

    print("[LAYERS] Image classified as: not-logo")
    return "not-logo", None

def build_layers(image_path: str) -> tuple:
    image_bgr = cv2.imread(image_path)
    image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
    img_h, img_w = image_rgb.shape[:2]

    # --- GroundingDINO ---
    print("[LAYERS] Running GroundingDINO...")
    gdino_model = load_grounding_model()
    detections  = detect_objects(image_path, gdino_model)

    object_proposals = [
        {
            "type":       "object",
            "label":      d["label"],
            "x":          d["x1"],
            "y":          d["y1"],
            "w":          d["x2"] - d["x1"],
            "h":          d["y2"] - d["y1"],
            "confidence": d["confidence"],
        }
        for d in detections
    ]

    # --- OCR ---
    print("[LAYERS] Running OCR...")
    text_blocks = extract_text_layers(image_path)

    print("\nOCR DETECTED:")
    for t in text_blocks:
        print(t["text"])

    # --- classify and route ---
    image_type, logo_detection = classify_image_type(detections, img_w, img_h)

    if image_type == "logo":
        from segment import load_sam2_model
        predictor      = load_sam2_model()
        layers, groups = decompose_logo(
            image_path, image_rgb, logo_detection, text_blocks, predictor
        )
        del predictor
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        print("[LAYERS] SAM2 VRAM freed")

        logo_box_dict = {
            "x": logo_detection["x1"],
            "y": logo_detection["y1"],
            "w": logo_detection["x2"] - logo_detection["x1"],
            "h": logo_detection["y2"] - logo_detection["y1"],
        }
        text_inside_ids = {id(b) for b in get_text_inside_logo(text_blocks, logo_box_dict)}
        next_id = max((l["id"] for l in layers), default=0) + 1

        for block in text_blocks:
            if id(block) in text_inside_ids:
                continue
            layers.append({
                "id":         next_id,
                "type":       "text",
                "text":       block["text"],
                "label":      block["text"],
                "x":          block["x"],
                "y":          block["y"],
                "w":          block["w"],
                "h":          block["h"],
                "confidence": block["confidence"],
                "group_id":   None,
                "group_role": None,
            })
            next_id += 1

        print(f"[LAYERS] Built {len(layers)} layers, {len(groups)} groups")
        return layers, groups

    # --- not-logo path ---
    clean_objects = run_proposal_engine(
        object_proposals,
        text_blocks,
        img_w,
        img_h,
    )
    print(f"[LAYERS] {len(clean_objects)} objects passed proposal engine")

    layers   = []
    layer_id = 1

    if clean_objects:
        print(f"[LAYERS] Running SAM2 on {len(clean_objects)} objects...")
        from segment import load_sam2_model, get_mask_for_box, mask_to_transparent_png
        predictor = load_sam2_model()

        for proposal in clean_objects:
            box = [
                proposal["x"],
                proposal["y"],
                proposal["x"] + proposal["w"],
                proposal["y"] + proposal["h"],
            ]
            try:
                mask = get_mask_for_box(predictor, image_rgb, box)
                rows = np.where(mask.any(axis=1))[0]
                cols = np.where(mask.any(axis=0))[0]
                if len(rows) == 0:
                    continue

                y1, y2 = int(rows.min()), int(rows.max())
                x1, x2 = int(cols.min()), int(cols.max())
                png_img = mask_to_transparent_png(image_rgb, mask)
                b64     = pil_to_base64(png_img, "PNG")

                layers.append({
                    "id":         layer_id,
                    "type":       "object",
                    "label":      proposal["label"],
                    "x":          x1,
                    "y":          y1,
                    "w":          x2 - x1,
                    "h":          y2 - y1,
                    "confidence": proposal["confidence"],
                    "base64":     b64,
                    "format":     "png",
                })
                layer_id += 1

            except Exception as e:
                print(f"[LAYERS] SAM2 failed for '{proposal['label']}': {e}")
                continue

        del predictor
        if torch.cuda.is_available():
            torch.cuda.empty_cache()
        print("[LAYERS] SAM2 VRAM freed")

    # --- text layers: reject any that sit inside an accepted object box ---
    object_boxes = [
        {"x": l["x"], "y": l["y"], "w": l["w"], "h": l["h"]}
        for l in layers
    ]

    text_proposals = [
        {
            "type":       "text",
            "text":       b["text"],
            "label":      b["text"],
            "x":          b["x"],
            "y":          b["y"],
            "w":          b["w"],
            "h":          b["h"],
            "confidence": b["confidence"],
        }
        for b in text_blocks
    ]

    clean_text, rejected_text = reject_text_inside_objects(text_proposals, object_boxes)
        
    if rejected_text:
        print(f"[LAYERS] Rejected {len(rejected_text)} text blocks inside object layers")

    for block in clean_text:
        layers.append({
            "id":         layer_id,
            "type":       "text",
            "text":       block["text"],
            "label":      block["text"],
            "x":          block["x"],
            "y":          block["y"],
            "w":          block["w"],
            "h":          block["h"],
            "confidence": block["confidence"],
        })
        layer_id += 1

    annotated, groups = compute_groups(layers)

    print(f"[LAYERS] Built {len(annotated)} layers ({len(clean_objects)} objects + {len(clean_text)} text), {len(groups)} groups")
    return annotated, groups