Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -452,7 +452,7 @@ def predict(
|
|
452 |
# Apply sharpness enhancement.
|
453 |
if isinstance(image, np.ndarray):
|
454 |
pil_image = Image.fromarray(image)
|
455 |
-
enhanced_image = ImageEnhance.Sharpness(pil_image).enhance(
|
456 |
image = np.array(enhanced_image)
|
457 |
|
458 |
# ---------------------
|
@@ -586,6 +586,20 @@ def predict(
|
|
586 |
gc.collect()
|
587 |
print("DXF generation completed in {:.2f} seconds".format(time.time() - t))
|
588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
589 |
# ---------------------
|
590 |
# 7) Add optional rectangular boundary (with the 0.75 bottom margin if annotation text is provided)
|
591 |
# ---------------------
|
@@ -597,39 +611,19 @@ def predict(
|
|
597 |
if boundary_polygon is not None:
|
598 |
final_polygons_inch.append(boundary_polygon)
|
599 |
|
600 |
-
# Compute bounding box of all polygons to know where to place text
|
601 |
-
min_x = float("inf")
|
602 |
-
min_y = float("inf")
|
603 |
-
max_x = -float("inf")
|
604 |
-
max_y = -float("inf")
|
605 |
-
for poly in final_polygons_inch:
|
606 |
-
b = poly.bounds
|
607 |
-
if b[0] < min_x:
|
608 |
-
min_x = b[0]
|
609 |
-
if b[1] < min_y:
|
610 |
-
min_y = b[1]
|
611 |
-
if b[2] > max_x:
|
612 |
-
max_x = b[2]
|
613 |
-
if b[3] > max_y:
|
614 |
-
max_y = b[3]
|
615 |
-
|
616 |
# ---------------------
|
617 |
# 8) Add annotation text (if provided) in the DXF
|
618 |
# - Text is 0.5 inches high
|
619 |
-
# - Placed so that
|
620 |
# ---------------------
|
621 |
msp = doc.modelspace()
|
622 |
if annotation_text.strip():
|
623 |
-
|
624 |
-
|
625 |
-
|
626 |
-
#
|
627 |
-
|
628 |
-
|
629 |
-
else:
|
630 |
-
# If no boundary, place it 0.125 above the min_y
|
631 |
-
text_y_dxf = min_y - 0.125
|
632 |
-
|
633 |
text_entity = msp.add_text(
|
634 |
annotation_text.strip(),
|
635 |
dxfattribs={
|
@@ -652,16 +646,17 @@ def predict(
|
|
652 |
draw_polygons_inch(final_polygons_inch, new_outlines, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
|
653 |
|
654 |
if annotation_text.strip():
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
659 |
-
|
660 |
-
|
661 |
-
|
662 |
-
|
663 |
-
|
664 |
-
|
|
|
665 |
|
666 |
cv2.putText(
|
667 |
output_img,
|
|
|
452 |
# Apply sharpness enhancement.
|
453 |
if isinstance(image, np.ndarray):
|
454 |
pil_image = Image.fromarray(image)
|
455 |
+
enhanced_image = ImageEnhance.Sharpness(pil_image).enhance(1.5)
|
456 |
image = np.array(enhanced_image)
|
457 |
|
458 |
# ---------------------
|
|
|
586 |
gc.collect()
|
587 |
print("DXF generation completed in {:.2f} seconds".format(time.time() - t))
|
588 |
|
589 |
+
# ---------------------
|
590 |
+
# Compute bounding box of inner tool contours BEFORE adding optional boundary
|
591 |
+
# ---------------------
|
592 |
+
inner_min_x = float("inf")
|
593 |
+
inner_min_y = float("inf")
|
594 |
+
inner_max_x = -float("inf")
|
595 |
+
inner_max_y = -float("inf")
|
596 |
+
for poly in final_polygons_inch:
|
597 |
+
b = poly.bounds
|
598 |
+
inner_min_x = min(inner_min_x, b[0])
|
599 |
+
inner_min_y = min(inner_min_y, b[1])
|
600 |
+
inner_max_x = max(inner_max_x, b[2])
|
601 |
+
inner_max_y = max(inner_max_y, b[3])
|
602 |
+
|
603 |
# ---------------------
|
604 |
# 7) Add optional rectangular boundary (with the 0.75 bottom margin if annotation text is provided)
|
605 |
# ---------------------
|
|
|
611 |
if boundary_polygon is not None:
|
612 |
final_polygons_inch.append(boundary_polygon)
|
613 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
614 |
# ---------------------
|
615 |
# 8) Add annotation text (if provided) in the DXF
|
616 |
# - Text is 0.5 inches high
|
617 |
+
# - Placed so that its top edge is 0.125 inches below the inner tools' contour
|
618 |
# ---------------------
|
619 |
msp = doc.modelspace()
|
620 |
if annotation_text.strip():
|
621 |
+
# Use the inner contours bounding box computed earlier.
|
622 |
+
text_x = (inner_min_x + inner_max_x) / 2.0
|
623 |
+
text_height_dxf = 0.5
|
624 |
+
# Place the text so that its top edge is 0.125 inches below the inner tool contour.
|
625 |
+
# Since text height is 0.5 inches, the baseline is at inner_min_y - 0.125 - text_height_dxf.
|
626 |
+
text_y_dxf = inner_min_y - 0.125 - text_height_dxf
|
|
|
|
|
|
|
|
|
627 |
text_entity = msp.add_text(
|
628 |
annotation_text.strip(),
|
629 |
dxfattribs={
|
|
|
646 |
draw_polygons_inch(final_polygons_inch, new_outlines, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
|
647 |
|
648 |
if annotation_text.strip():
|
649 |
+
# Use the same logic as DXF:
|
650 |
+
# Text is 0.5 inches high, and its baseline is at inner_min_y - 0.125 - 0.5.
|
651 |
+
text_height_cv = 0.5 # logical height in inches
|
652 |
+
# Calculate horizontal center (in inches) and convert to pixels
|
653 |
+
text_x_img = int(((inner_min_x + inner_max_x) / 2.0) / scaling_factor)
|
654 |
+
# Baseline position in inches:
|
655 |
+
text_y_in = inner_min_y - 0.125 - text_height_cv
|
656 |
+
# Convert the baseline from inches to pixel coordinate
|
657 |
+
text_y_img = int(processed_size[0] - (text_y_in / scaling_factor))
|
658 |
+
# Shift left to center the text approximately
|
659 |
+
org = (text_x_img - int(len(annotation_text.strip()) * 6), text_y_img)
|
660 |
|
661 |
cv2.putText(
|
662 |
output_img,
|