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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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import os
import torch
import numpy as np
import json
import time
import io
import zipfile
from PIL import Image, ImageDraw, ImageFont
from transformers import AutoProcessor, AutoModelForVision2Seq
import streamlit as st
import re

# Constants
MODEL_ID = "Qwen/Qwen3-VL-4B-Instruct" 

@st.cache_resource
def load_model():
    """
    Loads the Qwen-VL model and processor.
    """
    print(f"Loading model: {MODEL_ID}...")
    try:
        device_type = "cuda" if torch.cuda.is_available() else "cpu"
        # Use bfloat16 for CPU to save memory (4B params * 4 bytes is too big for 16GB)
        torch_dtype = torch.float16 if device_type == "cuda" else torch.bfloat16
        print(f"Using device: {device_type}, dtype: {torch_dtype}")

        processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
        model = AutoModelForVision2Seq.from_pretrained(
            MODEL_ID,
            device_map="auto",
            trust_remote_code=True,
            torch_dtype=torch_dtype
        )
    except Exception as e:
        print(f"Error loading {MODEL_ID}: {e}")
        st.error(f"Could not load model {MODEL_ID}. Error: {e}")
        return None, None
        
    return processor, model

def get_bounding_boxes(image: Image.Image, prompt: str, history: list, processor, model):
    """
    Generates bounding boxes based on the image, prompt, and conversation history.
    """
    start_time = time.time()
    
    if model is None or processor is None:
        return [], history, "Model not loaded.", {}
        
    # Construct conversation
    messages = []
    
    # Context
    context_text = ""
    if history:
        context_text = "History:\n"
        for msg in history:
            role = "User" if msg['role'] == 'user' else "Assistant"
            context_text += f"{role}: {msg['content']}\n"
        context_text += "\n"

    # Enhanced Prompt: JSON Focused With Reasoning
    final_prompt = f"{context_text}User Request: {prompt}\n\nTask: Detect objects mentioned in the User Request.\nConstraint: Return the result ONLY as a JSON object with a key 'objects'.\nEach object in the list should have 'label', 'bbox' [x1, y1, x2, y2] (common normalized coordinates 0-1000), AND 'reasoning' (a brief string explaining why this object matches).\nExample: {{'objects': [{{'label': 'cat', 'bbox': [100, 200, 500, 600], 'reasoning': 'Detected distinct feline features and whiskers.'}}]}}\nIf no objects are found, return {{'objects': []}}."

    messages = [
        {
            "role": "system", 
            "content": "You are a precise object detection assistant. Return JSON with 'objects' list containing 'label', 'bbox' [x1, y1, x2, y2] (common normalized coordinates 0-1000), and 'reasoning'."
        },
        {
            "role": "user",
            "content": [
                {"type": "image", "image": image},
                {"type": "text", "text": final_prompt}
            ]
        }
    ]

    # Process inputs
    text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    image_inputs, video_inputs = process_vision_info(messages)
    
    try:
        inputs = processor(
            text=[text],
            images=image_inputs,
            videos=video_inputs,
            padding=True,
            return_tensors="pt",
        )
        inputs = inputs.to(model.device)

        # Generate (Measured)
        generate_start = time.time()
        generated_ids = model.generate(**inputs, max_new_tokens=512)
        generate_end = time.time()
        
        generated_ids_trimmed = [
            out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
        ]
        output_text = processor.batch_decode(
            generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
        )[0]
        
    except Exception as e:
        print(f"Inference Error: {e}")
        output_text = f"Error: {e}"
        generate_end = time.time()

    # Update history
    history.append({"role": "user", "content": prompt})
    history.append({"role": "assistant", "content": output_text})
    
    # Parse detections
    detections = parse_qwen_output(output_text, image.width, image.height)
    
    # Filter
    filtered_detections = []
    total_area = image.width * image.height
    
    for det in detections:
        x1, y1, x2, y2 = det['box']
        box_area = (x2 - x1) * (y2 - y1)
        coverage = box_area / total_area
        
        is_suspicious_coverage = coverage > 0.95 
        is_whole_request = any(w in prompt.lower() for w in ["image", "picture", "photo", "background", "everything"])
        
        if is_suspicious_coverage and not is_whole_request:
            continue
        
        filtered_detections.append(det)
    
    # Metrics
    end_time = time.time()
    total_time = end_time - start_time
    inference_time = generate_end - generate_start
    
    metrics = {
        "total_time": round(total_time, 2),
        "inference_time": round(inference_time, 2),
        "token_count": len(generated_ids[0]) if 'generated_ids' in locals() else 0
    }
    
    return filtered_detections, history, output_text, metrics

