Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -310,7 +310,8 @@ def calculate_paper_scaling_factor(paper_contour: np.ndarray, paper_size: str) -
|
|
| 310 |
scale_y = expected_height_mm / detected_height_px
|
| 311 |
|
| 312 |
# Use average of both scales
|
| 313 |
-
scaling_factor = (scale_x + scale_y) / 2
|
|
|
|
| 314 |
|
| 315 |
logger.info(f"Paper detection: {detected_width_px}x{detected_height_px} px -> {expected_width_mm}x{expected_height_mm} mm")
|
| 316 |
logger.info(f"Calculated scaling factor: {scaling_factor:.4f} mm/px")
|
|
@@ -408,11 +409,25 @@ def exclude_paper_area(mask: np.ndarray, paper_contour: np.ndarray, expansion_fa
|
|
| 408 |
# Create paper mask with slight expansion to ensure complete removal
|
| 409 |
paper_mask = np.zeros(mask.shape[:2], dtype=np.uint8)
|
| 410 |
|
| 411 |
-
# Expand paper contour slightly
|
| 412 |
-
epsilon = expansion_factor * cv2.arcLength(paper_contour, True)
|
| 413 |
-
expanded_contour = cv2.approxPolyDP(paper_contour, epsilon, True)
|
| 414 |
|
| 415 |
-
cv2.fillPoly(paper_mask, [expanded_contour], 255)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
|
| 417 |
# Invert paper mask and apply to object mask
|
| 418 |
paper_mask_inv = cv2.bitwise_not(paper_mask)
|
|
@@ -469,7 +484,7 @@ def save_dxf_spline(inflated_contours, scaling_factor, height, finger_clearance=
|
|
| 469 |
original_polygons = []
|
| 470 |
|
| 471 |
# Scale correction factor
|
| 472 |
-
scale_correction = 1.
|
| 473 |
|
| 474 |
for contour in inflated_contours:
|
| 475 |
try:
|
|
@@ -840,11 +855,13 @@ def predict_with_paper(image, paper_size, offset,offset_unit, finger_clearance=F
|
|
| 840 |
# Apply edge rounding if specified
|
| 841 |
rounded_mask = objects_mask.copy()
|
| 842 |
|
| 843 |
-
# Apply dilation for offset
|
| 844 |
if offset_mm > 0:
|
| 845 |
-
offset_pixels = (float(offset_mm) / scaling_factor)
|
| 846 |
-
|
| 847 |
-
|
|
|
|
|
|
|
| 848 |
else:
|
| 849 |
dilated_mask = rounded_mask.copy()
|
| 850 |
|
|
|
|
| 310 |
scale_y = expected_height_mm / detected_height_px
|
| 311 |
|
| 312 |
# Use average of both scales
|
| 313 |
+
# scaling_factor = (scale_x + scale_y) / 2
|
| 314 |
+
scaling_factor = min(scale_x, scale_y)
|
| 315 |
|
| 316 |
logger.info(f"Paper detection: {detected_width_px}x{detected_height_px} px -> {expected_width_mm}x{expected_height_mm} mm")
|
| 317 |
logger.info(f"Calculated scaling factor: {scaling_factor:.4f} mm/px")
|
|
|
|
| 409 |
# Create paper mask with slight expansion to ensure complete removal
|
| 410 |
paper_mask = np.zeros(mask.shape[:2], dtype=np.uint8)
|
| 411 |
|
| 412 |
+
# # Expand paper contour slightly
|
| 413 |
+
# epsilon = expansion_factor * cv2.arcLength(paper_contour, True)
|
| 414 |
+
# expanded_contour = cv2.approxPolyDP(paper_contour, epsilon, True)
|
| 415 |
|
| 416 |
+
# cv2.fillPoly(paper_mask, [expanded_contour], 255)
|
| 417 |
+
# Create a more aggressive inward shrinking of paper bounds
|
| 418 |
+
rect = cv2.boundingRect(paper_contour)
|
| 419 |
+
shrink_pixels = int(min(rect[2], rect[3]) * 0.05) # Shrink by 5% of smaller dimension
|
| 420 |
+
|
| 421 |
+
# Create shrunken rectangle
|
| 422 |
+
x, y, w, h = rect
|
| 423 |
+
shrunken_contour = np.array([
|
| 424 |
+
[[x + shrink_pixels, y + shrink_pixels]],
|
| 425 |
+
[[x + w - shrink_pixels, y + shrink_pixels]],
|
| 426 |
+
[[x + w - shrink_pixels, y + h - shrink_pixels]],
|
| 427 |
+
[[x + shrink_pixels, y + h - shrink_pixels]]
|
| 428 |
+
])
|
| 429 |
+
|
| 430 |
+
cv2.fillPoly(paper_mask, [shrunken_contour], 255)
|
| 431 |
|
| 432 |
# Invert paper mask and apply to object mask
|
| 433 |
paper_mask_inv = cv2.bitwise_not(paper_mask)
|
|
|
|
| 484 |
original_polygons = []
|
| 485 |
|
| 486 |
# Scale correction factor
|
| 487 |
+
scale_correction = 1.0
|
| 488 |
|
| 489 |
for contour in inflated_contours:
|
| 490 |
try:
|
|
|
|
| 855 |
# Apply edge rounding if specified
|
| 856 |
rounded_mask = objects_mask.copy()
|
| 857 |
|
| 858 |
+
# Apply dilation for offset - more precise calculation
|
| 859 |
if offset_mm > 0:
|
| 860 |
+
offset_pixels = int(round(float(offset_mm) / scaling_factor))
|
| 861 |
+
if offset_pixels > 0:
|
| 862 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (offset_pixels*2+1, offset_pixels*2+1))
|
| 863 |
+
dilated_mask = cv2.dilate(rounded_mask, kernel, iterations=1)
|
| 864 |
+
|
| 865 |
else:
|
| 866 |
dilated_mask = rounded_mask.copy()
|
| 867 |
|