Update app.py
Browse files
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 |
-
#
|
| 1082 |
-
# ูุง ู
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 1117 |
-
|
| 1118 |
-
|
| 1119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1120 |
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
|
| 1125 |
-
|
| 1126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1127 |
|
| 1128 |
crop_w = right - left
|
| 1129 |
crop_h = bottom - top
|
| 1130 |
|
| 1131 |
-
|
| 1132 |
-
|
| 1133 |
-
print(f"โ ๏ธ Crop: ูุชูุฌุฉ ุตุบูุฑุฉ ุฌุฏุงู {crop_w}x{crop_h}ุ ุฅุฑุฌุงุน ุงูุฃุตููุฉ")
|
| 1134 |
return img
|
| 1135 |
|
| 1136 |
-
print(f"โ๏ธ
|
| 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 |
-
|
| 1155 |
img = crop_container(img)
|
| 1156 |
-
|
| 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
|