def smart_merge_detections(existing_detections, new_detections):
    """
    Merges new detections with existing ones.
    Strategy: SIMPLE OVERLAP ONLY.
    If IoU > 0.8 -> Assume duplicate/refinement -> Replace.
    Else -> Keep.
    """
    merged_list = existing_detections.copy()
    
    for new_det in new_detections:
        new_box = new_det['box']
        indices_to_remove = []
        
        for i, old_det in enumerate(merged_list):
            old_box = old_det['box']
            iou = calculate_iou(new_box, old_box)
            
            # Simple threshold check
            if iou > 0.8:
                indices_to_remove.append(i)
        
        for idx in sorted(indices_to_remove, reverse=True):
            merged_list.pop(idx)
            
        merged_list.append(new_det)
        
    return merged_list

def calculate_iou(boxA, boxB):
    xA = max(boxA[0], boxB[0])
    yA = max(boxA[1], boxB[1])
    xB = min(boxA[2], boxB[2])
    yB = min(boxA[3], boxB[3])

    interArea = max(0, xB - xA) * max(0, yB - yA)
    boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
    boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])

    iou = interArea / float(boxAArea + boxBArea - interArea + 1e-6)
    return iou

def parse_qwen_output(text, width, height):
    """
    Parses Qwen-VL output, prioritizing JSON with reasoning.
    """
    detections = []
    
    # 1. Try JSON Parsing (Primary Strategy)
    try:
        match = re.search(r'\{.*\}', text, re.DOTALL)
        if match:
            json_str = match.group()
            data = json.loads(json_str)
            
            if 'objects' in data and isinstance(data['objects'], list):
                for obj in data['objects']:
                    x1, y1, x2, y2 = obj['bbox']
                    label = obj.get('label', 'Object')
                    reasoning = obj.get('reasoning', 'No reasoning provided')
                    
                    real_x1 = (x1 / 1000) * width
                    real_y1 = (y1 / 1000) * height
                    real_x2 = (x2 / 1000) * width
                    real_y2 = (y2 / 1000) * height
                    
                    detections.append({
                        "label": label,
                        "box": [real_x1, real_y1, real_x2, real_y2],
                        "score": 1.0,
                        "reasoning": reasoning
                    })
    except Exception as e:
        print(f"JSON Parse Error: {e}")
        pass
            
    # 2. Fallback to Standard Tags
    if not detections:
        pattern_standard = r"<\|box_start\|>(\d+),(\d+),(\d+),(\d+)<\|box_end\|>(?:<\|object_start\|>(.*?)<\|object_end\|>)?"
        matches_standard = list(re.finditer(pattern_standard, text))
        for match in matches_standard:
            c1, c2, c3, c4 = map(int, match.groups()[:4])
            label = match.group(5) if match.group(5) else "Object"
            y1 = (c1 / 1000) * height
            x1 = (c2 / 1000) * width
            y2 = (c3 / 1000) * height
            x2 = (c4 / 1000) * width
            detections.append({
                "label": label,
                "box": [x1, y1, x2, y2],
                "score": 1.0,
                "reasoning": "Legacy detection mode"
            })

    return detections

def create_crops_zip(image: Image.Image, detections: list):
    """
    Creates a ZIP file containing cropped images of all detections.
    """
    zip_buffer = io.BytesIO()
    
    # Ensure distinct filenames
    counts = {}
    
    with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zip_file:
        for i, det in enumerate(detections):
            label = det.get('label', 'object').replace(" ", "_").lower()
            if label not in counts:
                counts[label] = 1
            else:
                counts[label] += 1
                label = f"{label}_{counts[label]}"
                
            x1, y1, x2, y2 = map(int, det['box'])
            x1 = max(0, x1)
            y1 = max(0, y1)
            x2 = min(image.width, x2)
            y2 = min(image.height, y2)
            
            if x2 > x1 and y2 > y1:
                crop = image.crop((x1, y1, x2, y2))
                crop_buffer = io.BytesIO()
                crop.save(crop_buffer, format="JPEG")
                zip_file.writestr(f"{label}.jpg", crop_buffer.getvalue())
                
    zip_buffer.seek(0)
    return zip_buffer

