makeitfr commited on
Commit
b4cc14b
Β·
verified Β·
1 Parent(s): 059ec81

Upload API_DOCUMENTATION.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. API_DOCUMENTATION.md +327 -0
API_DOCUMENTATION.md ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # UI Element Detection API
2
+
3
+ Complete server-based solution for detecting and locating all UI elements in screenshots using OmniParser and template matching.
4
+
5
+ ## Features
6
+
7
+ βœ… **Automatic UI Detection** - Uses OmniParser to detect all UI elements (buttons, text, icons, etc.)
8
+ βœ… **Precise Coordinates** - Returns pixel-perfect coordinates for each element
9
+ βœ… **Multiple Export Formats** - JSON, CSV, and visualization PNG
10
+ βœ… **Fast Processing** - ~15 seconds per screenshot on CPU
11
+ βœ… **Server-Side Storage** - Cropped images stored on server, not sent to clients
12
+ βœ… **Multiple Endpoints** - Flexible request/response options
13
+
14
+ ## Start the Server
15
+
16
+ ```bash
17
+ cd /workspaces/omoi-v2
18
+ python ui_element_api_server.py --port 8001
19
+ ```
20
+
21
+ Server will start at `http://127.0.0.1:8001`
22
+
23
+ ## API Endpoints
24
+
25
+ ### 1. Health Check
26
+ ```bash
27
+ GET /health
28
+ ```
29
+
30
+ **Response:**
31
+ ```json
32
+ {"status": "ok", "service": "UI Element Detection API"}
33
+ ```
34
+
35
+ ### 2. Analyze Image (Full Response)
36
+ ```bash
37
+ POST /analyze
38
+ Content-Type: multipart/form-data
39
+
40
+ file: <PNG image file>
41
+ ```
42
+
43
+ **Response:**
44
+ ```json
45
+ {
46
+ "status": "success",
47
+ "processing_time_seconds": 15.4,
48
+ "timing": {
49
+ "omniparser_seconds": 9.88,
50
+ "template_matching_seconds": 5.48
51
+ },
52
+ "image_info": {
53
+ "filename": "Screenshot.png",
54
+ "size": {"width": 1365, "height": 767}
55
+ },
56
+ "analysis": {
57
+ "total_elements_detected": 120,
58
+ "elements": [
59
+ {
60
+ "template_id": "crop_0000",
61
+ "template_file": "crop_0000.png",
62
+ "confidence": 1.0,
63
+ "bbox": {
64
+ "x1": 71, "y1": 13, "x2": 161, "y2": 29,
65
+ "width": 90, "height": 16
66
+ },
67
+ "center": {"x": 116, "y": 21},
68
+ "bbox_ratio": {
69
+ "x1": 0.052, "y1": 0.017, "x2": 0.118, "y2": 0.038
70
+ }
71
+ },
72
+ // ... 119 more elements
73
+ ]
74
+ },
75
+ "exports": {
76
+ "csv_data": "Element_ID,Template_File,Confidence,X1,Y1,...\n",
77
+ "visualization_png_base64": "iVBORw0KGgoAAAANSUhEUgAAA..."
78
+ }
79
+ }
80
+ ```
81
+
82
+ ### 3. Analyze Image (Structured Response)
83
+ ```bash
84
+ POST /analyze_batch
85
+ Content-Type: multipart/form-data
86
+
87
+ file: <PNG image file>
88
+ ```
89
+
90
+ **Response:**
91
+ ```json
92
+ {
93
+ "metadata": {
94
+ "filename": "Screenshot.png",
95
+ "image_size": {"width": 1365, "height": 767},
96
+ "total_elements_detected": 120,
97
+ "templates_loaded": 120
98
+ },
99
+ "coordinates_json": {
100
+ "source_image": "Screenshot.png",
101
+ "image_size": {"width": 1365, "height": 767},
102
+ "total_elements": 120,
103
+ "elements": [...]
104
+ },
105
+ "csv_data": "Element_ID,Template_File,...\n",
106
+ "visualization_png_base64": "iVBORw0KGgo..."
107
+ }
108
+ ```
109
+
110
+ ## Usage Examples
111
+
112
+ ### Python Client
113
+
114
+ ```python
115
+ from ui_element_client import UIElementDetectionClient
116
+
117
+ # Initialize client
118
+ client = UIElementDetectionClient(api_url="http://127.0.0.1:8001")
119
+
120
+ # Check API health
121
+ status = client.health_check()
122
+ print(status)
123
+
124
+ # Analyze image and get all elements
125
+ result = client.analyze_image("screenshot.png")
126
+ print(f"Found {result['analysis']['total_elements_detected']} UI elements")
127
+
128
+ # Get specific element
129
+ element = client.get_element_by_id("screenshot.png", "crop_0031")
130
+ print(f"Element at: ({element['center']['x']}, {element['center']['y']})")
131
+
132
+ # Find elements in a region (top 100 pixels)
133
+ elements = client.find_elements_in_region("screenshot.png", 0, 0, 1365, 100)
134
+ print(f"Found {len(elements)} elements in top region")
135
+ ```
136
+
137
+ ### Using curl
138
+
139
+ #### Analyze image and save outputs
140
+ ```bash
141
+ curl -X POST -F "file=@screenshot.png" http://127.0.0.1:8001/analyze > response.json
142
+
143
+ # Extract CSV data
144
+ python -c "import json; d=json.load(open('response.json')); print(d['exports']['csv_data'])" > coordinates.csv
145
+
146
+ # Extract visualization (base64 decode)
147
+ python -c "
148
+ import json, base64
149
+ d = json.load(open('response.json'))
150
+ with open('visualization.png', 'wb') as f:
151
+ f.write(base64.b64decode(d['exports']['visualization_png_base64']))
152
+ "
153
+ ```
154
+
155
+ ### JavaScript/Node.js
156
+
157
+ ```javascript
158
+ const FormData = require('form-data');
159
+ const fs = require('fs');
160
+ const axios = require('axios');
161
+
162
+ async function analyzeImage(imagePath) {
163
+ const formData = new FormData();
164
+ formData.append('file', fs.createReadStream(imagePath));
165
+
166
+ const response = await axios.post(
167
+ 'http://127.0.0.1:8001/analyze',
168
+ formData,
169
+ { headers: formData.getHeaders() }
170
+ );
171
+
172
+ const data = response.data;
173
+ console.log(`Found ${data.analysis.total_elements_detected} UI elements`);
174
+
175
+ // Save CSV
176
+ fs.writeFileSync('coordinates.csv', data.exports.csv_data);
177
+
178
+ // Save visualization
179
+ const vizBuffer = Buffer.from(data.exports.visualization_png_base64, 'base64');
180
+ fs.writeFileSync('visualization.png', vizBuffer);
181
+
182
+ return data;
183
+ }
184
+
185
+ analyzeImage('screenshot.png').catch(console.error);
186
+ ```
187
+
188
+ ## Response Data Structure
189
+
190
+ Each UI element contains:
191
+
192
+ ```json
193
+ {
194
+ "template_id": "crop_0031", // Element identifier
195
+ "template_file": "crop_0031.png", // Source template file
196
+ "confidence": 1.0, // Matching confidence (0-1)
197
+ "bbox": {
198
+ "x1": 587, // Top-left X
199
+ "y1": 393, // Top-left Y
200
+ "x2": 763, // Bottom-right X
201
+ "y2": 441, // Bottom-right Y
202
+ "width": 176, // Element width
203
+ "height": 48 // Element height
204
+ },
205
+ "center": {
206
+ "x": 675, // Center X (for clicking)
207
+ "y": 417 // Center Y (for clicking)
208
+ },
209
+ "bbox_ratio": {
210
+ "x1": 0.430, // Normalized X1 (0-1)
211
+ "y1": 0.512, // Normalized Y1 (0-1)
212
+ "x2": 0.559, // Normalized X2 (0-1)
213
+ "y2": 0.575 // Normalized Y2 (0-1)
214
+ }
215
+ }
216
+ ```
217
+
218
+ ## Export Formats
219
+
220
+ ### JSON
221
+ Complete structured data with all coordinates, confidence scores, and metadata.
222
+
223
+ ### CSV
224
+ Spreadsheet-friendly format with columns:
225
+ - Element_ID
226
+ - Template_File
227
+ - Confidence
228
+ - X1, Y1, X2, Y2 (pixel coordinates)
229
+ - Width, Height
230
+ - Center_X, Center_Y
231
+ - Ratio_X1, Ratio_Y1, Ratio_X2, Ratio_Y2
232
+
233
+ ### Visualization PNG
234
+ High-resolution image with:
235
+ - Green bounding boxes around each element
236
+ - Red center point marker
237
+ - Element ID and confidence label for each box
238
+
239
+ ## Server-Side File Storage
240
+
241
+ The server maintains a temporary cropped images directory:
242
+ ```
243
+ /tmp/omoi_cropped_images/
244
+ β”œβ”€β”€ crop_0000.png
245
+ β”œβ”€β”€ crop_0001.png
246
+ β”œβ”€β”€ crop_0002.png
247
+ └── ... (120+ images)
248
+ ```
249
+
250
+ These files are:
251
+ - βœ… Used for template matching
252
+ - βœ… Kept on server for reference
253
+ - ❌ NOT sent to clients
254
+ - ❌ Cleared on server restart
255
+
256
+ ## Performance
257
+
258
+ Typical performance on CPU:
259
+ - OmniParser detection: ~10 seconds
260
+ - Template matching: ~5 seconds
261
+ - Total: ~15 seconds per screenshot
262
+
263
+ ## Architecture
264
+
265
+ ```
266
+ Client Request (PNG)
267
+ ↓
268
+ [API Server]
269
+ 1. Receives PNG
270
+ 2. Runs OmniParser
271
+ β”œβ”€ Detects UI elements
272
+ └─ Saves cropped images (server-side only)
273
+ 3. Template matches crops back to original
274
+ 4. Generates coordinates
275
+ 5. Creates visualization
276
+ 6. Exports to JSON/CSV
277
+ ↓
278
+ Client Response (JSON, CSV, PNG)
279
+ - Coordinates metadata
280
+ - CSV data
281
+ - Visualization image
282
+ (NO cropped images to client)
283
+ ```
284
+
285
+ ## Coordinate Systems
286
+
287
+ ### Absolute Coordinates
288
+ Pixel coordinates in the original image:
289
+ - `bbox.x1, bbox.y1`: Top-left corner
290
+ - `bbox.x2, bbox.y2`: Bottom-right corner
291
+ - `center.x, center.y`: Center point (use for mouse clicks)
292
+
293
+ ### Normalized Coordinates
294
+ 0-1 scale for responsive designs:
295
+ - `bbox_ratio.x1, bbox_ratio.y1`: Top-left (normalized)
296
+ - `bbox_ratio.x2, bbox_ratio.y2`: Bottom-right (normalized)
297
+ - Useful for scaling to different screen sizes
298
+
299
+ ## Tips
300
+
301
+ 1. **Clicking Elements**: Use `center.x` and `center.y` for mouse position
302
+ 2. **Validation**: All elements have `confidence: 1.0` (perfect match)
303
+ 3. **Filtering**: Use `bbox_ratio` for responsive element filtering
304
+ 4. **Region Queries**: Client library supports finding elements in bounding boxes
305
+ 5. **Batch Processing**: Queue multiple images for analysis
306
+
307
+ ## Troubleshooting
308
+
309
+ **"OmniParser not initialized"** - Server failed to load models, check logs
310
+
311
+ **"Failed to decode image"** - Ensure you're uploading valid PNG/JPG files
312
+
313
+ **"Cropped images directory not found"** - OmniParser detection failed, check input image
314
+
315
+ **Timeout** - Processing large images takes time, increase request timeout
316
+
317
+ ## Files
318
+
319
+ - `ui_element_api_server.py` - Main API server
320
+ - `ui_element_client.py` - Python client library
321
+ - `ui_element_locator.py` - Template matching utility
322
+ - `ui_element_analyzer.py` - Analysis and export utilities
323
+
324
+ ---
325
+
326
+ **Status**: βœ… Production Ready
327
+ **Last Updated**: April 17, 2026