fix: 改进人数解析逻辑,从多种格式提取人数
This commit is contained in:
70
web/app.py
70
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(
|
||||
|
||||
Reference in New Issue
Block a user