Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -83,26 +83,18 @@ dangerarray=[10,30,90,50,80,20,40,70,100,60]#ここに各クラスターの危
|
|
| 83 |
|
| 84 |
thresholds = {
|
| 85 |
'text': 0.1,
|
| 86 |
-
'
|
| 87 |
-
'
|
| 88 |
-
'Mail': 0.3,
|
| 89 |
'Documents': 0.3,
|
| 90 |
-
'QR codes': 0.4,
|
| 91 |
-
'barcodes': 0.4,
|
| 92 |
-
'map': 0.5,
|
| 93 |
'digital screens': 0.6,
|
| 94 |
'information board': 0.5,
|
| 95 |
'signboard': 0.3,
|
| 96 |
'poster': 0.8,
|
| 97 |
'sign': 0.3,
|
| 98 |
'logo': 0.3,
|
| 99 |
-
'card': 0.4,
|
| 100 |
-
'window': 0.2,
|
| 101 |
-
'mirror': 0.2,
|
| 102 |
-
'Famous landmark': 0.7,
|
| 103 |
'cardboard': 0.6,
|
| 104 |
'manhole': 0.6,
|
| 105 |
-
'
|
| 106 |
}
|
| 107 |
|
| 108 |
'''
|
|
@@ -201,26 +193,28 @@ def analyze_mask_combined(original_image_path, mask_image_path, small_threshold,
|
|
| 201 |
|
| 202 |
|
| 203 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
#この下で消去対象を決定
|
| 205 |
def decide_to_object(risk_level):
|
| 206 |
-
'''
|
| 207 |
-
tex = [
|
| 208 |
-
'text','Name tag', 'License plate', 'Mail', 'Documents', 'QR codes',
|
| 209 |
-
'barcodes', 'Map', 'Digital screens', 'information board',
|
| 210 |
-
'signboard', 'poster', 'sign', 'utility pole'
|
| 211 |
|
| 212 |
-
]
|
| 213 |
-
'''
|
| 214 |
tex = [
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
]
|
| 220 |
|
| 221 |
|
| 222 |
#この配列の要素の順番を変えると消える順番が変わる。
|
| 223 |
-
risk_level = int(risk_level /
|
| 224 |
return tex[:int(risk_level)+1]
|
| 225 |
|
| 226 |
def create_mask(image, x1, y1, x2, y2):
|
|
@@ -233,83 +227,6 @@ def create_mask(image, x1, y1, x2, y2):
|
|
| 233 |
return mask
|
| 234 |
|
| 235 |
|
| 236 |
-
def special_process_image_yolo(risk_level, image_path, point1, point2, thresholds=None):
|
| 237 |
-
# デバイスの確認
|
| 238 |
-
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 239 |
-
print(f"Using device: {device}")
|
| 240 |
-
|
| 241 |
-
# モデルファイルのパス
|
| 242 |
-
model_path = './1113.pt'
|
| 243 |
-
|
| 244 |
-
# モデルファイルの存在確認
|
| 245 |
-
if not os.path.isfile(model_path):
|
| 246 |
-
raise FileNotFoundError(f"モデルファイル '{model_path}' が見つかりません。パスを確認してください。")
|
| 247 |
-
|
| 248 |
-
# YOLOv8モデルをロードし、指定デバイスに移動
|
| 249 |
-
model = YOLO(model_path).to(device)
|
| 250 |
-
print("モデルが正常にロードされ、デバイスに移動しました。")
|
| 251 |
-
print(model.names)
|
| 252 |
-
|
| 253 |
-
# タイムスタンプを作成
|
| 254 |
-
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
|
| 255 |
-
|
| 256 |
-
# リスクレベルに基づいた減衰率の計算
|
| 257 |
-
def logistic_decay(risk_level, k=0.1, r0=50):
|
| 258 |
-
return 1 / (1 + np.exp(-k * (risk_level - r0)))
|
| 259 |
-
|
| 260 |
-
decay_factor = logistic_decay(risk_level)
|
| 261 |
-
adjusted_thresholds = {key: max(value - decay_factor + 0.8, 0.01) / 2 for key, value in (thresholds or {}).items()}
|
| 262 |
-
|
| 263 |
-
# 画像の読み込みとRGB変換
|
| 264 |
-
image = cv2.imread(image_path)
|
| 265 |
-
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 266 |
-
|
| 267 |
-
# 推論実行
|
| 268 |
-
results = model(image_rgb)
|
| 269 |
-
|
| 270 |
-
# 初期化したマスク画像
|
| 271 |
-
mask = np.zeros(image.shape[:2], dtype=np.uint8)
|
| 272 |
-
|
| 273 |
-
# 各検出結果に基づきマスク作成
|
| 274 |
-
for box in results[0].boxes:
|
| 275 |
-
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 276 |
-
confidence = box.conf[0]
|
| 277 |
-
class_id = box.cls[0]
|
| 278 |
-
object_type = model.names[int(class_id)]
|
| 279 |
-
|
| 280 |
-
# クラス名に基づいたしきい値
|
| 281 |
-
threshold = adjusted_thresholds.get(object_type, 0.5)
|
| 282 |
-
if confidence >= threshold:
|
| 283 |
-
mask[y1:y2, x1:x2] = 255 # ボックス領域を白に設定
|
| 284 |
-
|
| 285 |
-
# 絶対座標に変換した点の範囲を黒に設定
|
| 286 |
-
p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
|
| 287 |
-
p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
|
| 288 |
-
x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
|
| 289 |
-
x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
|
| 290 |
-
mask[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
|
| 291 |
-
|
| 292 |
-
# デバッグ用に白い長方形を描画
|
| 293 |
-
debug_image = image_rgb.copy()
|
| 294 |
-
cv2.rectangle(debug_image, (x_min, y_min), (x_max, y_max), (255, 255, 255), 2)
|
| 295 |
-
|
| 296 |
-
# デバッグ画像とマスク画像を保存
|
| 297 |
-
save_dir = "./saved_images"
|
| 298 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 299 |
-
debug_image_pil = Image.fromarray(debug_image)
|
| 300 |
-
debug_image_path = os.path.join(save_dir, f"debug_image_with_rectangle_{timestamp}.jpg")
|
| 301 |
-
debug_image_pil.save(debug_image_path)
|
| 302 |
-
|
| 303 |
-
mask_image_pil = Image.fromarray(mask)
|
| 304 |
-
mask_image_path = os.path.join(save_dir, f"final_mask_{timestamp}.jpg")
|
| 305 |
-
mask_image_pil.save(mask_image_path)
|
| 306 |
-
|
| 307 |
-
print(f"デバッグ画像が {debug_image_path} に保存されました。")
|
| 308 |
-
print(f"マスク画像が {mask_image_path} に保存されました。")
|
| 309 |
-
|
| 310 |
-
return mask_image_path
|
| 311 |
-
|
| 312 |
-
|
| 313 |
def convert_image_format(input_path, output_format="png"):
|
| 314 |
"""
|
| 315 |
画像をJPGからPNGまたはPNGからJPGに変換する関数。
|
|
|
|
| 83 |
|
| 84 |
thresholds = {
|
| 85 |
'text': 0.1,
|
| 86 |
+
'License plate': 0.3,
|
| 87 |
+
'Mail or envelope': 0.3,
|
|
|
|
| 88 |
'Documents': 0.3,
|
|
|
|
|
|
|
|
|
|
| 89 |
'digital screens': 0.6,
|
| 90 |
'information board': 0.5,
|
| 91 |
'signboard': 0.3,
|
| 92 |
'poster': 0.8,
|
| 93 |
'sign': 0.3,
|
| 94 |
'logo': 0.3,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
'cardboard': 0.6,
|
| 96 |
'manhole': 0.6,
|
| 97 |
+
'electricity pole': 0.7
|
| 98 |
}
|
| 99 |
|
| 100 |
'''
|
|
|
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
+
|
| 197 |
+
def create_mask(image, x1, y1, x2, y2):
|
| 198 |
+
# Create a black image with the same size as the input image
|
| 199 |
+
mask = np.zeros((image.shape[0], image.shape[1]), dtype=np.uint8)
|
| 200 |
+
|
| 201 |
+
# Draw a white rectangle on the mask where the object is located
|
| 202 |
+
cv2.rectangle(mask, (int(x1), int(y1)), (int(x2), int(y2)), 255, -1)
|
| 203 |
+
|
| 204 |
+
return mask
|
| 205 |
+
|
| 206 |
+
|
| 207 |
#この下で消去対象を決定
|
| 208 |
def decide_to_object(risk_level):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
|
|
|
|
|
|
| 210 |
tex = [
|
| 211 |
+
'text', 'License plate', 'digital screens',
|
| 212 |
+
'signboard', 'poster', 'sign', 'logo', 'cardboard', 'manhole', 'electricity pole'
|
| 213 |
+
]
|
|
|
|
|
|
|
| 214 |
|
| 215 |
|
| 216 |
#この配列の要素の順番を変えると消える順番が変わる。
|
| 217 |
+
risk_level = int(risk_level / 10)*(len(tex)/10)#個数決定(1/2)
|
| 218 |
return tex[:int(risk_level)+1]
|
| 219 |
|
| 220 |
def create_mask(image, x1, y1, x2, y2):
|
|
|
|
| 227 |
return mask
|
| 228 |
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
def convert_image_format(input_path, output_format="png"):
|
| 231 |
"""
|
| 232 |
画像をJPGからPNGまたはPNGからJPGに変換する関数。
|