File size: 9,303 Bytes
77da9e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
"""
Detection Function Wrappers

Provides unified detection function signatures for different backends:
- Direct service access (for HF Spaces / local)
- API client access (for production service-oriented architecture)

This eliminates duplication of detection logic across app.py and ui/gradio_interface.py
"""

import os
import requests
import base64
import io
from PIL import Image
from typing import Tuple, Optional
import traceback

from detection.service_factory import get_detection_service
from detection import ocr_handler, response_builder


def detect_with_service(
    image: Image.Image,
    confidence_threshold: float,
    line_thickness: int,
    enable_clip: bool,
    enable_ocr: bool,
    enable_blip: bool,
    ocr_only: bool,
    blip_scope_choice: str,
    preprocess: bool = False,
    preprocess_mode_choice: str = "RF-DETR Optimized (Recommended)",
    preprocess_preset: str = "standard"
) -> Tuple[Optional[Image.Image], str, Optional[dict]]:
    """
    Detect UI elements using detection service directly (no API)
    
    Used by: app.py (HF Spaces / local mode)
    
    Returns:
        Tuple of (annotated_image, summary_text, json_payload)
    """
    try:
        if image is None:
            return None, "❌ Please upload an image first.", None

        # Map BLIP scope choice to internal value
        scope_value = "all" if (blip_scope_choice or "").lower().startswith("all") else "icons"
        
        # Map preprocessing mode choice to internal value
        preprocess_mode = "rfdetr" if "RF-DETR" in preprocess_mode_choice else "generic"

        # OCR-only path
        if ocr_only:
            detections = ocr_handler.process_ocr_only(image)
            annotated = ocr_handler.annotate_ocr_detections(
                image,
                detections,
                thickness=line_thickness,
                return_format="pil"
            )
            
            json_payload = response_builder.build_ocr_only_response(
                detections=detections,
                image_width=image.width,
                image_height=image.height,
                annotated_image=None,
                confidence_threshold=confidence_threshold,
                line_thickness=line_thickness
            )
            
            summary_text = response_builder.format_summary_text(
                detections=detections,
                parameters=json_payload["parameters"],
                ocr_only=True
            )
            
            return annotated, summary_text, json_payload

        # Standard detection path
        service = get_detection_service()
        
        # Run analysis (pass parameters directly to avoid race conditions)
        analysis = service.analyze(
            image,
            confidence_threshold=confidence_threshold,
            extract_text=enable_ocr,
            use_clip=enable_clip,
            use_blip=enable_blip,
            merge_global_ocr=True,
            blip_scope=scope_value,
            preprocess=preprocess,
            preprocess_mode=preprocess_mode,
            preprocess_preset=preprocess_preset
        )

        # Generate annotated image
        annotated = service.get_prediction_image(
            image,
            confidence_threshold=confidence_threshold,
            extract_content=True,
            thickness=line_thickness,
            return_format="pil",
            analysis=analysis
        )

        # Build JSON response
        json_payload = {
            "success": True,
            "detections": analysis["detections"],
            "total_detections": len(analysis["detections"]),
            "image_size": analysis["image_size"],
            "parameters": {
                "confidence_threshold": confidence_threshold,
                "enable_clip": enable_clip,
                "enable_ocr": enable_ocr,
                "enable_blip": enable_blip,
                "blip_scope": scope_value if enable_blip else None,
                "ocr_only": False,
                "line_thickness": line_thickness
            },
            "type_distribution": response_builder.build_type_distribution(analysis["detections"]) if enable_clip else None
        }
        
        # Build summary text
        summary_text = response_builder.format_summary_text(
            detections=analysis["detections"],
            parameters=json_payload["parameters"],
            ocr_only=False
        )

        return annotated, summary_text, json_payload
        
    except Exception as e:
        error_msg = f"""❌ **Error during detection:**

```
{str(e)}

{traceback.format_exc()}
```
"""
        print(error_msg)
        return None, error_msg, None


