habulaj commited on
Commit
797ec88
·
verified ·
1 Parent(s): 07441e3

Update detect_crop_image.py

Browse files
Files changed (1) hide show
  1. detect_crop_image.py +60 -6
detect_crop_image.py CHANGED
@@ -91,12 +91,66 @@ 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
- # Inset Logic (2px) - additional fixed safety margin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  inset = 2
96
- x_min = max(x + inset, 0)
97
- y_min = max(y + inset, 0)
98
- x_max = min(x + w - inset, img_w)
99
- y_max = min(y + h - inset, img_h)
100
 
101
  final_w = x_max - x_min
102
  final_h = y_max - y_min
@@ -142,4 +196,4 @@ if __name__ == "__main__":
142
  if result and os.path.exists(result):
143
  print(f"\n✅ Done! Cropped image saved as: {result}")
144
  else:
145
- print(f"\n❌ Failed to create cropped image.")
 
91
  if zoom_inset > 0:
92
  print(f"Smart Zoom applied: {zoom_inset}px inset to clear rounded corners.")
93
 
94
+ prop_x_min = x
95
+ prop_y_min = y
96
+ prop_x_max = x + w
97
+ prop_y_max = y + h
98
+
99
+ def validate_crop(region, border_region, threshold=0.75):
100
+ if region.size == 0:
101
+ return False
102
+
103
+ # 1. Edge must be solid white or black
104
+ if border_region.size > 0:
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
+ if dark_edge < threshold and light_edge < threshold:
108
+ return False
109
+
110
+ # 2. Region must be mostly white/black (allows text)
111
+ dark_region = np.count_nonzero(region < 20)
112
+ light_region = np.count_nonzero(region > 235)
113
+ region_ratio = (dark_region + light_region) / region.size
114
+
115
+ if region_ratio < 0.65:
116
+ return False
117
+
118
+ return True
119
+
120
+ # Validate Top Crop
121
+ if prop_y_min > 0:
122
+ top_region = gray[0:prop_y_min, :]
123
+ top_border = gray[0:min(2, prop_y_min), :]
124
+ if not validate_crop(top_region, top_border):
125
+ prop_y_min = 0
126
+
127
+ # Validate Bottom Crop
128
+ if prop_y_max < img_h:
129
+ bottom_region = gray[prop_y_max:img_h, :]
130
+ bottom_border = gray[max(img_h-2, prop_y_max):img_h, :]
131
+ if not validate_crop(bottom_region, bottom_border):
132
+ prop_y_max = img_h
133
+
134
+ # Validate Left Crop
135
+ if prop_x_min > 0:
136
+ left_region = gray[:, 0:prop_x_min]
137
+ left_border = gray[:, 0:min(2, prop_x_min)]
138
+ if not validate_crop(left_region, left_border):
139
+ prop_x_min = 0
140
+
141
+ # Validate Right Crop
142
+ if prop_x_max < img_w:
143
+ right_region = gray[:, prop_x_max:img_w]
144
+ right_border = gray[:, max(img_w-2, prop_x_max):img_w]
145
+ if not validate_crop(right_region, right_border):
146
+ prop_x_max = img_w
147
+
148
+ # Inset Logic (2px) - additional fixed safety margin ONLY for valid crops
149
  inset = 2
150
+ x_min = prop_x_min + inset if prop_x_min > 0 else 0
151
+ y_min = prop_y_min + inset if prop_y_min > 0 else 0
152
+ x_max = prop_x_max - inset if prop_x_max < img_w else img_w
153
+ y_max = prop_y_max - inset if prop_y_max < img_h else img_h
154
 
155
  final_w = x_max - x_min
156
  final_h = y_max - y_min
 
196
  if result and os.path.exists(result):
197
  print(f"\n✅ Done! Cropped image saved as: {result}")
198
  else:
199
+ print(f"\n❌ Failed to create cropped image.")