3rdaiOhpinFully commited on
Commit
d9d3094
·
verified ·
1 Parent(s): 91ff618

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +394 -0
app.py ADDED
@@ -0,0 +1,394 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from typing import Dict, Any, List, Tuple
3
+
4
+ import cv2
5
+ import numpy as np
6
+ from PIL import Image
7
+ import gradio as gr
8
+
9
+ try:
10
+ import spaces
11
+ except Exception:
12
+ class spaces:
13
+ @staticmethod
14
+ def GPU(*args, **kwargs):
15
+ def deco(fn):
16
+ return fn
17
+ return deco
18
+
19
+ try:
20
+ import torch
21
+ from transformers import AutoProcessor, AutoModelForZeroShotObjectDetection, pipeline
22
+ except Exception:
23
+ torch = None
24
+ AutoProcessor = None
25
+ AutoModelForZeroShotObjectDetection = None
26
+ pipeline = None
27
+
28
+
29
+ APP_VERSION = "soul-threading-hf-worker-v7-dino-cv-pillow"
30
+ GROUNDING_DINO_MODEL_ID = "IDEA-Research/grounding-dino-tiny"
31
+ OWLV2_MODEL_ID = "google/owlv2-base-patch16-ensemble"
32
+
33
+ _gd_processor = None
34
+ _gd_model = None
35
+ _owl_pipe = None
36
+
37
+ DEFAULT_PROMPTS = [
38
+ "person", "human", "face", "head", "body", "torso",
39
+ "animal", "main character", "primary subject", "object",
40
+ "logo", "text", "fire", "smoke", "vehicle", "building",
41
+ "abstract shape", "background"
42
+ ]
43
+
44
+
45
+ def clamp(v, lo, hi):
46
+ return max(lo, min(hi, v))
47
+
48
+
49
+ def pil_from_input(image):
50
+ if image is None:
51
+ raise ValueError("No image received.")
52
+ if isinstance(image, Image.Image):
53
+ return image.convert("RGB")
54
+ return Image.fromarray(image).convert("RGB")
55
+
56
+
57
+ def cv_focal_scan(img: Image.Image) -> Dict[str, Any]:
58
+ arr = np.array(img.convert("RGB"))
59
+ h, w = arr.shape[:2]
60
+ gray = cv2.cvtColor(arr, cv2.COLOR_RGB2GRAY)
61
+ edges = cv2.Canny(gray, 70, 170)
62
+ hsv = cv2.cvtColor(arr, cv2.COLOR_RGB2HSV)
63
+ sat = hsv[:, :, 1]
64
+ val = hsv[:, :, 2]
65
+
66
+ heat = (edges.astype(np.float32) / 255.0) * 0.65
67
+ heat += (sat.astype(np.float32) / 255.0) * 0.25
68
+ heat += ((255 - val).astype(np.float32) / 255.0) * 0.10
69
+ heat = cv2.GaussianBlur(heat, (31, 31), 0)
70
+
71
+ ys, xs = np.where(heat > np.percentile(heat, 86))
72
+ if len(xs) < 25:
73
+ return {
74
+ "label": "cv_center_fallback",
75
+ "confidence": 0.15,
76
+ "bbox_xyxy": [int(w * 0.20), int(h * 0.15), int(w * 0.80), int(h * 0.90)],
77
+ "center_xy_norm": [0.5, 0.5],
78
+ "method": "opencv_center_fallback"
79
+ }
80
+
81
+ x1 = int(np.percentile(xs, 4))
82
+ y1 = int(np.percentile(ys, 4))
83
+ x2 = int(np.percentile(xs, 96))
84
+ y2 = int(np.percentile(ys, 96))
85
+
86
+ if (x2 - x1) < w * 0.10 or (y2 - y1) < h * 0.10:
87
+ x1, y1, x2, y2 = int(w * 0.20), int(h * 0.15), int(w * 0.80), int(h * 0.90)
88
+
89
+ return {
90
+ "label": "cv_saliency",
91
+ "confidence": 0.35,
92
+ "bbox_xyxy": [x1, y1, x2, y2],
93
+ "center_xy_norm": [round(((x1 + x2) / 2) / max(w, 1), 4), round(((y1 + y2) / 2) / max(h, 1), 4)],
94
+ "method": "opencv_edges_saturation_heatmap"
95
+ }
96
+
97
+
98
+ def normalize_labels(query_text: str) -> List[str]:
99
+ if not query_text or not query_text.strip():
100
+ return DEFAULT_PROMPTS
101
+ raw = query_text.replace("\n", ",").replace(".", ",").split(",")
102
+ labels = [x.strip() for x in raw if x.strip()]
103
+ return labels or DEFAULT_PROMPTS
104
+
105
+
106
+ def labels_to_grounding_text(labels: List[str]) -> str:
107
+ return ". ".join(labels) + "."
108
+
109
+
110
+ def box_area(box):
111
+ x1, y1, x2, y2 = box
112
+ return max(0, x2 - x1) * max(0, y2 - y1)
113
+
114
+
115
+ def clean_detections(dets: List[Dict[str, Any]], img_size: Tuple[int, int]) -> List[Dict[str, Any]]:
116
+ w, h = img_size
117
+ clean = []
118
+ for d in dets:
119
+ box = [int(round(v)) for v in d.get("bbox_xyxy", [0, 0, 0, 0])]
120
+ x1, y1, x2, y2 = box
121
+ x1 = clamp(x1, 0, max(w - 1, 0))
122
+ y1 = clamp(y1, 0, max(h - 1, 0))
123
+ x2 = clamp(x2, x1 + 1, w)
124
+ y2 = clamp(y2, y1 + 1, h)
125
+ area_ratio = box_area([x1, y1, x2, y2]) / max(w * h, 1)
126
+ if area_ratio < 0.002 or area_ratio > 0.96:
127
+ continue
128
+ clean.append({
129
+ "label": str(d.get("label", "object")),
130
+ "confidence": float(d.get("confidence", 0.0)),
131
+ "bbox_xyxy": [x1, y1, x2, y2],
132
+ "center_xy_norm": [
133
+ round(((x1 + x2) / 2) / max(w, 1), 4),
134
+ round(((y1 + y2) / 2) / max(h, 1), 4)
135
+ ],
136
+ "area_ratio": round(area_ratio, 5),
137
+ "method": d.get("method", "unknown")
138
+ })
139
+ clean.sort(key=lambda x: (x["confidence"], x["area_ratio"]), reverse=True)
140
+ return clean[:12]
141
+
142
+
143
+ def load_grounding_dino():
144
+ global _gd_processor, _gd_model
145
+ if AutoProcessor is None or AutoModelForZeroShotObjectDetection is None or torch is None:
146
+ raise RuntimeError("transformers/torch not available")
147
+ if _gd_processor is None or _gd_model is None:
148
+ _gd_processor = AutoProcessor.from_pretrained(GROUNDING_DINO_MODEL_ID)
149
+ _gd_model = AutoModelForZeroShotObjectDetection.from_pretrained(GROUNDING_DINO_MODEL_ID)
150
+ device = "cuda" if torch.cuda.is_available() else "cpu"
151
+ _gd_model.to(device)
152
+ _gd_model.eval()
153
+ return _gd_processor, _gd_model
154
+
155
+
156
+ def run_grounding_dino(img: Image.Image, labels: List[str], box_threshold=0.25, text_threshold=0.20) -> List[Dict[str, Any]]:
157
+ processor, model = load_grounding_dino()
158
+ device = next(model.parameters()).device
159
+ text = labels_to_grounding_text(labels)
160
+
161
+ inputs = processor(images=img, text=text, return_tensors="pt")
162
+ inputs = {k: v.to(device) if hasattr(v, "to") else v for k, v in inputs.items()}
163
+
164
+ with torch.no_grad():
165
+ outputs = model(**inputs)
166
+
167
+ target_sizes = torch.tensor([img.size[::-1]], device=device)
168
+
169
+ if hasattr(processor, "post_process_grounded_object_detection"):
170
+ results = processor.post_process_grounded_object_detection(
171
+ outputs,
172
+ inputs.get("input_ids"),
173
+ box_threshold=box_threshold,
174
+ text_threshold=text_threshold,
175
+ target_sizes=target_sizes
176
+ )[0]
177
+ else:
178
+ results = processor.post_process_object_detection(
179
+ outputs,
180
+ threshold=box_threshold,
181
+ target_sizes=target_sizes
182
+ )[0]
183
+
184
+ dets = []
185
+ boxes = results.get("boxes", [])
186
+ scores = results.get("scores", [])
187
+ result_labels = results.get("labels", [])
188
+
189
+ for i in range(len(boxes)):
190
+ label = result_labels[i]
191
+ if not isinstance(label, str):
192
+ try:
193
+ label = labels[int(label)]
194
+ except Exception:
195
+ label = str(label)
196
+ score = scores[i].detach().cpu().item() if hasattr(scores[i], "detach") else scores[i]
197
+ box = boxes[i].detach().cpu().tolist() if hasattr(boxes[i], "detach") else boxes[i]
198
+ dets.append({
199
+ "label": label,
200
+ "confidence": float(score),
201
+ "bbox_xyxy": box,
202
+ "method": "grounding_dino_tiny_transformers"
203
+ })
204
+ return dets
205
+
206
+
207
+ def load_owlv2():
208
+ global _owl_pipe
209
+ if pipeline is None or torch is None:
210
+ raise RuntimeError("transformers pipeline/torch not available")
211
+ if _owl_pipe is None:
212
+ device = 0 if torch.cuda.is_available() else -1
213
+ _owl_pipe = pipeline(
214
+ task="zero-shot-object-detection",
215
+ model=OWLV2_MODEL_ID,
216
+ device=device
217
+ )
218
+ return _owl_pipe
219
+
220
+
221
+ def run_owlv2(img: Image.Image, labels: List[str], threshold=0.08) -> List[Dict[str, Any]]:
222
+ pipe = load_owlv2()
223
+ preds = pipe(img, candidate_labels=labels, threshold=threshold)
224
+ dets = []
225
+ for p in preds[:12]:
226
+ b = p.get("box", {})
227
+ dets.append({
228
+ "label": p.get("label", "object"),
229
+ "confidence": float(p.get("score", 0.0)),
230
+ "bbox_xyxy": [
231
+ int(b.get("xmin", 0)),
232
+ int(b.get("ymin", 0)),
233
+ int(b.get("xmax", 0)),
234
+ int(b.get("ymax", 0))
235
+ ],
236
+ "method": "owlv2_transformers_pipeline"
237
+ })
238
+ return dets
239
+
240
+
241
+ def choose_primary(detections: List[Dict[str, Any]], cv_subject: Dict[str, Any]) -> Dict[str, Any]:
242
+ priority_words = ["person", "human", "face", "head", "body", "torso", "main character", "primary subject"]
243
+ if detections:
244
+ def score(d):
245
+ label = d["label"].lower()
246
+ priority = 0.25 if any(w in label for w in priority_words) else 0
247
+ text_penalty = -0.25 if "text" in label or "logo" in label else 0
248
+ return d["confidence"] + priority + text_penalty + min(d.get("area_ratio", 0), 0.25)
249
+ return max(detections, key=score)
250
+ return cv_subject
251
+
252
+
253
+ def make_layout_plan(primary: Dict[str, Any]) -> Dict[str, Any]:
254
+ cx, cy = primary.get("center_xy_norm", [0.5, 0.5])
255
+ cx = float(cx)
256
+ cy = float(cy)
257
+ front_cx = clamp(cx, 0.38, 0.58)
258
+ front_cy = clamp(cy, 0.38, 0.62)
259
+
260
+ return {
261
+ "front_body": {
262
+ "center": [round(front_cx, 4), round(front_cy, 4)],
263
+ "zoom": 1.00,
264
+ "intent": "hold primary subject cleanly inside body panel"
265
+ },
266
+ "back_body": {
267
+ "center": [0.72, 0.50],
268
+ "zoom": 1.12,
269
+ "intent": "supporting environment, not random duplicate face"
270
+ },
271
+ "hood": {
272
+ "center": [0.50, 0.22],
273
+ "zoom": 1.18,
274
+ "intent": "upper atmosphere / background detail"
275
+ },
276
+ "left_sleeve": {
277
+ "center": [0.28, 0.52],
278
+ "zoom": 1.16,
279
+ "intent": "left-side color motion"
280
+ },
281
+ "right_sleeve": {
282
+ "center": [0.76, 0.52],
283
+ "zoom": 1.16,
284
+ "intent": "right-side color motion"
285
+ },
286
+ "kangaroo_pocket": {
287
+ "center": [round(front_cx, 4), 0.72],
288
+ "zoom": 1.06,
289
+ "intent": "front-lower underlay crop until exact assembly coordinates exist"
290
+ },
291
+ "trim": {
292
+ "center": [0.50, 0.85],
293
+ "zoom": 1.30,
294
+ "intent": "texture strip only"
295
+ }
296
+ }
297
+
298
+
299
+ def draw_debug(img: Image.Image, detections: List[Dict[str, Any]], primary: Dict[str, Any]) -> Image.Image:
300
+ arr = np.array(img.copy().convert("RGB"))
301
+ for d in detections[:8]:
302
+ x1, y1, x2, y2 = d["bbox_xyxy"]
303
+ cv2.rectangle(arr, (x1, y1), (x2, y2), (255, 255, 255), 3)
304
+ cv2.putText(arr, d["label"][:22], (x1, max(22, y1 - 6)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
305
+ if primary:
306
+ x1, y1, x2, y2 = primary["bbox_xyxy"]
307
+ cv2.rectangle(arr, (x1, y1), (x2, y2), (0, 255, 0), 5)
308
+ return Image.fromarray(arr)
309
+
310
+
311
+ @spaces.GPU(duration=90)
312
+ def analyze(image, query_text="", prefer_model="GroundingDINO first"):
313
+ img = pil_from_input(image)
314
+ labels = normalize_labels(query_text)
315
+ cv_subject = cv_focal_scan(img)
316
+
317
+ model_status = []
318
+ detections = []
319
+
320
+ if prefer_model in ["GroundingDINO first", "GroundingDINO only"]:
321
+ try:
322
+ detections = clean_detections(run_grounding_dino(img, labels), img.size)
323
+ model_status.append(f"GroundingDINO ok: {len(detections)} detections")
324
+ except Exception as e:
325
+ model_status.append("GroundingDINO failed: " + str(e))
326
+ if prefer_model == "GroundingDINO only":
327
+ detections = []
328
+
329
+ if not detections and prefer_model in ["GroundingDINO first", "OWLv2 fallback", "OWLv2 only"]:
330
+ try:
331
+ detections = clean_detections(run_owlv2(img, labels), img.size)
332
+ model_status.append(f"OWLv2 ok: {len(detections)} detections")
333
+ except Exception as e:
334
+ model_status.append("OWLv2 failed: " + str(e))
335
+
336
+ primary = choose_primary(detections, cv_subject)
337
+ layout_plan = make_layout_plan(primary)
338
+
339
+ result = {
340
+ "version": APP_VERSION,
341
+ "models": {
342
+ "grounding_dino": GROUNDING_DINO_MODEL_ID,
343
+ "owlv2_fallback": OWLV2_MODEL_ID,
344
+ "opencv_fallback": "edges + saturation heatmap",
345
+ "pillow": "image loading/cropping"
346
+ },
347
+ "model_status": model_status,
348
+ "source_size": list(img.size),
349
+ "query_labels": labels,
350
+ "primary_subject": primary,
351
+ "objects": detections,
352
+ "cv_fallback_subject": cv_subject,
353
+ "layout_plan": layout_plan,
354
+ "mapper_contract": {
355
+ "use_this_for": "focal detection and crop planning only",
356
+ "do_not_use_this_for": "final print pixels or garment shape extraction",
357
+ "final_print_rule": "local mapper clips selected artwork section to clean garment-piece mask with transparent outside"
358
+ }
359
+ }
360
+
361
+ debug = draw_debug(img, detections, primary)
362
+ return json.dumps(result, indent=2), debug
363
+
364
+
365
+ with gr.Blocks(title="Soul Threading AI Layout Worker v7") as demo:
366
+ gr.Markdown(
367
+ """
368
+ # Soul Threading AI Layout Worker v7
369
+
370
+ GroundingDINO → OWLv2 fallback → OpenCV/Pillow fallback.
371
+
372
+ This Space analyzes the uploaded artwork and returns layout JSON for the garment mapper.
373
+ It does **not** export final print pieces. The main mapper does that with deterministic template masks.
374
+ """
375
+ )
376
+ with gr.Row():
377
+ image = gr.Image(type="numpy", label="Artwork")
378
+ with gr.Column():
379
+ query = gr.Textbox(
380
+ label="Detection prompts",
381
+ value=", ".join(DEFAULT_PROMPTS),
382
+ lines=5
383
+ )
384
+ prefer = gr.Dropdown(
385
+ choices=["GroundingDINO first", "GroundingDINO only", "OWLv2 fallback", "OWLv2 only"],
386
+ value="GroundingDINO first",
387
+ label="Model route"
388
+ )
389
+ btn = gr.Button("Analyze artwork")
390
+ json_out = gr.Textbox(label="Layout JSON", lines=28)
391
+ debug_img = gr.Image(label="Detection debug preview")
392
+ btn.click(analyze, inputs=[image, query, prefer], outputs=[json_out, debug_img], api_name="analyze")
393
+
394
+ demo.queue(max_size=16).launch()