def process_vision_info(messages):
    try:
        from qwen_vl_utils import process_vision_info
        return process_vision_info(messages)
    except ImportError:
        images = []
        for msg in messages:
            for item in msg["content"]:
                if item["type"] == "image":
                    images.append(item["image"])
        return images, None

def draw_boxes(image: Image.Image, detections: list):
    """
    Draws bounding boxes with dynamic font scaling.
    """
    draw = ImageDraw.Draw(image)
    
    # Dynamic Scaling (UPDATED FOR BETTER VISIBILITY)
    min_dim = min(image.width, image.height)
    scaled_font_size = max(20, int(min_dim * 0.035)) 
    scaled_line_width = max(4, int(min_dim * 0.006))
    
    font = None
    try:
        # Search paths for fonts (Linux/Windows)
        font_paths = [
            # Windows
            "arial.ttf", "calibri.ttf", "seguiemj.ttf",
            # Linux (Standard)
            "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf",
            "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
            "LiberationSans-Regular.ttf",
            "DejaVuSans.ttf"
        ]
        
        for fn in font_paths:
            try:
                font = ImageFont.truetype(fn, scaled_font_size)
                print(f"Loaded font: {fn}")
                break
            except Exception as e:
                continue
    except:
        pass
        
    if font is None:
        try:
            print("Fallback to default font (Warning: Text may be tiny)")
            font = ImageFont.load_default()
        except:
             pass

    palette = [
        "#FF00FF", "#00FFFF", "#FF0000", "#00FF00", 
        "#FFFF00", "#FFA500", "#800080", "#008080"
    ]
    
    def get_color(text):
        if not text: return palette[0]
        idx = sum(ord(c) for c in text) % len(palette)
        return palette[idx]

    for det in detections:
        box = det['box']
        label = det.get('label', 'Object')
        score_val = det.get('score', 1.0)
        display_text = f"{label} {int(score_val*100)}%"
        
        color = get_color(label)
        
        x1, y1, x2, y2 = box
        draw.rectangle([x1, y1, x2, y2], outline=color, width=scaled_line_width)
        
        # Text box
        if font:
            text_bbox = draw.textbbox((x1, y1), display_text, font=font)
            text_width = text_bbox[2] - text_bbox[0]
            text_height = text_bbox[3] - text_bbox[1]
            
            label_y = y1 - text_height - (scaled_line_width * 2)
            if label_y < 0: label_y = y1
            
            draw.rectangle(
                [x1, label_y, x1 + text_width + (scaled_line_width * 4), label_y + text_height + (scaled_line_width * 2)], 
                fill=color
            )
            draw.text((x1 + (scaled_line_width), label_y), display_text, fill="black", font=font)
        
    return image

def convert_to_coco(detections, image_size=(1000, 1000), filename="image.jpg"):
    """
    Converts detections to full Standard COCO JSON format.
    """
    width, height = image_size
    
    # 1. Info
    info = {
        "year": 2025,
        "version": "1.0",
        "description": "Generated by Annotation Assistant (Qwen-VL)",
        "date_created": time.strftime("%Y-%m-%d")
    }
    
    # 2. Images
    images = [{
        "id": 1,
        "width": width,
        "height": height,
        "file_name": filename,
        "license": 0,
        "flickr_url": "",
        "coco_url": "",
        "date_captured": 0
    }]
    
    # 3. Categories & Annotations
    categories = []
    category_map = {}
    annotations = []
    cat_id_counter = 1
    
    for i, det in enumerate(detections):
        label = det.get('label', 'object')
        
        # Manage Categories
        if label not in category_map:
            category_map[label] = cat_id_counter
            categories.append({
                "id": cat_id_counter,
                "name": label,
                "supercategory": "object"
            })
            cat_id_counter += 1
            
        x1, y1, x2, y2 = det['box']
        w = x2 - x1
        h = y2 - y1
        
        ann = {
            "id": i + 1,
            "image_id": 1,
            "category_id": category_map[label],
            "bbox": [round(x1, 2), round(y1, 2), round(w, 2), round(h, 2)],
            "area": round(w * h, 2),
            "iscrowd": 0,
            "attributes": {
                "reasoning": det.get('reasoning', '')
            }
        }
        annotations.append(ann)
        
    coco_output = {
        "info": info,
        "images": images,
        "annotations": annotations,
        "categories": categories,
        "licenses": []
    }
    
    return json.dumps(coco_output, indent=2)