def detect_with_api(
    image: Image.Image,
    confidence_threshold: float,
    line_thickness: int,
    enable_clip: bool,
    enable_ocr: bool,
    enable_blip: bool,
    ocr_only: bool,
    blip_scope_choice: str,
    preprocess: bool = False,
    preprocess_mode_choice: str = "RF-DETR Optimized (Recommended)",
    preprocess_preset: str = "standard",
    api_url: str = "http://localhost:8000"
) -> Tuple[Optional[Image.Image], str, Optional[dict]]:
    """
    Detect UI elements by calling the API
    
    Used by: app_ui.py (service-oriented mode)
    
    Returns:
        Tuple of (annotated_image, summary_text, json_payload)
    """
    try:
        if image is None:
            return None, "❌ Please upload an image first.", None

        # Map BLIP scope choice to internal value
        scope_value = "all" if (blip_scope_choice or "").lower().startswith("all") else "icons"
        
        # Map preprocessing mode choice to internal value
        preprocess_mode = "rfdetr" if "RF-DETR" in preprocess_mode_choice else "generic"

        # Prepare image for upload
        img_byte_arr = io.BytesIO()
        image.save(img_byte_arr, format='PNG')
        img_byte_arr.seek(0)

        # Prepare form data
        files = {
            'image': ('image.png', img_byte_arr, 'image/png')
        }
        data = {
            'confidence_threshold': confidence_threshold,
            'line_thickness': line_thickness,
            'enable_clip': str(enable_clip).lower(),
            'enable_ocr': str(enable_ocr).lower(),
            'enable_blip': str(enable_blip).lower(),
            'blip_scope': scope_value,
            'ocr_only': str(ocr_only).lower(),
            'preprocess': str(preprocess).lower(),
            'preprocess_mode': preprocess_mode,
            'preprocess_preset': preprocess_preset
        }

        # Call API
        try:
            response = requests.post(
                f"{api_url}/detect",
                files=files,
                data=data,
                timeout=120
            )
            response.raise_for_status()
        except requests.exceptions.ConnectionError:
            return None, f"""❌ **Connection Error**

Cannot connect to API server at `{api_url}`

**To fix this:**
1. Make sure the API server is running:
   ```bash
   python app_api.py
   ```
2. The API should be accessible at http://localhost:8000
3. Check that no firewall is blocking the connection

**Current API URL:** {api_url}
You can change this by setting the `CU1_API_URL` environment variable.
""", None
        except requests.exceptions.Timeout:
            return None, f"""❌ **Timeout Error**

The API request timed out after 120 seconds.

This might happen with:
- Very large images
- First run (models need to download)
- CPU-only processing (slower than GPU)

**Try:**
- Using a smaller image
- Waiting for model downloads to complete
- Checking API server logs for errors
""", None
        except requests.exceptions.HTTPError as e:
            error_detail = "Unknown error"
            try:
                error_json = response.json()
                error_detail = error_json.get("detail", str(e))
            except:
                error_detail = str(e)
            return None, f"""❌ **API Error ({response.status_code})**

{error_detail}

**API URL:** {api_url}
""", None

        # Parse response
        json_payload = response.json()
        
        if not json_payload.get("success", False):
            return None, f"❌ Detection failed: {json_payload.get('error', 'Unknown error')}", json_payload

        # Decode annotated image
        annotated_image = None
        if "annotated_image" in json_payload and json_payload["annotated_image"]:
            try:
                img_data = base64.b64decode(json_payload["annotated_image"]["base64"])
                annotated_image = Image.open(io.BytesIO(img_data))
            except Exception as e:
                print(f"Failed to decode annotated image: {e}")

        # Build summary text using response_builder
        summary_text = response_builder.format_summary_text(
            detections=json_payload.get("detections", []),
            parameters=json_payload.get("parameters", {}),
            ocr_only=json_payload.get("parameters", {}).get("ocr_only", False)
        )

        return annotated_image, summary_text, json_payload
        
    except Exception as e:
        error_msg = f"""❌ **Error during detection:**

```
{str(e)}

{traceback.format_exc()}
```

**API URL:** {api_url}
"""
        print(error_msg)
        return None, error_msg, None