Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -477,13 +477,17 @@ def predict(
|
|
| 477 |
del objects_mask
|
| 478 |
gc.collect()
|
| 479 |
print("Mask dilation completed in {:.2f} seconds".format(time.time() - t))
|
|
|
|
| 480 |
Image.fromarray(dilated_mask).save("./outputs/scaled_mask_new.jpg")
|
|
|
|
| 481 |
t = time.time()
|
| 482 |
outlines, contours = extract_outlines(dilated_mask)
|
| 483 |
-
|
|
|
|
|
|
|
| 484 |
del shrunked_img
|
| 485 |
gc.collect()
|
| 486 |
-
|
| 487 |
t = time.time()
|
| 488 |
use_finger_clearance = True if finger_clearance.lower() == "yes" else False
|
| 489 |
doc, final_polygons_inch = save_dxf_spline(contours, scaling_factor, processed_size[0], finger_clearance=use_finger_clearance)
|
|
@@ -495,7 +499,7 @@ def predict(
|
|
| 495 |
boundary_polygon = add_rectangular_boundary(doc, final_polygons_inch, boundary_length, boundary_width, boundary_unit)
|
| 496 |
if boundary_polygon is not None:
|
| 497 |
final_polygons_inch.append(boundary_polygon)
|
| 498 |
-
# --- Annotation Text Placement (
|
| 499 |
min_x = float("inf")
|
| 500 |
min_y = float("inf")
|
| 501 |
max_x = -float("inf")
|
|
@@ -525,19 +529,20 @@ def predict(
|
|
| 525 |
text_entity.dxf.insert = (text_x, text_y)
|
| 526 |
dxf_filepath = os.path.join("./outputs", "out.dxf")
|
| 527 |
doc.saveas(dxf_filepath)
|
| 528 |
-
# ---
|
| 529 |
-
draw_polygons_inch(final_polygons_inch,
|
| 530 |
-
|
| 531 |
-
|
|
|
|
| 532 |
if annotation_text.strip():
|
| 533 |
text_px = int(text_x / scaling_factor)
|
| 534 |
text_py = int(processed_size[0] - (text_y / scaling_factor))
|
| 535 |
-
cv2.putText(
|
| 536 |
-
cv2.putText(
|
| 537 |
-
outlines_color = cv2.cvtColor(
|
| 538 |
print("Total prediction time: {:.2f} seconds".format(time.time() - overall_start))
|
| 539 |
return (
|
| 540 |
-
cv2.cvtColor(
|
| 541 |
outlines_color,
|
| 542 |
dxf_filepath,
|
| 543 |
dilated_mask,
|
|
|
|
| 477 |
del objects_mask
|
| 478 |
gc.collect()
|
| 479 |
print("Mask dilation completed in {:.2f} seconds".format(time.time() - t))
|
| 480 |
+
# Save the dilated mask for debugging if needed.
|
| 481 |
Image.fromarray(dilated_mask).save("./outputs/scaled_mask_new.jpg")
|
| 482 |
+
# --- Extract outlines (only used for DXF generation) ---
|
| 483 |
t = time.time()
|
| 484 |
outlines, contours = extract_outlines(dilated_mask)
|
| 485 |
+
print("Outline extraction completed in {:.2f} seconds".format(time.time() - t))
|
| 486 |
+
# Instead of drawing the original contours, we now prepare a clean copy of the shrunk image for drawing new contours.
|
| 487 |
+
output_img = shrunked_img.copy()
|
| 488 |
del shrunked_img
|
| 489 |
gc.collect()
|
| 490 |
+
# --- Generate DXF using the extracted contours and apply finger clearance ---
|
| 491 |
t = time.time()
|
| 492 |
use_finger_clearance = True if finger_clearance.lower() == "yes" else False
|
| 493 |
doc, final_polygons_inch = save_dxf_spline(contours, scaling_factor, processed_size[0], finger_clearance=use_finger_clearance)
|
|
|
|
| 499 |
boundary_polygon = add_rectangular_boundary(doc, final_polygons_inch, boundary_length, boundary_width, boundary_unit)
|
| 500 |
if boundary_polygon is not None:
|
| 501 |
final_polygons_inch.append(boundary_polygon)
|
| 502 |
+
# --- Annotation Text Placement (Centered horizontally) ---
|
| 503 |
min_x = float("inf")
|
| 504 |
min_y = float("inf")
|
| 505 |
max_x = -float("inf")
|
|
|
|
| 529 |
text_entity.dxf.insert = (text_x, text_y)
|
| 530 |
dxf_filepath = os.path.join("./outputs", "out.dxf")
|
| 531 |
doc.saveas(dxf_filepath)
|
| 532 |
+
# --- Draw only the new contours (final_polygons_inch) on the clean output image ---
|
| 533 |
+
draw_polygons_inch(final_polygons_inch, output_img, scaling_factor, processed_size[0], color=(0,0,255), thickness=2)
|
| 534 |
+
# Also prepare an "Outlines" image based on a blank canvas for clarity.
|
| 535 |
+
new_outlines = np.zeros_like(output_img)
|
| 536 |
+
draw_polygons_inch(final_polygons_inch, new_outlines, scaling_factor, processed_size[0], color=(0,0,255), thickness=2)
|
| 537 |
if annotation_text.strip():
|
| 538 |
text_px = int(text_x / scaling_factor)
|
| 539 |
text_py = int(processed_size[0] - (text_y / scaling_factor))
|
| 540 |
+
cv2.putText(output_img, annotation_text.strip(), (text_px, text_py), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
|
| 541 |
+
cv2.putText(new_outlines, annotation_text.strip(), (text_px, text_py), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
|
| 542 |
+
outlines_color = cv2.cvtColor(new_outlines, cv2.COLOR_BGR2RGB)
|
| 543 |
print("Total prediction time: {:.2f} seconds".format(time.time() - overall_start))
|
| 544 |
return (
|
| 545 |
+
cv2.cvtColor(output_img, cv2.COLOR_BGR2RGB),
|
| 546 |
outlines_color,
|
| 547 |
dxf_filepath,
|
| 548 |
dilated_mask,
|