Files
vision-record/config.py

161 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
配置管理模块
"""
import os
import json
from pathlib import Path
from datetime import datetime
# 基础路径
BASE_DIR = Path(__file__).parent.absolute()
DATA_DIR = BASE_DIR / "data"
DB_PATH = DATA_DIR / "events.db"
CONFIG_PATH = DATA_DIR / "config.json"
# 确保目录存在
DATA_DIR.mkdir(parents=True, exist_ok=True)
# Web 服务配置
WEB_PORT = 19016
WEB_HOST = "0.0.0.0"
# 默认配置
DEFAULT_CONFIG = {
"images_dir": str(DATA_DIR / "images"),
"camera_index": 0,
"capture_interval": 60,
"auto_analyze": True,
"refresh_interval": 5,
"display_limit": 20,
# 人体检测设置YOLO
"use_yolo": True,
"yolo_min_confidence": 0.3,
# 人员识别设置
"use_face_recognition": True,
"use_mediapipe_face": True,
"use_color_histogram": True,
"face_match_threshold": 0.6,
# 连续判断设置
"confirm_frames": 3,
"leave_frames": 2,
# AI大模型分析
"use_vision_api": False,
"vision_api_trigger": "person_change",
# Vision API 配置
"vision_api_url": os.environ.get("VISION_API_URL", "https://dashscope.aliyuncs.com/compatible-mode/v1"),
"vision_api_key": os.environ.get("VISION_API_KEY", "sk-lm-fuP5tGU8:Hi7YU87jHyDP6Ay8Tl2j"),
"vision_model": os.environ.get("VISION_MODEL", "qwen-vl-plus")
}
# 分析配置
ANALYSIS_PROMPT = """请分析这张图片,识别其中的重要事件或变化。
重点关注:
1. 人物活动(有人出现、离开、动作等)
2. 物体变化(物品移动、新增、消失等)
3. 环境变化(光线、天气、状态等)
4. 异常情况(潜在危险、异常行为等)
请用简洁的中文描述你观察到的事件,格式如下:
事件类型: [类型]
描述: [简短描述]
置信度: [高/中/低]
如果没有明显事件,请回答"无明显事件"
"""
# 数据库配置
DB_SCHEMA = """
CREATE TABLE IF NOT EXISTS config (
key TEXT PRIMARY KEY,
value TEXT
);
CREATE TABLE IF NOT EXISTS images (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
camera_id INTEGER DEFAULT 0,
analyzed BOOLEAN DEFAULT 0,
date_folder TEXT
);
CREATE TABLE IF NOT EXISTS events (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_id INTEGER NOT NULL,
event_type TEXT,
description TEXT,
confidence TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (image_id) REFERENCES images(id)
);
CREATE INDEX IF NOT EXISTS idx_images_timestamp ON images(timestamp);
CREATE INDEX IF NOT EXISTS idx_images_date_folder ON images(date_folder);
CREATE INDEX IF NOT EXISTS idx_events_type ON events(event_type);
"""
class ConfigManager:
"""配置管理器"""
def __init__(self):
self.config = DEFAULT_CONFIG.copy()
self.load()
def load(self):
"""加载配置"""
if CONFIG_PATH.exists():
try:
with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
saved = json.load(f)
self.config.update(saved)
except:
pass
def save(self):
"""保存配置"""
with open(CONFIG_PATH, 'w', encoding='utf-8') as f:
json.dump(self.config, f, ensure_ascii=False, indent=2)
def get(self, key, default=None):
"""获取配置项"""
return self.config.get(key, default)
def set(self, key, value):
"""设置配置项"""
self.config[key] = value
self.save()
def get_images_dir(self, date=None):
"""获取图片保存目录(按日期)"""
base_dir = Path(self.config['images_dir'])
if date is None:
date = datetime.now().strftime('%Y-%m-%d')
# 每天的图片放在单独文件夹
date_folder = base_dir / date
date_folder.mkdir(parents=True, exist_ok=True)
return date_folder
def get_all(self):
"""获取所有配置"""
return self.config.copy()
def update(self, updates):
"""批量更新配置"""
for key, value in updates.items():
if key in self.config:
self.config[key] = value
self.save()
# 全局配置实例
config_mgr = ConfigManager()