makeitfr commited on
Commit
0423a76
·
verified ·
1 Parent(s): 15c985a

Upload ui_element_api_server.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. ui_element_api_server.py +12 -50
ui_element_api_server.py CHANGED
@@ -140,65 +140,27 @@ def visualize_matches(
140
  original_image_array: np.ndarray,
141
  matches: list
142
  ) -> np.ndarray:
143
- """Create clean visualization with smart label positioning."""
144
  img = original_image_array.copy()
145
 
146
- font = cv2.FONT_HERSHEY_DUPLEX
147
- font_scale = 0.7
148
- font_thickness = 1
149
-
150
- # Sort by confidence to prioritize top detections for labeling
151
- sorted_matches = sorted(matches, key=lambda x: x['confidence'], reverse=True)
152
-
153
- # Draw boxes for all matches
154
  for match in matches:
155
  bbox = match['bbox']
156
  center = match['center']
157
-
158
- # Draw bounding box - thicker and more visible
159
- color = (0, 255, 0) # Green
160
- cv2.rectangle(img, (bbox['x1'], bbox['y1']), (bbox['x2'], bbox['y2']), color, 2)
161
-
162
- # Draw center point with larger radius
163
- cv2.circle(img, (center['x'], center['y']), 4, (0, 0, 255), -1) # Red dot
164
- cv2.circle(img, (center['x'], center['y']), 5, (255, 255, 255), 1) # White outline
165
-
166
- # Draw labels only for top matches to reduce clutter
167
- num_labels = min(30, len(sorted_matches)) # Label only top 30
168
-
169
- for idx in range(num_labels):
170
- match = sorted_matches[idx]
171
- bbox = match['bbox']
172
  confidence = match['confidence']
 
173
 
174
- # Use simple index labels for top matches
175
- label = f"{idx + 1}"
176
-
177
- # Position label at top-right corner of box
178
- label_x = bbox['x2'] + 5
179
- label_y = bbox['y1'] + 15
180
-
181
- # Clamp coordinate to image bounds
182
- h, w = img.shape[:2]
183
- label_x = min(label_x, w - 30)
184
- label_y = min(label_y, h - 10)
185
-
186
- # Get text size
187
- text_size = cv2.getTextSize(label, font, font_scale, font_thickness)[0]
188
-
189
- # Background rectangle
190
- bg_pt1 = (label_x - 3, label_y - text_size[1] - 4)
191
- bg_pt2 = (label_x + text_size[0] + 3, label_y + 2)
192
 
193
- cv2.rectangle(img, bg_pt1, bg_pt2, (0, 0, 0), -1) # Black bg
194
- cv2.rectangle(img, bg_pt1, bg_pt2, (255, 255, 0), 2) # Cyan border
195
 
196
- # Draw label text
197
- cv2.putText(img, label, (label_x, label_y), font, font_scale, (255, 255, 0), font_thickness)
198
-
199
- # Add info text to image
200
- info_text = f"Total Elements: {len(matches)} | Top {num_labels} labeled"
201
- cv2.putText(img, info_text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
202
 
203
  return img
204
 
 
140
  original_image_array: np.ndarray,
141
  matches: list
142
  ) -> np.ndarray:
143
+ """Create visualization with bounding boxes."""
144
  img = original_image_array.copy()
145
 
 
 
 
 
 
 
 
 
146
  for match in matches:
147
  bbox = match['bbox']
148
  center = match['center']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  confidence = match['confidence']
150
+ template_id = match['template_id']
151
 
152
+ # Draw bounding box
153
+ color = (0, 255, 0) # Green
154
+ thickness = 2
155
+ cv2.rectangle(img, (bbox['x1'], bbox['y1']), (bbox['x2'], bbox['y2']), color, thickness)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
+ # Draw center point
158
+ cv2.circle(img, (center['x'], center['y']), 3, (0, 0, 255), -1) # Red
159
 
160
+ # Draw label
161
+ label = f"ID:{template_id} ({confidence:.2f})"
162
+ cv2.putText(img, label, (bbox['x1'], bbox['y1'] - 5),
163
+ cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 0, 0), 1)
 
 
164
 
165
  return img
166