Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -974,62 +974,47 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
|
|
974 |
raise gr.Error(f"Error processing image: {str(e)}")
|
975 |
|
976 |
try:
|
977 |
-
|
978 |
-
|
979 |
-
|
980 |
-
|
981 |
-
|
982 |
-
|
983 |
-
|
984 |
-
|
985 |
-
|
986 |
-
|
987 |
-
|
988 |
-
|
989 |
-
|
990 |
-
|
991 |
-
|
992 |
-
|
993 |
-
|
994 |
-
|
995 |
-
|
996 |
-
|
997 |
-
|
998 |
-
|
999 |
-
|
1000 |
-
|
1001 |
-
|
1002 |
-
|
1003 |
-
|
1004 |
-
|
1005 |
-
|
1006 |
-
|
1007 |
-
|
1008 |
-
|
1009 |
-
|
1010 |
-
|
1011 |
-
|
1012 |
-
|
1013 |
-
|
1014 |
-
|
1015 |
-
|
1016 |
-
|
1017 |
-
|
1018 |
-
full_mask[y_min:y_min+resized_mask.shape[0], x_min:x_min+resized_mask.shape[1]] = resized_mask
|
1019 |
-
|
1020 |
-
# Remove paper area from mask to focus only on objects
|
1021 |
-
objects_mask = exclude_paper_area(full_mask, paper_contour)
|
1022 |
-
|
1023 |
-
# Debug: Save intermediate masks
|
1024 |
-
cv2.imwrite("./debug/objects_mask_after_yolo.jpg", objects_mask)
|
1025 |
-
|
1026 |
-
# Check if we actually have object pixels after paper exclusion
|
1027 |
-
object_pixels = np.count_nonzero(objects_mask)
|
1028 |
-
if object_pixels < 300: # Minimum threshold
|
1029 |
-
raise NoObjectDetectedError("No significant object detected after excluding paper area")
|
1030 |
-
|
1031 |
-
# Validate single object
|
1032 |
-
validate_single_object(objects_mask, paper_contour)
|
1033 |
|
1034 |
except (MultipleObjectsError, NoObjectDetectedError) as e:
|
1035 |
return (
|
@@ -1065,7 +1050,7 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
|
|
1065 |
dxf, finger_polygons, original_polygons = save_dxf_spline(
|
1066 |
contours,
|
1067 |
scaling_factor, # This should be mm/px
|
1068 |
-
|
1069 |
finger_clearance=(finger_clearance == "On")
|
1070 |
)
|
1071 |
except FingerCutOverlapError as e:
|
@@ -1079,7 +1064,7 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
|
|
1079 |
for poly in finger_polygons:
|
1080 |
try:
|
1081 |
coords = np.array([
|
1082 |
-
(int(x / scaling_factor), int(
|
1083 |
for x, y in poly.exterior.coords
|
1084 |
], np.int32).reshape((-1, 1, 2))
|
1085 |
|
|
|
974 |
raise gr.Error(f"Error processing image: {str(e)}")
|
975 |
|
976 |
try:
|
977 |
+
# Get paper bounds with expansion
|
978 |
+
rect = cv2.boundingRect(paper_contour)
|
979 |
+
expansion = max(20, int(min(rect[2], rect[3]) * 0.05)) # 5% expansion
|
980 |
+
|
981 |
+
x, y, w, h = rect
|
982 |
+
x_min = max(0, x - expansion)
|
983 |
+
y_min = max(0, y - expansion)
|
984 |
+
x_max = min(image.shape[1], x + w + expansion)
|
985 |
+
y_max = min(image.shape[0], y + h + expansion)
|
986 |
+
|
987 |
+
# Process the expanded paper area
|
988 |
+
cropped_image = image[y_min:y_max, x_min:x_max]
|
989 |
+
crop_offset = (x_min, y_min)
|
990 |
+
|
991 |
+
# Remove background
|
992 |
+
objects_mask = remove_bg(cropped_image)
|
993 |
+
|
994 |
+
# Resize mask back to cropped image size
|
995 |
+
target_height = y_max - y_min
|
996 |
+
target_width = x_max - x_min
|
997 |
+
objects_mask_resized = cv2.resize(objects_mask, (target_width, target_height))
|
998 |
+
|
999 |
+
# Place back in full image space
|
1000 |
+
full_mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
|
1001 |
+
full_mask[y_min:y_max, x_min:x_max] = objects_mask_resized
|
1002 |
+
|
1003 |
+
# Light filtering only - don't exclude paper area aggressively
|
1004 |
+
# Just remove small noise
|
1005 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
|
1006 |
+
objects_mask = cv2.morphologyEx(full_mask, cv2.MORPH_OPEN, kernel)
|
1007 |
+
|
1008 |
+
# Debug: Save intermediate masks
|
1009 |
+
cv2.imwrite("./debug/objects_mask_after_processing.jpg", objects_mask)
|
1010 |
+
|
1011 |
+
# Check if we actually have object pixels
|
1012 |
+
object_pixels = np.count_nonzero(objects_mask)
|
1013 |
+
if object_pixels < 300: # Minimum threshold
|
1014 |
+
raise NoObjectDetectedError("No significant object detected")
|
1015 |
+
|
1016 |
+
# Validate single object
|
1017 |
+
validate_single_object(objects_mask, paper_contour)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1018 |
|
1019 |
except (MultipleObjectsError, NoObjectDetectedError) as e:
|
1020 |
return (
|
|
|
1050 |
dxf, finger_polygons, original_polygons = save_dxf_spline(
|
1051 |
contours,
|
1052 |
scaling_factor, # This should be mm/px
|
1053 |
+
image.shape[0], # Use original image height
|
1054 |
finger_clearance=(finger_clearance == "On")
|
1055 |
)
|
1056 |
except FingerCutOverlapError as e:
|
|
|
1064 |
for poly in finger_polygons:
|
1065 |
try:
|
1066 |
coords = np.array([
|
1067 |
+
(int(x / scaling_factor), int(image.shape[0] - y / scaling_factor))
|
1068 |
for x, y in poly.exterior.coords
|
1069 |
], np.int32).reshape((-1, 1, 2))
|
1070 |
|