Update detect_crop_image.py
Browse files- detect_crop_image.py +55 -5
detect_crop_image.py
CHANGED
|
@@ -91,12 +91,62 @@ def detect_and_crop_image(image_path, output_image_path=None):
|
|
| 91 |
if zoom_inset > 0:
|
| 92 |
print(f"Smart Zoom applied: {zoom_inset}px inset to clear rounded corners.")
|
| 93 |
|
| 94 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
inset = 2
|
| 96 |
-
x_min =
|
| 97 |
-
y_min =
|
| 98 |
-
x_max =
|
| 99 |
-
y_max =
|
| 100 |
|
| 101 |
final_w = x_max - x_min
|
| 102 |
final_h = y_max - y_min
|
|
|
|
| 91 |
if zoom_inset > 0:
|
| 92 |
print(f"Smart Zoom applied: {zoom_inset}px inset to clear rounded corners.")
|
| 93 |
|
| 94 |
+
# --- Validate Crops ---
|
| 95 |
+
# Only crop if the excluded region is genuinely a white/black background
|
| 96 |
+
prop_x_min = x
|
| 97 |
+
prop_y_min = y
|
| 98 |
+
prop_x_max = x + w
|
| 99 |
+
prop_y_max = y + h
|
| 100 |
+
|
| 101 |
+
def validate_crop(region, border_region, edge_thresh=0.80, region_thresh=0.60):
|
| 102 |
+
if region.size == 0 or border_region.size == 0:
|
| 103 |
+
return False
|
| 104 |
+
|
| 105 |
+
dark_edge = np.count_nonzero(border_region < 20) / border_region.size
|
| 106 |
+
light_edge = np.count_nonzero(border_region > 235) / border_region.size
|
| 107 |
+
|
| 108 |
+
dark_region = np.count_nonzero(region < 20) / region.size
|
| 109 |
+
light_region = np.count_nonzero(region > 235) / region.size
|
| 110 |
+
|
| 111 |
+
is_dark_bg = (dark_edge >= edge_thresh) and (dark_region >= region_thresh)
|
| 112 |
+
is_light_bg = (light_edge >= edge_thresh) and (light_region >= region_thresh)
|
| 113 |
+
|
| 114 |
+
return is_dark_bg or is_light_bg
|
| 115 |
+
|
| 116 |
+
# Validate Top Crop
|
| 117 |
+
if prop_y_min > 0:
|
| 118 |
+
top_region = gray[0:prop_y_min, :]
|
| 119 |
+
top_border = gray[0:min(3, prop_y_min), :]
|
| 120 |
+
if not validate_crop(top_region, top_border):
|
| 121 |
+
prop_y_min = 0
|
| 122 |
+
|
| 123 |
+
# Validate Bottom Crop
|
| 124 |
+
if prop_y_max < img_h:
|
| 125 |
+
bottom_region = gray[prop_y_max:img_h, :]
|
| 126 |
+
bottom_border = gray[max(img_h-3, prop_y_max):img_h, :]
|
| 127 |
+
if not validate_crop(bottom_region, bottom_border):
|
| 128 |
+
prop_y_max = img_h
|
| 129 |
+
|
| 130 |
+
# Validate Left Crop
|
| 131 |
+
if prop_x_min > 0:
|
| 132 |
+
left_region = gray[:, 0:prop_x_min]
|
| 133 |
+
left_border = gray[:, 0:min(3, prop_x_min)]
|
| 134 |
+
if not validate_crop(left_region, left_border):
|
| 135 |
+
prop_x_min = 0
|
| 136 |
+
|
| 137 |
+
# Validate Right Crop
|
| 138 |
+
if prop_x_max < img_w:
|
| 139 |
+
right_region = gray[:, prop_x_max:img_w]
|
| 140 |
+
right_border = gray[:, max(img_w-3, prop_x_max):img_w]
|
| 141 |
+
if not validate_crop(right_region, right_border):
|
| 142 |
+
prop_x_max = img_w
|
| 143 |
+
|
| 144 |
+
# Inset Logic (2px) - additional fixed safety margin ONLY for valid crops
|
| 145 |
inset = 2
|
| 146 |
+
x_min = prop_x_min + inset if prop_x_min > 0 else 0
|
| 147 |
+
y_min = prop_y_min + inset if prop_y_min > 0 else 0
|
| 148 |
+
x_max = prop_x_max - inset if prop_x_max < img_w else img_w
|
| 149 |
+
y_max = prop_y_max - inset if prop_y_max < img_h else img_h
|
| 150 |
|
| 151 |
final_w = x_max - x_min
|
| 152 |
final_h = y_max - y_min
|