Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -458,7 +458,7 @@ def predict(
|
|
458 |
# Apply brightness and sharpness enhancement.
|
459 |
if isinstance(image, np.ndarray):
|
460 |
pil_image = Image.fromarray(image)
|
461 |
-
Bright = ImageEnhance.Brightness(pil_image).enhance(1)
|
462 |
enhanced_image = ImageEnhance.Sharpness(Bright).enhance(0.5)
|
463 |
image = np.array(enhanced_image)
|
464 |
|
@@ -469,13 +469,14 @@ def predict(
|
|
469 |
t = time.time()
|
470 |
drawer_img = yolo_detect(image)
|
471 |
print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
|
472 |
-
|
473 |
-
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
|
|
479 |
|
480 |
# ---------------------
|
481 |
# 2) Detect the reference box with YOLO
|
@@ -484,8 +485,8 @@ def predict(
|
|
484 |
t = time.time()
|
485 |
reference_obj_img, scaling_box_coords = detect_reference_square(shrunked_img)
|
486 |
print("Reference square detection completed in {:.2f} seconds".format(time.time() - t))
|
487 |
-
except ReferenceBoxNotDetectedError:
|
488 |
-
|
489 |
|
490 |
# ---------------------
|
491 |
# 3) Remove background of the reference box to compute scaling factor
|
@@ -523,7 +524,6 @@ def predict(
|
|
523 |
image_height_px, image_width_px = shrunked_img.shape[:2]
|
524 |
image_height_in = image_height_px * scaling_factor
|
525 |
image_width_in = image_width_px * scaling_factor
|
526 |
-
# If units are mm, convert them to inches if the values look small
|
527 |
if offset_unit.lower() == "mm":
|
528 |
if boundary_length < 50:
|
529 |
boundary_length = boundary_length * 25.4
|
@@ -535,7 +535,6 @@ def predict(
|
|
535 |
boundary_length_in = boundary_length
|
536 |
boundary_width_in = boundary_width
|
537 |
|
538 |
-
# Basic check so user doesn't request an impossible boundary
|
539 |
if boundary_length_in > (image_height_in - 1) or boundary_width_in > (image_width_in - 1):
|
540 |
raise BoundaryExceedsError(
|
541 |
"Error: The specified boundary dimensions exceed the allowed image dimensions. Please enter smaller values."
|
@@ -556,7 +555,6 @@ def predict(
|
|
556 |
objects_mask = remove_bg(shrunked_img)
|
557 |
processed_size = objects_mask.shape[:2]
|
558 |
|
559 |
-
# Exclude the reference box region from the mask
|
560 |
objects_mask = exclude_scaling_box(objects_mask, scaling_box_coords, orig_size, processed_size, expansion_factor=1.2)
|
561 |
objects_mask = resize_img(objects_mask, (shrunked_img.shape[1], shrunked_img.shape[0]))
|
562 |
del scaling_box_coords
|
@@ -608,7 +606,7 @@ def predict(
|
|
608 |
inner_max_y = max(inner_max_y, b[3])
|
609 |
|
610 |
# ---------------------
|
611 |
-
# 7) Add optional rectangular boundary
|
612 |
# ---------------------
|
613 |
boundary_polygon = None
|
614 |
if add_boundary.lower() == "yes":
|
@@ -619,16 +617,14 @@ def predict(
|
|
619 |
boundary_width,
|
620 |
offset_unit,
|
621 |
annotation_text,
|
622 |
-
image_height_in=
|
623 |
-
image_width_in=
|
624 |
)
|
625 |
if boundary_polygon is not None:
|
626 |
final_polygons_inch.append(boundary_polygon)
|
627 |
|
628 |
# ---------------------
|
629 |
# 8) Add annotation text (if provided) in the DXF
|
630 |
-
# - Text is 0.5 inches high
|
631 |
-
# - Placed so that its top edge is 0.125 inches below the inner tools' contour
|
632 |
# ---------------------
|
633 |
msp = doc.modelspace()
|
634 |
if annotation_text.strip():
|
|
|
458 |
# Apply brightness and sharpness enhancement.
|
459 |
if isinstance(image, np.ndarray):
|
460 |
pil_image = Image.fromarray(image)
|
461 |
+
Bright = ImageEnhance.Brightness(pil_image).enhance(1)
|
462 |
enhanced_image = ImageEnhance.Sharpness(Bright).enhance(0.5)
|
463 |
image = np.array(enhanced_image)
|
464 |
|
|
|
469 |
t = time.time()
|
470 |
drawer_img = yolo_detect(image)
|
471 |
print("Drawer detection completed in {:.2f} seconds".format(time.time() - t))
|
472 |
+
except DrawerNotDetectedError as e:
|
473 |
+
return None, None, None, None, f"Error: {str(e)}"
|
474 |
+
# Ensure that shrunked_img is defined only after successful detection.
|
475 |
+
t = time.time()
|
476 |
+
shrunked_img = make_square(shrink_bbox(drawer_img, 0.90))
|
477 |
+
del drawer_img
|
478 |
+
gc.collect()
|
479 |
+
print("Image shrinking completed in {:.2f} seconds".format(time.time() - t))
|
480 |
|
481 |
# ---------------------
|
482 |
# 2) Detect the reference box with YOLO
|
|
|
485 |
t = time.time()
|
486 |
reference_obj_img, scaling_box_coords = detect_reference_square(shrunked_img)
|
487 |
print("Reference square detection completed in {:.2f} seconds".format(time.time() - t))
|
488 |
+
except ReferenceBoxNotDetectedError as e:
|
489 |
+
return None, None, None, None, f"Error: {str(e)}"
|
490 |
|
491 |
# ---------------------
|
492 |
# 3) Remove background of the reference box to compute scaling factor
|
|
|
524 |
image_height_px, image_width_px = shrunked_img.shape[:2]
|
525 |
image_height_in = image_height_px * scaling_factor
|
526 |
image_width_in = image_width_px * scaling_factor
|
|
|
527 |
if offset_unit.lower() == "mm":
|
528 |
if boundary_length < 50:
|
529 |
boundary_length = boundary_length * 25.4
|
|
|
535 |
boundary_length_in = boundary_length
|
536 |
boundary_width_in = boundary_width
|
537 |
|
|
|
538 |
if boundary_length_in > (image_height_in - 1) or boundary_width_in > (image_width_in - 1):
|
539 |
raise BoundaryExceedsError(
|
540 |
"Error: The specified boundary dimensions exceed the allowed image dimensions. Please enter smaller values."
|
|
|
555 |
objects_mask = remove_bg(shrunked_img)
|
556 |
processed_size = objects_mask.shape[:2]
|
557 |
|
|
|
558 |
objects_mask = exclude_scaling_box(objects_mask, scaling_box_coords, orig_size, processed_size, expansion_factor=1.2)
|
559 |
objects_mask = resize_img(objects_mask, (shrunked_img.shape[1], shrunked_img.shape[0]))
|
560 |
del scaling_box_coords
|
|
|
606 |
inner_max_y = max(inner_max_y, b[3])
|
607 |
|
608 |
# ---------------------
|
609 |
+
# 7) Add optional rectangular boundary
|
610 |
# ---------------------
|
611 |
boundary_polygon = None
|
612 |
if add_boundary.lower() == "yes":
|
|
|
617 |
boundary_width,
|
618 |
offset_unit,
|
619 |
annotation_text,
|
620 |
+
image_height_in=output_img.shape[0] * scaling_factor,
|
621 |
+
image_width_in=output_img.shape[1] * scaling_factor
|
622 |
)
|
623 |
if boundary_polygon is not None:
|
624 |
final_polygons_inch.append(boundary_polygon)
|
625 |
|
626 |
# ---------------------
|
627 |
# 8) Add annotation text (if provided) in the DXF
|
|
|
|
|
628 |
# ---------------------
|
629 |
msp = doc.modelspace()
|
630 |
if annotation_text.strip():
|