Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -286,52 +286,36 @@ def polygon_to_exterior_coords(poly: Polygon):
|
|
286 |
return []
|
287 |
return list(poly.exterior.coords)
|
288 |
|
289 |
-
def place_finger_cut_adjusted(
|
290 |
-
tool_polygon,
|
291 |
-
points_inch,
|
292 |
-
existing_centers,
|
293 |
-
all_polygons,
|
294 |
-
circle_diameter=1.0,
|
295 |
-
min_gap=0.25,
|
296 |
-
max_attempts=30
|
297 |
-
):
|
298 |
import random
|
299 |
needed_center_distance = circle_diameter + min_gap
|
300 |
radius = circle_diameter / 2.0
|
301 |
attempts = 0
|
302 |
indices = list(range(len(points_inch)))
|
303 |
-
random.shuffle(indices) # Shuffle
|
304 |
|
305 |
for i in indices:
|
306 |
if attempts >= max_attempts:
|
307 |
break
|
308 |
cx, cy = points_inch[i]
|
309 |
-
# Try small adjustments around the candidate
|
310 |
for dx in np.linspace(-0.1, 0.1, 5):
|
311 |
for dy in np.linspace(-0.1, 0.1, 5):
|
312 |
candidate_center = (cx + dx, cy + dy)
|
313 |
# Check distance from already placed centers
|
314 |
if any(np.hypot(candidate_center[0] - ex, candidate_center[1] - ey) < needed_center_distance for ex, ey in existing_centers):
|
315 |
continue
|
316 |
-
# Build the circle polygon
|
317 |
circle_poly = Point(candidate_center).buffer(radius, resolution=64)
|
318 |
-
# Ensure the circle actually intersects the tool polygon
|
319 |
-
if not circle_poly.intersects(tool_polygon):
|
320 |
-
continue
|
321 |
-
# Compute the union
|
322 |
union_poly = tool_polygon.union(circle_poly)
|
323 |
-
# If the union is a MultiPolygon, then the circle is not fused with the tool
|
324 |
-
if union_poly.geom_type == "MultiPolygon":
|
325 |
-
continue
|
326 |
-
# Check overlap with other tools
|
327 |
overlap = False
|
|
|
328 |
for poly in all_polygons:
|
329 |
if union_poly.intersects(poly) or circle_poly.buffer(min_gap).intersects(poly):
|
330 |
overlap = True
|
331 |
break
|
332 |
if overlap:
|
333 |
continue
|
334 |
-
#
|
335 |
existing_centers.append(candidate_center)
|
336 |
return union_poly, candidate_center
|
337 |
attempts += 1
|
@@ -433,7 +417,7 @@ def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width
|
|
433 |
else:
|
434 |
if text_top > (min_y - 0.75):
|
435 |
raise TextOverlapError("Error: The Text is overlapping the inner contours of the object.")
|
436 |
-
|
437 |
return boundary_polygon
|
438 |
|
439 |
def draw_polygons_inch(polygons_inch, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
@@ -695,8 +679,10 @@ def predict(
|
|
695 |
text_y_img = int(processed_size[0] - (text_y_in / scaling_factor))
|
696 |
org = (text_x_img - int(len(annotation_text.strip()) * 6), text_y_img)
|
697 |
|
698 |
-
#
|
|
|
699 |
temp_img = np.zeros_like(output_img)
|
|
|
700 |
cv2.putText(
|
701 |
temp_img,
|
702 |
annotation_text.strip().upper(),
|
@@ -707,18 +693,21 @@ def predict(
|
|
707 |
4, # Thicker outline
|
708 |
cv2.LINE_AA
|
709 |
)
|
|
|
710 |
cv2.putText(
|
711 |
temp_img,
|
712 |
annotation_text.strip().upper(),
|
713 |
org,
|
714 |
cv2.FONT_HERSHEY_SIMPLEX,
|
715 |
2,
|
716 |
-
(0, 0, 0), # Black to create
|
717 |
2, # Thinner inner part
|
718 |
cv2.LINE_AA
|
719 |
)
|
|
|
720 |
outline_mask = cv2.cvtColor(temp_img, cv2.COLOR_BGR2GRAY)
|
721 |
_, outline_mask = cv2.threshold(outline_mask, 1, 255, cv2.THRESH_BINARY)
|
|
|
722 |
output_img[outline_mask > 0] = temp_img[outline_mask > 0]
|
723 |
|
724 |
cv2.putText(
|
@@ -731,6 +720,7 @@ def predict(
|
|
731 |
4, # Thicker outline
|
732 |
cv2.LINE_AA
|
733 |
)
|
|
|
734 |
cv2.putText(
|
735 |
new_outlines,
|
736 |
annotation_text.strip().upper(),
|
|
|
286 |
return []
|
287 |
return list(poly.exterior.coords)
|
288 |
|
289 |
+
def place_finger_cut_adjusted(tool_polygon, points_inch, existing_centers, all_polygons, circle_diameter=1.0, min_gap=0.25, max_attempts=30):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
import random
|
291 |
needed_center_distance = circle_diameter + min_gap
|
292 |
radius = circle_diameter / 2.0
|
293 |
attempts = 0
|
294 |
indices = list(range(len(points_inch)))
|
295 |
+
random.shuffle(indices) # Shuffle indices for randomness
|
296 |
|
297 |
for i in indices:
|
298 |
if attempts >= max_attempts:
|
299 |
break
|
300 |
cx, cy = points_inch[i]
|
301 |
+
# Try small adjustments around the chosen candidate
|
302 |
for dx in np.linspace(-0.1, 0.1, 5):
|
303 |
for dy in np.linspace(-0.1, 0.1, 5):
|
304 |
candidate_center = (cx + dx, cy + dy)
|
305 |
# Check distance from already placed centers
|
306 |
if any(np.hypot(candidate_center[0] - ex, candidate_center[1] - ey) < needed_center_distance for ex, ey in existing_centers):
|
307 |
continue
|
|
|
308 |
circle_poly = Point(candidate_center).buffer(radius, resolution=64)
|
|
|
|
|
|
|
|
|
309 |
union_poly = tool_polygon.union(circle_poly)
|
|
|
|
|
|
|
|
|
310 |
overlap = False
|
311 |
+
# Check against other tool polygons for overlap or proximity issues
|
312 |
for poly in all_polygons:
|
313 |
if union_poly.intersects(poly) or circle_poly.buffer(min_gap).intersects(poly):
|
314 |
overlap = True
|
315 |
break
|
316 |
if overlap:
|
317 |
continue
|
318 |
+
# If candidate passes, accept it
|
319 |
existing_centers.append(candidate_center)
|
320 |
return union_poly, candidate_center
|
321 |
attempts += 1
|
|
|
417 |
else:
|
418 |
if text_top > (min_y - 0.75):
|
419 |
raise TextOverlapError("Error: The Text is overlapping the inner contours of the object.")
|
420 |
+
|
421 |
return boundary_polygon
|
422 |
|
423 |
def draw_polygons_inch(polygons_inch, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
|
|
679 |
text_y_img = int(processed_size[0] - (text_y_in / scaling_factor))
|
680 |
org = (text_x_img - int(len(annotation_text.strip()) * 6), text_y_img)
|
681 |
|
682 |
+
# Method 2: Use two different thicknesses
|
683 |
+
# Draw thicker outline
|
684 |
temp_img = np.zeros_like(output_img)
|
685 |
+
|
686 |
cv2.putText(
|
687 |
temp_img,
|
688 |
annotation_text.strip().upper(),
|
|
|
693 |
4, # Thicker outline
|
694 |
cv2.LINE_AA
|
695 |
)
|
696 |
+
|
697 |
cv2.putText(
|
698 |
temp_img,
|
699 |
annotation_text.strip().upper(),
|
700 |
org,
|
701 |
cv2.FONT_HERSHEY_SIMPLEX,
|
702 |
2,
|
703 |
+
(0, 0, 0), # Black to create hole
|
704 |
2, # Thinner inner part
|
705 |
cv2.LINE_AA
|
706 |
)
|
707 |
+
|
708 |
outline_mask = cv2.cvtColor(temp_img, cv2.COLOR_BGR2GRAY)
|
709 |
_, outline_mask = cv2.threshold(outline_mask, 1, 255, cv2.THRESH_BINARY)
|
710 |
+
|
711 |
output_img[outline_mask > 0] = temp_img[outline_mask > 0]
|
712 |
|
713 |
cv2.putText(
|
|
|
720 |
4, # Thicker outline
|
721 |
cv2.LINE_AA
|
722 |
)
|
723 |
+
|
724 |
cv2.putText(
|
725 |
new_outlines,
|
726 |
annotation_text.strip().upper(),
|