# -*- coding: utf-8 -*-
"""
Created on Fri Oct 18 13:09:42 2024

@author: ym
"""
import argparse
import os
import sys
import torch
from pathlib import Path
import numpy as np

# from matplotlib.pylab import mpl 
# mpl.use('Qt5Agg')
import matplotlib.pyplot as plt

FILE = Path(__file__).resolve()
ROOT = FILE.parents[0]  # YOLOv5 root directory
if str(ROOT) not in sys.path:
    sys.path.append(str(ROOT))  # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd()))  # relative

from models.common import DetectMultiBackend

from utils.augmentations import letterbox


from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
                           increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh)
from utils.torch_utils import select_device, smart_inference_mode

'''集成跟踪模块，输出跟踪结果文件 .npy'''
# from ultralytics.engine.results import Boxes    # Results
# from ultralytics.utils import IterableSimpleNamespace, yaml_load
from tracking.utils.plotting import Annotator, colors
from tracking.utils import Boxes, IterableSimpleNamespace, yaml_load, boxes_add_fid
from tracking.trackers import BOTSORT, BYTETracker
from tracking.utils.showtrack import drawtracks
from hands.hand_inference import hand_pose

# from tracking.trackers.reid.reid_interface import ReIDInterface 
# from tracking.trackers.reid.config import config as ReIDConfig
# ReIDEncoder = ReIDInterface(ReIDConfig)

from contrast.feat_extract.config import config as conf
from contrast.feat_extract.inference import FeatsInterface
ReIDEncoder = FeatsInterface(conf)


IMG_FORMATS = 'bmp', 'dng', 'jpeg', 'jpg', 'mpo', 'png', 'tif', 'tiff', 'webp', 'pfm'  # include image suffixes
VID_FORMATS = 'asf', 'avi', 'gif', 'm4v', 'mkv', 'mov', 'mp4', 'mpeg', 'mpg', 'ts', 'wmv'  # include video suffixes


'''================== 对图像进行旋转 ================== '''
class LoadImages:
    # YOLOv5 image/video dataloader, i.e. `python detect.py --source image.jpg/vid.mp4`
    def __init__(self, files, img_size=640, stride=32, auto=True, transforms=None, vid_stride=1):
        images = [x for x in files if x.split('.')[-1].lower() in IMG_FORMATS]
        videos = [x for x in files if x.split('.')[-1].lower() in VID_FORMATS]
        ni, nv = len(images), len(videos)

        

        self.img_size = img_size
        self.stride = stride
        self.files = images + videos
        self.nf = ni + nv  # number of files
        self.video_flag = [False] * ni + [True] * nv
        self.mode = 'image'
        self.auto = auto
        self.transforms = transforms  # optional
        self.vid_stride = vid_stride  # video frame-rate stride
        if any(videos):
            self._new_video(videos[0])  # new video
        else:
            self.cap = None


    def __iter__(self):
        self.count = 0
        return self

    def __next__(self):
        if self.count == self.nf:
            raise StopIteration
        path = self.files[self.count]

        if self.video_flag[self.count]:
            # Read video
            self.mode = 'video'
            for _ in range(self.vid_stride):
                self.cap.grab()
            ret_val, im0 = self.cap.retrieve()
            while not ret_val:
                self.count += 1
                self.cap.release()
                if self.count == self.nf:  # last video
                    raise StopIteration
                path = self.files[self.count]
                self._new_video(path)
                ret_val, im0 = self.cap.read()

            self.frame += 1
            # im0 = self._cv2_rotate(im0)  # for use if cv2 autorotation is False
            s = f'video {self.count + 1}/{self.nf} ({self.frame}/{self.frames}) {path}: '

        else:
            # Read image
            self.count += 1
            im0 = cv2.imread(path)  # BGR
            
            # image rorate
            (h, w) = im0.shape[:2]
            center = (w // 2, h // 2)
            angle = 90
            scale = 1.0
            M = cv2.getRotationMatrix2D(center, angle, scale) 
            im0 = cv2.warpAffine(im0, M, (h, w))
 
            assert im0 is not None, f'Image Not Found {path}'
            s = f'image {self.count}/{self.nf} {path}: '

        if self.transforms:
            im = self.transforms(im0)  # transforms
        else:
            im = letterbox(im0, self.img_size, stride=self.stride, auto=self.auto)[0]  # padded resize
            im = im.transpose((2, 0, 1))[::-1]  # HWC to CHW, BGR to RGB
            im = np.ascontiguousarray(im)  # contiguous

        return path, im, im0, self.cap, s

    def _new_video(self, path):
        # Create a new video capture object
        self.frame = 0
        self.cap = cv2.VideoCapture(path)
        self.frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT) / self.vid_stride)
        self.orientation = int(self.cap.get(cv2.CAP_PROP_ORIENTATION_META))  # rotation degrees
        # self.cap.set(cv2.CAP_PROP_ORIENTATION_AUTO, 0)  # disable https://github.com/ultralytics/yolov5/issues/8493

    def _cv2_rotate(self, im):
        # Rotate a cv2 video manually
        if self.orientation == 0:
            return cv2.rotate(im, cv2.ROTATE_90_CLOCKWISE)
        elif self.orientation == 180:
            return cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE)
        elif self.orientation == 90:
            return cv2.rotate(im, cv2.ROTATE_180)
        return im

    def __len__(self):
        return self.nf  # number of files

