|
| 1 | +from utils.video import clip_frame_span, frames_to_seconds |
| 2 | + |
| 3 | + |
| 4 | +def merge_candidate_events(candidates, clip_length, stride, fps, max_gap=1): |
| 5 | + if not candidates: |
| 6 | + return [] |
| 7 | + |
| 8 | + candidates = sorted(candidates, key=lambda item: item["clip_id"]) |
| 9 | + events = [] |
| 10 | + current_event = None |
| 11 | + |
| 12 | + for candidate in candidates: |
| 13 | + clip_id = candidate["clip_id"] |
| 14 | + start_frame, end_frame = clip_frame_span(clip_id, clip_length, stride) |
| 15 | + label = candidate["vlm_output"]["label"] |
| 16 | + |
| 17 | + if label in {"normal", "uncertain"}: |
| 18 | + continue |
| 19 | + |
| 20 | + if ( |
| 21 | + current_event is None or |
| 22 | + current_event["label"] != label or |
| 23 | + clip_id - current_event["last_clip_id"] > max_gap |
| 24 | + ): |
| 25 | + if current_event is not None: |
| 26 | + events.append(current_event) |
| 27 | + |
| 28 | + current_event = { |
| 29 | + "event_id": len(events), |
| 30 | + "label": label, |
| 31 | + "start_frame": start_frame, |
| 32 | + "end_frame": end_frame, |
| 33 | + "confidence": candidate["vlm_output"]["confidence"], |
| 34 | + "evidence": candidate["vlm_output"]["evidence"], |
| 35 | + "clip_ids": [clip_id], |
| 36 | + "last_clip_id": clip_id |
| 37 | + } |
| 38 | + continue |
| 39 | + |
| 40 | + current_event["end_frame"] = end_frame |
| 41 | + current_event["clip_ids"].append(clip_id) |
| 42 | + current_event["last_clip_id"] = clip_id |
| 43 | + current_event["confidence"] = max( |
| 44 | + current_event["confidence"], |
| 45 | + candidate["vlm_output"]["confidence"] |
| 46 | + ) |
| 47 | + |
| 48 | + if current_event is not None: |
| 49 | + events.append(current_event) |
| 50 | + |
| 51 | + for event in events: |
| 52 | + event["start_time"] = frames_to_seconds(event["start_frame"], fps) |
| 53 | + event["end_time"] = frames_to_seconds(event["end_frame"] + 1, fps) |
| 54 | + event.pop("last_clip_id", None) |
| 55 | + |
| 56 | + return events |
0 commit comments