Spaces:
Sleeping
Sleeping
resolved feature issue, added library page
Browse files- crud.py +12 -1
- grounding.py +11 -37
- layer_extractor.py +11 -1
- main.py +48 -10
- ocr.py +1 -1
- proposal_engine.py +63 -9
crud.py
CHANGED
|
@@ -69,4 +69,15 @@ def get_saved_state(db, file_id: str):
|
|
| 69 |
"filename": project.filename,
|
| 70 |
"file_id": project.file_id,
|
| 71 |
"layer_count": project.layer_count,
|
| 72 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
"filename": project.filename,
|
| 70 |
"file_id": project.file_id,
|
| 71 |
"layer_count": project.layer_count,
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
def delete_project(db: Session, file_id: str):
|
| 75 |
+
project = db.query(Project).filter(Project.file_id == file_id).first()
|
| 76 |
+
|
| 77 |
+
if not project:
|
| 78 |
+
return None
|
| 79 |
+
|
| 80 |
+
db.delete(project)
|
| 81 |
+
db.commit()
|
| 82 |
+
|
| 83 |
+
return project
|
grounding.py
CHANGED
|
@@ -4,10 +4,8 @@ import torch
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# tell Python where groundingdino package is
|
| 8 |
from groundingdino.util.inference import load_model, load_image, predict
|
| 9 |
|
| 10 |
-
# config
|
| 11 |
GROUNDING_CONFIG = os.path.join(os.path.dirname(__file__), "models", "GroundingDINO_SwinT_OGC.py")
|
| 12 |
GROUNDING_WEIGHTS = os.path.join(os.path.dirname(__file__), "models", "groundingdino_swint_ogc.pth")
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -15,6 +13,7 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 15 |
# detection thresholds
|
| 16 |
BOX_THRESHOLD = 0.30 # confidence needed to keep a box
|
| 17 |
TEXT_THRESHOLD = 0.25 # confidence needed to keep a text label
|
|
|
|
| 18 |
|
| 19 |
print(f"[GDINO] Using device: {DEVICE}")
|
| 20 |
|
|
@@ -28,16 +27,8 @@ def load_grounding_model():
|
|
| 28 |
|
| 29 |
def detect_objects(image_path: str, model, text_prompt: str = None) -> list:
|
| 30 |
"""
|
| 31 |
-
Detect objects
|
| 32 |
-
|
| 33 |
-
text_prompt examples:
|
| 34 |
-
"product . text . logo . background"
|
| 35 |
-
"person . car . sky"
|
| 36 |
-
If None, uses a default poster-focused prompt.
|
| 37 |
-
|
| 38 |
-
Returns list of dicts:
|
| 39 |
-
[{label, confidence, x1, y1, x2, y2, cx, cy, w, h}]
|
| 40 |
-
All coordinates are absolute pixels.
|
| 41 |
"""
|
| 42 |
if text_prompt is None:
|
| 43 |
# text_prompt = (
|
|
@@ -45,16 +36,14 @@ def detect_objects(image_path: str, model, text_prompt: str = None) -> list:
|
|
| 45 |
# "image . graphic . illustration . icon . button"
|
| 46 |
# )
|
| 47 |
text_prompt = (
|
| 48 |
-
"product . logo . person . icon . illustration . graphic"
|
| 49 |
)
|
| 50 |
|
| 51 |
-
# load_image returns (PIL image, transformed tensor) β both needed
|
| 52 |
image_pil, image_tensor = load_image(image_path)
|
| 53 |
|
| 54 |
|
| 55 |
img_h, img_w = image_pil.shape[:2]
|
| 56 |
|
| 57 |
-
# run detection
|
| 58 |
with torch.no_grad():
|
| 59 |
boxes, confidences, labels = predict(
|
| 60 |
model = model,
|
|
@@ -94,21 +83,17 @@ def detect_objects(image_path: str, model, text_prompt: str = None) -> list:
|
|
| 94 |
|
| 95 |
# sort by confidence descending
|
| 96 |
results.sort(key=lambda r: r["confidence"], reverse=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return results
|
| 98 |
|
| 99 |
|
| 100 |
def boxes_to_sam_prompts(detections: list) -> tuple:
|
| 101 |
"""
|
| 102 |
-
Convert
|
| 103 |
-
|
| 104 |
-
SAM2 accepts:
|
| 105 |
-
- point_coords: array of (x, y) centre points
|
| 106 |
-
- point_labels: array of 1s (foreground)
|
| 107 |
-
|
| 108 |
-
We use the centre of each detected box as a SAM2 prompt point.
|
| 109 |
-
This tells SAM2 exactly where to segment instead of using blind grid.
|
| 110 |
-
|
| 111 |
-
Returns: (point_coords np.array, point_labels np.array, labels list)
|
| 112 |
"""
|
| 113 |
if not detections:
|
| 114 |
return None, None, []
|
|
@@ -128,8 +113,7 @@ def boxes_to_sam_prompts(detections: list) -> tuple:
|
|
| 128 |
|
| 129 |
def save_detection_debug(image_path: str, detections: list, output_dir: str = None):
|
| 130 |
"""
|
| 131 |
-
Save
|
| 132 |
-
Useful to visually verify detection quality before passing to SAM2.
|
| 133 |
"""
|
| 134 |
import cv2
|
| 135 |
|
|
@@ -159,7 +143,6 @@ def save_detection_debug(image_path: str, detections: list, output_dir: str = No
|
|
| 159 |
return out_path
|
| 160 |
|
| 161 |
|
| 162 |
-
# run directly to test
|
| 163 |
if __name__ == "__main__":
|
| 164 |
BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 165 |
TEST_IMAGE = os.path.join(BACKEND_DIR, "test_images", "sale_img.jpg")
|
|
@@ -177,15 +160,6 @@ if __name__ == "__main__":
|
|
| 177 |
|
| 178 |
point_coords, point_labels, sam_labels = boxes_to_sam_prompts(detections)
|
| 179 |
|
| 180 |
-
# print("\nSAM2 Prompt Points:")
|
| 181 |
-
# print(point_coords)
|
| 182 |
-
|
| 183 |
-
# print("\nSAM2 Point Labels:")
|
| 184 |
-
# print(point_labels)
|
| 185 |
-
|
| 186 |
-
# print("\nDetection Labels:")
|
| 187 |
-
# print(sam_labels)
|
| 188 |
-
|
| 189 |
print(f"\nDetected {len(detections)} objects:")
|
| 190 |
for d in detections:
|
| 191 |
print(f" {d['label']:20s} conf={d['confidence']} box=({d['x1']},{d['y1']}) β ({d['x2']},{d['y2']})")
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
|
|
|
|
| 7 |
from groundingdino.util.inference import load_model, load_image, predict
|
| 8 |
|
|
|
|
| 9 |
GROUNDING_CONFIG = os.path.join(os.path.dirname(__file__), "models", "GroundingDINO_SwinT_OGC.py")
|
| 10 |
GROUNDING_WEIGHTS = os.path.join(os.path.dirname(__file__), "models", "groundingdino_swint_ogc.pth")
|
| 11 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 13 |
# detection thresholds
|
| 14 |
BOX_THRESHOLD = 0.30 # confidence needed to keep a box
|
| 15 |
TEXT_THRESHOLD = 0.25 # confidence needed to keep a text label
|
| 16 |
+
MIN_BOX_SIZE = 10
|
| 17 |
|
| 18 |
print(f"[GDINO] Using device: {DEVICE}")
|
| 19 |
|
|
|
|
| 27 |
|
| 28 |
def detect_objects(image_path: str, model, text_prompt: str = None) -> list:
|
| 29 |
"""
|
| 30 |
+
Detect objects using GroundingDINO.
|
| 31 |
+
Returns bounding boxes in image coordinates.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
"""
|
| 33 |
if text_prompt is None:
|
| 34 |
# text_prompt = (
|
|
|
|
| 36 |
# "image . graphic . illustration . icon . button"
|
| 37 |
# )
|
| 38 |
text_prompt = (
|
| 39 |
+
"product . logo . person . icon . illustration . graphic . shape . cup"
|
| 40 |
)
|
| 41 |
|
|
|
|
| 42 |
image_pil, image_tensor = load_image(image_path)
|
| 43 |
|
| 44 |
|
| 45 |
img_h, img_w = image_pil.shape[:2]
|
| 46 |
|
|
|
|
| 47 |
with torch.no_grad():
|
| 48 |
boxes, confidences, labels = predict(
|
| 49 |
model = model,
|
|
|
|
| 83 |
|
| 84 |
# sort by confidence descending
|
| 85 |
results.sort(key=lambda r: r["confidence"], reverse=True)
|
| 86 |
+
|
| 87 |
+
results = [
|
| 88 |
+
r for r in results
|
| 89 |
+
if r["w"] >= MIN_BOX_SIZE and r["h"] >= MIN_BOX_SIZE
|
| 90 |
+
]
|
| 91 |
return results
|
| 92 |
|
| 93 |
|
| 94 |
def boxes_to_sam_prompts(detections: list) -> tuple:
|
| 95 |
"""
|
| 96 |
+
Convert detections into SAM2 point prompts.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
"""
|
| 98 |
if not detections:
|
| 99 |
return None, None, []
|
|
|
|
| 113 |
|
| 114 |
def save_detection_debug(image_path: str, detections: list, output_dir: str = None):
|
| 115 |
"""
|
| 116 |
+
Save detection boxes for debugging.
|
|
|
|
| 117 |
"""
|
| 118 |
import cv2
|
| 119 |
|
|
|
|
| 143 |
return out_path
|
| 144 |
|
| 145 |
|
|
|
|
| 146 |
if __name__ == "__main__":
|
| 147 |
BACKEND_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 148 |
TEST_IMAGE = os.path.join(BACKEND_DIR, "test_images", "sale_img.jpg")
|
|
|
|
| 160 |
|
| 161 |
point_coords, point_labels, sam_labels = boxes_to_sam_prompts(detections)
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
print(f"\nDetected {len(detections)} objects:")
|
| 164 |
for d in detections:
|
| 165 |
print(f" {d['label']:20s} conf={d['confidence']} box=({d['x1']},{d['y1']}) β ({d['x2']},{d['y2']})")
|
layer_extractor.py
CHANGED
|
@@ -75,6 +75,10 @@ def build_layers(image_path: str) -> tuple:
|
|
| 75 |
print("[LAYERS] Running OCR...")
|
| 76 |
text_blocks = extract_text_layers(image_path)
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
# --- classify and route ---
|
| 79 |
image_type, logo_detection = classify_image_type(detections, img_w, img_h)
|
| 80 |
|
|
@@ -120,7 +124,12 @@ def build_layers(image_path: str) -> tuple:
|
|
| 120 |
return layers, groups
|
| 121 |
|
| 122 |
# --- not-logo path ---
|
| 123 |
-
clean_objects = run_proposal_engine(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
print(f"[LAYERS] {len(clean_objects)} objects passed proposal engine")
|
| 125 |
|
| 126 |
layers = []
|
|
@@ -194,6 +203,7 @@ def build_layers(image_path: str) -> tuple:
|
|
| 194 |
]
|
| 195 |
|
| 196 |
clean_text, rejected_text = reject_text_inside_objects(text_proposals, object_boxes)
|
|
|
|
| 197 |
if rejected_text:
|
| 198 |
print(f"[LAYERS] Rejected {len(rejected_text)} text blocks inside object layers")
|
| 199 |
|
|
|
|
| 75 |
print("[LAYERS] Running OCR...")
|
| 76 |
text_blocks = extract_text_layers(image_path)
|
| 77 |
|
| 78 |
+
print("\nOCR DETECTED:")
|
| 79 |
+
for t in text_blocks:
|
| 80 |
+
print(t["text"])
|
| 81 |
+
|
| 82 |
# --- classify and route ---
|
| 83 |
image_type, logo_detection = classify_image_type(detections, img_w, img_h)
|
| 84 |
|
|
|
|
| 124 |
return layers, groups
|
| 125 |
|
| 126 |
# --- not-logo path ---
|
| 127 |
+
clean_objects = run_proposal_engine(
|
| 128 |
+
object_proposals,
|
| 129 |
+
text_blocks,
|
| 130 |
+
img_w,
|
| 131 |
+
img_h,
|
| 132 |
+
)
|
| 133 |
print(f"[LAYERS] {len(clean_objects)} objects passed proposal engine")
|
| 134 |
|
| 135 |
layers = []
|
|
|
|
| 203 |
]
|
| 204 |
|
| 205 |
clean_text, rejected_text = reject_text_inside_objects(text_proposals, object_boxes)
|
| 206 |
+
|
| 207 |
if rejected_text:
|
| 208 |
print(f"[LAYERS] Rejected {len(rejected_text)} text blocks inside object layers")
|
| 209 |
|
main.py
CHANGED
|
@@ -6,6 +6,7 @@ from fastapi import FastAPI, File, UploadFile, HTTPException, Depends
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from sqlalchemy.orm import Session
|
|
|
|
| 9 |
|
| 10 |
from config import UPLOAD_DIR, PROJECT_NAME
|
| 11 |
from database import Base, engine, get_db
|
|
@@ -18,7 +19,8 @@ from crud import (
|
|
| 18 |
get_all_projects,
|
| 19 |
get_project_by_file_id,
|
| 20 |
save_project_state,
|
| 21 |
-
get_saved_state
|
|
|
|
| 22 |
)
|
| 23 |
from pipeline import run_pipeline
|
| 24 |
|
|
@@ -96,13 +98,17 @@ def list_projects(db: Session = Depends(get_db)):
|
|
| 96 |
projects = get_all_projects(db)
|
| 97 |
return [
|
| 98 |
{
|
| 99 |
-
"id":
|
| 100 |
-
"filename":
|
| 101 |
-
"file_id":
|
| 102 |
-
"layer_count":
|
| 103 |
-
"status":
|
| 104 |
-
"processing_ms":
|
| 105 |
-
"created_at":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
for p in projects
|
| 108 |
]
|
|
@@ -121,7 +127,11 @@ def get_project(file_id: str, db: Session = Depends(get_db)):
|
|
| 121 |
"layer_count": project.layer_count,
|
| 122 |
"status": project.status,
|
| 123 |
"processing_ms": project.processing_ms,
|
| 124 |
-
"created_at":
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
}
|
| 126 |
|
| 127 |
|
|
@@ -229,4 +239,32 @@ async def load_saved_project(file_id: str, db: Session = Depends(get_db)):
|
|
| 229 |
data = get_saved_state(db, file_id)
|
| 230 |
if not data:
|
| 231 |
raise HTTPException(404, "Project not found")
|
| 232 |
-
return JSONResponse(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
from fastapi.responses import JSONResponse
|
| 8 |
from sqlalchemy.orm import Session
|
| 9 |
+
from datetime import timezone, timedelta
|
| 10 |
|
| 11 |
from config import UPLOAD_DIR, PROJECT_NAME
|
| 12 |
from database import Base, engine, get_db
|
|
|
|
| 19 |
get_all_projects,
|
| 20 |
get_project_by_file_id,
|
| 21 |
save_project_state,
|
| 22 |
+
get_saved_state,
|
| 23 |
+
delete_project,
|
| 24 |
)
|
| 25 |
from pipeline import run_pipeline
|
| 26 |
|
|
|
|
| 98 |
projects = get_all_projects(db)
|
| 99 |
return [
|
| 100 |
{
|
| 101 |
+
"id": p.id,
|
| 102 |
+
"filename": p.filename,
|
| 103 |
+
"file_id": p.file_id,
|
| 104 |
+
"layer_count": p.layer_count,
|
| 105 |
+
"status": p.status,
|
| 106 |
+
"processing_ms": p.processing_ms,
|
| 107 |
+
"created_at": (p.created_at.replace(tzinfo=timezone.utc)
|
| 108 |
+
.astimezone(timezone(timedelta(hours=5, minutes=30)))
|
| 109 |
+
.isoformat()
|
| 110 |
+
),
|
| 111 |
+
"thumbnail_b64": p.thumbnail_b64,
|
| 112 |
}
|
| 113 |
for p in projects
|
| 114 |
]
|
|
|
|
| 127 |
"layer_count": project.layer_count,
|
| 128 |
"status": project.status,
|
| 129 |
"processing_ms": project.processing_ms,
|
| 130 |
+
"created_at": (
|
| 131 |
+
project.created_at.replace(tzinfo=timezone.utc)
|
| 132 |
+
.astimezone(timezone(timedelta(hours=5, minutes=30)))
|
| 133 |
+
.isoformat()
|
| 134 |
+
)
|
| 135 |
}
|
| 136 |
|
| 137 |
|
|
|
|
| 239 |
data = get_saved_state(db, file_id)
|
| 240 |
if not data:
|
| 241 |
raise HTTPException(404, "Project not found")
|
| 242 |
+
return JSONResponse(data)
|
| 243 |
+
|
| 244 |
+
@app.delete("/projects/{file_id}")
|
| 245 |
+
async def remove_project(file_id: str, db: Session = Depends(get_db)):
|
| 246 |
+
|
| 247 |
+
project = get_project_by_file_id(db, file_id)
|
| 248 |
+
|
| 249 |
+
if not project:
|
| 250 |
+
raise HTTPException(404, "Project not found")
|
| 251 |
+
|
| 252 |
+
# Delete uploaded image
|
| 253 |
+
if project.upload_path and os.path.exists(project.upload_path):
|
| 254 |
+
try:
|
| 255 |
+
os.remove(project.upload_path)
|
| 256 |
+
except:
|
| 257 |
+
pass
|
| 258 |
+
|
| 259 |
+
# Delete generated inpaint image (if present)
|
| 260 |
+
inpaint_path = os.path.join("outputs", f"inpaint_{file_id}.jpg")
|
| 261 |
+
if os.path.exists(inpaint_path):
|
| 262 |
+
try:
|
| 263 |
+
os.remove(inpaint_path)
|
| 264 |
+
except:
|
| 265 |
+
pass
|
| 266 |
+
|
| 267 |
+
# Delete database record LAST
|
| 268 |
+
delete_project(db, file_id)
|
| 269 |
+
|
| 270 |
+
return {"status": "deleted"}
|
ocr.py
CHANGED
|
@@ -148,7 +148,7 @@ if __name__ == "__main__":
|
|
| 148 |
TEST_IMAGE = os.path.join(
|
| 149 |
BACKEND_DIR,
|
| 150 |
"test_images",
|
| 151 |
-
"
|
| 152 |
)
|
| 153 |
|
| 154 |
if not os.path.exists(TEST_IMAGE):
|
|
|
|
| 148 |
TEST_IMAGE = os.path.join(
|
| 149 |
BACKEND_DIR,
|
| 150 |
"test_images",
|
| 151 |
+
"logo_company.jpg"
|
| 152 |
)
|
| 153 |
|
| 154 |
if not os.path.exists(TEST_IMAGE):
|
proposal_engine.py
CHANGED
|
@@ -91,13 +91,26 @@ def container_filter(proposals):
|
|
| 91 |
|
| 92 |
|
| 93 |
def reject_text_inside_objects(text_proposals, accepted_objects):
|
| 94 |
-
kept
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
else:
|
| 100 |
-
kept.append(
|
|
|
|
| 101 |
return kept, rejected
|
| 102 |
|
| 103 |
|
|
@@ -150,14 +163,44 @@ def print_proposal_report(accepted, all_rejected):
|
|
| 150 |
print(f"[PROPOSAL] {label:<25} Rejected ({reason})")
|
| 151 |
print("[PROPOSAL] βββββββββββββββββββββββββββββββββββββ\n")
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
all_rejected = []
|
| 156 |
|
| 157 |
after_semantic, rej = semantic_filter(object_proposals)
|
|
|
|
| 158 |
all_rejected.extend(rej)
|
| 159 |
|
| 160 |
-
after_area, rej = area_filter(
|
| 161 |
all_rejected.extend(rej)
|
| 162 |
|
| 163 |
after_dedup, rej = deduplicate(after_area)
|
|
@@ -168,4 +211,15 @@ def run_proposal_engine(object_proposals, img_w, img_h):
|
|
| 168 |
|
| 169 |
print_proposal_report(after_container, all_rejected)
|
| 170 |
|
| 171 |
-
return after_container
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
def reject_text_inside_objects(text_proposals, accepted_objects):
|
| 94 |
+
kept = []
|
| 95 |
+
rejected = []
|
| 96 |
+
|
| 97 |
+
for text in text_proposals:
|
| 98 |
+
text_area = text["w"] * text["h"]
|
| 99 |
+
|
| 100 |
+
remove = False
|
| 101 |
+
|
| 102 |
+
for obj in accepted_objects:
|
| 103 |
+
overlap = intersection_area(text, obj)
|
| 104 |
+
|
| 105 |
+
if overlap / text_area >= 0.90:
|
| 106 |
+
remove = True
|
| 107 |
+
break
|
| 108 |
+
|
| 109 |
+
if remove:
|
| 110 |
+
rejected.append((text, "inside object"))
|
| 111 |
else:
|
| 112 |
+
kept.append(text)
|
| 113 |
+
|
| 114 |
return kept, rejected
|
| 115 |
|
| 116 |
|
|
|
|
| 163 |
print(f"[PROPOSAL] {label:<25} Rejected ({reason})")
|
| 164 |
print("[PROPOSAL] βββββββββββββββββββββββββββββββββββββ\n")
|
| 165 |
|
| 166 |
+
def reject_text_like_objects(object_proposals, text_proposals):
|
| 167 |
+
kept = []
|
| 168 |
+
rejected = []
|
| 169 |
+
|
| 170 |
+
for obj in object_proposals:
|
| 171 |
+
obj_area = obj["w"] * obj["h"]
|
| 172 |
+
remove = False
|
| 173 |
+
|
| 174 |
+
for text in text_proposals:
|
| 175 |
+
overlap = intersection_area(obj, text)
|
| 176 |
+
|
| 177 |
+
if overlap == 0:
|
| 178 |
+
continue
|
| 179 |
|
| 180 |
+
text_area = text["w"] * text["h"]
|
| 181 |
+
|
| 182 |
+
text_inside = overlap / text_area
|
| 183 |
+
object_is_text = overlap / obj_area
|
| 184 |
+
|
| 185 |
+
if text_inside > 0.90 and object_is_text > 0.70:
|
| 186 |
+
remove = True
|
| 187 |
+
break
|
| 188 |
+
|
| 189 |
+
if remove:
|
| 190 |
+
rejected.append((obj, "text object"))
|
| 191 |
+
else:
|
| 192 |
+
kept.append(obj)
|
| 193 |
+
|
| 194 |
+
return kept, rejected
|
| 195 |
+
|
| 196 |
+
def run_proposal_engine(object_proposals, text_proposals, img_w, img_h):
|
| 197 |
all_rejected = []
|
| 198 |
|
| 199 |
after_semantic, rej = semantic_filter(object_proposals)
|
| 200 |
+
after_text, rej = reject_text_like_objects(after_semantic, text_proposals)
|
| 201 |
all_rejected.extend(rej)
|
| 202 |
|
| 203 |
+
after_area, rej = area_filter(after_text, img_w, img_h)
|
| 204 |
all_rejected.extend(rej)
|
| 205 |
|
| 206 |
after_dedup, rej = deduplicate(after_area)
|
|
|
|
| 211 |
|
| 212 |
print_proposal_report(after_container, all_rejected)
|
| 213 |
|
| 214 |
+
return after_container
|
| 215 |
+
|
| 216 |
+
def intersection_area(a, b):
|
| 217 |
+
x1 = max(a["x"], b["x"])
|
| 218 |
+
y1 = max(a["y"], b["y"])
|
| 219 |
+
x2 = min(a["x"] + a["w"], b["x"] + b["w"])
|
| 220 |
+
y2 = min(a["y"] + a["h"], b["y"] + b["h"])
|
| 221 |
+
|
| 222 |
+
if x2 <= x1 or y2 <= y1:
|
| 223 |
+
return 0
|
| 224 |
+
|
| 225 |
+
return (x2 - x1) * (y2 - y1)
|