def inference_image(image, detections):
    H, W, _ = np.shape(image)
    imgs = []
    batch_patches = []
    patches = []
    for d in range(np.size(detections, 0)):
        tlbr = detections[d, :4].astype(np.int_)
        tlbr[0] = max(0, tlbr[0])
        tlbr[1] = max(0, tlbr[1])
        tlbr[2] = min(W - 1, tlbr[2])
        tlbr[3] = min(H - 1, tlbr[3])
        img1 = image[tlbr[1]:tlbr[3], tlbr[0]:tlbr[2], :]

        img = img1[:, :, ::-1].copy()    # the model expects RGB inputs
        patch = ReIDEncoder.transform(img)
        
        imgs.append(img1)
        # patch = patch.to(device=self.device).half()
        if str(ReIDEncoder.device) != "cpu":
            patch = patch.to(device=ReIDEncoder.device).half()
        else:
            patch = patch.to(device=ReIDEncoder.device)
        
        patches.append(patch)
        if (d + 1) % ReIDEncoder.batch_size == 0:
            patches = torch.stack(patches, dim=0)
            batch_patches.append(patches)
            patches = []

    if len(patches):
        patches = torch.stack(patches, dim=0)
        batch_patches.append(patches)

    features = np.zeros((0, ReIDEncoder.embedding_size))
    for patches in batch_patches:
        pred = ReIDEncoder.model(patches)
        pred[torch.isinf(pred)] = 1.0
        feat = pred.cpu().data.numpy()
        features = np.vstack((features, feat))

    return imgs, features


def init_trackers(tracker_yaml = None, bs=1):
    """
    Initialize trackers for object tracking during prediction.
    """      
    # tracker_yaml = r"./tracking/trackers/cfg/botsort.yaml"
    
    TRACKER_MAP = {'bytetrack': BYTETracker, 'botsort': BOTSORT}
    
    cfg = IterableSimpleNamespace(**yaml_load(tracker_yaml))
    trackers = []
    for _ in range(bs):
        tracker = TRACKER_MAP[cfg.tracker_type](args=cfg, frame_rate=30)
        trackers.append(tracker)
    
    return trackers


