12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343 |
- 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
|