DrAbbas commited on
Commit
148fe6a
ยท
verified ยท
1 Parent(s): 23cce76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -44
app.py CHANGED
@@ -668,7 +668,7 @@ def classify_image(img):
668
  if img.mode != 'RGB':
669
  img = img.convert('RGB')
670
 
671
- # โœ‚๏ธ ู‚ุต ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ ู‚ุจู„ ุงู„ุชุญู„ูŠู„
672
  img = crop_container(img)
673
 
674
  input_224 = transform_224(img).unsqueeze(0).to(DEVICE)
@@ -1078,67 +1078,89 @@ def enhance_single_independent(img, technique):
1078
 
1079
 
1080
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1081
- # โšก YOLO11x-cls ูู‚ุท โ€” ุชุญู„ูŠู„ ุณุฑูŠุน ู…ุณุชู‚ู„
1082
- # ู„ุง ูŠุณุชุฎุฏู… classify_image ูˆู„ุง analyze_image
1083
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1084
 
1085
  def crop_container(img):
1086
  """
1087
- โœ‚๏ธ ู‚ุต ุงู„ุญุงูˆูŠุฉ ูˆุฅุฒุงู„ุฉ ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ ุงู„ุฎู„ููŠ
1088
- ูŠุจุญุซ ุนู† ุญุฏูˆุฏ ุงู„ู…ุญุชูˆู‰ ุงู„ุญู‚ูŠู‚ูŠ ูˆูŠู‚ุต ุงู„ุฎู„ููŠุฉ ุงู„ุฒุฑู‚ุงุก
 
 
1089
  """
1090
  import numpy as np
1091
  arr = np.array(img)
1092
  h, w = arr.shape[:2]
1093
 
1094
- # ุงู„ุฎู„ููŠุฉ ุงู„ุฒุฑู‚ุงุก: R<100, G<100, B>130
1095
- # ุงู„ู…ุญุชูˆู‰: ุฃูŠ ุจูƒุณู„ ู„ูŠุณ ุฃุฒุฑู‚ ุฎุงู„ุต
1096
- if len(arr.shape) == 3:
1097
- is_bg = (arr[:,:,0].astype(int) < 100) & \
1098
- (arr[:,:,1].astype(int) < 100) & \
1099
- (arr[:,:,2].astype(int) > 130)
1100
- # ุฃูŠุถุงู‹ ุงู„ุฃุจูŠุถ/ุงู„ุฑู…ุงุฏูŠ ุงู„ูุงุชุญ ุฌุฏุงู‹ ูƒุฎู„ููŠุฉ
1101
- is_bg_white = (arr[:,:,0] > 240) & (arr[:,:,1] > 240) & (arr[:,:,2] > 240)
1102
- is_bg = is_bg | is_bg_white
1103
- else:
1104
- return img # grayscale โ€” ู„ุง crop
1105
 
1106
- is_content = ~is_bg
 
 
 
 
1107
 
1108
- # ุงู„ุจุญุซ ุนู† ุญุฏูˆุฏ ุงู„ู…ุญุชูˆู‰
1109
- rows_with_content = np.any(is_content, axis=1)
1110
  cols_with_content = np.any(is_content, axis=0)
1111
-
1112
- if not rows_with_content.any() or not cols_with_content.any():
1113
- print("โš ๏ธ Crop: ู„ู… ูŠูุนุซุฑ ุนู„ู‰ ู…ุญุชูˆู‰ุŒ ุฅุฑุฌุงุน ุงู„ุฃุตู„ูŠุฉ")
1114
  return img
1115
 
1116
- top = int(np.argmax(rows_with_content))
1117
- bottom = int(len(rows_with_content) - np.argmax(rows_with_content[::-1]))
1118
- left = int(np.argmax(cols_with_content))
1119
- right = int(len(cols_with_content) - np.argmax(cols_with_content[::-1]))
 
 
 
 
 
 
 
1120
 
1121
- # ู‡ุงู…ุด 5 ุจูƒุณู„
1122
- margin = 5
1123
- top = max(0, top - margin)
1124
- bottom = min(h, bottom + margin)
1125
- left = max(0, left - margin)
1126
- right = min(w, right + margin)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1127
 
1128
  crop_w = right - left
1129
  crop_h = bottom - top
1130
 
