tools(cleanup): 添加清理旧ROI配置的脚本
问题分析: - 用户只配置了3个ROI (使用cam_xxx格式的camera_id) - 数据库中有7个旧ROI (使用中文名称格式的camera_id) - 旧ROI是残留数据,需要清理 功能: - 自动识别cam_xxx格式的用户ROI - 识别中文名称格式的旧ROI - 删除旧ROI及其关联的算法绑定 - 保留用户实际配置的ROI Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
116
cleanup_old_rois.py
Normal file
116
cleanup_old_rois.py
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
清理旧的ROI配置 - 只保留用户实际配置的3个ROI
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
sys.path.insert(0, str(Path(__file__).parent))
|
||||||
|
|
||||||
|
from core.config_sync import get_config_sync_manager
|
||||||
|
|
||||||
|
def main():
|
||||||
|
print("=== Old ROI Cleanup Tool ===\n")
|
||||||
|
|
||||||
|
config_manager = get_config_sync_manager()
|
||||||
|
|
||||||
|
# Get all ROIs
|
||||||
|
all_rois = config_manager.get_roi_configs(force_refresh=True)
|
||||||
|
binds = config_manager.get_bindings()
|
||||||
|
|
||||||
|
print(f"Current ROIs: {len(all_rois)}")
|
||||||
|
|
||||||
|
# Separate old ROIs (using Chinese names) from user-configured ROIs (using cam_xxx)
|
||||||
|
user_rois = [roi for roi in all_rois if roi.camera_id.startswith('cam_')]
|
||||||
|
old_rois = [roi for roi in all_rois if not roi.camera_id.startswith('cam_')]
|
||||||
|
|
||||||
|
print(f"\nUser-configured ROIs (cam_xxx format): {len(user_rois)}")
|
||||||
|
for roi in user_rois:
|
||||||
|
print(f" - ROI: {roi.roi_id}")
|
||||||
|
print(f" Camera: {roi.camera_id}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
print(f"Old ROIs (Chinese name format, to be deleted): {len(old_rois)}")
|
||||||
|
for roi in old_rois:
|
||||||
|
print(f" - ROI: {roi.roi_id}")
|
||||||
|
print(f" Camera: {roi.camera_id}")
|
||||||
|
print()
|
||||||
|
|
||||||
|
if not old_rois:
|
||||||
|
print("[OK] No old ROIs found. Database is clean!")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Find related bindings
|
||||||
|
old_roi_ids = {roi.roi_id for roi in old_rois}
|
||||||
|
old_binds = [b for b in binds if b.roi_id in old_roi_ids]
|
||||||
|
|
||||||
|
print(f"Related old bindings: {len(old_binds)}")
|
||||||
|
for bind in old_binds:
|
||||||
|
print(f" - Bind: {bind.bind_id} (ROI: {bind.roi_id}, Algo: {bind.algo_code})")
|
||||||
|
|
||||||
|
# Ask for confirmation
|
||||||
|
print("\n" + "=" * 50)
|
||||||
|
print("Cleanup Plan:")
|
||||||
|
print(f" 1. Delete {len(old_rois)} old ROI records (Chinese name format)")
|
||||||
|
print(f" 2. Delete {len(old_binds)} related old binding records")
|
||||||
|
print(f" 3. Keep {len(user_rois)} user-configured ROIs (cam_xxx format)")
|
||||||
|
print("=" * 50)
|
||||||
|
|
||||||
|
response = input("\nProceed with cleanup? (yes/no): ").strip().lower()
|
||||||
|
|
||||||
|
if response != 'yes':
|
||||||
|
print("\n[CANCELLED] Cleanup aborted")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Perform cleanup
|
||||||
|
print("\n[EXECUTING] Cleanup in progress...")
|
||||||
|
|
||||||
|
config_manager._init_database()
|
||||||
|
db_manager = config_manager._db_manager
|
||||||
|
|
||||||
|
if not db_manager:
|
||||||
|
print("[ERROR] Database manager not available")
|
||||||
|
return
|
||||||
|
|
||||||
|
deleted_rois = 0
|
||||||
|
deleted_binds = 0
|
||||||
|
|
||||||
|
# Delete old bindings first
|
||||||
|
for bind in old_binds:
|
||||||
|
try:
|
||||||
|
db_manager.delete_binding(bind.bind_id)
|
||||||
|
deleted_binds += 1
|
||||||
|
print(f" [OK] Deleted binding: {bind.bind_id}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" [FAIL] Failed to delete binding {bind.bind_id}: {e}")
|
||||||
|
|
||||||
|
# Delete old ROIs
|
||||||
|
for roi in old_rois:
|
||||||
|
try:
|
||||||
|
db_manager.delete_roi(roi.roi_id)
|
||||||
|
deleted_rois += 1
|
||||||
|
print(f" [OK] Deleted ROI: {roi.roi_id} (Camera: {roi.camera_id})")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" [FAIL] Failed to delete ROI {roi.roi_id}: {e}")
|
||||||
|
|
||||||
|
# Invalidate cache
|
||||||
|
config_manager.invalidate_all_cache()
|
||||||
|
|
||||||
|
# Summary
|
||||||
|
print("\n" + "=" * 50)
|
||||||
|
print("Cleanup Summary")
|
||||||
|
print("=" * 50)
|
||||||
|
print(f"Deleted old ROIs: {deleted_rois}/{len(old_rois)}")
|
||||||
|
print(f"Deleted old bindings: {deleted_binds}/{len(old_binds)}")
|
||||||
|
print(f"Remaining user ROIs: {len(user_rois)}")
|
||||||
|
|
||||||
|
if deleted_rois == len(old_rois):
|
||||||
|
print("\n[SUCCESS] Old ROI records cleaned up!")
|
||||||
|
print("\nNow you have:")
|
||||||
|
print(f" - {len(user_rois)} ROI configs (your actual configurations)")
|
||||||
|
print(" - 0 ROI configs (old data)")
|
||||||
|
print("\nNext: You need to add the 3 camera configs for cam_xxx IDs")
|
||||||
|
else:
|
||||||
|
print("\n[WARNING] Some records failed to delete")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user