Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -41,7 +41,7 @@ material_params = {
|
|
| 41 |
"mirror": {"alpha": 0.7, "eps": 0.1, "I": 2000, "name": "آینه"},
|
| 42 |
"foliage": {"alpha": 0.25, "eps": 0.98, "I": 900, "name": "گیاهان"},
|
| 43 |
"water": {"alpha": 0.06, "eps": 0.98, "I": 4200, "name": "آب"},
|
| 44 |
-
|
| 45 |
}
|
| 46 |
|
| 47 |
material_categories = {
|
|
@@ -72,6 +72,10 @@ material_categories = {
|
|
| 72 |
"water_bodies": {
|
| 73 |
"members": ["water"],
|
| 74 |
"candidates": ["water"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
}
|
| 76 |
}
|
| 77 |
|
|
@@ -104,6 +108,9 @@ replacement_text = {
|
|
| 104 |
},
|
| 105 |
"water_bodies": {
|
| 106 |
"water": "حفظ منابع آبی به عنوان عنصر خنککننده"
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
}
|
| 109 |
|
|
@@ -133,17 +140,23 @@ def calc_deltaT(material, T_air, RH, u, S):
|
|
| 133 |
gamma = S / max(h_c, 1e-6)
|
| 134 |
return gamma * C_m / 1000.0
|
| 135 |
|
| 136 |
-
def get_patches(image, size=224, stride=
|
| 137 |
patches = []
|
| 138 |
w, h = image.size
|
| 139 |
|
| 140 |
-
for
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
return patches
|
| 149 |
|
|
@@ -158,8 +171,9 @@ def analyze_image(img, T_air, RH, u, S):
|
|
| 158 |
if len(patches) == 0:
|
| 159 |
return "⛔ تصویر نامعتبر است یا کوچک است."
|
| 160 |
|
| 161 |
-
# پیشبینی ب
|
| 162 |
all_predictions = []
|
|
|
|
| 163 |
|
| 164 |
for patch in patches:
|
| 165 |
inputs = processor(images=patch, return_tensors="pt")
|
|
@@ -167,21 +181,26 @@ def analyze_image(img, T_air, RH, u, S):
|
|
| 167 |
with torch.no_grad():
|
| 168 |
outputs = model(**inputs)
|
| 169 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
|
|
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
|
| 175 |
if not all_predictions:
|
| 176 |
-
return "⛔ هیچ مصالح معتبری شناسایی نشد."
|
| 177 |
|
| 178 |
counter = Counter(all_predictions)
|
| 179 |
-
total_patches = len(
|
| 180 |
|
| 181 |
# فیلتر مواد با فراوانی کافی
|
| 182 |
materials_found = {}
|
|
|
|
|
|
|
| 183 |
for m, count in counter.items():
|
| 184 |
-
if m in material_params and
|
|
|
|
|
|
|
| 185 |
materials_found[m] = count
|
| 186 |
|
| 187 |
if not materials_found:
|
|
|
|
| 41 |
"mirror": {"alpha": 0.7, "eps": 0.1, "I": 2000, "name": "آینه"},
|
| 42 |
"foliage": {"alpha": 0.25, "eps": 0.98, "I": 900, "name": "گیاهان"},
|
| 43 |
"water": {"alpha": 0.06, "eps": 0.98, "I": 4200, "name": "آب"},
|
| 44 |
+
"sky": {"alpha": 1.0, "eps": 1.0, "I": 0, "name": "آسمان"},
|
| 45 |
}
|
| 46 |
|
| 47 |
material_categories = {
|
|
|
|
| 72 |
"water_bodies": {
|
| 73 |
"members": ["water"],
|
| 74 |
"candidates": ["water"]
|
| 75 |
+
},
|
| 76 |
+
"background": {
|
| 77 |
+
"members": ["sky"],
|
| 78 |
+
"candidates": ["sky"]
|
| 79 |
}
|
| 80 |
}
|
| 81 |
|
|
|
|
| 108 |
},
|
| 109 |
"water_bodies": {
|
| 110 |
"water": "حفظ منابع آبی به عنوان عنصر خنککننده"
|
| 111 |
+
},
|
| 112 |
+
"background": {
|
| 113 |
+
"sky": "عنصر طبیعی بدون نیاز به تغییر"
|
| 114 |
}
|
| 115 |
}
|
| 116 |
|
|
|
|
| 140 |
gamma = S / max(h_c, 1e-6)
|
| 141 |
return gamma * C_m / 1000.0
|
| 142 |
|
| 143 |
+
def get_patches(image, size=224, stride=100):
|
| 144 |
patches = []
|
| 145 |
w, h = image.size
|
| 146 |
|
| 147 |
+
for scale in [1.0, 0.75]:
|
| 148 |
+
scaled_w, scaled_h = int(w * scale), int(h * scale)
|
| 149 |
+
if min(scaled_w, scaled_h) < size:
|
| 150 |
+
continue
|
| 151 |
|
| 152 |
+
scaled_img = image.resize((scaled_w, scaled_h), Image.Resampling.LANCZOS)
|
| 153 |
+
|
| 154 |
+
for i in range(0, scaled_w, stride):
|
| 155 |
+
for j in range(0, scaled_h, stride):
|
| 156 |
+
box = (i, j, min(i+size, scaled_w), min(j+size, scaled_h))
|
| 157 |
+
patch = scaled_img.crop(box)
|
| 158 |
+
if patch.size[0] >= size and patch.size[1] >= size:
|
| 159 |
+
patches.append(patch)
|
| 160 |
|
| 161 |
return patches
|
| 162 |
|
|
|
|
| 171 |
if len(patches) == 0:
|
| 172 |
return "⛔ تصویر نامعتبر است یا کوچک است."
|
| 173 |
|
| 174 |
+
# پیشبینی با اعتماد بیشتر
|
| 175 |
all_predictions = []
|
| 176 |
+
confidence_threshold = 0.7 # افزایش آستانه اطمینان
|
| 177 |
|
| 178 |
for patch in patches:
|
| 179 |
inputs = processor(images=patch, return_tensors="pt")
|
|
|
|
| 181 |
with torch.no_grad():
|
| 182 |
outputs = model(**inputs)
|
| 183 |
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 184 |
+
confidence, pred = torch.max(probs, dim=-1)
|
| 185 |
|
| 186 |
+
if confidence.item() > confidence_threshold:
|
| 187 |
+
label = model.config.id2label[pred.item()]
|
| 188 |
+
all_predictions.append(label)
|
| 189 |
|
| 190 |
if not all_predictions:
|
| 191 |
+
return "⛔ هیچ مصالح معتبری با اطمینان کافی شناسایی نشد."
|
| 192 |
|
| 193 |
counter = Counter(all_predictions)
|
| 194 |
+
total_patches = len(all_predictions)
|
| 195 |
|
| 196 |
# فیلتر مواد با فراوانی کافی
|
| 197 |
materials_found = {}
|
| 198 |
+
ignore_classes = ["food", "skin", "other", "wallpaper", "carpet", "fabric"]
|
| 199 |
+
|
| 200 |
for m, count in counter.items():
|
| 201 |
+
if (m in material_params and
|
| 202 |
+
m not in ignore_classes and
|
| 203 |
+
count >= max(3, total_patches * 0.1)):
|
| 204 |
materials_found[m] = count
|
| 205 |
|
| 206 |
if not materials_found:
|