def run(
        weights=ROOT / 'yolov5s.pt',  # model path or triton URL
        source=ROOT / 'data/images',  # file/dir/URL/glob/screen/0(webcam)
        
        project=ROOT / 'runs/detect',  # save results to project/name
        name='exp',  # save results to project/name
        
        tracker_yaml = "./tracking/trackers/cfg/botsort.yaml",
        imgsz=(640, 640),  # inference size (height, width)
        conf_thres=0.25,  # confidence threshold
        iou_thres=0.45,  # NMS IOU threshold
        max_det=1000,  # maximum detections per image
        device='',  # cuda device, i.e. 0 or 0,1,2,3 or cpu
        view_img=False,  # show results
        save_txt=False,  # save results to *.txt
        save_csv=False,  # save results in CSV format
        save_conf=False,  # save confidences in --save-txt labels
        save_crop=False,  # save cropped prediction boxes
        save_img = True,
        nosave=False,  # do not save images/videos
        classes=None,  # filter by class: --class 0, or --class 0 2 3
        agnostic_nms=False,  # class-agnostic NMS
        augment=False,  # augmented inference
        visualize=False,  # visualize features
        update=False,  # update all models
        exist_ok=False,  # existing project/name ok, do not increment
        line_thickness=3,  # bounding box thickness (pixels)
        hide_labels=False,  # hide labels
        hide_conf=False,  # hide confidencesL
        half=False,  # use FP16 half-precision inference
        dnn=False,  # use OpenCV DNN for ONNX inference
        vid_stride=1,  # video frame-rate stride
        data=ROOT / 'data/coco128.yaml',  # dataset.yaml path
        ):
    assert isinstance(source,list), "source must be a list"

    fulldir, imgname = os.path.split(source[0])
    imgbase, ext = os.path.splitext(imgname)
    
    
    # 事件名、相机类型
    EventName = fulldir.split('\\')[-2] + "_" + str(Path(fulldir).stem)
    CamerType = imgbase.split('_')[1]
    
    save_dir = Path(project) / Path(EventName)
    if save_dir.exists():
        print(Path(fulldir).stem)
        
        # save_dir = increment_path(Path(project) / name, exist_ok=exist_ok)  # increment run
        # save_dir.mkdir(parents=True, exist_ok=True)  # make dir
    else:
        save_dir.mkdir(parents=True, exist_ok=True) 

    save_path_video = os.path.join(str(save_dir), f"{EventName}_{CamerType}")
    # Load model
    device = select_device(device)
    model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
    stride, names, pt = model.stride, model.names, model.pt
    imgsz = check_img_size(imgsz, s=stride)  # check image size
    
    bs = 1  # batch_size
    
    dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)    
    vid_path, vid_writer = [None] * bs, [None] * bs

    # Run inference
    model.warmup(imgsz=(1 if pt or model.triton else bs, 3, *imgsz))  # warmup
    seen, dt = 0, (Profile(), Profile(), Profile())
    
    tracker = init_trackers(tracker_yaml, bs)[0]
    track_boxes = np.empty((0, 10), dtype = np.float32)
    k = 0
    for path, im, im0s, vid_cap, s in dataset:
        # k +=1
        # if k==60: 
        #     break
        
        timeStamp = Path(path).stem.split('_')[2]

        with dt[0]:
            im = torch.from_numpy(im).to(model.device)
            im = im.half() if model.fp16 else im.float()  # uint8 to fp16/32
            im /= 255  # 0 - 255 to 0.0 - 1.0
            if len(im.shape) == 3:
                im = im[None]  # expand for batch dim

        # Inference
        with dt[1]:
            visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
            pred = model(im, augment=augment, visualize=visualize)

        # NMS
        with dt[2]:
            pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)

        # Process predictions
        for i, det in enumerate(pred):  # per image
            seen += 1
            im0 = im0s.copy()
            
            s += '%gx%g ' % im.shape[2:]  # print string
            annotator = Annotator(im0s, line_width=line_thickness, example=str(names))
            
            nd = len(det)
            if nd: 
                # Rescale boxes from img_size to im0 size
                det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
           
                '''tracks: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index, timestamp]
                            0   1   2   3      4        5     6       7           8
                          这里，frame_index 也可以用视频的 帧ID 代替, box_index 保持不变
                '''

                det_tracking = Boxes(det, im0.shape).cpu().numpy()
                tracks = tracker.update(det_tracking, im0)
                if len(tracks) == 0:
                    continue
                tracks[:, 7] = dataset.count
                
                
                stamp = np.ones((len(tracks), 1)) * int(timeStamp)
                tracks = np.concatenate((tracks, stamp), axis=1)
                
                '''================== 1. 存储 dets/subimgs/features Dict ============='''
                # imgs, features = inference_image(im0, tracks)   
                track_boxes = np.concatenate([track_boxes, tracks], axis=0)
                
                for *xyxy, id, conf, cls, fid, bid, t in reversed(tracks):
                    name = ('' if id==-1 else f'id:{int(id)} ') + names[int(cls)]                   
                    label = None if hide_labels else (name if hide_conf else f'{name} {conf:.2f}')
                    
                    if id >=0 and cls==0:
                        color = colors(int(cls), True)
                    elif id >=0 and cls!=0:
                        color = colors(int(id), True)
                    else:
                        color = colors(19, True)     # 19为调色板的最后一个元素
           
                    annotator.box_label(xyxy, label, color=color)

            # Save results (image and video with tracking)
            im0 = annotator.result()            
            p = Path(path)  # to Path
            if save_img:
                imgpath = str(save_dir/p.stem) + f"_{dataset.count}.png"   
                cv2.imwrite(Path(imgpath), im0)
                if vid_path[i] != save_path_video:  # new video
                    vid_path[i] = save_path_video
                    if isinstance(vid_writer[i], cv2.VideoWriter):
                        vid_writer[i].release()  # release previous video writer
                    fps, w, h = 30, im0.shape[1], im0.shape[0]
                    vpath = str(Path(save_path_video).with_suffix('.mp4'))
                    vid_writer[i] = cv2.VideoWriter(vpath, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))

                vid_writer[i].write(im0)

        LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")
    
    for v in vid_writer:
        v.release()
    
    if track_boxes.size == 0:
        return CamerType, []
    
    save_path_np = os.path.join(str(fulldir), f"{EventName}_{CamerType}")
    np.save(save_path_np, track_boxes) 

    # Print results
    t = tuple(x.t / seen * 1E3 for x in dt)  # speeds per image
    LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
    if save_txt or save_img:
        s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
        LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
    if update:
        strip_optimizer(weights[0])  # update model (to fix SourceChangeWarning)

        
    return CamerType, track_boxes


