from pathlib import Path
import matplotlib.path as mat
from utils.general import strtolstl
from utils.general import compute_IOU
from torchvision import transforms
from PIL import Image
import torch
import torch.nn.functional as F
import numpy as np
from ultralytics.engine.results import Results
mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
test = transforms.Compose([transforms.Resize((224,224)),
        #transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=mean, std=std)
                           ])
def clapre(modelcla,claimg,clapoint):
    imgten = torch.stack(claimg,dim=0)
    clapoint = torch.stack(clapoint,dim=0)
    imgten = imgten.to(0)
    result = modelcla(imgten)
    result = F.softmax(result)
    print(result)
    index = result.argmax(1)
    index = index.cpu().numpy()
    index = np.argwhere(index<5)
    index = index.reshape(-1)
    print(index)
    if len(index)>0:
        print(clapoint[index])
        return clapoint[index]
    else:
        return None

class Helmet:
    def __init__(self):
        self.flag = False
    def getflag(self,det,persondet,annotator,fence=0,point=None,names=None,rname=None,num=1):
        #print(type(det))
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag
class Uniform:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                          xyxy[3].cpu().item())
                                    p2 = (
                                    int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                    xyxy[3].cpu().item())
                                    pt = [p1, p2]
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag

class Fall:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag

class Personcount:
    def __init__(self):
        self.flag = False

    def getflag(self, det,persondet, annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        detnum = 0
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                #self.flag = True
                                detnum = detnum+1
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        detnum = detnum+1
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        #self.flag = True
                else:
                    if persondet is None:
                        #self.flag = True
                        detnum = detnum+1
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                detnum = detnum+1
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                #self.flag = True
        if detnum >= num:
            self.flag = True
        return self.flag
    
class Arm:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                            # c = int(cls)  # integer class
                            # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0,0,255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag
    
class Bag:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag
    
class Cross:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        detnum = 0
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                detnum = detnum+1
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        detnum = detnum+1
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        detnum = detnum+1
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                detnum = detnum +1 
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag

class Extinguisher:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag

class Persontre:
    def __init__(self):
        self.flag = False
        self.classifier_model = torch.load('/home/h3c/yolo/persontrecls.pt') 
    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1,im0=None):
        self.flag = False
        dirp = {}
        dirf = {}
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:
                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                            if c==0:
                                dirp.setdefault(0,[])
                                dirp[0].append(xyxy)
                            elif c in [1,2]:
                                dirp.setdefault(1,[])
                                dirp[1].append(xyxy)
                                dirf.setdefault(1,[])
                                dirf[1].append(xyxy)
                            elif c==3:
                                dirp.setdefault(1,[])
                                dirp[1].append(xyxy)
                            elif c==4:
                                dirf.setdefault(0,[])
                                dirf[0].append(xyxy)
                                #annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        #annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        #self.flag = True
                                        if c==0:
                                            dirp.setdefault(0,[])
                                            dirp[0].append(xyxy)
                                        elif c in [1,2]:
                                            dirp.setdefault(1,[])
                                            dirp[1].append(xyxy)
                                            dirf.setdefault(1,[])
                                            dirf[1].append(xyxy)
                                        elif c==3:
                                            dirp.setdefault(1,[])
                                            dirp[1].append(xyxy)
                                        elif c==4:
                                            dirf.setdefault(0,[])
                                            dirf[0].append(xyxy)
                else:
                    if persondet is None:
                        #self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        #label = None
                        #annotator.box_label(xyxy, None, color=(0, 0, 255))
                        if c==0:
                            dirp.setdefault(0,[])
                            dirp[0].append(xyxy)
                        elif c in [1,2]:
                            dirp.setdefault(1,[])
                            dirp[1].append(xyxy)
                            dirf.setdefault(1,[])
                            dirf[1].append(xyxy)
                        elif c==3:
                            dirp.setdefault(1,[])
                            dirp[1].append(xyxy)
                        elif c==4:
                            dirf.setdefault(0,[])
                            dirf[0].append(xyxy)
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                #annotator.box_label(xyxy, None, color=(0, 0, 255))
                                #self.flag = True
                                if c==0:
                                    dirp.setdefault(0,[])
                                    dirp[0].append(xyxy)
                                elif c in [1,2]:
                                    dirp.setdefault(1,[])
                                    dirp[1].append(xyxy)
                                    dirf.setdefault(1,[])
                                    dirf[1].append(xyxy)
                                elif c==3:
                                    dirp.setdefault(1,[])
                                    dirp[1].append(xyxy)
                                elif c==4:
                                    dirf.setdefault(0,[])
                                    dirf[0].append(xyxy)
        if len(dirp.keys()) == 2:
            claimg = []
            clapoint = []
            for person in dirp[0]:
                for other in dirp[1]:
                    iou, newxyxy = compute_IOU(person, other)
                    if iou>0.1:
                        print(newxyxy)
                        imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
                        imgtmp = imgtmp[...,::-1]
                        imgtmp = Image.fromarray(imgtmp)
                        imgten1 = test(imgtmp)
                        claimg.append(imgten1)
                        clapoint.append((newxyxy))
                        result = clapre(self.classifier_model,claimg,clapoint)
                        #imgten = imgten1[None]
                        #imgten = imgten.to(0)
                        #result = modelcla(imgten)
                        #result = F.softmax(result, dim=1)
                        #cla = result.argmax(1)
                        if result is not None:
                            self.flag = True
                            for res in result:
                                print(res)
                                annotator.box_label(res, None, color=(0,0,255))
        if len(dirp.keys()) == 2:
            claimg = []
            clapoint = []
            for person in dirp[0]:
                for other in dirp[1]:
                    iou, newxyxy = compute_IOU(person, other)
                    if iou>0.1:
                        print(newxyxy)
                        imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
                        imgtmp = imgtmp[...,::-1]
                        imgtmp = Image.fromarray(imgtmp)
                        imgten1 = test(imgtmp)
                        claimg.append(imgten1)
                        clapoint.append((newxyxy))
                        result = clapre(self.classifier_model,claimg,clapoint)
                        #imgten = imgten1[None]
                        #imgten = imgten.to(0)
                        #result = modelcla(imgten)
                        #result = F.softmax(result, dim=1)
                        #cla = result.argmax(1)
                        if result is not None:
                            self.flag = True
                            for res in result:
                                print(res)
                                annotator.box_label(res, None, color=(0,0,255))
        return self.flag

