From 70a71347bdedb8b4fb0073569c28986b6d9227c8 Mon Sep 17 00:00:00 2001 From: hubian <908234780@qq.com> Date: Fri, 17 Apr 2026 16:17:04 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B9=E8=BF=9B=E4=BA=BA=E6=95=B0?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E9=80=BB=E8=BE=91=EF=BC=8C=E4=BB=8E=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E6=A0=BC=E5=BC=8F=E6=8F=90=E5=8F=96=E4=BA=BA=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/app.py | 70 ++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/web/app.py b/web/app.py index 15e8a1c..d4c3550 100644 --- a/web/app.py +++ b/web/app.py @@ -199,13 +199,14 @@ async def rename_person(person_id: str, name: str): async def get_daily_stats(date: str = None): """获取每日统计数据""" from datetime import datetime, timedelta + import re # 默认昨天的数据 if date is None: yesterday = datetime.now() - timedelta(days=1) date = yesterday.strftime('%Y-%m-%d') - conn = db._get_conn() + conn = sqlite3.connect(db.db_path) conn.row_factory = sqlite3.Row # 获取当天所有图片 @@ -219,22 +220,65 @@ async def get_daily_stats(date: str = None): timeline = [] for img in images: # 获取该图片的事件 - events = db.get_events_by_image(img['id']) + cursor = conn.execute( + "SELECT event_type, description FROM events WHERE image_id = ?", + (img['id'],) + ) + events = cursor.fetchall() - # 计算人数 + # 计算人数 - 从事件描述中提取 person_count = 0 + + # 方法1: 从"共 X 人"格式提取 for event in events: - if '人物活动' in event['event_type'] or '人员进出' in event['event_type']: - # 从描述中提取人数 + desc = event['description'] + if '共 ' in desc and ' 人' in desc: + try: + count_str = desc.split('共 ')[1].split(' 人')[0] + person_count = int(count_str) + break + except: + pass + + # 方法2: 从"当前 X 人"格式提取 + if person_count == 0: + for event in events: desc = event['description'] - if '共 ' in desc and ' 人' in desc: + if '当前 ' in desc and ' 人' in desc: try: - count_str = desc.split('共 ')[1].split(' 人')[0] + count_str = desc.split('当前 ')[1].split(' 人')[0] person_count = int(count_str) + break except: - person_count = 1 - elif '#1' in desc or '#2' in desc or '#3' in desc: - person_count = max(person_count, 1) + pass + + # 方法3: 从"当前剩 X 人"格式提取 + if person_count == 0: + for event in events: + desc = event['description'] + if '当前剩 ' in desc and ' 人' in desc: + try: + count_str = desc.split('当前剩 ')[1].split(' 人')[0] + person_count = int(count_str) + break + except: + pass + + # 方法4: 从人物活动事件推断 + if person_count == 0: + person_events = [e for e in events if '人物活动' in e['event_type'] or '人员进出' in e['event_type']] + if person_events: + # 统计 #1, #2, #3 等序号 + max_index = 0 + for e in person_events: + desc = e['description'] + # 提取所有 #数字 格式 + import re + matches = re.findall(r'#(\d+)', desc) + if matches: + max_index = max(max_index, max([int(m) for m in matches])) + if max_index > 0: + person_count = max_index timeline.append({ 'time': img['timestamp'], @@ -245,10 +289,12 @@ async def get_daily_stats(date: str = None): # 统计汇总 total_images = len(images) - total_events = conn.execute( + + cursor = conn.execute( "SELECT COUNT(*) FROM events WHERE image_id IN (SELECT id FROM images WHERE date_folder = ?)", (date,) - ).fetchone()[0] + ) + total_events = cursor.fetchone()[0] # 事件类型统计 cursor = conn.execute(