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 from shapely.geometry import Point from shapely.geometry.polygon import Polygon 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 def calculate_iou(box1, box2): # 计算交集区域 也就是找到次左上 次右下 x1_int = max(box1[0], box2[0]) y1_int = max(box1[1], box2[1]) x2_int = min(box1[2], box2[2]) y2_int = min(box1[3], box2[3]) # 交集区域的宽高 width_int = max(0, x2_int - x1_int) height_int = max(0, y2_int - y1_int) area_int = width_int * height_int # 计算两个框的面积 area_box1 = (box1[2] - box1[0]) * (box1[3] - box1[1]) area_box2 = (box2[2] - box2[0]) * (box2[3] - box1[1]) # 计算并集面积 area_union = area_box1 + area_box2 - area_int # 计算IoU iou = area_int / area_union if area_union > 0 else 0 return iou class Persontre1: 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 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 target_classes = [1, 2, 3] results = Results(annotator.result(),path=None,names=names,boxes=det) boxes = results.boxes person_boxes = [] target_boxes = [] cls4_boxes = [] # 处理检测结果 for i in range(len(boxes)): cls = int(boxes.cls[i].item()) con = boxes.conf[i].item() if cls == 0 and con > 0.1: # 如果是 "person" 类别 x1, y1, x2, y2 = boxes.xyxy[i].tolist() person_boxes.append([x1, y1, x2, y2]) if cls in target_classes and con > 0.1: # 目标类别(bag, box, cart) x1, y1, x2, y2 = boxes.xyxy[i].tolist() target_boxes.append([x1, y1, x2, y2]) if cls == 4 and con > 0.1: # 如果是 "cls 4" 类别 x1, y1, x2, y2 = boxes.xyxy[i].tolist() cls4_boxes.append([x1, y1, x2, y2]) # 如果检测到 "person" 类别和目标框,计算IoU if person_boxes and target_boxes: for i, person_box in enumerate(person_boxes): person_center_y = (person_box[1] + person_box[3]) / 2 for j, target_box in enumerate(target_boxes): target_center_y = (target_box[1] + target_box[3]) / 2 # 判断目标框的中心点是否在person框的下方 if target_center_y + 20 > person_center_y: # 根据需要调整此阈值 iou = calculate_iou(person_box, target_box) if iou > 0: # IoU大于0,进入新的判断 # 创建一个包围person和target框的区域 本来思路是判断脚是否在这个大框框里面 但是这个不合理 应该判断脚是不是在这个交集里面,再检测脚和人与物交集有没有交集 min_x = max(person_box[0], target_box[0]) min_y = max(person_box[1], target_box[1]) max_x = min(person_box[2], target_box[2]) max_y = min(person_box[3], target_box[3]) target_area = (min_x, min_y, max_x, max_y) # 检查是否有cls 4物体在这个区域内 for cls4_box in cls4_boxes: #比较巧妙 提前存储这个变量 然后检测这个物体是不是在这里面 # 判断cls 4框是否与这个区域有交集 inter_x1 = max(cls4_box[0], target_area[0]) inter_y1 = max(cls4_box[1], target_area[1]) inter_x2 = min(cls4_box[2], target_area[2]) inter_y2 = min(cls4_box[3], target_area[3]) if inter_x1 < inter_x2 and inter_y1 < inter_y2: # 计算交集区域的面积 intersection_area = (inter_x2 - inter_x1) * (inter_y2 - inter_y1) # 计算 cls4_box 的面积 cls4_area = (cls4_box[2] - cls4_box[0]) * (cls4_box[3] - cls4_box[1]) # 计算交集区域占 cls4_box 面积的比例 overlap_ratio = intersection_area / cls4_area if overlap_ratio > 0.5: self.flag = True 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()) / 2), int(xyxy[1].cpu().item() + (xyxy[3].cpu().item() - xyxy[1].cpu().item()) / 2)) #p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2), #xyxy[3].cpu().item()) pt = [p1] print(f'pt = {pt}') print(f'poi = {poi}') 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: print('head') 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 = [] self.polygon_areas = [Polygon(strtolstl(point)[0])] for result in results: boxes = result.boxes for box in boxes: #print(box.conf.cpu()) 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, names[cls]]) elif cls == 1: head_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls == 3: belt_objs.append([left, top, right, bottom, conf, names[cls]]) #print(head_objs) #print(person_objs) illegal_objs = self.selectNoBeltPerson(person_objs, belt_objs) + self.selectWithPersonHead(person_objs, head_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag class newUniform: def __init__(self): self.flag = False def selectNoUniformPerson(self, person_objs, uniform_objs): objs = [] print(person_objs) print(uniform_objs) polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs] polygon_uniform = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs] for person_obj, person in zip(person_objs, polygon_person): with_uniform = False for uniform in polygon_uniform: if person.intersection(uniform).area / uniform.area > 0.3: with_uniform = True break if not with_uniform: print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}') objs.append(person_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) print(results.boxes) person_objs = [] uniform_objs = [] #belt_objs = [] if len(point)>1: self.polygon_areas = [Polygon(strtolstl(point)[0])] else: self.polygon_areas = None for result in results: boxes = result.boxes for box in boxes: left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0] if self.polygon_areas is not None and box.cls.cpu().numpy().tolist()[0]==4: 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]) print(f'inter = {intersection_areas_ratio}') if intersection_areas_ratio[-1] < 0.9: continue conf = box.conf.cpu().numpy().tolist()[0] cls = box.cls.cpu().numpy().tolist()[0] if cls == 4 and conf >=0.7: width = right - left height = bottom - top if width * height >10000: person_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls in [1,2]: #width = right - left #height = bottom - top #if width * height >4096: uniform_objs.append([left, top, right, bottom, conf, names[cls]]) illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag class newHelmet: def __init__(self): self.flag = False def selectNoHelmetPerson(self, person_objs, head_objs): objs = [] polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) 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 person_obj, person in zip(person_objs, polygon_person): with_head = False for head in polygon_head: print('head') if person.intersection(head).area / head.area > 0.3: with_head = True break if with_head: objs.append(person_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 = [] helmet_objs = [] headtmp = [] #belt_objs = [] if len(point)>1: self.polygon_areas = [Polygon(strtolstl(point)[0])] else: self.polygon_areas = None for result in results: boxes = result.boxes for box in boxes: left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0] if self.polygon_areas is not None and box.cls.cpu().numpy().tolist()[0]==4: 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.3: continue conf = box.conf.cpu().numpy().tolist()[0] cls = box.cls.cpu().numpy().tolist()[0] if cls == 4 and conf >=0.7: width = right - left height = bottom - top if width * height >4096: person_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls == 0: #width = right - left #height = bottom - top #if width * height >4096: print('------') headtmp.append([left, top, right, bottom, conf, names[cls]]) elif cls in [5,6,7,8,9,10]: helmet_objs.append([(left, top), (right, top), (right, bottom), (left, bottom)]) print(headtmp) print(helmet_objs) for left, top, right, bottom, conf, name in headtmp: flag = False pt = [(int((left+right)/2),int((top+bottom)/2))] for helmet in helmet_objs: pflag = mat.Path(helmet).contains_points(pt) if pflag.any(): flag = True break if not flag: head_objs.append([left, top, right, bottom, conf, name]) illegal_objs = self.selectNoHelmetPerson(person_objs, head_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag class newHelmetn: def __init__(self): self.flag = False def selectNoHelmetPerson(self, person_objs, head_objs): objs = [] polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) 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 person_obj, person in zip(person_objs, polygon_person): with_head = False for head in polygon_head: if person.intersection(head).area / head.area > 0.3: with_head = True break if with_head: objs.append(person_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 = [] if len(point)>1: self.polygon_areas = [Polygon(strtolstl(point)[0])] else: self.polygon_areas = None for result in results: boxes = result.boxes for box in boxes: left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0] if self.polygon_areas is not None: 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 == 4: width = right - left height = bottom - top if width * height >4096: person_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls == 0: #width = right - left #height = bottom - top #if width * height >4096: head_objs.append([left, top, right, bottom, conf, names[cls]]) illegal_objs = self.selectNoHelmetPerson(person_objs, head_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag class newUniformt: def __init__(self): self.flag = False def selectNoUniformPerson(self, person_objs, uniform_objs): objs = [] print(person_objs) print(uniform_objs) polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs] polygon_uniform = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs] for person_obj, person in zip(person_objs, polygon_person): with_uniform = False for uniform in polygon_uniform: if person.intersection(uniform).area / uniform.area > 0.3: with_uniform = True break if not with_uniform: print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}') objs.append(person_obj) return objs def selectHeadPerson(self, person_objs, head_objs): objs = [] print(person_objs) print(head_objs) #personlist = [] polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) 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 uniform_objs] for person_obj, person in zip(person_objs, polygon_person): #with_uniform = False for head in polygon_head: if person.intersection(head).area / head.area > 0.3: #with_uniform = True #break #if not with_uniform: # print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}') objs.append(person_obj) break 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) print(results.boxes) person_objs = [] uniform_objs = [] head_objs = [] #belt_objs = [] if len(point)>1: self.polygon_areas = [Polygon(strtolstl(point)[0])] else: self.polygon_areas = None for result in results: boxes = result.boxes for box in boxes: left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0] if self.polygon_areas is not None: 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 == 4: width = right - left height = bottom - top if width * height >4096: person_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls in [1,2]: #width = right - left #height = bottom - top #if width * height >4096: uniform_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls != 3: head_objs.append([left, top, right, bottom, conf, names[cls]]) person_objs = self.selectNoUniformPerson(person_objs, head_objs) illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag class newUniformi1: def __init__(self): self.flag = False def selectNoUniformPerson(self, person_objs, uniform_objs): objs = [] print(person_objs) print(uniform_objs) polygon_person = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs] polygon_uniform = [Polygon( [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs] for person_obj, person in zip(person_objs, polygon_person): with_uniform = False for uniform in polygon_uniform: if person.intersection(uniform).area / uniform.area > 0.3: with_uniform = True break if not with_uniform: print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}') objs.append(person_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) print(results.boxes) person_objs = [] uniform_objs = [] print(f'persondet = {persondet}') #belt_objs = [] if len(point)>1: self.polygon_areas = [Polygon(strtolstl(point)[0])] else: self.polygon_areas = None for result in results: boxes = result.boxes for box in boxes: left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0] if self.polygon_areas is not None: 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] print(conf) print(cls) if cls == 4 and conf >=0.7: width = right - left height = bottom - top print(width*height) if width * height >10000: 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()) personcenterx = int((person[0][0]+person[1][0])/2) personcentery = int((person[0][1]+person[2][1])/2) p1 = (personcenterx,personcentery) print(p1) pt = [p1] print(f'p1 = {p1}') print(f'person = {person}') personinflag = mat.Path([(left, top), (right, top), (right, bottom), (left, bottom)]).contains_points(pt) if personinflag.any(): person_objs.append([left, top, right, bottom, conf, names[cls]]) elif cls in [1,2]: #width = right - left #height = bottom - top #if width * height >4096: uniform_objs.append([left, top, right, bottom, conf, names[cls]]) illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs) if len(illegal_objs)>0: for obj in illegal_objs: annotator.box_label(obj[:4], None, color=(0, 0, 255)) self.flag = True return self.flag