Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -283,67 +283,109 @@ def llm_to_process_image_simple(risk_level, image_path, point1, point2, threshol
|
|
283 |
return save_dir + debug_image_path
|
284 |
|
285 |
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
x2, y2 = int(bbox[3]* image.shape[1]), int(bbox[2]* image.shape[0])
|
316 |
-
mask_llm[y1:y2, x1:x2] = 255 # テキスト領域をマスク
|
317 |
-
p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
|
318 |
-
p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
|
319 |
-
x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
|
320 |
-
x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
|
321 |
-
mask_llm[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
|
322 |
-
save_dir = "./saved_images"
|
323 |
-
os.makedirs(save_dir, exist_ok=True)
|
324 |
-
debug_image_pil = Image.fromarray(mask_llm)
|
325 |
-
debug_image_pil.save(save_dir + debug_image_path)
|
326 |
-
return save_dir + debug_image_path,response
|
327 |
|
|
|
|
|
|
|
328 |
|
|
|
329 |
|
|
|
|
|
330 |
|
|
|
|
|
|
|
331 |
|
|
|
|
|
|
|
|
|
332 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
|
|
|
|
334 |
|
|
|
|
|
|
|
335 |
|
|
|
|
|
|
|
|
|
336 |
|
|
|
|
|
337 |
|
|
|
|
|
|
|
338 |
|
|
|
|
|
|
|
|
|
|
|
339 |
|
|
|
|
|
340 |
|
|
|
341 |
|
|
|
|
|
|
|
342 |
|
|
|
|
|
343 |
|
|
|
344 |
|
|
|
|
|
345 |
|
|
|
|
|
|
|
346 |
|
|
|
347 |
|
348 |
|
349 |
|
|
|
283 |
return save_dir + debug_image_path
|
284 |
|
285 |
|
286 |
+
# ObjectDetector と WebScraper は非同期対応が必要です。
|
287 |
+
# 仮のクラス定義(実際のあなたのクラスに置き換えてください)
|
288 |
+
class ObjectDetector:
|
289 |
+
def __init__(self, API_KEY):
|
290 |
+
self.API_KEY = API_KEY
|
291 |
+
self.prompt_objects = []
|
292 |
+
self.text = ""
|
293 |
+
|
294 |
+
async def detect_auto(self, image_path):
|
295 |
+
print(f"Detecting objects automatically for {image_path}")
|
296 |
+
await asyncio.sleep(0.1) # 非同期処理のシミュレーション
|
297 |
+
return {"objects_to_remove": ["人", "車"]} # 例の戻り値
|
298 |
+
|
299 |
+
async def detect_objects(self, image_path):
|
300 |
+
print(f"Detecting specific objects for {image_path}")
|
301 |
+
await asyncio.sleep(0.1) # 非同期処理のシミュレーション
|
302 |
+
return [
|
303 |
+
{'box_2d': [0.1, 0.1, 0.3, 0.3]}, # 例のバウンディングボックス (y1, x1, y2, x2)
|
304 |
+
{'box_2d': [0.5, 0.5, 0.7, 0.7]}
|
305 |
+
]
|
306 |
+
|
307 |
+
class WebScraper:
|
308 |
+
def __init__(self, headless):
|
309 |
+
self.headless = headless
|
310 |
+
|
311 |
+
async def get_processed_documents(self, search_query, num_search_results):
|
312 |
+
print(f"Scraping for: {search_query}")
|
313 |
+
await asyncio.sleep(0.1) # 非同期処理のシミュレーション
|
314 |
+
return {"cleaned_html_content": "個人情報漏洩に関するクリーンなコンテンツの例。"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
|
316 |
+
async def llm_to_process_image_simple_auto(risk_level, image_path, point1, point2, thresholds=None):
|
317 |
+
print(f"リスクレベル: {risk_level}, 画像パス: {image_path}, point1: {point1}, point2: {point2}, しきい値: {thresholds}")
|
318 |
+
print(f"point1, point2: {point1}, {point2}")
|
319 |
|
320 |
+
GEMINI_API_KEY = os.getenv('GEMINI_API_KEY')
|
321 |
|
322 |
+
# 画像処理のロジックを追加
|
323 |
+
Objectdetector = ObjectDetector(API_KEY=GEMINI_API_KEY)
|
324 |
|
325 |
+
# デバッグ用の画像パスを定義。保存時にファイル名として使われます。
|
326 |
+
debug_image_name = "masked_image.jpg" # デバッグ画像名を具体的に
|
327 |
+
debug_image_path = os.path.join("./saved_images", debug_image_name)
|
328 |
|
329 |
+
# 非同期メソッドをawaitで呼び出す
|
330 |
+
response = await Objectdetector.detect_auto(image_path)
|
331 |
+
print(f"削除対象オブジェクト: {response['objects_to_remove']}")
|
332 |
+
Objectdetector.prompt_objects = response["objects_to_remove"]
|
333 |
|
334 |
+
# 個人情報流出に関する事例を検索し、クリーンなコンテンツを取得
|
335 |
+
scraper = WebScraper(headless=True)
|
336 |
+
# ここでasyncio.run()は不要。単にawaitする
|
337 |
+
personal_breach_docs = await scraper.get_processed_documents(
|
338 |
+
search_query="個人情報流出 事�� SNS",
|
339 |
+
num_search_results=10
|
340 |
+
)
|
341 |
|
342 |
+
print(f"取得したドキュメント: {personal_breach_docs}")
|
343 |
+
Objectdetector.text = personal_breach_docs["cleaned_html_content"]
|
344 |
|
345 |
+
# 画像の読み込みとRGB変換
|
346 |
+
print(f"Objectdetector.prompt_objects: {Objectdetector.prompt_objects}")
|
347 |
+
image = cv2.imread(image_path)
|
348 |
|
349 |
+
# 画像の読み込みに失敗した場合のハンドリング
|
350 |
+
if image is None:
|
351 |
+
print(f"エラー: {image_path} から画像を読み込めませんでした。")
|
352 |
+
return None, {"error": "画像を読み込めませんでした"}
|
353 |
|
354 |
+
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
355 |
+
mask_llm = np.zeros(image.shape[:2], dtype=np.uint8)
|
356 |
|
357 |
+
# 非同期メソッドをawaitで呼び出す
|
358 |
+
llm_results = await Objectdetector.detect_objects(image_path)
|
359 |
+
print(f"LLM検出結果: {llm_results}")
|
360 |
|
361 |
+
for result in llm_results:
|
362 |
+
bbox = result['box_2d']
|
363 |
+
# バウンディングボックスの座標を画像サイズに変換し、範囲内に収める
|
364 |
+
x1, y1 = int(bbox[1] * image.shape[1]), int(bbox[0] * image.shape[0])
|
365 |
+
x2, y2 = int(bbox[3] * image.shape[1]), int(bbox[2] * image.shape[0])
|
366 |
|
367 |
+
x1, y1 = max(0, x1), max(0, y1)
|
368 |
+
x2, y2 = min(image.shape[1], x2), min(image.shape[0], y2)
|
369 |
|
370 |
+
mask_llm[y1:y2, x1:x2] = 255 # テキスト領域をマスク
|
371 |
|
372 |
+
# 指定された2点間の領域を黒く設定
|
373 |
+
p1_x, p1_y = int(point1[0] * image.shape[1]), int(point1[1] * image.shape[0])
|
374 |
+
p2_x, p2_y = int(point2[0] * image.shape[1]), int(point2[1] * image.shape[0])
|
375 |
|
376 |
+
x_min, y_min = max(0, min(p1_x, p2_x)), max(0, min(p1_y, p2_y))
|
377 |
+
x_max, y_max = min(image.shape[1], max(p1_x, p2_x)), min(image.shape[0], max(p1_y, p2_y))
|
378 |
|
379 |
+
mask_llm[y_min:y_max, x_min:x_max] = 0 # 範囲を黒に設定
|
380 |
|
381 |
+
save_dir = "./saved_images"
|
382 |
+
os.makedirs(save_dir, exist_ok=True) # 保存ディレクトリが存在しない場合に作成
|
383 |
|
384 |
+
debug_image_pil = Image.fromarray(mask_llm)
|
385 |
+
mask_save_path = os.path.join(save_dir, debug_image_name) # 保存パスを構築
|
386 |
+
debug_image_pil.save(mask_save_path)
|
387 |
|
388 |
+
return mask_save_path, response
|
389 |
|
390 |
|
391 |
|