api.py
6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
import time
from aabd.base.enhance_dict import value_or_default
import os
os.environ['APP_LOG_TYPE'] = 'console,file'
from aabd.base.patched_logging import init_logging, adapt_sys_out
init_logging(log_fmt="log_fmt_3")
# adapt_sys_out()
import logging
logger = logging.getLogger("api")
from utils.football_replay_video_event_by_llm import FootballReplayVideoEvent
from utils.football_replay_match_live import FootballReplayMatchLive
# from utils.my_logger import get_logger
# logger = get_logger()
class FootballReplayMatch:
def __init__(self, settings):
self.settings = settings
self.match_by_time_threshold = value_or_default(settings.match_by_time_threshold, 30) * 1000
llm_base_url = value_or_default(settings.llm.base_url, None)
model_name = value_or_default(settings.llm.model_name, None)
temperature = value_or_default(settings.llm.temperature, 0.7)
api_key = value_or_default(settings.llm.api_key, 'no_key')
self.cache_dir = value_or_default(settings.common.cache_dir, None)
self.task_log_dir = value_or_default(settings.common.task_log_dir, None)
save_frames_enable = value_or_default(settings.save_frames_enable, False)
self.videoEventRecognition = FootballReplayVideoEvent(llm_base_url, model_name, temperature, api_key, self.cache_dir, save_frames_enable)
self.videoMatchLive = FootballReplayMatchLive(llm_base_url, model_name, temperature, api_key, self.cache_dir, save_frames_enable)
def match_by_time(self, replay, events):
start_utc = replay.get('start_utc')
if start_utc is None:
return None
events = [(start_utc - e.get('event_utc'), e) for e in events if e.get('event_utc') is not None]
if len(events) == 0:
return None
events.sort(key=lambda x: x[0])
target_index = next((i for i, e in enumerate(events) if e[0] > 0), -1) # 找到第一个 event_utc 早于 start_utc 的事件
if target_index == -1:
return None
time_diff, event = events[target_index]
if time_diff < self.match_by_time_threshold:
return event
else:
return None
def det_goal_replay(self, replay, task_id):
video_url = replay.get('url')
if video_url.endswith('.m3u8'):
mp4_name = os.path.basename(video_url)
else:
# mp4_name = os.path.basename(os.path.dirname(os.path.dirname(os.path.dirname(video_url))))
mp4_name = os.path.basename(os.path.dirname(os.path.dirname(video_url)))
return self.videoEventRecognition.video_event(replay, cache_dir=os.path.join(self.cache_dir, mp4_name, "replays"), task_log_dir=os.path.join(self.task_log_dir, mp4_name, task_id))
def match_by_llm(self, replay, events, task_id):
video_url = replay.get('url')
if video_url.endswith('.m3u8'):
mp4_name = os.path.basename(video_url)
else:
# mp4_name = os.path.basename(os.path.dirname(os.path.dirname(os.path.dirname(video_url))))
mp4_name = os.path.basename(os.path.dirname(os.path.dirname(video_url)))
return self.videoMatchLive.match_batch(replay, events, max_parallel=3, cache_dir=os.path.join(self.cache_dir, mp4_name, "events"), task_log_dir=os.path.join(self.task_log_dir, mp4_name, task_id))
def replay_match_event(self, data):
"""
:param data:
{
id:xx,
match_id: 比赛id,
replay:{
id:,
url:,
start_utc:,
end_utc:,
}
events:[{
id:xx,
type:xx,
url:xx,
event_utc,
}]
}
:return:
"""
task_id = data.get("id")
match_id = data.get("match_id")
matched_event_id = None
# 该时间段附近是否有进球事件
replay_info = data['replay']
replay_id = replay_info.get('id')
replay_start_utc = replay_info.get('start_utc', None)
replay_end_utc = replay_info.get('end_utc', None)
live_events = data['events']
goal_live_events = [e for e in live_events if str(e.get('type', '')) == '1']
matched_event = self.match_by_time(replay_info, goal_live_events)
# print(f"{task_id}_match_by_time matched_event: {matched_event}")
if matched_event is None:
start_time = time.time()
replay_det_info = self.det_goal_replay(replay_info, task_id)
end_time = time.time()
logger.info(f"Goal_recognition_all_cost_time:{end_time-start_time:.2f}seconds")
# print(f"判断是否是进球耗时: {end_time - start_time} 秒")
replay_event_name = replay_det_info.get('event_name', '')
if replay_event_name == '进球':
start_time1 = time.time()
matched_event = self.match_by_llm(replay_info, goal_live_events, task_id)
end_time1 = time.time()
logger.info(f"LLM_match_all_cost_time:{end_time1-start_time1:.2f}seconds")
if matched_event is None:
logger.info(f"{task_id}_LLM_think_{replay_info['url']}_{replay_start_utc}_{replay_end_utc}_is_goal_but_no_matched_event")
# print(f"{task_id}_LLM认为视频{replay_info['url']}_{replay_start_utc}_{replay_end_utc} 是进球但是未能找到匹配的事件")
else:
matched_event_id = matched_event.get('video_id')
logger.info(f"{task_id}_LLM_think_{replay_info['url']}_{replay_start_utc}_{replay_end_utc}_matched_with_{matched_event_id}")
# print(f"{task_id}_LLM认为视频{replay_info['url']}_{replay_start_utc}_{replay_end_utc} 对应的进球的事件视频是{matched_event_id}")
else:
logger.info(f"{task_id}_LLM_think_{replay_info['url']}_{replay_start_utc}_{replay_end_utc}_is_not_goal_no_need_match")
# print(f"{task_id}_LLM判断{replay_info['url']}_{replay_start_utc}_{replay_end_utc} 不是进球,无需匹配")
else:
matched_event_id = matched_event.get('id')
logger.info(f"{task_id}_matched_by_time{replay_info['url']}_{replay_start_utc}_{replay_end_utc}_matched_with_{matched_event_id}")
# print(f"{task_id}_通过时间找到匹配认为视频{replay_info['url']}_{replay_start_utc}_{replay_end_utc} 对应的进球的事件视频是{matched_event_id}")
return {
"id": task_id,
"match_id": match_id,
"replay_id": replay_id,
"event_id": matched_event_id
}