rein0421 commited on
Commit
a5dea16
·
verified ·
1 Parent(s): 9beecd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -28
app.py CHANGED
@@ -224,10 +224,6 @@ def create_mask(image, x1, y1, x2, y2):
224
 
225
 
226
  def special_process_image_yolo(risk_level, image_path, point1, point2, thresholds=None):
227
- import os
228
- import torch
229
- from ultralytics import YOLO
230
-
231
  # デバイスの確認
232
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
233
  print(f"Using device: {device}")
@@ -238,14 +234,10 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
238
  # モデルファイルの存在確認
239
  if not os.path.isfile(model_path):
240
  raise FileNotFoundError(f"モデルファイル '{model_path}' が見つかりません。パスを確認してください。")
241
- else:
242
- print("モデルが正常にロードされ、デバイスに移動しました。")
243
- # YOLOv8モデルをロードし、指定デバイスに移動
244
- model = YOLO(model_path).to(device)
245
-
246
-
247
- model.to(device) # モデルをGPUに移動
248
-
249
 
250
  # タイムスタンプを作成
251
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
@@ -255,56 +247,56 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
255
  return 1 / (1 + np.exp(-k * (risk_level - r0)))
256
 
257
  decay_factor = logistic_decay(risk_level)
258
- adjusted_thresholds = {key: max(value - decay_factor + 0.8, 0.01) / 2 for key, value in thresholds.items()}
259
-
260
-
261
 
262
  # 画像の読み込みとRGB変換
263
  image = cv2.imread(image_path)
264
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
265
- image_np = np.array(image_rgb, dtype=np.uint8)
266
 
267
  # 推論実行
268
  results = model(image_rgb)
269
 
270
  # 初期化したマスク画像
271
- mask = np.zeros((image_np.shape[0], image_np.shape[1]), 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
-
283
  if confidence >= threshold:
284
  mask[y1:y2, x1:x2] = 255 # ボックス領域を白に設定
285
 
286
  # 絶対座標に変換した点の範囲を黒に設定
287
- p1_x, p1_y = int(point1[0] * image_np.shape[1]), int(point1[1] * image_np.shape[0])
288
- p2_x, p2_y = int(point2[0] * image_np.shape[1]), int(point2[1] * image_np.shape[0])
289
  x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
290
- x_max, y_max = min(image_np.shape[1], max(p1_x, p2_x)), min(image_np.shape[0], max(p1_y, p2_y))
291
  mask[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
292
 
293
  # デバッグ用に白い長方形を描画
294
- debug_image = image_np.copy()
295
  cv2.rectangle(debug_image, (x_min, y_min), (x_max, y_max), (255, 255, 255), 2)
296
 
297
  # デバッグ画像とマスク画像を保存
 
 
298
  debug_image_pil = Image.fromarray(debug_image)
299
- debug_image_pil.save(f"./debug_image_with_rectangle_{timestamp}.jpg")
 
300
 
301
  mask_image_pil = Image.fromarray(mask)
302
- mask_image_pil.save(f"./final_mask_{timestamp}.jpg")
303
-
304
- return f"./final_mask_{timestamp}.jpg"
305
-
306
 
 
 
307
 
 
308
 
309
 
310
  def convert_image_format(input_path, output_format="png"):
 
224
 
225
 
226
  def special_process_image_yolo(risk_level, image_path, point1, point2, thresholds=None):
 
 
 
 
227
  # デバイスの確認
228
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
229
  print(f"Using device: {device}")
 
234
  # モデルファイルの存在確認
235
  if not os.path.isfile(model_path):
236
  raise FileNotFoundError(f"モデルファイル '{model_path}' が見つかりません。パスを確認してください。")
237
+
238
+ # YOLOv8モデルをロードし、指定デバイスに移動
239
+ model = YOLO(model_path).to(device)
240
+ print("モデルが正常にロードされ、デバイスに移動しました。")
 
 
 
 
241
 
242
  # タイムスタンプを作成
243
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
 
247
  return 1 / (1 + np.exp(-k * (risk_level - r0)))
248
 
249
  decay_factor = logistic_decay(risk_level)
250
+ adjusted_thresholds = {key: max(value - decay_factor + 0.8, 0.01) / 2 for key, value in (thresholds or {}).items()}
 
 
251
 
252
  # 画像の読み込みとRGB変換
253
  image = cv2.imread(image_path)
254
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
255
 
256
  # 推論実行
257
  results = model(image_rgb)
258
 
259
  # 初期化したマスク画像
260
+ mask = np.zeros(image.shape[:2], dtype=np.uint8)
261
 
262
  # 各検出結果に基づきマスク作成
263
  for box in results[0].boxes:
264
+ x1, y1, x2, y2 = map(int, box.xyxy[0])
265
  confidence = box.conf[0]
266
  class_id = box.cls[0]
267
  object_type = model.names[int(class_id)]
268
 
269
  # クラス名に基づいたしきい値
270
  threshold = adjusted_thresholds.get(object_type, 0.5)
 
271
  if confidence >= threshold:
272
  mask[y1:y2, x1:x2] = 255 # ボックス領域を白に設定
273
 
274
  # 絶対座標に変換した点の範囲を黒に設定
275
+ p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
276
+ p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
277
  x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
278
+ x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
279
  mask[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
280
 
281
  # デバッグ用に白い長方形を描画
282
+ debug_image = image_rgb.copy()
283
  cv2.rectangle(debug_image, (x_min, y_min), (x_max, y_max), (255, 255, 255), 2)
284
 
285
  # デバッグ画像とマスク画像を保存
286
+ save_dir = "./saved_images"
287
+ os.makedirs(save_dir, exist_ok=True)
288
  debug_image_pil = Image.fromarray(debug_image)
289
+ debug_image_path = os.path.join(save_dir, f"debug_image_with_rectangle_{timestamp}.jpg")
290
+ debug_image_pil.save(debug_image_path)
291
 
292
  mask_image_pil = Image.fromarray(mask)
293
+ mask_image_path = os.path.join(save_dir, f"final_mask_{timestamp}.jpg")
294
+ mask_image_pil.save(mask_image_path)
 
 
295
 
296
+ print(f"デバッグ画像が {debug_image_path} に保存されました。")
297
+ print(f"マスク画像が {mask_image_path} に保存されました。")
298
 
299
+ return mask_image_path
300
 
301
 
302
  def convert_image_format(input_path, output_format="png"):