character_openpose_editor / utils /notifications.py
gearmachine's picture
feat: 通知機能の改善
65c2919
# Notification utilities for dwpose-editor
import gradio as gr
# 通知設定
NOTIFICATION_SETTINGS = {
'show_success': True,
'show_warnings': True,
'show_errors': True,
'auto_dismiss': True,
'info_duration': 3.0, # Info系:3秒
'error_duration': 5.0 # Error系:5秒
}
def notify_success(message):
"""成功通知"""
if NOTIFICATION_SETTINGS['show_success']:
duration = NOTIFICATION_SETTINGS['info_duration'] # 2秒
return gr.Info(message, duration=duration)
return None
def notify_warning(message):
"""警告通知"""
if NOTIFICATION_SETTINGS['show_warnings']:
duration = NOTIFICATION_SETTINGS['info_duration'] # 2秒
return gr.Warning(message, duration=duration)
return None
def notify_error(message):
"""エラー通知"""
if NOTIFICATION_SETTINGS['show_errors']:
duration = NOTIFICATION_SETTINGS['error_duration'] # 5秒
return gr.Error(message, duration=duration)
return None
def notify_progress(message, progress=None):
"""進捗通知"""
if progress is not None:
# 進捗バーつき通知(Gradio 4.0以降)
try:
return gr.Progress(progress, desc=message)
except:
# フォールバック
return gr.Info(f"{message} ({int(progress*100)}%)")
else:
return gr.Info(message)
def configure_notifications(settings):
"""通知設定の更新"""
global NOTIFICATION_SETTINGS
NOTIFICATION_SETTINGS.update(settings)
return NOTIFICATION_SETTINGS
# よく使用される通知メッセージ
class NotificationMessages:
# 成功メッセージ
MODEL_LOADED = "DWPoseモデル読み込み完了"
POSE_DETECTED = "ポーズ検出完了"
IMAGE_UPLOADED = "画像アップロード完了"
CANVAS_UPDATED = "キャンバス更新完了"
# 警告メッセージ
NO_PERSON_DETECTED = "人物が検出されませんでした。別の画像をお試しください"
MODEL_LOADING = "モデル読み込み中です。しばらくお待ちください"
# エラーメッセージ
MODEL_LOAD_FAILED = "モデルの読み込みに失敗しました"
POSE_DETECTION_FAILED = "ポーズ検出に失敗しました"
IMAGE_PROCESSING_FAILED = "画像処理に失敗しました"
NETWORK_ERROR = "ネットワークエラーが発生しました"
# 進捗付きポーズ検出
def detect_pose_with_progress(detector, image):
"""進捗表示付きポーズ検出"""
try:
notify_progress("画像を処理中...", 0.1)
# 画像前処理
notify_progress("画像前処理中...", 0.2)
# 人物検出
notify_progress("人物を検出中...", 0.4)
# ポーズ推定
notify_progress("ポーズを解析中...", 0.7)
# 結果取得
result = detector.detect(image)
notify_progress("完了", 1.0)
return result
except Exception as e:
notify_error(f"処理中にエラーが発生しました: {str(e)}")
return None, str(e)