1131
- # ู„ุง crop ุฅุฐุง ูƒุงู† ุงู„ู…ุญุชูˆู‰ ุฃู‚ู„ ู…ู† 20% ู…ู† ุงู„ุตูˆุฑุฉ (ุฎุทุฃ ููŠ ุงู„ูƒุดู)
1132
- if crop_w < w * 0.2 or crop_h < h * 0.2:
1133
- print(f"โš ๏ธ Crop: ู†ุชูŠุฌุฉ ุตุบูŠุฑุฉ ุฌุฏุงู‹ {crop_w}x{crop_h}ุŒ ุฅุฑุฌุงุน ุงู„ุฃุตู„ูŠุฉ")
1134
  return img
1135
 
1136
- print(f"โœ‚๏ธ Crop: {w}x{h} โ†’ {crop_w}x{crop_h} | L={left} T={top} R={right} B={bottom}")
1137
- print(f"โœ‚๏ธ ุชู… ุญุฐู {w-crop_w} ุจูƒุณู„ ุฃูู‚ูŠุŒ {h-crop_h} ุจูƒุณู„ ุฑุฃุณูŠ")
1138
-
1139
  return img.crop((left, top, right, bottom))
1140
 
1141
 
 
 
 
 
1142
  def yolo11_fast_classify(img, declared_text):
1143
  """โšก ุชุตู†ูŠู ุณุฑูŠุน ุจู€ YOLO11x-cls ูู‚ุท (3-8 ุซูˆุงู†ูŠ)"""
1144
  if img is None:
@@ -1150,13 +1172,10 @@ def yolo11_fast_classify(img, declared_text):
1150
  if img.mode != 'RGB':
1151
  img = img.convert('RGB')
1152
 
1153
- # โœ‚๏ธ ู‚ุต ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ โ€” ู†ูุณ ู…ุง ูŠุฑุงู‡ ุงู„ู…ุณุชุฎุฏู… ููŠ ุงู„ูˆุงุฌู‡ุฉ
1154
- img_original_size = img.size
1155
  img = crop_container(img)
1156
- if img.size != img_original_size:
1157
- print(f"โœ‚๏ธ ุจุนุฏ ุงู„ู€ Crop: {img.size[0]}x{img.size[1]}")
1158
- else:
1159
- print("โ„น๏ธ ู„ู… ูŠุชู… Crop (ุงู„ุตูˆุฑุฉ ุจุฏูˆู† ูุฑุงุบ)")
1160
 
1161
  # โ•โ•โ• ุงู„ุจุญุซ ุนู† YOLO11x ูู‚ุท โ•โ•โ•
1162
  yolo_model = None
 
668
  if img.mode != 'RGB':
669
  img = img.convert('RGB')
670
 
671
+ # โœ‚๏ธ ู‚ุต ุงู„ุญุงูˆูŠุฉ
672
  img = crop_container(img)
673
 
674
  input_224 = transform_224(img).unsqueeze(0).to(DEVICE)
 
1078
 
1079
 
1080
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1081
+ # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1082
+ # โœ‚๏ธ ุฏุงู„ุฉ ุงู„ู‚ุต ุงู„ุฐูƒูŠ โ€” ุชุฒูŠู„ ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ ูˆู‡ูŠูƒู„ ุงู„ุดุงุญู†ุฉ
1083
  # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1084
 
1085
  def crop_container(img):
1086
  """
1087
+ โœ‚๏ธ ู‚ุต ุงู„ุญุงูˆูŠุฉ ุจุฐูƒุงุก:
1088
+ - ุฅุฒุงู„ุฉ ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ (ูŠู…ูŠู†/ูŠุณุงุฑ)
1089
+ - ุฅุฒุงู„ุฉ ู‡ูŠูƒู„ ุงู„ุดุงุญู†ุฉ ูˆุงู„ุนุฌู„ุงุช (ุฃุณูู„)
1090
+ ูŠุนู…ู„ ุจุฅูŠุฌุงุฏ ุฃูƒุจุฑ ูƒุชู„ุฉ ู…ุชุตู„ุฉ ู…ู† ุงู„ุตููˆู ุงู„ูƒุซูŠูุฉ
1091
  """
1092
  import numpy as np
1093
  arr = np.array(img)
1094
  h, w = arr.shape[:2]
1095
 
1096
+ if len(arr.shape) != 3 or arr.shape[2] < 3:
1097
+ return img
 
 
 
 
 
 
 
 
 
1098
 
1099
+ # ุงู„ุฎู„ููŠุฉ ุงู„ุฒุฑู‚ุงุก ุงู„ุฎุงู„ุตุฉ
1100
+ is_blue = (arr[:,:,0].astype(int) < 100) & \
1101
+ (arr[:,:,1].astype(int) < 100) & \
1102
+ (arr[:,:,2].astype(int) > 130)
1103
+ is_content = ~is_blue
1104
 
 
 
