renwu0122.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. from pathlib import Path
  2. import matplotlib.path as mat
  3. from utils.general import strtolstl
  4. from utils.general import compute_IOU
  5. from torchvision import transforms
  6. from PIL import Image
  7. import torch
  8. import torch.nn.functional as F
  9. import numpy as np
  10. from ultralytics.engine.results import Results
  11. from shapely.geometry import Point
  12. from shapely.geometry.polygon import Polygon
  13. mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
  14. test = transforms.Compose([transforms.Resize((224,224)),
  15. #transforms.CenterCrop(224),
  16. transforms.ToTensor(),
  17. transforms.Normalize(mean=mean, std=std)
  18. ])
  19. def clapre(modelcla,claimg,clapoint):
  20. imgten = torch.stack(claimg,dim=0)
  21. clapoint = torch.stack(clapoint,dim=0)
  22. imgten = imgten.to(0)
  23. result = modelcla(imgten)
  24. result = F.softmax(result)
  25. print(result)
  26. index = result.argmax(1)
  27. index = index.cpu().numpy()
  28. index = np.argwhere(index<5)
  29. index = index.reshape(-1)
  30. print(index)
  31. if len(index)>0:
  32. print(clapoint[index])
  33. return clapoint[index]
  34. else:
  35. return None
  36. class Helmet:
  37. def __init__(self):
  38. self.flag = False
  39. def getflag(self,det,persondet,annotator,fence=0,point=None,names=None,rname=None,num=1):
  40. #print(type(det))
  41. self.flag = False
  42. for *xyxy, conf, cls in reversed(det):
  43. c = int(cls)
  44. labelname = names[c]
  45. if labelname in rname:
  46. if fence == 1:
  47. pointa = strtolstl(point)
  48. for poi in pointa:
  49. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  50. xyxy[3].cpu().item())
  51. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  52. xyxy[3].cpu().item())
  53. pt = [p1, p2]
  54. inflag = mat.Path(poi).contains_points(pt)
  55. if inflag.any():
  56. if persondet is None:
  57. self.flag = True
  58. # c = int(cls) # integer class
  59. # label = f'{self.names[c]} {conf:.2f}'
  60. label = None
  61. annotator.box_label(xyxy, None, color=(0, 0, 255))
  62. else:
  63. for person in persondet:
  64. personinflag = mat.Path(person).contains_points(pt)
  65. if personinflag.any():
  66. annotator.box_label(xyxy, None, color=(0, 0, 255))
  67. self.flag = True
  68. else:
  69. if persondet is None:
  70. self.flag = True
  71. # c = int(cls) # integer class
  72. # label = f'{self.names[c]} {conf:.2f}'
  73. label = None
  74. annotator.box_label(xyxy, None, color=(0, 0, 255))
  75. else:
  76. for person in persondet:
  77. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  78. xyxy[3].cpu().item())
  79. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  80. xyxy[3].cpu().item())
  81. pt = [p1, p2]
  82. personinflag = mat.Path(person).contains_points(pt)
  83. if personinflag.any():
  84. annotator.box_label(xyxy, None, color=(0, 0, 255))
  85. self.flag = True
  86. return self.flag
  87. class Uniform:
  88. def __init__(self):
  89. self.flag = False
  90. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  91. self.flag = False
  92. for *xyxy, conf, cls in reversed(det):
  93. c = int(cls)
  94. labelname = names[c]
  95. if labelname in rname:
  96. if fence == 1:
  97. pointa = strtolstl(point)
  98. for poi in pointa:
  99. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  100. xyxy[3].cpu().item())
  101. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  102. xyxy[3].cpu().item())
  103. pt = [p1, p2]
  104. inflag = mat.Path(poi).contains_points(pt)
  105. if inflag.any():
  106. if persondet is None:
  107. self.flag = True
  108. # c = int(cls) # integer class
  109. # label = f'{self.names[c]} {conf:.2f}'
  110. label = None
  111. annotator.box_label(xyxy, None, color=(0, 0, 255))
  112. else:
  113. for person in persondet:
  114. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  115. xyxy[3].cpu().item())
  116. p2 = (
  117. int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  118. xyxy[3].cpu().item())
  119. pt = [p1, p2]
  120. personinflag = mat.Path(person).contains_points(pt)
  121. if personinflag.any():
  122. annotator.box_label(xyxy, None, color=(0, 0, 255))
  123. self.flag = True
  124. else:
  125. if persondet is None:
  126. self.flag = True
  127. # c = int(cls) # integer class
  128. # label = f'{self.names[c]} {conf:.2f}'
  129. label = None
  130. annotator.box_label(xyxy, None, color=(0, 0, 255))
  131. else:
  132. for person in persondet:
  133. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  134. xyxy[3].cpu().item())
  135. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  136. xyxy[3].cpu().item())
  137. pt = [p1, p2]
  138. personinflag = mat.Path(person).contains_points(pt)
  139. if personinflag.any():
  140. annotator.box_label(xyxy, None, color=(0, 0, 255))
  141. self.flag = True
  142. return self.flag
  143. class Fall:
  144. def __init__(self):
  145. self.flag = False
  146. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  147. self.flag = False
  148. for *xyxy, conf, cls in reversed(det):
  149. c = int(cls)
  150. labelname = names[c]
  151. if labelname in rname:
  152. if fence == 1:
  153. pointa = strtolstl(point)
  154. for poi in pointa:
  155. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  156. xyxy[3].cpu().item())
  157. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  158. xyxy[3].cpu().item())
  159. pt = [p1, p2]
  160. inflag = mat.Path(poi).contains_points(pt)
  161. if inflag.any():
  162. if persondet is None:
  163. self.flag = True
  164. # c = int(cls) # integer class
  165. # label = f'{self.names[c]} {conf:.2f}'
  166. label = None
  167. annotator.box_label(xyxy, None, color=(0, 0, 255))
  168. else:
  169. for person in persondet:
  170. personinflag = mat.Path(person).contains_points(pt)
  171. if personinflag.any():
  172. annotator.box_label(xyxy, None, color=(0, 0, 255))
  173. self.flag = True
  174. else:
  175. if persondet is None:
  176. self.flag = True
  177. # c = int(cls) # integer class
  178. # label = f'{self.names[c]} {conf:.2f}'
  179. label = None
  180. annotator.box_label(xyxy, None, color=(0, 0, 255))
  181. else:
  182. for person in persondet:
  183. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  184. xyxy[3].cpu().item())
  185. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  186. xyxy[3].cpu().item())
  187. pt = [p1, p2]
  188. personinflag = mat.Path(person).contains_points(pt)
  189. if personinflag.any():
  190. annotator.box_label(xyxy, None, color=(0, 0, 255))
  191. self.flag = True
  192. return self.flag
  193. class Personcount:
  194. def __init__(self):
  195. self.flag = False
  196. def getflag(self, det,persondet, annotator, fence=0, point=None, names=None, rname=None,num=1):
  197. self.flag = False
  198. detnum = 0
  199. for *xyxy, conf, cls in reversed(det):
  200. c = int(cls)
  201. labelname = names[c]
  202. if labelname in rname:
  203. if fence == 1:
  204. pointa = strtolstl(point)
  205. for poi in pointa:
  206. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  207. xyxy[3].cpu().item())
  208. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  209. xyxy[3].cpu().item())
  210. pt = [p1, p2]
  211. inflag = mat.Path(poi).contains_points(pt)
  212. if inflag.any():
  213. if persondet is None:
  214. #self.flag = True
  215. detnum = detnum+1
  216. # c = int(cls) # integer class
  217. # label = f'{self.names[c]} {conf:.2f}'
  218. label = None
  219. annotator.box_label(xyxy, None, color=(0, 0, 255))
  220. else:
  221. for person in persondet:
  222. personinflag = mat.Path(person).contains_points(pt)
  223. if personinflag.any():
  224. detnum = detnum+1
  225. annotator.box_label(xyxy, None, color=(0, 0, 255))
  226. #self.flag = True
  227. else:
  228. if persondet is None:
  229. #self.flag = True
  230. detnum = detnum+1
  231. # c = int(cls) # integer class
  232. # label = f'{self.names[c]} {conf:.2f}'
  233. label = None
  234. annotator.box_label(xyxy, None, color=(0, 0, 255))
  235. else:
  236. for person in persondet:
  237. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  238. xyxy[3].cpu().item())
  239. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  240. xyxy[3].cpu().item())
  241. pt = [p1, p2]
  242. personinflag = mat.Path(person).contains_points(pt)
  243. if personinflag.any():
  244. detnum = detnum+1
  245. annotator.box_label(xyxy, None, color=(0, 0, 255))
  246. #self.flag = True
  247. if detnum >= num:
  248. self.flag = True
  249. return self.flag
  250. class Arm:
  251. def __init__(self):
  252. self.flag = False
  253. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  254. self.flag = False
  255. for *xyxy, conf, cls in reversed(det):
  256. c = int(cls)
  257. labelname = names[c]
  258. if labelname in rname:
  259. if fence == 1:
  260. pointa = strtolstl(point)
  261. for poi in pointa:
  262. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  263. xyxy[3].cpu().item())
  264. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  265. xyxy[3].cpu().item())
  266. pt = [p1, p2]
  267. inflag = mat.Path(poi).contains_points(pt)
  268. if inflag.any():
  269. if persondet is None:
  270. self.flag = True
  271. # c = int(cls) # integer class
  272. # label = f'{self.names[c]} {conf:.2f}'
  273. label = None
  274. annotator.box_label(xyxy, None, color=(0,0,255))
  275. else:
  276. for person in persondet:
  277. personinflag = mat.Path(person).contains_points(pt)
  278. if personinflag.any():
  279. annotator.box_label(xyxy, None, color=(0, 0, 255))
  280. self.flag = True
  281. else:
  282. if persondet is None:
  283. self.flag = True
  284. # c = int(cls) # integer class
  285. # label = f'{self.names[c]} {conf:.2f}'
  286. label = None
  287. annotator.box_label(xyxy, None, color=(0, 0, 255))
  288. else:
  289. for person in persondet:
  290. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  291. xyxy[3].cpu().item())
  292. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  293. xyxy[3].cpu().item())
  294. pt = [p1, p2]
  295. personinflag = mat.Path(person).contains_points(pt)
  296. if personinflag.any():
  297. annotator.box_label(xyxy, None, color=(0, 0, 255))
  298. self.flag = True
  299. return self.flag
  300. class Bag:
  301. def __init__(self):
  302. self.flag = False
  303. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  304. self.flag = False
  305. for *xyxy, conf, cls in reversed(det):
  306. c = int(cls)
  307. labelname = names[c]
  308. if labelname in rname:
  309. if fence == 1:
  310. pointa = strtolstl(point)
  311. for poi in pointa:
  312. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  313. xyxy[3].cpu().item())
  314. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  315. xyxy[3].cpu().item())
  316. pt = [p1, p2]
  317. inflag = mat.Path(poi).contains_points(pt)
  318. if inflag.any():
  319. if persondet is None:
  320. self.flag = True
  321. # c = int(cls) # integer class
  322. # label = f'{self.names[c]} {conf:.2f}'
  323. label = None
  324. annotator.box_label(xyxy, None, color=(0, 0, 255))
  325. else:
  326. for person in persondet:
  327. personinflag = mat.Path(person).contains_points(pt)
  328. if personinflag.any():
  329. annotator.box_label(xyxy, None, color=(0, 0, 255))
  330. self.flag = True
  331. else:
  332. if persondet is None:
  333. self.flag = True
  334. # c = int(cls) # integer class
  335. # label = f'{self.names[c]} {conf:.2f}'
  336. label = None
  337. annotator.box_label(xyxy, None, color=(0, 0, 255))
  338. else:
  339. for person in persondet:
  340. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  341. xyxy[3].cpu().item())
  342. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  343. xyxy[3].cpu().item())
  344. pt = [p1, p2]
  345. personinflag = mat.Path(person).contains_points(pt)
  346. if personinflag.any():
  347. annotator.box_label(xyxy, None, color=(0, 0, 255))
  348. self.flag = True
  349. return self.flag
  350. class Cross:
  351. def __init__(self):
  352. self.flag = False
  353. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  354. self.flag = False
  355. detnum = 0
  356. for *xyxy, conf, cls in reversed(det):
  357. c = int(cls)
  358. labelname = names[c]
  359. if labelname in rname:
  360. if fence == 1:
  361. pointa = strtolstl(point)
  362. for poi in pointa:
  363. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  364. xyxy[3].cpu().item())
  365. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  366. xyxy[3].cpu().item())
  367. pt = [p1, p2]
  368. inflag = mat.Path(poi).contains_points(pt)
  369. if inflag.any():
  370. if persondet is None:
  371. detnum = detnum+1
  372. self.flag = True
  373. # c = int(cls) # integer class
  374. # label = f'{self.names[c]} {conf:.2f}'
  375. label = None
  376. annotator.box_label(xyxy, None, color=(0, 0, 255))
  377. else:
  378. for person in persondet:
  379. personinflag = mat.Path(person).contains_points(pt)
  380. if personinflag.any():
  381. detnum = detnum+1
  382. annotator.box_label(xyxy, None, color=(0, 0, 255))
  383. self.flag = True
  384. else:
  385. if persondet is None:
  386. self.flag = True
  387. # c = int(cls) # integer class
  388. # label = f'{self.names[c]} {conf:.2f}'
  389. detnum = detnum+1
  390. label = None
  391. annotator.box_label(xyxy, None, color=(0, 0, 255))
  392. else:
  393. for person in persondet:
  394. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  395. xyxy[3].cpu().item())
  396. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  397. xyxy[3].cpu().item())
  398. pt = [p1, p2]
  399. personinflag = mat.Path(person).contains_points(pt)
  400. if personinflag.any():
  401. detnum = detnum +1
  402. annotator.box_label(xyxy, None, color=(0, 0, 255))
  403. self.flag = True
  404. return self.flag
  405. class Extinguisher:
  406. def __init__(self):
  407. self.flag = False
  408. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  409. self.flag = False
  410. for *xyxy, conf, cls in reversed(det):
  411. c = int(cls)
  412. labelname = names[c]
  413. if labelname in rname:
  414. if fence == 1:
  415. pointa = strtolstl(point)
  416. for poi in pointa:
  417. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  418. xyxy[3].cpu().item())
  419. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  420. xyxy[3].cpu().item())
  421. pt = [p1, p2]
  422. inflag = mat.Path(poi).contains_points(pt)
  423. if inflag.any():
  424. if persondet is None:
  425. self.flag = True
  426. # c = int(cls) # integer class
  427. # label = f'{self.names[c]} {conf:.2f}'
  428. label = None
  429. annotator.box_label(xyxy, None, color=(0, 0, 255))
  430. else:
  431. for person in persondet:
  432. personinflag = mat.Path(person).contains_points(pt)
  433. if personinflag.any():
  434. annotator.box_label(xyxy, None, color=(0, 0, 255))
  435. self.flag = True
  436. else:
  437. if persondet is None:
  438. self.flag = True
  439. # c = int(cls) # integer class
  440. # label = f'{self.names[c]} {conf:.2f}'
  441. label = None
  442. annotator.box_label(xyxy, None, color=(0, 0, 255))
  443. else:
  444. for person in persondet:
  445. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  446. xyxy[3].cpu().item())
  447. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  448. xyxy[3].cpu().item())
  449. pt = [p1, p2]
  450. personinflag = mat.Path(person).contains_points(pt)
  451. if personinflag.any():
  452. annotator.box_label(xyxy, None, color=(0, 0, 255))
  453. self.flag = True
  454. return self.flag
  455. def calculate_iou(box1, box2):
  456. # 计算交集区域 也就是找到次左上 次右下
  457. x1_int = max(box1[0], box2[0])
  458. y1_int = max(box1[1], box2[1])
  459. x2_int = min(box1[2], box2[2])
  460. y2_int = min(box1[3], box2[3])
  461. # 交集区域的宽高
  462. width_int = max(0, x2_int - x1_int)
  463. height_int = max(0, y2_int - y1_int)
  464. area_int = width_int * height_int
  465. # 计算两个框的面积
  466. area_box1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
  467. area_box2 = (box2[2] - box2[0]) * (box2[3] - box1[1])
  468. # 计算并集面积
  469. area_union = area_box1 + area_box2 - area_int
  470. # 计算IoU
  471. iou = area_int / area_union if area_union > 0 else 0
  472. return iou
  473. class Persontre1:
  474. def __init__(self):
  475. self.flag = False
  476. self.classifier_model = torch.load('/home/h3c/yolo/persontrecls.pt')
  477. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1,im0=None):
  478. self.flag = False
  479. dirp = {}
  480. dirf = {}
  481. for *xyxy, conf, cls in reversed(det):
  482. c = int(cls)
  483. labelname = names[c]
  484. if labelname in rname:
  485. if fence == 1:
  486. pointa = strtolstl(point)
  487. for poi in pointa:
  488. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  489. xyxy[3].cpu().item())
  490. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  491. xyxy[3].cpu().item())
  492. pt = [p1, p2]
  493. inflag = mat.Path(poi).contains_points(pt)
  494. if inflag.any():
  495. if persondet is None:
  496. self.flag = True
  497. # c = int(cls) # integer class
  498. # label = f'{self.names[c]} {conf:.2f}'
  499. label = None
  500. if c==0:
  501. dirp.setdefault(0,[])
  502. dirp[0].append(xyxy)
  503. elif c in [1,2]:
  504. dirp.setdefault(1,[])
  505. dirp[1].append(xyxy)
  506. dirf.setdefault(1,[])
  507. dirf[1].append(xyxy)
  508. elif c==3:
  509. dirp.setdefault(1,[])
  510. dirp[1].append(xyxy)
  511. elif c==4:
  512. dirf.setdefault(0,[])
  513. dirf[0].append(xyxy)
  514. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  515. else:
  516. for person in persondet:
  517. personinflag = mat.Path(person).contains_points(pt)
  518. if personinflag.any():
  519. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  520. #self.flag = True
  521. if c==0:
  522. dirp.setdefault(0,[])
  523. dirp[0].append(xyxy)
  524. elif c in [1,2]:
  525. dirp.setdefault(1,[])
  526. dirp[1].append(xyxy)
  527. dirf.setdefault(1,[])
  528. dirf[1].append(xyxy)
  529. elif c==3:
  530. dirp.setdefault(1,[])
  531. dirp[1].append(xyxy)
  532. elif c==4:
  533. dirf.setdefault(0,[])
  534. dirf[0].append(xyxy)
  535. else:
  536. if persondet is None:
  537. #self.flag = True
  538. # c = int(cls) # integer class
  539. # label = f'{self.names[c]} {conf:.2f}'
  540. #label = None
  541. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  542. if c==0:
  543. dirp.setdefault(0,[])
  544. dirp[0].append(xyxy)
  545. elif c in [1,2]:
  546. dirp.setdefault(1,[])
  547. dirp[1].append(xyxy)
  548. dirf.setdefault(1,[])
  549. dirf[1].append(xyxy)
  550. elif c==3:
  551. dirp.setdefault(1,[])
  552. dirp[1].append(xyxy)
  553. elif c==4:
  554. dirf.setdefault(0,[])
  555. dirf[0].append(xyxy)
  556. else:
  557. for person in persondet:
  558. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  559. xyxy[3].cpu().item())
  560. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  561. xyxy[3].cpu().item())
  562. pt = [p1, p2]
  563. personinflag = mat.Path(person).contains_points(pt)
  564. if personinflag.any():
  565. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  566. #self.flag = True
  567. if c==0:
  568. dirp.setdefault(0,[])
  569. dirp[0].append(xyxy)
  570. elif c in [1,2]:
  571. dirp.setdefault(1,[])
  572. dirp[1].append(xyxy)
  573. dirf.setdefault(1,[])
  574. dirf[1].append(xyxy)
  575. elif c==3:
  576. dirp.setdefault(1,[])
  577. dirp[1].append(xyxy)
  578. elif c==4:
  579. dirf.setdefault(0,[])
  580. dirf[0].append(xyxy)
  581. if len(dirp.keys()) == 2:
  582. claimg = []
  583. clapoint = []
  584. for person in dirp[0]:
  585. for other in dirp[1]:
  586. iou, newxyxy = compute_IOU(person, other)
  587. if iou>0.1:
  588. print(newxyxy)
  589. imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
  590. imgtmp = imgtmp[...,::-1]
  591. imgtmp = Image.fromarray(imgtmp)
  592. imgten1 = test(imgtmp)
  593. claimg.append(imgten1)
  594. clapoint.append((newxyxy))
  595. result = clapre(self.classifier_model,claimg,clapoint)
  596. #imgten = imgten1[None]
  597. #imgten = imgten.to(0)
  598. #result = modelcla(imgten)
  599. #result = F.softmax(result, dim=1)
  600. #cla = result.argmax(1)
  601. if result is not None:
  602. self.flag = True
  603. for res in result:
  604. print(res)
  605. annotator.box_label(res, None, color=(0,0,255))
  606. if len(dirp.keys()) == 2:
  607. claimg = []
  608. clapoint = []
  609. for person in dirp[0]:
  610. for other in dirp[1]:
  611. iou, newxyxy = compute_IOU(person, other)
  612. if iou>0.1:
  613. print(newxyxy)
  614. imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
  615. imgtmp = imgtmp[...,::-1]
  616. imgtmp = Image.fromarray(imgtmp)
  617. imgten1 = test(imgtmp)
  618. claimg.append(imgten1)
  619. clapoint.append((newxyxy))
  620. result = clapre(self.classifier_model,claimg,clapoint)
  621. #imgten = imgten1[None]
  622. #imgten = imgten.to(0)
  623. #result = modelcla(imgten)
  624. #result = F.softmax(result, dim=1)
  625. #cla = result.argmax(1)
  626. if result is not None:
  627. self.flag = True
  628. for res in result:
  629. print(res)
  630. annotator.box_label(res, None, color=(0,0,255))
  631. return self.flag
  632. class Persontre:
  633. def __init__(self):
  634. self.flag = False
  635. self.classifier_model = torch.load('/home/h3c/yolo/persontrecls.pt')
  636. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1,im0=None):
  637. self.flag = False
  638. target_classes = [1, 2, 3]
  639. results = Results(annotator.result(),path=None,names=names,boxes=det)
  640. boxes = results.boxes
  641. person_boxes = []
  642. target_boxes = []
  643. cls4_boxes = []
  644. # 处理检测结果
  645. for i in range(len(boxes)):
  646. cls = int(boxes.cls[i].item())
  647. con = boxes.conf[i].item()
  648. if cls == 0 and con > 0.1: # 如果是 "person" 类别
  649. x1, y1, x2, y2 = boxes.xyxy[i].tolist()
  650. person_boxes.append([x1, y1, x2, y2])
  651. if cls in target_classes and con > 0.1: # 目标类别(bag, box, cart)
  652. x1, y1, x2, y2 = boxes.xyxy[i].tolist()
  653. target_boxes.append([x1, y1, x2, y2])
  654. if cls == 4 and con > 0.1: # 如果是 "cls 4" 类别
  655. x1, y1, x2, y2 = boxes.xyxy[i].tolist()
  656. cls4_boxes.append([x1, y1, x2, y2])
  657. # 如果检测到 "person" 类别和目标框,计算IoU
  658. if person_boxes and target_boxes:
  659. for i, person_box in enumerate(person_boxes):
  660. person_center_y = (person_box[1] + person_box[3]) / 2
  661. for j, target_box in enumerate(target_boxes):
  662. target_center_y = (target_box[1] + target_box[3]) / 2
  663. # 判断目标框的中心点是否在person框的下方
  664. if target_center_y + 20 > person_center_y: # 根据需要调整此阈值
  665. iou = calculate_iou(person_box, target_box)
  666. if iou > 0: # IoU大于0,进入新的判断
  667. # 创建一个包围person和target框的区域 本来思路是判断脚是否在这个大框框里面 但是这个不合理 应该判断脚是不是在这个交集里面,再检测脚和人与物交集有没有交集
  668. min_x = max(person_box[0], target_box[0])
  669. min_y = max(person_box[1], target_box[1])
  670. max_x = min(person_box[2], target_box[2])
  671. max_y = min(person_box[3], target_box[3])
  672. target_area = (min_x, min_y, max_x, max_y)
  673. # 检查是否有cls 4物体在这个区域内
  674. for cls4_box in cls4_boxes: #比较巧妙 提前存储这个变量 然后检测这个物体是不是在这里面
  675. # 判断cls 4框是否与这个区域有交集
  676. inter_x1 = max(cls4_box[0], target_area[0])
  677. inter_y1 = max(cls4_box[1], target_area[1])
  678. inter_x2 = min(cls4_box[2], target_area[2])
  679. inter_y2 = min(cls4_box[3], target_area[3])
  680. if inter_x1 < inter_x2 and inter_y1 < inter_y2:
  681. # 计算交集区域的面积
  682. intersection_area = (inter_x2 - inter_x1) * (inter_y2 - inter_y1)
  683. # 计算 cls4_box 的面积
  684. cls4_area = (cls4_box[2] - cls4_box[0]) * (cls4_box[3] - cls4_box[1])
  685. # 计算交集区域占 cls4_box 面积的比例
  686. overlap_ratio = intersection_area / cls4_area
  687. if overlap_ratio > 0.5:
  688. self.flag = True
  689. return self.flag
  690. class Danager:
  691. def __init__(self):
  692. self.flag = False
  693. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  694. self.flag = False
  695. for *xyxy, conf, cls in reversed(det):
  696. c = int(cls)
  697. labelname = names[c]
  698. if labelname in rname:
  699. if fence == 1:
  700. pointa = strtolstl(point)
  701. for poi in pointa:
  702. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 2),
  703. int(xyxy[1].cpu().item() + (xyxy[3].cpu().item() - xyxy[1].cpu().item()) / 2))
  704. #p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  705. #xyxy[3].cpu().item())
  706. pt = [p1]
  707. print(f'pt = {pt}')
  708. print(f'poi = {poi}')
  709. inflag = mat.Path(poi).contains_points(pt)
  710. if inflag.any():
  711. if persondet is None:
  712. self.flag = True
  713. # c = int(cls) # integer class
  714. # label = f'{self.names[c]} {conf:.2f}'
  715. label = None
  716. annotator.box_label(xyxy, None, color=(0, 0, 255))
  717. else:
  718. for person in persondet:
  719. personinflag = mat.Path(person).contains_points(pt)
  720. if personinflag.any():
  721. annotator.box_label(xyxy, None, color=(0, 0, 255))
  722. self.flag = True
  723. else:
  724. if persondet is None:
  725. self.flag = True
  726. # c = int(cls) # integer class
  727. # label = f'{self.names[c]} {conf:.2f}'
  728. label = None
  729. annotator.box_label(xyxy, None, color=(0, 0, 255))
  730. else:
  731. for person in persondet:
  732. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  733. xyxy[3].cpu().item())
  734. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  735. xyxy[3].cpu().item())
  736. pt = [p1, p2]
  737. personinflag = mat.Path(person).contains_points(pt)
  738. if personinflag.any():
  739. annotator.box_label(xyxy, None, color=(0, 0, 255))
  740. self.flag = True
  741. return self.flag
  742. class CarHelmetBelt:
  743. def __init__(self):
  744. self.flag = False
  745. def selectNoBeltPerson(self, person_objs, belt_objs):
  746. objs = []
  747. polygon_person = [Polygon(
  748. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  749. polygon_belt = [Polygon(
  750. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in belt_objs]
  751. for person_obj, person in zip(person_objs, polygon_person):
  752. with_belt = False
  753. for belt in polygon_belt:
  754. if person.intersection(belt).area / belt.area > 0.5:
  755. with_belt = True
  756. break
  757. if not with_belt:
  758. objs.append(person_obj)
  759. return objs
  760. def selectWithPersonHead(self, person_objs, head_objs):
  761. objs = []
  762. polygon_person = [Polygon(
  763. [(left, top), (right, top), (right, top + (bottom - top)/2), (left, top + (bottom - top)/2)]) for left, top, right, bottom, _, _ in person_objs]
  764. polygon_head = [Polygon(
  765. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in head_objs]
  766. for head_obj, head in zip(head_objs, polygon_head):
  767. with_person = False
  768. for person in polygon_person:
  769. print('head')
  770. if person.intersection(head).area / head.area > 0.5:
  771. with_person = True
  772. break
  773. if with_person:
  774. objs.append(head_obj)
  775. return objs
  776. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  777. self.flag = False
  778. results = Results(annotator.result(),path=None,names=names,boxes=det)
  779. person_objs = []
  780. head_objs = []
  781. belt_objs = []
  782. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  783. for result in results:
  784. boxes = result.boxes
  785. for box in boxes:
  786. #print(box.conf.cpu())
  787. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  788. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  789. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  790. if sum(intersection_areas) == 0:
  791. continue
  792. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  793. if intersection_areas_ratio[-1] < 0.9:
  794. continue
  795. conf = box.conf.cpu().numpy().tolist()[0]
  796. cls = box.cls.cpu().numpy().tolist()[0]
  797. if cls == 0:
  798. person_objs.append([left, top, right, bottom, conf, names[cls]])
  799. elif cls == 1:
  800. head_objs.append([left, top, right, bottom, conf, names[cls]])
  801. elif cls == 3:
  802. belt_objs.append([left, top, right, bottom, conf, names[cls]])
  803. #print(head_objs)
  804. #print(person_objs)
  805. illegal_objs = self.selectNoBeltPerson(person_objs, belt_objs) + self.selectWithPersonHead(person_objs, head_objs)
  806. if len(illegal_objs)>0:
  807. for obj in illegal_objs:
  808. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  809. self.flag = True
  810. return self.flag
  811. class newUniform:
  812. def __init__(self):
  813. self.flag = False
  814. def selectNoUniformPerson(self, person_objs, uniform_objs):
  815. objs = []
  816. print(person_objs)
  817. print(uniform_objs)
  818. polygon_person = [Polygon(
  819. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  820. polygon_uniform = [Polygon(
  821. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs]
  822. for person_obj, person in zip(person_objs, polygon_person):
  823. with_uniform = False
  824. for uniform in polygon_uniform:
  825. if person.intersection(uniform).area / uniform.area > 0.3:
  826. with_uniform = True
  827. break
  828. if not with_uniform:
  829. print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}')
  830. objs.append(person_obj)
  831. return objs
  832. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  833. self.flag = False
  834. results = Results(annotator.result(),path=None,names=names,boxes=det)
  835. print(results.boxes)
  836. person_objs = []
  837. uniform_objs = []
  838. #belt_objs = []
  839. if len(point)>1:
  840. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  841. else:
  842. self.polygon_areas = None
  843. for result in results:
  844. boxes = result.boxes
  845. for box in boxes:
  846. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  847. if self.polygon_areas is not None and box.cls.cpu().numpy().tolist()[0]==4:
  848. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  849. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  850. if sum(intersection_areas) == 0:
  851. continue
  852. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  853. print(f'inter = {intersection_areas_ratio}')
  854. if intersection_areas_ratio[-1] < 0.9:
  855. continue
  856. conf = box.conf.cpu().numpy().tolist()[0]
  857. cls = box.cls.cpu().numpy().tolist()[0]
  858. if cls == 4 and conf >=0.7:
  859. width = right - left
  860. height = bottom - top
  861. if width * height >10000:
  862. person_objs.append([left, top, right, bottom, conf, names[cls]])
  863. elif cls in [1,2]:
  864. #width = right - left
  865. #height = bottom - top
  866. #if width * height >4096:
  867. uniform_objs.append([left, top, right, bottom, conf, names[cls]])
  868. illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs)
  869. if len(illegal_objs)>0:
  870. for obj in illegal_objs:
  871. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  872. self.flag = True
  873. return self.flag
  874. class newHelmet:
  875. def __init__(self):
  876. self.flag = False
  877. def selectNoHelmetPerson(self, person_objs, head_objs):
  878. objs = []
  879. polygon_person = [Polygon(
  880. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  881. polygon_head = [Polygon(
  882. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in head_objs]
  883. for person_obj, person in zip(person_objs, polygon_person):
  884. with_head = False
  885. for head in polygon_head:
  886. print('head')
  887. if person.intersection(head).area / head.area > 0.3:
  888. with_head = True
  889. break
  890. if with_head:
  891. objs.append(person_obj)
  892. return objs
  893. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  894. self.flag = False
  895. results = Results(annotator.result(),path=None,names=names,boxes=det)
  896. person_objs = []
  897. head_objs = []
  898. helmet_objs = []
  899. headtmp = []
  900. #belt_objs = []
  901. if len(point)>1:
  902. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  903. else:
  904. self.polygon_areas = None
  905. for result in results:
  906. boxes = result.boxes
  907. for box in boxes:
  908. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  909. if self.polygon_areas is not None and box.cls.cpu().numpy().tolist()[0]==4:
  910. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  911. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  912. if sum(intersection_areas) == 0:
  913. continue
  914. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  915. if intersection_areas_ratio[-1] < 0.3:
  916. continue
  917. conf = box.conf.cpu().numpy().tolist()[0]
  918. cls = box.cls.cpu().numpy().tolist()[0]
  919. if cls == 4 and conf >=0.7:
  920. width = right - left
  921. height = bottom - top
  922. if width * height >4096:
  923. person_objs.append([left, top, right, bottom, conf, names[cls]])
  924. elif cls == 0:
  925. #width = right - left
  926. #height = bottom - top
  927. #if width * height >4096:
  928. print('------')
  929. headtmp.append([left, top, right, bottom, conf, names[cls]])
  930. elif cls in [5,6,7,8,9,10]:
  931. helmet_objs.append([(left, top), (right, top), (right, bottom), (left, bottom)])
  932. print(headtmp)
  933. print(helmet_objs)
  934. for left, top, right, bottom, conf, name in headtmp:
  935. flag = False
  936. pt = [(int((left+right)/2),int((top+bottom)/2))]
  937. for helmet in helmet_objs:
  938. pflag = mat.Path(helmet).contains_points(pt)
  939. if pflag.any():
  940. flag = True
  941. break
  942. if not flag:
  943. head_objs.append([left, top, right, bottom, conf, name])
  944. illegal_objs = self.selectNoHelmetPerson(person_objs, head_objs)
  945. if len(illegal_objs)>0:
  946. for obj in illegal_objs:
  947. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  948. self.flag = True
  949. return self.flag
  950. class newHelmetn:
  951. def __init__(self):
  952. self.flag = False
  953. def selectNoHelmetPerson(self, person_objs, head_objs):
  954. objs = []
  955. polygon_person = [Polygon(
  956. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  957. polygon_head = [Polygon(
  958. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in head_objs]
  959. for person_obj, person in zip(person_objs, polygon_person):
  960. with_head = False
  961. for head in polygon_head:
  962. if person.intersection(head).area / head.area > 0.3:
  963. with_head = True
  964. break
  965. if with_head:
  966. objs.append(person_obj)
  967. return objs
  968. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  969. self.flag = False
  970. results = Results(annotator.result(),path=None,names=names,boxes=det)
  971. person_objs = []
  972. head_objs = []
  973. #belt_objs = []
  974. if len(point)>1:
  975. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  976. else:
  977. self.polygon_areas = None
  978. for result in results:
  979. boxes = result.boxes
  980. for box in boxes:
  981. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  982. if self.polygon_areas is not None:
  983. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  984. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  985. if sum(intersection_areas) == 0:
  986. continue
  987. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  988. if intersection_areas_ratio[-1] < 0.9:
  989. continue
  990. conf = box.conf.cpu().numpy().tolist()[0]
  991. cls = box.cls.cpu().numpy().tolist()[0]
  992. if cls == 4:
  993. width = right - left
  994. height = bottom - top
  995. if width * height >4096:
  996. person_objs.append([left, top, right, bottom, conf, names[cls]])
  997. elif cls == 0:
  998. #width = right - left
  999. #height = bottom - top
  1000. #if width * height >4096:
  1001. head_objs.append([left, top, right, bottom, conf, names[cls]])
  1002. illegal_objs = self.selectNoHelmetPerson(person_objs, head_objs)
  1003. if len(illegal_objs)>0:
  1004. for obj in illegal_objs:
  1005. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  1006. self.flag = True
  1007. return self.flag
  1008. class newUniformt:
  1009. def __init__(self):
  1010. self.flag = False
  1011. def selectNoUniformPerson(self, person_objs, uniform_objs):
  1012. objs = []
  1013. print(person_objs)
  1014. print(uniform_objs)
  1015. polygon_person = [Polygon(
  1016. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  1017. polygon_uniform = [Polygon(
  1018. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs]
  1019. for person_obj, person in zip(person_objs, polygon_person):
  1020. with_uniform = False
  1021. for uniform in polygon_uniform:
  1022. if person.intersection(uniform).area / uniform.area > 0.3:
  1023. with_uniform = True
  1024. break
  1025. if not with_uniform:
  1026. print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}')
  1027. objs.append(person_obj)
  1028. return objs
  1029. def selectHeadPerson(self, person_objs, head_objs):
  1030. objs = []
  1031. print(person_objs)
  1032. print(head_objs)
  1033. #personlist = []
  1034. polygon_person = [Polygon(
  1035. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  1036. polygon_head = [Polygon(
  1037. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs]
  1038. for person_obj, person in zip(person_objs, polygon_person):
  1039. #with_uniform = False
  1040. for head in polygon_head:
  1041. if person.intersection(head).area / head.area > 0.3:
  1042. #with_uniform = True
  1043. #break
  1044. #if not with_uniform:
  1045. # print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}')
  1046. objs.append(person_obj)
  1047. break
  1048. return objs
  1049. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  1050. self.flag = False
  1051. results = Results(annotator.result(),path=None,names=names,boxes=det)
  1052. print(results.boxes)
  1053. person_objs = []
  1054. uniform_objs = []
  1055. head_objs = []
  1056. #belt_objs = []
  1057. if len(point)>1:
  1058. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  1059. else:
  1060. self.polygon_areas = None
  1061. for result in results:
  1062. boxes = result.boxes
  1063. for box in boxes:
  1064. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  1065. if self.polygon_areas is not None:
  1066. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  1067. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  1068. if sum(intersection_areas) == 0:
  1069. continue
  1070. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  1071. if intersection_areas_ratio[-1] < 0.9:
  1072. continue
  1073. conf = box.conf.cpu().numpy().tolist()[0]
  1074. cls = box.cls.cpu().numpy().tolist()[0]
  1075. if cls == 4:
  1076. width = right - left
  1077. height = bottom - top
  1078. if width * height >4096:
  1079. person_objs.append([left, top, right, bottom, conf, names[cls]])
  1080. elif cls in [1,2]:
  1081. #width = right - left
  1082. #height = bottom - top
  1083. #if width * height >4096:
  1084. uniform_objs.append([left, top, right, bottom, conf, names[cls]])
  1085. elif cls != 3:
  1086. head_objs.append([left, top, right, bottom, conf, names[cls]])
  1087. person_objs = self.selectNoUniformPerson(person_objs, head_objs)
  1088. illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs)
  1089. if len(illegal_objs)>0:
  1090. for obj in illegal_objs:
  1091. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  1092. self.flag = True
  1093. return self.flag
  1094. class newUniformi1:
  1095. def __init__(self):
  1096. self.flag = False
  1097. def selectNoUniformPerson(self, person_objs, uniform_objs):
  1098. objs = []
  1099. print(person_objs)
  1100. print(uniform_objs)
  1101. polygon_person = [Polygon(
  1102. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  1103. polygon_uniform = [Polygon(
  1104. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in uniform_objs]
  1105. for person_obj, person in zip(person_objs, polygon_person):
  1106. with_uniform = False
  1107. for uniform in polygon_uniform:
  1108. if person.intersection(uniform).area / uniform.area > 0.3:
  1109. with_uniform = True
  1110. break
  1111. if not with_uniform:
  1112. print(f'illperson_obj {person_obj} illpolygon_uniform {polygon_uniform}')
  1113. objs.append(person_obj)
  1114. return objs
  1115. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  1116. self.flag = False
  1117. results = Results(annotator.result(),path=None,names=names,boxes=det)
  1118. print(results.boxes)
  1119. person_objs = []
  1120. uniform_objs = []
  1121. print(f'persondet = {persondet}')
  1122. #belt_objs = []
  1123. if len(point)>1:
  1124. self.polygon_areas = [Polygon(strtolstl(point)[0])]
  1125. else:
  1126. self.polygon_areas = None
  1127. for result in results:
  1128. boxes = result.boxes
  1129. for box in boxes:
  1130. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  1131. if self.polygon_areas is not None:
  1132. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  1133. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  1134. if sum(intersection_areas) == 0:
  1135. continue
  1136. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  1137. if intersection_areas_ratio[-1] < 0.9:
  1138. continue
  1139. conf = box.conf.cpu().numpy().tolist()[0]
  1140. cls = box.cls.cpu().numpy().tolist()[0]
  1141. print(conf)
  1142. print(cls)
  1143. if cls == 4 and conf >=0.7:
  1144. width = right - left
  1145. height = bottom - top
  1146. print(width*height)
  1147. if width * height >10000:
  1148. for person in persondet:
  1149. #p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  1150. #xyxy[3].cpu().item())
  1151. #p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  1152. # xyxy[3].cpu().item())
  1153. personcenterx = int((person[0][0]+person[1][0])/2)
  1154. personcentery = int((person[0][1]+person[2][1])/2)
  1155. p1 = (personcenterx,personcentery)
  1156. print(p1)
  1157. pt = [p1]
  1158. print(f'p1 = {p1}')
  1159. print(f'person = {person}')
  1160. personinflag = mat.Path([(left, top), (right, top), (right, bottom), (left, bottom)]).contains_points(pt)
  1161. if personinflag.any():
  1162. person_objs.append([left, top, right, bottom, conf, names[cls]])
  1163. elif cls in [1,2]:
  1164. #width = right - left
  1165. #height = bottom - top
  1166. #if width * height >4096:
  1167. uniform_objs.append([left, top, right, bottom, conf, names[cls]])
  1168. illegal_objs = self.selectNoUniformPerson(person_objs, uniform_objs)
  1169. if len(illegal_objs)>0:
  1170. for obj in illegal_objs:
  1171. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  1172. self.flag = True
  1173. return self.flag