Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -349,7 +349,7 @@ def save_dxf_spline(inflated_contours, scaling_factor, height, finger_clearance=
|
|
| 349 |
|
| 350 |
def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width, offset_unit, annotation_text="", image_height_in=None, image_width_in=None):
|
| 351 |
msp = doc.modelspace()
|
| 352 |
-
# Convert from mm if necessary
|
| 353 |
if offset_unit.lower() == "mm":
|
| 354 |
if boundary_length < 50:
|
| 355 |
boundary_length = boundary_length * 25.4
|
|
@@ -361,7 +361,7 @@ def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width
|
|
| 361 |
boundary_length_in = boundary_length
|
| 362 |
boundary_width_in = boundary_width
|
| 363 |
|
| 364 |
-
# Compute bounding box of inner contours
|
| 365 |
min_x = float("inf")
|
| 366 |
min_y = float("inf")
|
| 367 |
max_x = -float("inf")
|
|
@@ -376,30 +376,30 @@ def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width
|
|
| 376 |
print("No tool polygons found, skipping boundary.")
|
| 377 |
return None
|
| 378 |
|
| 379 |
-
# Compute inner bounding box dimensions
|
| 380 |
inner_width = max_x - min_x
|
| 381 |
inner_length = max_y - min_y
|
| 382 |
|
| 383 |
-
# Set clearance margins
|
| 384 |
clearance_side = 0.25 # left/right clearance
|
| 385 |
clearance_tb = 0.25 # top/bottom clearance
|
| 386 |
if annotation_text.strip():
|
| 387 |
clearance_tb = 0.75
|
| 388 |
|
| 389 |
-
# Check if
|
| 390 |
if boundary_width_in <= inner_width + 2 * clearance_side or boundary_length_in <= inner_length + 2 * clearance_tb:
|
| 391 |
raise BoundaryOverlapError("Error: The specified boundary dimensions are too small and overlap with the inner contours. Please provide larger values.")
|
| 392 |
|
| 393 |
-
# Check if boundary exceeds image limits (
|
| 394 |
if image_height_in is not None and image_width_in is not None:
|
| 395 |
if boundary_length_in > (image_height_in - 1) or boundary_width_in > (image_width_in - 1):
|
| 396 |
raise BoundaryExceedsError("Error: The specified boundary dimensions exceed the allowed image dimensions. Please enter smaller values.")
|
| 397 |
|
| 398 |
-
# Calculate
|
| 399 |
center_x = (min_x + max_x) / 2
|
| 400 |
center_y = (min_y + max_y) / 2
|
| 401 |
|
| 402 |
-
# Draw rectangle centered at (center_x, center_y)
|
| 403 |
left = center_x - boundary_width_in / 2
|
| 404 |
right = center_x + boundary_width_in / 2
|
| 405 |
bottom = center_y - boundary_length_in / 2
|
|
@@ -445,7 +445,7 @@ def predict(
|
|
| 445 |
annotation_text: str
|
| 446 |
):
|
| 447 |
overall_start = time.time()
|
| 448 |
-
# Convert image to NumPy array if needed
|
| 449 |
if isinstance(image, str):
|
| 450 |
if os.path.exists(image):
|
| 451 |
image = np.array(Image.open(image).convert("RGB"))
|
|
@@ -455,7 +455,7 @@ def predict(
|
|
| 455 |
except Exception:
|
| 456 |
raise ValueError("Invalid base64 image data")
|
| 457 |
|
| 458 |
-
# Apply brightness and sharpness enhancement
|
| 459 |
if isinstance(image, np.ndarray):
|
| 460 |
pil_image = Image.fromarray(image)
|
| 461 |
enhanced_image = ImageEnhance.Sharpness(pil_image).enhance(0.5)
|
|
@@ -470,7 +470,7 @@ def predict(
|
|
| 470 |
print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
|
| 471 |
except DrawerNotDetectedError as e:
|
| 472 |
return None, None, None, None, f"Error: {str(e)}"
|
| 473 |
-
# Ensure that shrunked_img is defined only after successful detection
|
| 474 |
t = time.time()
|
| 475 |
shrunked_img = make_square(shrink_bbox(drawer_img, 0.90))
|
| 476 |
del drawer_img
|
|
@@ -492,7 +492,7 @@ def predict(
|
|
| 492 |
# ---------------------
|
| 493 |
t = time.time()
|
| 494 |
reference_obj_img = make_square(reference_obj_img)
|
| 495 |
-
reference_square_mask =
|
| 496 |
print("Reference image processing completed in {:.2f} seconds".format(time.time() - t))
|
| 497 |
|
| 498 |
t = time.time()
|
|
|
|
| 349 |
|
| 350 |
def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width, offset_unit, annotation_text="", image_height_in=None, image_width_in=None):
|
| 351 |
msp = doc.modelspace()
|
| 352 |
+
# Convert from mm if necessary
|
| 353 |
if offset_unit.lower() == "mm":
|
| 354 |
if boundary_length < 50:
|
| 355 |
boundary_length = boundary_length * 25.4
|
|
|
|
| 361 |
boundary_length_in = boundary_length
|
| 362 |
boundary_width_in = boundary_width
|
| 363 |
|
| 364 |
+
# Compute bounding box of inner contours
|
| 365 |
min_x = float("inf")
|
| 366 |
min_y = float("inf")
|
| 367 |
max_x = -float("inf")
|
|
|
|
| 376 |
print("No tool polygons found, skipping boundary.")
|
| 377 |
return None
|
| 378 |
|
| 379 |
+
# Compute inner bounding box dimensions
|
| 380 |
inner_width = max_x - min_x
|
| 381 |
inner_length = max_y - min_y
|
| 382 |
|
| 383 |
+
# Set clearance margins
|
| 384 |
clearance_side = 0.25 # left/right clearance
|
| 385 |
clearance_tb = 0.25 # top/bottom clearance
|
| 386 |
if annotation_text.strip():
|
| 387 |
clearance_tb = 0.75
|
| 388 |
|
| 389 |
+
# Check if boundary dimensions are at least larger than inner box plus clearance
|
| 390 |
if boundary_width_in <= inner_width + 2 * clearance_side or boundary_length_in <= inner_length + 2 * clearance_tb:
|
| 391 |
raise BoundaryOverlapError("Error: The specified boundary dimensions are too small and overlap with the inner contours. Please provide larger values.")
|
| 392 |
|
| 393 |
+
# Check if boundary exceeds image limits (1 inch less than image dimensions)
|
| 394 |
if image_height_in is not None and image_width_in is not None:
|
| 395 |
if boundary_length_in > (image_height_in - 1) or boundary_width_in > (image_width_in - 1):
|
| 396 |
raise BoundaryExceedsError("Error: The specified boundary dimensions exceed the allowed image dimensions. Please enter smaller values.")
|
| 397 |
|
| 398 |
+
# Calculate center of inner contours
|
| 399 |
center_x = (min_x + max_x) / 2
|
| 400 |
center_y = (min_y + max_y) / 2
|
| 401 |
|
| 402 |
+
# Draw rectangle centered at (center_x, center_y)
|
| 403 |
left = center_x - boundary_width_in / 2
|
| 404 |
right = center_x + boundary_width_in / 2
|
| 405 |
bottom = center_y - boundary_length_in / 2
|
|
|
|
| 445 |
annotation_text: str
|
| 446 |
):
|
| 447 |
overall_start = time.time()
|
| 448 |
+
# Convert image to NumPy array if needed
|
| 449 |
if isinstance(image, str):
|
| 450 |
if os.path.exists(image):
|
| 451 |
image = np.array(Image.open(image).convert("RGB"))
|
|
|
|
| 455 |
except Exception:
|
| 456 |
raise ValueError("Invalid base64 image data")
|
| 457 |
|
| 458 |
+
# Apply brightness and sharpness enhancement
|
| 459 |
if isinstance(image, np.ndarray):
|
| 460 |
pil_image = Image.fromarray(image)
|
| 461 |
enhanced_image = ImageEnhance.Sharpness(pil_image).enhance(0.5)
|
|
|
|
| 470 |
print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
|
| 471 |
except DrawerNotDetectedError as e:
|
| 472 |
return None, None, None, None, f"Error: {str(e)}"
|
| 473 |
+
# Ensure that shrunked_img is defined only after successful detection
|
| 474 |
t = time.time()
|
| 475 |
shrunked_img = make_square(shrink_bbox(drawer_img, 0.90))
|
| 476 |
del drawer_img
|
|
|
|
| 492 |
# ---------------------
|
| 493 |
t = time.time()
|
| 494 |
reference_obj_img = make_square(reference_obj_img)
|
| 495 |
+
reference_square_mask = remove_bg_u2netp(reference_obj_img)
|
| 496 |
print("Reference image processing completed in {:.2f} seconds".format(time.time() - t))
|
| 497 |
|
| 498 |
t = time.time()
|