1105
  cols_with_content = np.any(is_content, axis=0)
1106
+ if not cols_with_content.any():
 
 
1107
  return img
1108
 
1109
+ # ุงู„ุญุฏูˆุฏ ุงู„ุฃูู‚ูŠุฉ
1110
+ left = int(np.argmax(cols_with_content))
1111
+ right = int(len(cols_with_content) - np.argmax(cols_with_content[::-1]))
1112
+
1113
+ # ู†ุณุจุฉ ุงู„ู…ุญุชูˆู‰ ููŠ ูƒู„ ุตู
1114
+ row_content_ratio = np.mean(is_content, axis=1)
1115
+
1116
+ # ุงู„ุจุญุซ ุนู† ุฃูƒุจุฑ ูƒุชู„ุฉ ู…ุชุตู„ุฉ (ุงู„ุญุงูˆูŠุฉ ุงู„ุฑุฆูŠุณูŠุฉ > 35% ู…ุญุชูˆู‰)
1117
+ dense = row_content_ratio > 0.35
1118
+ best_start, best_end, best_len = 0, h, 0
1119
+ cur_start = None
1120
 
1121
+ for i in range(h):
1122
+ if dense[i]:
1123
+ if cur_start is None:
1124
+ cur_start = i
1125
+ else:
1126
+ if cur_start is not None:
1127
+ length = i - cur_start
1128
+ if length > best_len:
1129
+ best_len = length
1130
+ best_start = cur_start
1131
+ best_end = i
1132
+ cur_start = None
1133
+ if cur_start is not None:
1134
+ length = h - cur_start
1135
+ if length > best_len:
1136
+ best_start = cur_start
1137
+ best_end = h
1138
+
1139
+ if best_len > h * 0.10:
1140
+ top = max(0, best_start - 5)
1141
+ bottom = min(h, best_end + 10)
1142
+ print(f"โœ‚๏ธ ูƒุชู„ุฉ ุงู„ุญุงูˆูŠุฉ: ุตููˆู {best_start}โ†’{best_end}")
1143
+ else:
1144
+ # fallback: ุฎุฐ ุฃุนู„ู‰ 72%
1145
+ top = 0
1146
+ bottom = int(h * 0.72)
1147
+ print(f"โœ‚๏ธ fallback crop: ุฃุนู„ู‰ 72% = {bottom}px")
1148
 
1149
  crop_w = right - left
1150
  crop_h = bottom - top
1151
 
1152
+ if crop_w < w * 0.15 or crop_h < 30:
1153
+ print(f"โš ๏ธ Crop ุตุบูŠุฑ ุฌุฏุงู‹ุŒ ุฅุฑุฌุงุน ุงู„ุฃุตู„ูŠุฉ")
 
1154
  return img
1155
 
1156
+ print(f"โœ‚๏ธ {w}x{h} โ†’ {crop_w}x{crop_h} | L={left} R={right} T={top} B={bottom}")
 
 
1157
  return img.crop((left, top, right, bottom))
1158
 
1159
 
1160
+ # โšก YOLO11x-cls ูู‚ุท โ€” ุชุญู„ูŠู„ ุณุฑูŠุน ู…ุณุชู‚ู„
1161
+ # ู„ุง ูŠุณุชุฎุฏู… classify_image ูˆู„ุง analyze_image
1162
+ # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
1163
+
1164
  def yolo11_fast_classify(img, declared_text):
1165
  """โšก ุชุตู†ูŠู ุณุฑูŠุน ุจู€ YOLO11x-cls ูู‚ุท (3-8 ุซูˆุงู†ูŠ)"""
1166
  if img is None:
 
1172
  if img.mode != 'RGB':
1173
  img = img.convert('RGB')
1174
 
1175
+ # โœ‚๏ธ ู‚ุต ุงู„ุญุงูˆูŠุฉ ูˆุฅุฒุงู„ุฉ ุงู„ูุฑุงุบ ุงู„ุฃุฒุฑู‚ + ู‡ูŠูƒู„ ุงู„ุดุงุญู†ุฉ
1176
+ orig_size = img.size
1177
  img = crop_container(img)
1178
+ print(f"โœ‚๏ธ ู‚ุจู„: {orig_size} | ุจุนุฏ: {img.size}")
 
 
 
1179
 
1180
  # โ•โ•โ• ุงู„ุจุญุซ ุนู† YOLO11x ูู‚ุท โ•โ•โ•
1181
  yolo_model = None