# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 09:39:42 2024

@author: ym
"""
import os
import time
import datetime
import numpy as np
import sys
sys.path.append(r"D:\DetectTracking")
from tracking.utils.read_data import extract_data, read_weight_timeConsuming



def main():
    directory = r"\\192.168.1.28\share\测试_202406\0821\images"
    
    TimeConsuming = []
    DayHMS = []
    for root, dirs, files in os.walk(directory):   
        if root.find('20240821') == -1: continue       
        for name in files:
            if name.find('process.data') == -1: continue
            datename = os.path.basename(root)[:15]

            fpath = os.path.join(root, name)
            WeightDict, SensorDict, ProcessTimeDict = read_weight_timeConsuming(fpath)
            try:
                t1 = ProcessTimeDict['algroDoStart']           # 算法处理的第一帧图像时间
                t2 = ProcessTimeDict['breakinFirst']           # 第一次入侵时间
                t3 = ProcessTimeDict['algroLastFrame']         # 算法处理的最后一帧图像时间
                t4 = ProcessTimeDict['breakinLast']            # 最后一次入侵时间
                t5 = ProcessTimeDict['weightStablityTime']     # 重力稳定时间
                wv = ProcessTimeDict['weightValue']            # 重力值
                t6 = ProcessTimeDict['YoloResnetTrackerEnd']   # Yolo、Resnet、tracker执行结束时间
                t7 = ProcessTimeDict['trackingEnd']            # 轨迹分析结束时间
                t8 = ProcessTimeDict['contrastEnd']            # 比对结束时间
                t9 = ProcessTimeDict['algroStartToEnd']        # 算法总执行时间
                t10 = ProcessTimeDict['weightstablityToEnd']   # 重力稳定至算法结束时间
                t11 = ProcessTimeDict['frameEndToEnd']         # 最后一帧图像至算法结束时间

                TimeConsuming.append((t1, t2, t3, t4, t5, wv, t6, t7, t8, t9, t10, t11))
                DayHMS.append(datename)
            except Exception as e:
                print(f'Error! {datename}, {e}')
    
    TimeConsuming = np.array(TimeConsuming, dtype = np.int64)
    
    TimeTotal = np.concatenate((TimeConsuming, 
                                TimeConsuming[:,4][:, None] - TimeConsuming[:,0][:, None], 
                                TimeConsuming[:,4][:, None] - TimeConsuming[:,2][:, None]), axis=1)
    
    tt = TimeTotal[:, 3]==0
    
    TimeTotal0 = TimeTotal[tt]
    DayHMS0 = [DayHMS[ti] for i, ti in enumerate(tt) if ti]  
    
    TimeTotalMinus = TimeTotal[TimeTotal[:, 5]<0]
    TimeTotalAdd = TimeTotal[TimeTotal[:, 5]>=0]
    
    TimeTotalAdd0 = TimeTotalAdd[TimeTotalAdd[:,3] == 0]
    TimeTotalAdd1 = TimeTotalAdd[TimeTotalAdd[:,3] != 0]
    
    TimeTotalMinus0 = TimeTotalMinus[TimeTotalMinus[:,3] == 0]
    TimeTotalMinus1 = TimeTotalMinus[TimeTotalMinus[:,3] != 0]
    
    print(f"Total number is {len(TimeConsuming)}")

    
if __name__ == "__main__":
    main()
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    