Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -171,76 +171,6 @@ def get_birefnet():
|
|
| 171 |
logger.info("BiRefNet model loaded successfully")
|
| 172 |
return birefnet
|
| 173 |
|
| 174 |
-
# def detect_paper_contour(image: np.ndarray) -> Tuple[np.ndarray, float]:
|
| 175 |
-
# """
|
| 176 |
-
# Detect paper in the image using contour detection as fallback
|
| 177 |
-
# Returns the paper contour and estimated scaling factor
|
| 178 |
-
# """
|
| 179 |
-
# logger.info("Using contour-based paper detection")
|
| 180 |
-
|
| 181 |
-
# # Convert to grayscale
|
| 182 |
-
# gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) if len(image.shape) == 3 else image
|
| 183 |
-
|
| 184 |
-
# # Apply bilateral filter to reduce noise while preserving edges
|
| 185 |
-
# filtered = cv2.bilateralFilter(gray, 9, 75, 75)
|
| 186 |
-
|
| 187 |
-
# # Apply adaptive threshold
|
| 188 |
-
# thresh = cv2.adaptiveThreshold(filtered, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
| 189 |
-
# cv2.THRESH_BINARY, 11, 2)
|
| 190 |
-
|
| 191 |
-
# # Edge detection with multiple thresholds
|
| 192 |
-
# edges1 = cv2.Canny(filtered, 50, 150)
|
| 193 |
-
# edges2 = cv2.Canny(filtered, 30, 100)
|
| 194 |
-
# edges = cv2.bitwise_or(edges1, edges2)
|
| 195 |
-
|
| 196 |
-
# # Morphological operations to connect broken edges
|
| 197 |
-
# kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
|
| 198 |
-
# edges = cv2.morphologyEx(edges, cv2.MORPH_CLOSE, kernel)
|
| 199 |
-
|
| 200 |
-
# # Find contours
|
| 201 |
-
# contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 202 |
-
|
| 203 |
-
# # Filter contours by area and aspect ratio to find paper-like rectangles
|
| 204 |
-
# paper_contours = []
|
| 205 |
-
# image_area = image.shape[0] * image.shape[1]
|
| 206 |
-
# min_area = image_area * 0.20 # At least 15% of image
|
| 207 |
-
# max_area = image_area * 0.85 # At most 95% of image
|
| 208 |
-
|
| 209 |
-
# for contour in contours:
|
| 210 |
-
# area = cv2.contourArea(contour)
|
| 211 |
-
# if min_area < area < max_area:
|
| 212 |
-
# # Approximate contour to polygon
|
| 213 |
-
# epsilon = 0.015 * cv2.arcLength(contour, True)
|
| 214 |
-
# approx = cv2.approxPolyDP(contour, epsilon, True)
|
| 215 |
-
|
| 216 |
-
# # Check if it's roughly rectangular (4 corners) or close to it
|
| 217 |
-
# if len(approx) >= 4:
|
| 218 |
-
# # Calculate bounding rectangle
|
| 219 |
-
# rect = cv2.boundingRect(approx)
|
| 220 |
-
# w, h = rect[2], rect[3]
|
| 221 |
-
# aspect_ratio = w / h if h > 0 else 0
|
| 222 |
-
|
| 223 |
-
# # Check if aspect ratio matches common paper ratios
|
| 224 |
-
# # A4: 1.414, A3: 1.414, US Letter: 1.294
|
| 225 |
-
# if 1.3 < aspect_ratio < 1.5: # More lenient tolerance
|
| 226 |
-
# # Check if contour area is close to bounding rect area (rectangularity)
|
| 227 |
-
# rect_area = w * h
|
| 228 |
-
# if rect_area > 0:
|
| 229 |
-
# extent = area / rect_area
|
| 230 |
-
# if extent > 0.85: # At least 70% rectangular
|
| 231 |
-
# paper_contours.append((contour, area, aspect_ratio, extent))
|
| 232 |
-
|
| 233 |
-
# if not paper_contours:
|
| 234 |
-
# logger.error("No paper-like contours found")
|
| 235 |
-
# raise ReferenceBoxNotDetectedError("Could not detect paper in the image using contour detection")
|
| 236 |
-
|
| 237 |
-
# # Select the best paper contour based on area and rectangularity
|
| 238 |
-
# paper_contours.sort(key=lambda x: (x[1] * x[3]), reverse=True) # Sort by area * extent
|
| 239 |
-
# best_contour = paper_contours[0][0]
|
| 240 |
-
|
| 241 |
-
# logger.info(f"Paper detected using contours: area={paper_contours[0][1]}, aspect_ratio={paper_contours[0][2]:.2f}")
|
| 242 |
-
|
| 243 |
-
# return best_contour, 0.0 # Return 0.0 as placeholder scaling factor
|
| 244 |
def detect_paper_contour(image: np.ndarray, output_unit: str = "mm") -> Tuple[np.ndarray, float]:
|
| 245 |
"""
|
| 246 |
Detect paper in the image using contour detection as fallback
|
|
@@ -492,38 +422,6 @@ def remove_bg(image: np.ndarray) -> np.ndarray:
|
|
| 492 |
logger.error(f"Error in BiRefNet background removal: {e}")
|
| 493 |
raise
|
| 494 |
|
| 495 |
-
# def exclude_paper_area(mask: np.ndarray, paper_contour: np.ndarray, expansion_factor: float = 1.2) -> np.ndarray:
|
| 496 |
-
# """
|
| 497 |
-
# Remove paper area from the mask to focus only on objects
|
| 498 |
-
# """
|
| 499 |
-
# # Create paper mask with slight expansion to ensure complete removal
|
| 500 |
-
# paper_mask = np.zeros(mask.shape[:2], dtype=np.uint8)
|
| 501 |
-
|
| 502 |
-
# # # Expand paper contour slightly
|
| 503 |
-
# # epsilon = expansion_factor * cv2.arcLength(paper_contour, True)
|
| 504 |
-
# # expanded_contour = cv2.approxPolyDP(paper_contour, epsilon, True)
|
| 505 |
-
|
| 506 |
-
# # cv2.fillPoly(paper_mask, [expanded_contour], 255)
|
| 507 |
-
# # Create a more aggressive inward shrinking of paper bounds
|
| 508 |
-
# rect = cv2.boundingRect(paper_contour)
|
| 509 |
-
# shrink_pixels = int(min(rect[2], rect[3]) * 0.05) # Shrink by 5% of smaller dimension
|
| 510 |
-
|
| 511 |
-
# # Create shrunken rectangle
|
| 512 |
-
# x, y, w, h = rect
|
| 513 |
-
# shrunken_contour = np.array([
|
| 514 |
-
# [[x + shrink_pixels, y + shrink_pixels]],
|
| 515 |
-
# [[x + w - shrink_pixels, y + shrink_pixels]],
|
| 516 |
-
# [[x + w - shrink_pixels, y + h - shrink_pixels]],
|
| 517 |
-
# [[x + shrink_pixels, y + h - shrink_pixels]]
|
| 518 |
-
# ])
|
| 519 |
-
|
| 520 |
-
# cv2.fillPoly(paper_mask, [shrunken_contour], 255)
|
| 521 |
-
|
| 522 |
-
# # Invert paper mask and apply to object mask
|
| 523 |
-
# paper_mask_inv = cv2.bitwise_not(paper_mask)
|
| 524 |
-
# result_mask = cv2.bitwise_and(mask, paper_mask_inv)
|
| 525 |
-
|
| 526 |
-
# return result_mask
|
| 527 |
def mask_paper_area_in_image(image: np.ndarray, paper_contour: np.ndarray) -> np.ndarray:
|
| 528 |
"""Less aggressive masking to preserve corner objects"""
|
| 529 |
masked_image = image.copy()
|
|
@@ -1102,7 +1000,7 @@ def predict_with_paper(image, paper_size, offset, offset_unit, finger_clearance=
|
|
| 1102 |
scale_info
|
| 1103 |
)
|
| 1104 |
|
| 1105 |
-
def predict_full_paper(image, paper_size, offset_value_mm,offset_unit, enable_finger_cut, selected_outputs):
|
| 1106 |
finger_flag = "On" if enable_finger_cut == "On" else "Off"
|
| 1107 |
|
| 1108 |
# Always get all outputs from predict_with_paper
|
|
@@ -1128,18 +1026,29 @@ if __name__ == "__main__":
|
|
| 1128 |
os.makedirs("./outputs", exist_ok=True)
|
| 1129 |
|
| 1130 |
with gr.Blocks(title="Paper-Based DXF Generator", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 1131 |
gr.Markdown("""
|
| 1132 |
-
|
| 1133 |
-
|
| 1134 |
-
|
| 1135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1136 |
|
| 1137 |
-
**Instructions:**
|
| 1138 |
-
1. Place a single object on paper
|
| 1139 |
-
2. Select the correct paper size
|
| 1140 |
-
3. Configure options as needed
|
| 1141 |
-
4. Click Submit to generate DXF
|
| 1142 |
-
""")
|
| 1143 |
|
| 1144 |
with gr.Row():
|
| 1145 |
with gr.Column():
|
|
@@ -1156,31 +1065,31 @@ if __name__ == "__main__":
|
|
| 1156 |
info="Select the paper size used in your image"
|
| 1157 |
)
|
| 1158 |
|
| 1159 |
-
with gr.Group():
|
| 1160 |
-
|
| 1161 |
-
|
| 1162 |
-
|
| 1163 |
-
|
| 1164 |
-
|
| 1165 |
-
|
| 1166 |
-
|
| 1167 |
-
|
| 1168 |
-
|
| 1169 |
-
|
| 1170 |
-
|
| 1171 |
-
|
| 1172 |
-
|
| 1173 |
-
|
| 1174 |
-
|
| 1175 |
-
|
| 1176 |
-
with gr.Group():
|
| 1177 |
-
|
| 1178 |
-
|
| 1179 |
-
|
| 1180 |
-
|
| 1181 |
-
|
| 1182 |
-
|
| 1183 |
-
|
| 1184 |
|
| 1185 |
output_options = gr.CheckboxGroup(
|
| 1186 |
choices=["Annotated Image", "Outlines", "Mask"],
|
|
@@ -1222,22 +1131,14 @@ if __name__ == "__main__":
|
|
| 1222 |
inputs=[
|
| 1223 |
input_image,
|
| 1224 |
paper_size,
|
| 1225 |
-
|
| 1226 |
-
|
| 1227 |
-
|
| 1228 |
output_options
|
| 1229 |
],
|
| 1230 |
outputs=[dxf_file, output_image, outlines_image, mask_image, scale_info]
|
| 1231 |
)
|
| 1232 |
|
| 1233 |
-
|
| 1234 |
-
with gr.Row():
|
| 1235 |
-
gr.Markdown("""
|
| 1236 |
-
### Tips for Best Results:
|
| 1237 |
-
- Ensure good lighting and clear paper edges
|
| 1238 |
-
- Place object completely on the paper
|
| 1239 |
-
- Avoid shadows that might interfere with detection
|
| 1240 |
-
- Use high contrast between object and paper
|
| 1241 |
-
""")
|
| 1242 |
|
| 1243 |
demo.launch(share=True)
|
|
|
|
| 171 |
logger.info("BiRefNet model loaded successfully")
|
| 172 |
return birefnet
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
def detect_paper_contour(image: np.ndarray, output_unit: str = "mm") -> Tuple[np.ndarray, float]:
|
| 175 |
"""
|
| 176 |
Detect paper in the image using contour detection as fallback
|
|
|
|
| 422 |
logger.error(f"Error in BiRefNet background removal: {e}")
|
| 423 |
raise
|
| 424 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
def mask_paper_area_in_image(image: np.ndarray, paper_contour: np.ndarray) -> np.ndarray:
|
| 426 |
"""Less aggressive masking to preserve corner objects"""
|
| 427 |
masked_image = image.copy()
|
|
|
|
| 1000 |
scale_info
|
| 1001 |
)
|
| 1002 |
|
| 1003 |
+
def predict_full_paper(image, paper_size, offset_value_mm = 0.02,offset_unit='mm', enable_finger_cut='Off', selected_outputs):
|
| 1004 |
finger_flag = "On" if enable_finger_cut == "On" else "Off"
|
| 1005 |
|
| 1006 |
# Always get all outputs from predict_with_paper
|
|
|
|
| 1026 |
os.makedirs("./outputs", exist_ok=True)
|
| 1027 |
|
| 1028 |
with gr.Blocks(title="Paper-Based DXF Generator", theme=gr.themes.Soft()) as demo:
|
| 1029 |
+
# Example gallery
|
| 1030 |
gr.Markdown("""
|
| 1031 |
+
# Paper-Based DXF Generator
|
| 1032 |
+
|
| 1033 |
+
Upload an image with a single object placed on paper (A4, A3, or US Letter).
|
| 1034 |
+
The paper serves as a size reference for accurate DXF generation.
|
| 1035 |
+
""")
|
| 1036 |
+
with gr.Row():
|
| 1037 |
+
gr.Markdown("""
|
| 1038 |
+
**Instructions:**
|
| 1039 |
+
1. Place a single object on paper
|
| 1040 |
+
2. Select the correct paper size
|
| 1041 |
+
3. Configure options as needed
|
| 1042 |
+
4. Click Submit to generate DXF
|
| 1043 |
+
""")
|
| 1044 |
+
gr.Markdown("""
|
| 1045 |
+
### Tips for Best Results:
|
| 1046 |
+
- Ensure good lighting and clear paper edges
|
| 1047 |
+
- Place object completely at the center of the paper
|
| 1048 |
+
- Avoid shadows that might interfere with detection
|
| 1049 |
+
- Use high contrast between object and paper
|
| 1050 |
+
""")
|
| 1051 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1052 |
|
| 1053 |
with gr.Row():
|
| 1054 |
with gr.Column():
|
|
|
|
| 1065 |
info="Select the paper size used in your image"
|
| 1066 |
)
|
| 1067 |
|
| 1068 |
+
# with gr.Group():
|
| 1069 |
+
# gr.Markdown("### Contour Offset")
|
| 1070 |
+
# with gr.Row():
|
| 1071 |
+
# offset_value_mm = gr.Number(
|
| 1072 |
+
# value=0.02,
|
| 1073 |
+
# label="Offset",
|
| 1074 |
+
# info="Expand contours outward by this amount",
|
| 1075 |
+
# precision=3,
|
| 1076 |
+
# minimum=0,
|
| 1077 |
+
# maximum=50
|
| 1078 |
+
# )
|
| 1079 |
+
# offset_unit = gr.Dropdown(
|
| 1080 |
+
# choices=["mm", "inches"],
|
| 1081 |
+
# value="mm",
|
| 1082 |
+
# label="Unit"
|
| 1083 |
+
# )
|
| 1084 |
+
|
| 1085 |
+
# with gr.Group():
|
| 1086 |
+
# gr.Markdown("### Finger Cuts")
|
| 1087 |
+
# enable_finger_cut = gr.Radio(
|
| 1088 |
+
# choices=["On", "Off"],
|
| 1089 |
+
# value="Off",
|
| 1090 |
+
# label="Enable Finger Cuts",
|
| 1091 |
+
# info="Add circular cuts for easier handling"
|
| 1092 |
+
# )
|
| 1093 |
|
| 1094 |
output_options = gr.CheckboxGroup(
|
| 1095 |
choices=["Annotated Image", "Outlines", "Mask"],
|
|
|
|
| 1131 |
inputs=[
|
| 1132 |
input_image,
|
| 1133 |
paper_size,
|
| 1134 |
+
0.02,
|
| 1135 |
+
'mm',
|
| 1136 |
+
'Off',
|
| 1137 |
output_options
|
| 1138 |
],
|
| 1139 |
outputs=[dxf_file, output_image, outlines_image, mask_image, scale_info]
|
| 1140 |
)
|
| 1141 |
|
| 1142 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1143 |
|
| 1144 |
demo.launch(share=True)
|