12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526 |
- from pathlib import Path
- import matplotlib.path as mat
- from ultralytics.models.utils import ops
- 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
- import torch.nn as nn
- from ultralytics.engine.results import Results
- from shapely.geometry import Point
- from shapely.geometry.polygon import Polygon
- from pydantic import BaseModel
- 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 Model(nn.Module):
- def __init__(self, A, nnode, nfeature, nclass):
- super().__init__()
- self.fc1 = nn.Linear(nnode * nfeature, 512)
- self.fc2 = nn.Linear(512, nclass)
- def forward(self, x):
- x = x.view(-1, int(x.size(1) * x.size(2)))
- x = F.relu(self.fc1(x))
- x = F.dropout(x, 0.7, training=self.training)
- return self.fc2(x)
- class GetKeypoint(BaseModel):
- NOSE: int = 0
- LEFT_EYE: int = 1
- RIGHT_EYE: int = 2
- LEFT_EAR: int = 3
- RIGHT_EAR: int = 4
- LEFT_SHOULDER: int = 5
- RIGHT_SHOULDER: int = 6
- LEFT_ELBOW: int = 7
- RIGHT_ELBOW: int = 8
- LEFT_WRIST: int = 9
- RIGHT_WRIST: int = 10
- LEFT_HIP: int = 11
- RIGHT_HIP: int = 12
- LEFT_KNEE: int = 13
- RIGHT_KNEE: int = 14
- LEFT_ANKLE: int = 15
- RIGHT_ANKLE: int = 16
- def extract_keypoint(get_keypoint, keypoint):
- # nose
- nose_x, nose_y = keypoint[get_keypoint.NOSE]
- # eye
- # left_eye_x, left_eye_y = keypoint[get_keypoint.LEFT_EYE]
- # right_eye_x, right_eye_y = keypoint[get_keypoint.RIGHT_EYE]
- # # ear
- # left_ear_x, left_ear_y = keypoint[get_keypoint.LEFT_EAR]
- # right_ear_x, right_ear_y = keypoint[get_keypoint.RIGHT_EAR]
- # shoulder
- left_shoulder_x, left_shoulder_y = keypoint[get_keypoint.LEFT_SHOULDER]
- right_shoulder_x, right_shoulder_y = keypoint[get_keypoint.RIGHT_SHOULDER]
- # elbow
- left_elbow_x, left_elbow_y = keypoint[get_keypoint.LEFT_ELBOW]
- right_elbow_x, right_elbow_y = keypoint[get_keypoint.RIGHT_ELBOW]
- # wrist
- left_wrist_x, left_wrist_y = keypoint[get_keypoint.LEFT_WRIST]
- right_wrist_x, right_wrist_y = keypoint[get_keypoint.RIGHT_WRIST]
- # hip
- left_hip_x, left_hip_y = keypoint[get_keypoint.LEFT_HIP]
- right_hip_x, right_hip_y = keypoint[get_keypoint.RIGHT_HIP]
- # knee
- left_knee_x, left_knee_y = keypoint[get_keypoint.LEFT_KNEE]
- right_knee_x, right_knee_y = keypoint[get_keypoint.RIGHT_KNEE]
- # ankle
- left_ankle_x, left_ankle_y = keypoint[get_keypoint.LEFT_ANKLE]
- right_ankle_x, right_ankle_y = keypoint[get_keypoint.RIGHT_ANKLE]
- return [
- nose_x, nose_y,
- left_shoulder_x, left_shoulder_y,
- right_shoulder_x, right_shoulder_y,
- left_elbow_x, left_elbow_y,
- right_elbow_x, right_elbow_y,
- left_wrist_x, left_wrist_y,
- right_wrist_x, right_wrist_y,
- left_hip_x, left_hip_y,
- right_hip_x, right_hip_y,
- left_knee_x, left_knee_y,
- right_knee_x, right_knee_y,
- left_ankle_x, left_ankle_y,
- right_ankle_x, right_ankle_y
- ]
- 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 newFall:
- def __init__(self):
- self.flag = False
- self.pose_classfier_model = Model(None, 13, 2, 2)
- self.pose_classfier_model.load_state_dict(torch.load("posefallcls.pt"))
- self.pose_classfier_model.eval()
- self.pose_classfier_model.to(0)
- def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1,imshape=None):
- self.flag = False
- kptshape = [17,3]
- pred_kpts = det[:, 6:].view(len(det), *kptshape) if len(det) else det[:, 6:]
- pred_kpts = ops.scale_coords(imshape, pred_kpts, annotator.result().shape)
- results =Results(annotator.result(), path=None, names=names, boxes=det[:, :6], keypoints=pred_kpts)
- print(results.boxes)
- if len(point) > 1:
- self.polygon_areas = [Polygon(strtolstl(point)[0])]
- print(self.polygon_areas)
- else:
- self.polygon_areas = None
- tmppersondet = []
- if self.polygon_areas is not None:
- for person in persondet:
- polygon_box = Polygon(
- [(person[0], person[1]), (person[2], person[3]), (person[4], person[5]), (person[6], person[7])])
- 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(intersection_areas_ratio)
- if intersection_areas_ratio[-1] < 0.9:
- continue
- tmppersondet.append(person)
- persondet = tmppersondet
- boxes = results.boxes.xyxy.cpu().numpy().tolist()
- confs = results.boxes.conf.cpu().numpy().tolist()
- all_keypoints = results.keypoints.data.cpu().numpy().tolist()
- pose_classfier_results = []
- for box, conf, keypoints in zip(boxes, confs, all_keypoints):
- iouflag = False
- for personbox in persondet:
- iou, _ = compute_IOU(box, personbox)
- print(f'judgeiou = {iou}')
- if iou > 0.5:
- iouflag = True
- break;
- if not iouflag:
- break
- x1, y1, x2, y2 = box
- x, y, w, h = x1, y1, x2 - x1, y2 - y1
- n_keypoints = [[(kp[0] - x) / w - 0.5, (kp[1] - y) / h - 0.5] if kp[0] > 0 and kp[1] > 0 else kp[:2] for kp
- in keypoints]
- n_keypoints = extract_keypoint(self.get_keypoint, n_keypoints)
- if n_keypoints[-12:].count(0) >= 2 * 2:
- continue
- if n_keypoints.count(0) >= 4 * 2:
- continue
- if w < h:
- continue
- pose_data = torch.Tensor([n_keypoints]).to(self.device)
- pose_data = pose_data.reshape(1, 13, 2)
- with torch.no_grad():
- p = self.pose_classfier_model(pose_data)
- prob = F.softmax(p)
- index = prob.argmax()
- if index == 0:
- score = float(prob[0][index].cpu().numpy())
- pose_classfier_results.append(box)
- if len(pose_classfier_results)>0:
- for xyxy in pose_classfier_results:
- 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(np.array(person).reshape(-1,2)).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
- def check_cls4_overlap(person_box, target_box, cls4_boxes):
- """
- 检查是否有 cls4 物体在目标框与人物框交集区域内,并计算交集面积与 cls4 物体面积的重叠比率。
- 返回重叠比率最大值以及其对应的交集区域坐标和 cls4 框。
- 参数:
- person_box (tuple): 人物框 (x_min, y_min, x_max, y_max)
- target_box (tuple): 目标框 (x_min, y_min, x_max, y_max)
- cls4_boxes (list): cls4 物体框的列表,每个框为 (x_min, y_min, x_max, y_max)
- 返回:
- tuple:
- - 最大的重叠比率 (float),如果没有符合条件的物体则为 None。
- - 最大重叠比率对应的交集区域的坐标 (inter_x1, inter_y1)。
- - 对应的 cls4 框 (tuple),如果没有符合条件的物体则为 None。
- """
- # 计算目标框与人物框的交集区域
- 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) # 交集区域
- max_overlap_ratio = -1 # 用于存储最大的重叠比率 这设置为-1有助于区别是否计算了
- best_inter_x1 = 0 # 用于存储最大重叠比率对应的交集坐标
- best_inter_y1 = 0
- best_cls4_box = [] # 用于存储对应的 cls4 框 不能初始化为整数0 整数不可迭代 后序不能添加
- # 检查是否有 cls4 物体在交集区域内
- for cls4_box in cls4_boxes:
- # 判断 cls4 框是否与交集区域有交集
- 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)
- #print("交集的面积", intersection_area)
- # 计算 cls4_box 的面积
- cls4_area = (cls4_box[2] - cls4_box[0]) * (cls4_box[3] - cls4_box[1])
- #print("cls4_area的面积是", cls4_area)
- # 计算交集区域占 cls4_box 面积的比例
- overlap_ratio = intersection_area / cls4_area
- # 更新最大重叠比率及其相关信息
- if max_overlap_ratio == 0 or overlap_ratio > max_overlap_ratio:
- max_overlap_ratio = overlap_ratio
- best_inter_x1 = inter_x1
- best_inter_y1 = inter_y1
- best_cls4_box = cls4_box
- return max_overlap_ratio, (best_inter_x1, best_inter_y1), best_cls4_box
- 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 Persontree:
- 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 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
- cls3 =3 #对cart单独一类
- cls4 = 4 # 新增加的类别编号(假设为 4)
- target_classes = [1, 2, 3]
- results = Results(annotator.result(),path=None,names=names,boxes=det)
- boxes = results.boxes
- person_boxes = [] # 人 车子 脚 以及 指定区域内的脚
- cls3_boxes = []
- cls4_boxes = []
- best_cls4_box = []
- # 处理检测结果
- 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 == cls3 and con > 0.1: # 目标类别 cart
- x1, y1, x2, y2 = boxes.xyxy[i].tolist()
- cls3_boxes.append([x1, y1, x2, y2])
- if cls == cls4 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 cls3_boxes: # 先查看这俩个类别有没有
- for i, person_box in enumerate(person_boxes):
- for j, cls3_box in enumerate(cls3_boxes):
- # 判断目标框的中心点是否在person框的下方
- iou = calculate_iou(person_box, cls3_box)
- # 踩的逻辑 一个是要求车子和人有交并比 其次判断脚是不是在这个交并比内 并且比重不能太低
- if iou > 0.2: # 用来判断坐 如果iou足够高 就视为是坐
- # 加载图像并绘制标注
- # 看看这个值大概有多少 好衡量一下
- #print(f"这个iou大于0.2的 这是第{frame_idx},iou是{iou}") # 写入介绍
- annotator.box_label(person_box, None, color=(0, 0, 255))
- annotator.box_label(cls3_box, None, color=(0, 0, 255))
- self.flag = True
- #print("这是坐",f"{saved_image_path}")
- elif iou > 0 : # IoU大于0,进入新的判断 代表不是坐
- # 创建一个包围person和target框的区域 本来思路是判断脚是否在这个大框框里面 但是这个不合理 应该判断脚是不是在这个交集里面,再检测脚和人与物交集有没有交集
- overlap_ratio, inter_xy, best_cls4_box1 = check_cls4_overlap(person_box, cls3_box,cls4_boxes)
- best_cls4_box.extend(best_cls4_box1) #拿到这四个点的坐标
- inter_x1, inter_y1 = inter_xy
- # 如果占比大于某个阈值,可以执行进一步的操作
- if overlap_ratio > 0.2:
- annotator.box_label(person_box, None, color=(0, 0, 255))
- annotator.box_label(cls3_box, None, color=(0, 0, 255))
- annotator.box_label(best_cls4_box, None, color=(0, 0, 255))
- self.flag = True
- class newPersontre:
- 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
- cls3 = 1 # 对cart单独一类
- # cls4 = 4 # 新增加的类别编号(假设为 4)
- # target_classes = [1, 2, 3]
- results = Results(annotator.result(), path=None, names=names, boxes=det)
- boxes = results.boxes
- print(boxes)
- person_boxes = [] # 人 车子 脚 以及 指定区域内的脚
- cls3_boxes = []
- # cls4_boxes = []
- # best_cls4_box = []
- # 处理检测结果
- 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 == cls3 and con > 0.1: # 目标类别 cart
- x1, y1, x2, y2 = boxes.xyxy[i].tolist()
- cls3_boxes.append([x1, y1, x2, y2])
- print(person_boxes)
- print(cls3_boxes)
- # 如果检测到 "person" 类别和目标框,计算IoU
- if person_boxes and cls3_boxes: # 先查看这俩个类别有没有
- for i, person_box in enumerate(person_boxes):
- for j, cls3_box in enumerate(cls3_boxes):
- # 判断是否存在交集 而且不能低
- iou = calculate_iou(person_box, cls3_box)
- print(f"IOU的大小这是第", iou)
- if iou > 0.15: # 用来判断坐 如果iou足够高 就视为是坐
- # 加载图像并绘制标注
- # 看看这个值大概有多少 好衡量一下
- print(f"这个iou大于0.2的 这是第,iou是{iou}") # 写入介绍
- # img = Image.fromarray(cv2.cvtColor(frame,
- # cv2.COLOR_BGR2RGB)) # 注意 cap.read 获取到的帧是numpy形式 不能直接配合Draw函数 需要转换
- # 创建绘图上下文
- # draw = ImageDraw.Draw(img)
- # 继续进行绘制操作
- # 绘制 "person" 类别框
- annotator.box_label(person_box, None, color=(0, 0, 255))
- # 绘制目标框(bag, box, cart)
- annotator.box_label(cls3_box, None, color=(0, 0, 255))
- 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]
- 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 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 = []
- #belt_objs = []
- if len(point)>1:
- self.polygon_areas = [Polygon(strtolstl(point)[0])]
- else:
- self.polygon_areas = None
- if persondet is not None:
- if self.polygon_areas is not None:
- for left, top, right,top,right, bottom,left,bottom in persondet:
- 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.3:
- print("person in check space!!!")
- width = right - left
- height = bottom - top
- print(f'{left}, {top}, {right},{top},{right}, {bottom},{left},{bottom}')
- print(f'hw = {width* height}')
- if width * height > 10000:
- person_objs.append([left, top, right, bottom, 0.25, 'person'])
- 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 :
- # if persondet is 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.3:
- # continue
- conf = box.conf.cpu().numpy().tolist()[0]
- cls = box.cls.cpu().numpy().tolist()[0]
- # if persondet is not None:
- # 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]])
- if cls in [1,2]:
- #width = right - left
- #height = bottom - top
- #if width * height >4096:
- uniform_objs.append([left, top, right, bottom, conf, names[cls]])
- print(f'personobjs = {person_objs}')
- print(f'uniformobjs = {uniform_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 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:
- 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(f'{conf} {cls}')
- #print(cls in [5,6,7,8,9,10])
- 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 and conf >= 0.5:
- #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(f'headtmp= {headtmp}')
- print(f'helmet_objs = {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 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 = []
- print(f'persondet = {persondet}')
- #belt_objs = []
- if len(point)>1:
- self.polygon_areas = [Polygon(strtolstl(point)[0])]
- print(self.polygon_areas)
- else:
- self.polygon_areas = None
- tmppersondet = []
- if self.polygon_areas is not None:
- for person in persondet:
- polygon_box = Polygon([(person[0], person[1]), (person[2], person[3]), (person[4], person[5]), (person[6], person[7])])
- 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(intersection_areas_ratio)
- if intersection_areas_ratio[-1] < 0.9:
- continue
- tmppersondet.append(person)
- persondet = tmppersondet
- print(f'tmppersondet = {tmppersondet}')
- #persondet = tmppersondet
- for result in results:
- boxes = result.boxes
- for box in boxes:
- left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
- conf = box.conf.cpu().numpy().tolist()[0]
- cls = box.cls.cpu().numpy().tolist()[0]
- #if self.polygon_areas is not None and cls == 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.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]+person[2])/2)
- personcentery = int((person[1]+person[7])/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
|