# -*- coding: utf-8 -*-
"""
Created on Mon Mar 10 09:33:35 2025
基准数据集筛选，选取tracking输出多个轨迹的事件

@author: ym
"""
import os
import numpy as np

import sys
sys.path.append(r"D:\DetectTracking")
from tracking.utils.read_data import extract_data, read_tracking_output_realtime


def get_multitraj_file(spath, pattern):
    multi_traj_events = []
    n = 0
    for evtname in os.listdir(spath):        
        name, ext = os.path.splitext(evtname)
        eventpath = os.path.join(spath, evtname)
        
        evt = name.split('_')
        condt = len(evt)>=2 and evt[-1].isdigit() and len(evt[-1])>=8
        if not condt: continue
        if not os.path.isdir(eventpath): continue

        trackingboxes = []
        for dataname in os.listdir(eventpath):            
            if os.path.splitext(dataname)[-1] in [".jpg", ".png"]:
                continue
            
            datapath = os.path.join(eventpath, dataname)
            if not os.path.isfile(datapath): continue
            CamerType = dataname.split('_')[0]
            
            if pattern=="realtime" and dataname.find("_tracking_output.data")>0:                
                trackingboxes, trackingfeats, tracking_outboxes, tracking_outfeats = read_tracking_output_realtime(datapath)
            if pattern=="evtsplit" and dataname.find("_track.data")>0:
                bboxes, ffeats, trackerboxes, trackerfeats, trackingboxes, trackingfeats = extract_data(datapath)

            if len(trackingboxes)>=2:
                multi_traj_events.append(evtname) 
                n += 1
                print(f"{n}: {evtname}")
                break
                
       
    multi_traj_file = os.path.join(spath, "multi_traj_file.txt")
    with open(multi_traj_file, "w") as file:
        for item in multi_traj_events:
            file.write(item + "\n")

def main():
    spaths = [r"\\192.168.1.28\share\测试视频数据以及日志\各模块测试记录\比对测试\1212",
              r"\\192.168.1.28\share\测试视频数据以及日志\各模块测试记录\比对测试\1216",
              r"\\192.168.1.28\share\测试视频数据以及日志\各模块测试记录\比对测试\1218",
              r"\\192.168.1.28\share\测试视频数据以及日志\各模块测试记录\比对测试\202412"
             ] 
    
    pattern = "evtsplit"  # realtime  # 全实时版、事件切分版数据读取方式
    for spath in spaths:
        get_multitraj_file(spath, pattern)
        
    
    
    
    
    
    
if __name__ == '__main__':
    main()