rein0421 commited on
Commit
128a3a6
·
verified ·
1 Parent(s): de21c48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -9
app.py CHANGED
@@ -222,6 +222,8 @@ def create_mask(image, x1, y1, x2, y2):
222
  cv2.rectangle(mask, (int(x1), int(y1)), (int(x2), int(y2)), 255, -1)
223
  return mask
224
 
 
 
225
  # 特殊な処理を行う関数
226
  def special_process_image_yolo(risk_level, image_path, point1, point2, thresholds=None):
227
  # デバイスの確認
@@ -239,6 +241,9 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
239
  model = YOLO(model_path).to(device)
240
  print("モデルが正常にロードされ、デバイスに移動しました。")
241
 
 
 
 
242
  # タイムスタンプを作成
243
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
244
 
@@ -250,7 +255,6 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
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 = {}
@@ -262,13 +266,19 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
262
  image = cv2.imread(image_path)
263
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
264
 
265
- # 推論実行
266
- results = model(image_rgb)
 
 
 
 
 
267
 
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]
@@ -278,14 +288,17 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
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])
285
  p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
286
  x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
287
  x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
288
- mask[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
289
 
290
  # デバッグ用に白い長方形を描画
291
  debug_image = image_rgb.copy()
@@ -298,7 +311,7 @@ def special_process_image_yolo(risk_level, image_path, point1, point2, threshold
298
  debug_image_path = os.path.join(save_dir, f"debug_image_with_rectangle_{timestamp}.jpg")
299
  debug_image_pil.save(debug_image_path)
300
 
301
- mask_image_pil = Image.fromarray(mask)
302
  mask_image_path = os.path.join(save_dir, f"final_mask_{timestamp}.jpg")
303
  mask_image_pil.save(mask_image_path)
304
 
 
222
  cv2.rectangle(mask, (int(x1), int(y1)), (int(x2), int(y2)), 255, -1)
223
  return mask
224
 
225
+ import easyocr
226
+
227
  # 特殊な処理を行う関数
228
  def special_process_image_yolo(risk_level, image_path, point1, point2, thresholds=None):
229
  # デバイスの確認
 
241
  model = YOLO(model_path).to(device)
242
  print("モデルが正常にロードされ、デバイスに移動しました。")
243
 
244
+ # OCRモデルの初期化
245
+ reader = easyocr.Reader(['en', 'ja']) # 言語は必要に応じて調整
246
+
247
  # タイムスタンプを作成
248
  timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
249
 
 
255
 
256
  def logistic_decay_for_label(risk_level, label_index, k=0.1, r0=50):
257
  base_decay = 1 / (1 + np.exp(-k * (risk_level - r0)))
 
258
  return max(base_decay + 0.05 * label_index, 0.01)
259
 
260
  adjusted_thresholds = {}
 
266
  image = cv2.imread(image_path)
267
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
268
 
269
+ # OCRによる文字領域マスク作成
270
+ mask_ocr = np.zeros(image.shape[:2], dtype=np.uint8)
271
+ ocr_results = reader.readtext(image_rgb)
272
+ for (bbox, text, ocr_conf) in ocr_results:
273
+ x1, y1 = int(bbox[0][0]), int(bbox[0][1])
274
+ x2, y2 = int(bbox[2][0]), int(bbox[2][1])
275
+ mask_ocr[y1:y2, x1:x2] = 255 # テキスト領域をマスク
276
 
277
+ # YOLO推論実行
278
+ results = model(image_rgb)
279
 
280
+ # YOLOによる物体検出マスク作成
281
+ mask_yolo = np.zeros(image.shape[:2], dtype=np.uint8)
282
  for box in results[0].boxes:
283
  x1, y1, x2, y2 = map(int, box.xyxy[0])
284
  confidence = box.conf[0]
 
288
  # オブジェクトの閾値を確認し、マスクを適用
289
  threshold = adjusted_thresholds.get(object_type, 0.5)
290
  if confidence >= threshold:
291
+ mask_yolo = create_mask(image, x1, y1, x2, y2)
292
+
293
+ # OCRマスクとYOLOマスクの結合
294
+ final_mask = cv2.bitwise_or(mask_ocr, mask_yolo)
295
 
296
  # 絶対座標に変換した点の範囲を黒に設定
297
  p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
298
  p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
299
  x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
300
  x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
301
+ final_mask[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
302
 
303
  # デバッグ用に白い長方形を描画
304
  debug_image = image_rgb.copy()
 
311
  debug_image_path = os.path.join(save_dir, f"debug_image_with_rectangle_{timestamp}.jpg")
312
  debug_image_pil.save(debug_image_path)
313
 
314
+ mask_image_pil = Image.fromarray(final_mask)
315
  mask_image_path = os.path.join(save_dir, f"final_mask_{timestamp}.jpg")
316
  mask_image_pil.save(mask_image_path)
317