实用脚本统计区域防守漏洞出现几次?

wen 实用脚本 3

统计区域防守漏洞出现次数的脚本

下面是一个实用的 Python 脚本,用于统计比赛/训练视频或数据中区域防守漏洞出现的次数,我提供两种常见场景的方案:

实用脚本统计区域防守漏洞出现几次?


基于事件日志/标注数据统计

如果你有比赛的事件记录(CSV/Excel),比如每次防守被突破、空档出现、失位等标注,可直接统计。

import pandas as pd
from collections import Counter
def count_defensive_holes(file_path, zone_col="区域", event_col="事件类型",
                           hole_keywords=("漏洞", "失位", "空档", "被突破", "漏防")):
    """
    统计各区域防守漏洞出现的次数
    """
    df = pd.read_csv(file_path)  # 或 pd.read_excel(...)
    # 筛选出属于"防守漏洞"的事件
    mask = df[event_col].astype(str).str.contains("|".join(hole_keywords), na=False)
    holes = df[mask]
    # 总次数
    total = len(holes)
    print(f"防守漏洞总次数: {total}")
    # 按区域统计
    if zone_col in df.columns:
        by_zone = holes[zone_col].value_counts()
        print("\n各区域漏洞次数:")
        print(by_zone.to_string())
        # 找出漏洞最多的区域(薄弱区)
        if not by_zone.empty:
            print(f"\n最薄弱区域: {by_zone.idxmax()} ({by_zone.max()} 次)")
    # 按时间统计(若有时间列)
    if "时间" in df.columns:
        by_time = holes.groupby("时间").size()
        print("\n按时间分布:")
        print(by_time.to_string())
    return holes
if __name__ == "__main__":
    # 示例:events.csv 含列 [时间, 区域, 事件类型, 球员]
    count_defensive_holes("events.csv")

输出示例:

防守漏洞总次数: 12
各区域漏洞次数:
左路    5
中路    4
右路    3
最薄弱区域: 左路 (5 次)

基于视频逐帧检测(OpenCV + 简单区域划分)

若要从录像中自动统计,可结合目标检测/跟踪模型(如 YOLO),把场地划分为若干防守区域,当攻击方球员进入某区域且附近无防守球员时,记一次"漏洞"。

import cv2
import numpy as np
# 场地划分为 3x3 区域(可自定义)
def get_zone(x, y, w, h):
    col = min(int(x / (w / 3)), 2)
    row = min(int(y / (h / 3)), 2)
    return f"区{row+1}-{col+1}"
def count_holes_video(video_path, detector, distance_threshold=80,
                      cooldown_frames=30):
    """
    detector: 需返回 [(x, y, label)],label 为 'attacker'/'defender'
    当攻击球员周围 distance_threshold 内无防守球员时,记一次漏洞。
    cooldown_frames 防止同一漏洞连续重复计数。
    """
    cap = cv2.VideoCapture(video_path)
    w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    hole_counter = Counter()
    last_hole_frame = {}
    frame_idx = 0
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frame_idx += 1
        detections = detector(frame)
        attackers = [(x, y, z) for x, y, z, l in detections if l == "attacker"]
        defenders = [(x, y) for x, y, z, l in detections if l == "defender"]
        for ax, ay, az in attackers:
            # 最近防守球员距离
            if defenders:
                dist = min(np.hypot(ax-dx, ay-dy) for dx, dy in defenders)
            else:
                dist = float("inf")
            if dist > distance_threshold:
                zone = get_zone(ax, ay, w, h)
                key = zone
                # 冷却机制,避免逐帧重复计数
                if frame_idx - last_hole_frame.get(key, -999) > cooldown_frames:
                    hole_counter[zone] += 1
                    last_hole_frame[key] = frame_idx
    cap.release()
    print("各区域防守漏洞次数:")
    for z, c in hole_counter.most_common():
        print(f"  {z}: {c} 次")
    print(f"总计: {sum(hole_counter.values())} 次")
    return hole_counter

使用建议

场景 推荐方案 说明
已有标注/事件表 方案一 最准确,几行代码搞定
需要从视频自动分析 方案二 需接入 YOLO 等检测器,阈值需调参
教练人工打点 方案一 配合手机/平板标注 App 导出 CSV

如果你能告诉我:

  1. 数据形式(CSV?视频?还是手动记录?)
  2. 区域如何划分(左/中/右?九宫格?按位置编号?)
  3. "漏洞"的判定标准(失位?无人防守?被穿透?)

我可以帮你把脚本改得更贴合你的实际场景。

抱歉,评论功能暂时关闭!