Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -242,12 +242,21 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
|
|
| 242 |
# タイムスタンプを作成
|
| 243 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 244 |
|
| 245 |
-
#
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
|
|
|
|
|
|
| 251 |
|
| 252 |
# 画像の読み込みとRGB変換
|
| 253 |
image = cv2.imread(image_path)
|
|
@@ -259,21 +268,17 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
|
|
| 259 |
# 初期化したマスク画像
|
| 260 |
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 261 |
|
| 262 |
-
#
|
| 263 |
-
target_objects = decide_to_object(risk_level)
|
| 264 |
-
|
| 265 |
-
# 各検出結果に基づきマスク作成
|
| 266 |
for box in results[0].boxes:
|
| 267 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 268 |
confidence = box.conf[0]
|
| 269 |
class_id = box.cls[0]
|
| 270 |
object_type = model.names[int(class_id)]
|
| 271 |
|
| 272 |
-
#
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
mask = create_mask(image, x1, y1, x2, y2)
|
| 277 |
|
| 278 |
# 絶対座標に変換した点の範囲を黒に設定
|
| 279 |
p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
|
|
|
|
| 242 |
# タイムスタンプを作成
|
| 243 |
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 244 |
|
| 245 |
+
# テキストラベルのリストとその優先順に基づいた閾値の減衰率の計算
|
| 246 |
+
tex = [
|
| 247 |
+
'text', 'poster', 'Name tag', 'License plate', 'Digital screens',
|
| 248 |
+
'signboard', 'sign', 'logo', 'manhole', 'electricity pole', 'cardboard'
|
| 249 |
+
]
|
| 250 |
+
|
| 251 |
+
def logistic_decay_for_label(risk_level, label_index, k=0.1, r0=50):
|
| 252 |
+
base_decay = 1 / (1 + np.exp(-k * (risk_level - r0)))
|
| 253 |
+
# ラベルの順序に応じた減衰の段階を追加
|
| 254 |
+
return max(base_decay + 0.05 * label_index, 0.01)
|
| 255 |
|
| 256 |
+
adjusted_thresholds = {}
|
| 257 |
+
for i, label in enumerate(tex):
|
| 258 |
+
decay_factor = logistic_decay_for_label(risk_level, i)
|
| 259 |
+
adjusted_thresholds[label] = max(0.01, decay_factor / 2)
|
| 260 |
|
| 261 |
# 画像の読み込みとRGB変換
|
| 262 |
image = cv2.imread(image_path)
|
|
|
|
| 268 |
# 初期化したマスク画像
|
| 269 |
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 270 |
|
| 271 |
+
# 全ての検出オブジェクトを対象としてマスク作成
|
|
|
|
|
|
|
|
|
|
| 272 |
for box in results[0].boxes:
|
| 273 |
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 274 |
confidence = box.conf[0]
|
| 275 |
class_id = box.cls[0]
|
| 276 |
object_type = model.names[int(class_id)]
|
| 277 |
|
| 278 |
+
# オブジェクトの閾値を確認し、マスクを適用
|
| 279 |
+
threshold = adjusted_thresholds.get(object_type, 0.5)
|
| 280 |
+
if confidence >= threshold:
|
| 281 |
+
mask = create_mask(image, x1, y1, x2, y2)
|
|
|
|
| 282 |
|
| 283 |
# 絶対座標に変換した点の範囲を黒に設定
|
| 284 |
p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
|