class Danager:
    def __init__(self):
        self.flag = False

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        for *xyxy, conf, cls in reversed(det):
            c = int(cls)
            labelname = names[c]
            if labelname in rname:
                if fence == 1:

                    pointa = strtolstl(point)
                    for poi in pointa:
                        p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                              xyxy[3].cpu().item())
                        p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                              xyxy[3].cpu().item())
                        pt = [p1, p2]
                        inflag = mat.Path(poi).contains_points(pt)
                        if inflag.any():
                            if persondet is None:
                                self.flag = True
                                # c = int(cls)  # integer class
                                # label = f'{self.names[c]} {conf:.2f}'
                                label = None
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                            else:
                                for person in persondet:
                                    personinflag = mat.Path(person).contains_points(pt)
                                    if personinflag.any():
                                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                                        self.flag = True
                else:
                    if persondet is None:
                        self.flag = True
                        # c = int(cls)  # integer class
                        # label = f'{self.names[c]} {conf:.2f}'
                        label = None
                        annotator.box_label(xyxy, None, color=(0, 0, 255))
                    else:
                        for person in persondet:
                            p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
                                  xyxy[3].cpu().item())
                            p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
                                  xyxy[3].cpu().item())
                            pt = [p1, p2]
                            personinflag = mat.Path(person).contains_points(pt)
                            if personinflag.any():
                                annotator.box_label(xyxy, None, color=(0, 0, 255))
                                self.flag = True
        return self.flag
        
class CarHelmetBelt:
    def __init__(self):
        self.flag = False

    def selectNoBeltPerson(self, person_objs, belt_objs):
        objs = []
        polygon_person = [Polygon(
            [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
        polygon_belt = [Polygon(
            [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in belt_objs]
        for person_obj, person in zip(person_objs, polygon_person):
            with_belt = False
            for belt in polygon_belt:
                if person.intersection(belt).area / belt.area > 0.5:
                    with_belt = True
                    break
            if not with_belt:
                objs.append(person_obj)
        return objs

    def selectWithPersonHead(self, person_objs, head_objs):
        objs = []
        polygon_person = [Polygon(
            [(left, top), (right, top), (right, top + (bottom - top)/2), (left, top + (bottom - top)/2)]) for left, top, right, bottom, _, _ in person_objs]
        polygon_head = [Polygon(
            [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in head_objs]
        for head_obj, head in zip(head_objs, polygon_head):
            with_person = False
            for person in polygon_person:
                if person.intersection(head).area / head.area > 0.5:
                    with_person = True
                    break
            if with_person:
                objs.append(head_obj)
        return objs

    def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
        self.flag = False
        results = Results(annotator.result(),path=None,names=names,boxes=det)
        person_objs = []
        head_objs = []
        belt_objs = []
        polygon_area = strtolstl(point)[0]
        for result in results:
            boxes = result.boxes
            for box in boxes:
                left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
                polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])

                intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
                if sum(intersection_areas) == 0:
                    continue

                intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
                if intersection_areas_ratio[-1] < 0.9:
                    continue

                conf = box.conf.cpu().numpy().tolist()[0]
                cls = box.cls.cpu().numpy().tolist()[0]
                if cls == 0:
                    person_objs.append([left, top, right, bottom, conf, self.names[cls]])
                elif cls == 1:
                    head_objs.append([left, top, right, bottom, conf, self.names[cls]])
                elif cls == 3:
                    belt_objs.append([left, top, right, bottom, conf, self.names[cls]])
        illegal_objs = self.selectNoBeltPerson(person_objs, belt_objs) + self.selectWithPersonHead(person_objs, head_objs)
        if illegal_objs>0:
            for obj in illegal_objs:
                annotator.box_label(obj[:4], None, color=(0, 0, 255))
            self.flag = True