def parse_opt():
    modelpath = ROOT / 'ckpts/best_cls10_0906.pt'       # 'ckpts/best_15000_0908.pt',  'ckpts/yolov5s.pt', 'ckpts/best_20000_cls30.pt, best_yolov5m_250000'
 
    parser = argparse.ArgumentParser()
    parser.add_argument('--weights', nargs='+', type=str, default=modelpath, help='model path or triton URL')   # 'yolov5s.pt', best_15000_0908.pt
    parser.add_argument('--source', type=str, default='', help='file/dir/URL/glob/screen/0(webcam)')    # images, videos
    parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
    parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
    parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
    parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
    parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
    parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
    parser.add_argument('--view-img', action='store_true', help='show results')
    parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
    parser.add_argument('--save-csv', action='store_true', help='save results in CSV format')
    parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
    parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
    parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
    parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
    parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
    parser.add_argument('--augment', action='store_true', help='augmented inference')
    parser.add_argument('--visualize', action='store_true', help='visualize features')
    parser.add_argument('--update', action='store_true', help='update all models')
    parser.add_argument('--project', default=ROOT / 'runs/detect', help='save results to project/name')
    parser.add_argument('--name', default='exp', help='save results to project/name')
    parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
    parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
    parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
    parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
    parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
    parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
    parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
    opt = parser.parse_args()
    opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1  # expand
    print_args(vars(opt))
    return opt

def run_yolo(eventdir, savedir):
    
    opt = parse_opt()
    optdict = vars(opt)
    optdict["project"] = savedir
    optdict["source"] = eventdir
    run(**vars(opt))
    

    


