renwua.py 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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. mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
  12. test = transforms.Compose([transforms.Resize((224,224)),
  13. #transforms.CenterCrop(224),
  14. transforms.ToTensor(),
  15. transforms.Normalize(mean=mean, std=std)
  16. ])
  17. def clapre(modelcla,claimg,clapoint):
  18. imgten = torch.stack(claimg,dim=0)
  19. clapoint = torch.stack(clapoint,dim=0)
  20. imgten = imgten.to(0)
  21. result = modelcla(imgten)
  22. result = F.softmax(result)
  23. print(result)
  24. index = result.argmax(1)
  25. index = index.cpu().numpy()
  26. index = np.argwhere(index<5)
  27. index = index.reshape(-1)
  28. print(index)
  29. if len(index)>0:
  30. print(clapoint[index])
  31. return clapoint[index]
  32. else:
  33. return None
  34. class Helmet:
  35. def __init__(self):
  36. self.flag = False
  37. def getflag(self,det,persondet,annotator,fence=0,point=None,names=None,rname=None,num=1):
  38. #print(type(det))
  39. self.flag = False
  40. for *xyxy, conf, cls in reversed(det):
  41. c = int(cls)
  42. labelname = names[c]
  43. if labelname in rname:
  44. if fence == 1:
  45. pointa = strtolstl(point)
  46. for poi in pointa:
  47. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  48. xyxy[3].cpu().item())
  49. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  50. xyxy[3].cpu().item())
  51. pt = [p1, p2]
  52. inflag = mat.Path(poi).contains_points(pt)
  53. if inflag.any():
  54. if persondet is None:
  55. self.flag = True
  56. # c = int(cls) # integer class
  57. # label = f'{self.names[c]} {conf:.2f}'
  58. label = None
  59. annotator.box_label(xyxy, None, color=(0, 0, 255))
  60. else:
  61. for person in persondet:
  62. personinflag = mat.Path(person).contains_points(pt)
  63. if personinflag.any():
  64. annotator.box_label(xyxy, None, color=(0, 0, 255))
  65. self.flag = True
  66. else:
  67. if persondet is None:
  68. self.flag = True
  69. # c = int(cls) # integer class
  70. # label = f'{self.names[c]} {conf:.2f}'
  71. label = None
  72. annotator.box_label(xyxy, None, color=(0, 0, 255))
  73. else:
  74. for person in persondet:
  75. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  76. xyxy[3].cpu().item())
  77. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  78. xyxy[3].cpu().item())
  79. pt = [p1, p2]
  80. personinflag = mat.Path(person).contains_points(pt)
  81. if personinflag.any():
  82. annotator.box_label(xyxy, None, color=(0, 0, 255))
  83. self.flag = True
  84. return self.flag
  85. class Uniform:
  86. def __init__(self):
  87. self.flag = False
  88. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  89. self.flag = False
  90. for *xyxy, conf, cls in reversed(det):
  91. c = int(cls)
  92. labelname = names[c]
  93. if labelname in rname:
  94. if fence == 1:
  95. pointa = strtolstl(point)
  96. for poi in pointa:
  97. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  98. xyxy[3].cpu().item())
  99. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  100. xyxy[3].cpu().item())
  101. pt = [p1, p2]
  102. inflag = mat.Path(poi).contains_points(pt)
  103. if inflag.any():
  104. if persondet is None:
  105. self.flag = True
  106. # c = int(cls) # integer class
  107. # label = f'{self.names[c]} {conf:.2f}'
  108. label = None
  109. annotator.box_label(xyxy, None, color=(0, 0, 255))
  110. else:
  111. for person in persondet:
  112. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  113. xyxy[3].cpu().item())
  114. p2 = (
  115. int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  116. xyxy[3].cpu().item())
  117. pt = [p1, p2]
  118. personinflag = mat.Path(person).contains_points(pt)
  119. if personinflag.any():
  120. annotator.box_label(xyxy, None, color=(0, 0, 255))
  121. self.flag = True
  122. else:
  123. if persondet is None:
  124. self.flag = True
  125. # c = int(cls) # integer class
  126. # label = f'{self.names[c]} {conf:.2f}'
  127. label = None
  128. annotator.box_label(xyxy, None, color=(0, 0, 255))
  129. else:
  130. for person in persondet:
  131. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  132. xyxy[3].cpu().item())
  133. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  134. xyxy[3].cpu().item())
  135. pt = [p1, p2]
  136. personinflag = mat.Path(person).contains_points(pt)
  137. if personinflag.any():
  138. annotator.box_label(xyxy, None, color=(0, 0, 255))
  139. self.flag = True
  140. return self.flag
  141. class Fall:
  142. def __init__(self):
  143. self.flag = False
  144. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  145. self.flag = False
  146. for *xyxy, conf, cls in reversed(det):
  147. c = int(cls)
  148. labelname = names[c]
  149. if labelname in rname:
  150. if fence == 1:
  151. pointa = strtolstl(point)
  152. for poi in pointa:
  153. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  154. xyxy[3].cpu().item())
  155. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  156. xyxy[3].cpu().item())
  157. pt = [p1, p2]
  158. inflag = mat.Path(poi).contains_points(pt)
  159. if inflag.any():
  160. if persondet is None:
  161. self.flag = True
  162. # c = int(cls) # integer class
  163. # label = f'{self.names[c]} {conf:.2f}'
  164. label = None
  165. annotator.box_label(xyxy, None, color=(0, 0, 255))
  166. else:
  167. for person in persondet:
  168. personinflag = mat.Path(person).contains_points(pt)
  169. if personinflag.any():
  170. annotator.box_label(xyxy, None, color=(0, 0, 255))
  171. self.flag = True
  172. else:
  173. if persondet is None:
  174. self.flag = True
  175. # c = int(cls) # integer class
  176. # label = f'{self.names[c]} {conf:.2f}'
  177. label = None
  178. annotator.box_label(xyxy, None, color=(0, 0, 255))
  179. else:
  180. for person in persondet:
  181. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  182. xyxy[3].cpu().item())
  183. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  184. xyxy[3].cpu().item())
  185. pt = [p1, p2]
  186. personinflag = mat.Path(person).contains_points(pt)
  187. if personinflag.any():
  188. annotator.box_label(xyxy, None, color=(0, 0, 255))
  189. self.flag = True
  190. return self.flag
  191. class Personcount:
  192. def __init__(self):
  193. self.flag = False
  194. def getflag(self, det,persondet, annotator, fence=0, point=None, names=None, rname=None,num=1):
  195. self.flag = False
  196. detnum = 0
  197. for *xyxy, conf, cls in reversed(det):
  198. c = int(cls)
  199. labelname = names[c]
  200. if labelname in rname:
  201. if fence == 1:
  202. pointa = strtolstl(point)
  203. for poi in pointa:
  204. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  205. xyxy[3].cpu().item())
  206. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  207. xyxy[3].cpu().item())
  208. pt = [p1, p2]
  209. inflag = mat.Path(poi).contains_points(pt)
  210. if inflag.any():
  211. if persondet is None:
  212. #self.flag = True
  213. detnum = detnum+1
  214. # c = int(cls) # integer class
  215. # label = f'{self.names[c]} {conf:.2f}'
  216. label = None
  217. annotator.box_label(xyxy, None, color=(0, 0, 255))
  218. else:
  219. for person in persondet:
  220. personinflag = mat.Path(person).contains_points(pt)
  221. if personinflag.any():
  222. detnum = detnum+1
  223. annotator.box_label(xyxy, None, color=(0, 0, 255))
  224. #self.flag = True
  225. else:
  226. if persondet is None:
  227. #self.flag = True
  228. detnum = detnum+1
  229. # c = int(cls) # integer class
  230. # label = f'{self.names[c]} {conf:.2f}'
  231. label = None
  232. annotator.box_label(xyxy, None, color=(0, 0, 255))
  233. else:
  234. for person in persondet:
  235. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  236. xyxy[3].cpu().item())
  237. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  238. xyxy[3].cpu().item())
  239. pt = [p1, p2]
  240. personinflag = mat.Path(person).contains_points(pt)
  241. if personinflag.any():
  242. detnum = detnum+1
  243. annotator.box_label(xyxy, None, color=(0, 0, 255))
  244. #self.flag = True
  245. if detnum >= num:
  246. self.flag = True
  247. return self.flag
  248. class Arm:
  249. def __init__(self):
  250. self.flag = False
  251. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  252. self.flag = False
  253. for *xyxy, conf, cls in reversed(det):
  254. c = int(cls)
  255. labelname = names[c]
  256. if labelname in rname:
  257. if fence == 1:
  258. pointa = strtolstl(point)
  259. for poi in pointa:
  260. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  261. xyxy[3].cpu().item())
  262. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  263. xyxy[3].cpu().item())
  264. pt = [p1, p2]
  265. inflag = mat.Path(poi).contains_points(pt)
  266. if inflag.any():
  267. if persondet is None:
  268. self.flag = True
  269. # c = int(cls) # integer class
  270. # label = f'{self.names[c]} {conf:.2f}'
  271. label = None
  272. annotator.box_label(xyxy, None, color=(0,0,255))
  273. else:
  274. for person in persondet:
  275. personinflag = mat.Path(person).contains_points(pt)
  276. if personinflag.any():
  277. annotator.box_label(xyxy, None, color=(0, 0, 255))
  278. self.flag = True
  279. else:
  280. if persondet is None:
  281. self.flag = True
  282. # c = int(cls) # integer class
  283. # label = f'{self.names[c]} {conf:.2f}'
  284. label = None
  285. annotator.box_label(xyxy, None, color=(0, 0, 255))
  286. else:
  287. for person in persondet:
  288. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  289. xyxy[3].cpu().item())
  290. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  291. xyxy[3].cpu().item())
  292. pt = [p1, p2]
  293. personinflag = mat.Path(person).contains_points(pt)
  294. if personinflag.any():
  295. annotator.box_label(xyxy, None, color=(0, 0, 255))
  296. self.flag = True
  297. return self.flag
  298. class Bag:
  299. def __init__(self):
  300. self.flag = False
  301. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  302. self.flag = False
  303. for *xyxy, conf, cls in reversed(det):
  304. c = int(cls)
  305. labelname = names[c]
  306. if labelname in rname:
  307. if fence == 1:
  308. pointa = strtolstl(point)
  309. for poi in pointa:
  310. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  311. xyxy[3].cpu().item())
  312. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  313. xyxy[3].cpu().item())
  314. pt = [p1, p2]
  315. inflag = mat.Path(poi).contains_points(pt)
  316. if inflag.any():
  317. if persondet is None:
  318. self.flag = True
  319. # c = int(cls) # integer class
  320. # label = f'{self.names[c]} {conf:.2f}'
  321. label = None
  322. annotator.box_label(xyxy, None, color=(0, 0, 255))
  323. else:
  324. for person in persondet:
  325. personinflag = mat.Path(person).contains_points(pt)
  326. if personinflag.any():
  327. annotator.box_label(xyxy, None, color=(0, 0, 255))
  328. self.flag = True
  329. else:
  330. if persondet is None:
  331. self.flag = True
  332. # c = int(cls) # integer class
  333. # label = f'{self.names[c]} {conf:.2f}'
  334. label = None
  335. annotator.box_label(xyxy, None, color=(0, 0, 255))
  336. else:
  337. for person in persondet:
  338. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  339. xyxy[3].cpu().item())
  340. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  341. xyxy[3].cpu().item())
  342. pt = [p1, p2]
  343. personinflag = mat.Path(person).contains_points(pt)
  344. if personinflag.any():
  345. annotator.box_label(xyxy, None, color=(0, 0, 255))
  346. self.flag = True
  347. return self.flag
  348. class Cross:
  349. def __init__(self):
  350. self.flag = False
  351. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  352. self.flag = False
  353. detnum = 0
  354. for *xyxy, conf, cls in reversed(det):
  355. c = int(cls)
  356. labelname = names[c]
  357. if labelname in rname:
  358. if fence == 1:
  359. pointa = strtolstl(point)
  360. for poi in pointa:
  361. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  362. xyxy[3].cpu().item())
  363. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  364. xyxy[3].cpu().item())
  365. pt = [p1, p2]
  366. inflag = mat.Path(poi).contains_points(pt)
  367. if inflag.any():
  368. if persondet is None:
  369. detnum = detnum+1
  370. self.flag = True
  371. # c = int(cls) # integer class
  372. # label = f'{self.names[c]} {conf:.2f}'
  373. label = None
  374. annotator.box_label(xyxy, None, color=(0, 0, 255))
  375. else:
  376. for person in persondet:
  377. personinflag = mat.Path(person).contains_points(pt)
  378. if personinflag.any():
  379. detnum = detnum+1
  380. annotator.box_label(xyxy, None, color=(0, 0, 255))
  381. self.flag = True
  382. else:
  383. if persondet is None:
  384. self.flag = True
  385. # c = int(cls) # integer class
  386. # label = f'{self.names[c]} {conf:.2f}'
  387. detnum = detnum+1
  388. label = None
  389. annotator.box_label(xyxy, None, color=(0, 0, 255))
  390. else:
  391. for person in persondet:
  392. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  393. xyxy[3].cpu().item())
  394. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  395. xyxy[3].cpu().item())
  396. pt = [p1, p2]
  397. personinflag = mat.Path(person).contains_points(pt)
  398. if personinflag.any():
  399. detnum = detnum +1
  400. annotator.box_label(xyxy, None, color=(0, 0, 255))
  401. self.flag = True
  402. return self.flag
  403. class Extinguisher:
  404. def __init__(self):
  405. self.flag = False
  406. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  407. self.flag = False
  408. for *xyxy, conf, cls in reversed(det):
  409. c = int(cls)
  410. labelname = names[c]
  411. if labelname in rname:
  412. if fence == 1:
  413. pointa = strtolstl(point)
  414. for poi in pointa:
  415. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  416. xyxy[3].cpu().item())
  417. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  418. xyxy[3].cpu().item())
  419. pt = [p1, p2]
  420. inflag = mat.Path(poi).contains_points(pt)
  421. if inflag.any():
  422. if persondet is None:
  423. self.flag = True
  424. # c = int(cls) # integer class
  425. # label = f'{self.names[c]} {conf:.2f}'
  426. label = None
  427. annotator.box_label(xyxy, None, color=(0, 0, 255))
  428. else:
  429. for person in persondet:
  430. personinflag = mat.Path(person).contains_points(pt)
  431. if personinflag.any():
  432. annotator.box_label(xyxy, None, color=(0, 0, 255))
  433. self.flag = True
  434. else:
  435. if persondet is None:
  436. self.flag = True
  437. # c = int(cls) # integer class
  438. # label = f'{self.names[c]} {conf:.2f}'
  439. label = None
  440. annotator.box_label(xyxy, None, color=(0, 0, 255))
  441. else:
  442. for person in persondet:
  443. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  444. xyxy[3].cpu().item())
  445. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  446. xyxy[3].cpu().item())
  447. pt = [p1, p2]
  448. personinflag = mat.Path(person).contains_points(pt)
  449. if personinflag.any():
  450. annotator.box_label(xyxy, None, color=(0, 0, 255))
  451. self.flag = True
  452. return self.flag
  453. class Persontre:
  454. def __init__(self):
  455. self.flag = False
  456. self.classifier_model = torch.load('/home/h3c/yolo/persontrecls.pt')
  457. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1,im0=None):
  458. self.flag = False
  459. dirp = {}
  460. dirf = {}
  461. for *xyxy, conf, cls in reversed(det):
  462. c = int(cls)
  463. labelname = names[c]
  464. if labelname in rname:
  465. if fence == 1:
  466. pointa = strtolstl(point)
  467. for poi in pointa:
  468. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  469. xyxy[3].cpu().item())
  470. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  471. xyxy[3].cpu().item())
  472. pt = [p1, p2]
  473. inflag = mat.Path(poi).contains_points(pt)
  474. if inflag.any():
  475. if persondet is None:
  476. self.flag = True
  477. # c = int(cls) # integer class
  478. # label = f'{self.names[c]} {conf:.2f}'
  479. label = None
  480. if c==0:
  481. dirp.setdefault(0,[])
  482. dirp[0].append(xyxy)
  483. elif c in [1,2]:
  484. dirp.setdefault(1,[])
  485. dirp[1].append(xyxy)
  486. dirf.setdefault(1,[])
  487. dirf[1].append(xyxy)
  488. elif c==3:
  489. dirp.setdefault(1,[])
  490. dirp[1].append(xyxy)
  491. elif c==4:
  492. dirf.setdefault(0,[])
  493. dirf[0].append(xyxy)
  494. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  495. else:
  496. for person in persondet:
  497. personinflag = mat.Path(person).contains_points(pt)
  498. if personinflag.any():
  499. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  500. #self.flag = True
  501. if c==0:
  502. dirp.setdefault(0,[])
  503. dirp[0].append(xyxy)
  504. elif c in [1,2]:
  505. dirp.setdefault(1,[])
  506. dirp[1].append(xyxy)
  507. dirf.setdefault(1,[])
  508. dirf[1].append(xyxy)
  509. elif c==3:
  510. dirp.setdefault(1,[])
  511. dirp[1].append(xyxy)
  512. elif c==4:
  513. dirf.setdefault(0,[])
  514. dirf[0].append(xyxy)
  515. else:
  516. if persondet is None:
  517. #self.flag = True
  518. # c = int(cls) # integer class
  519. # label = f'{self.names[c]} {conf:.2f}'
  520. #label = None
  521. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  522. if c==0:
  523. dirp.setdefault(0,[])
  524. dirp[0].append(xyxy)
  525. elif c in [1,2]:
  526. dirp.setdefault(1,[])
  527. dirp[1].append(xyxy)
  528. dirf.setdefault(1,[])
  529. dirf[1].append(xyxy)
  530. elif c==3:
  531. dirp.setdefault(1,[])
  532. dirp[1].append(xyxy)
  533. elif c==4:
  534. dirf.setdefault(0,[])
  535. dirf[0].append(xyxy)
  536. else:
  537. for person in persondet:
  538. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  539. xyxy[3].cpu().item())
  540. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  541. xyxy[3].cpu().item())
  542. pt = [p1, p2]
  543. personinflag = mat.Path(person).contains_points(pt)
  544. if personinflag.any():
  545. #annotator.box_label(xyxy, None, color=(0, 0, 255))
  546. #self.flag = True
  547. if c==0:
  548. dirp.setdefault(0,[])
  549. dirp[0].append(xyxy)
  550. elif c in [1,2]:
  551. dirp.setdefault(1,[])
  552. dirp[1].append(xyxy)
  553. dirf.setdefault(1,[])
  554. dirf[1].append(xyxy)
  555. elif c==3:
  556. dirp.setdefault(1,[])
  557. dirp[1].append(xyxy)
  558. elif c==4:
  559. dirf.setdefault(0,[])
  560. dirf[0].append(xyxy)
  561. if len(dirp.keys()) == 2:
  562. claimg = []
  563. clapoint = []
  564. for person in dirp[0]:
  565. for other in dirp[1]:
  566. iou, newxyxy = compute_IOU(person, other)
  567. if iou>0.1:
  568. print(newxyxy)
  569. imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
  570. imgtmp = imgtmp[...,::-1]
  571. imgtmp = Image.fromarray(imgtmp)
  572. imgten1 = test(imgtmp)
  573. claimg.append(imgten1)
  574. clapoint.append((newxyxy))
  575. result = clapre(self.classifier_model,claimg,clapoint)
  576. #imgten = imgten1[None]
  577. #imgten = imgten.to(0)
  578. #result = modelcla(imgten)
  579. #result = F.softmax(result, dim=1)
  580. #cla = result.argmax(1)
  581. if result is not None:
  582. self.flag = True
  583. for res in result:
  584. print(res)
  585. annotator.box_label(res, None, color=(0,0,255))
  586. if len(dirp.keys()) == 2:
  587. claimg = []
  588. clapoint = []
  589. for person in dirp[0]:
  590. for other in dirp[1]:
  591. iou, newxyxy = compute_IOU(person, other)
  592. if iou>0.1:
  593. print(newxyxy)
  594. imgtmp = im0[int(newxyxy[1]):int(newxyxy[3]),int(newxyxy[0]):int(newxyxy[2])]
  595. imgtmp = imgtmp[...,::-1]
  596. imgtmp = Image.fromarray(imgtmp)
  597. imgten1 = test(imgtmp)
  598. claimg.append(imgten1)
  599. clapoint.append((newxyxy))
  600. result = clapre(self.classifier_model,claimg,clapoint)
  601. #imgten = imgten1[None]
  602. #imgten = imgten.to(0)
  603. #result = modelcla(imgten)
  604. #result = F.softmax(result, dim=1)
  605. #cla = result.argmax(1)
  606. if result is not None:
  607. self.flag = True
  608. for res in result:
  609. print(res)
  610. annotator.box_label(res, None, color=(0,0,255))
  611. return self.flag
  612. class Danager:
  613. def __init__(self):
  614. self.flag = False
  615. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  616. self.flag = False
  617. for *xyxy, conf, cls in reversed(det):
  618. c = int(cls)
  619. labelname = names[c]
  620. if labelname in rname:
  621. if fence == 1:
  622. pointa = strtolstl(point)
  623. for poi in pointa:
  624. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  625. xyxy[3].cpu().item())
  626. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  627. xyxy[3].cpu().item())
  628. pt = [p1, p2]
  629. inflag = mat.Path(poi).contains_points(pt)
  630. if inflag.any():
  631. if persondet is None:
  632. self.flag = True
  633. # c = int(cls) # integer class
  634. # label = f'{self.names[c]} {conf:.2f}'
  635. label = None
  636. annotator.box_label(xyxy, None, color=(0, 0, 255))
  637. else:
  638. for person in persondet:
  639. personinflag = mat.Path(person).contains_points(pt)
  640. if personinflag.any():
  641. annotator.box_label(xyxy, None, color=(0, 0, 255))
  642. self.flag = True
  643. else:
  644. if persondet is None:
  645. self.flag = True
  646. # c = int(cls) # integer class
  647. # label = f'{self.names[c]} {conf:.2f}'
  648. label = None
  649. annotator.box_label(xyxy, None, color=(0, 0, 255))
  650. else:
  651. for person in persondet:
  652. p1 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3),
  653. xyxy[3].cpu().item())
  654. p2 = (int(xyxy[0].cpu().item() + (xyxy[2].cpu().item() - xyxy[0].cpu().item()) / 3 * 2),
  655. xyxy[3].cpu().item())
  656. pt = [p1, p2]
  657. personinflag = mat.Path(person).contains_points(pt)
  658. if personinflag.any():
  659. annotator.box_label(xyxy, None, color=(0, 0, 255))
  660. self.flag = True
  661. return self.flag
  662. class CarHelmetBelt:
  663. def __init__(self):
  664. self.flag = False
  665. def selectNoBeltPerson(self, person_objs, belt_objs):
  666. objs = []
  667. polygon_person = [Polygon(
  668. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in person_objs]
  669. polygon_belt = [Polygon(
  670. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in belt_objs]
  671. for person_obj, person in zip(person_objs, polygon_person):
  672. with_belt = False
  673. for belt in polygon_belt:
  674. if person.intersection(belt).area / belt.area > 0.5:
  675. with_belt = True
  676. break
  677. if not with_belt:
  678. objs.append(person_obj)
  679. return objs
  680. def selectWithPersonHead(self, person_objs, head_objs):
  681. objs = []
  682. polygon_person = [Polygon(
  683. [(left, top), (right, top), (right, top + (bottom - top)/2), (left, top + (bottom - top)/2)]) for left, top, right, bottom, _, _ in person_objs]
  684. polygon_head = [Polygon(
  685. [(left, top), (right, top), (right, bottom), (left, bottom)]) for left, top, right, bottom, _, _ in head_objs]
  686. for head_obj, head in zip(head_objs, polygon_head):
  687. with_person = False
  688. for person in polygon_person:
  689. if person.intersection(head).area / head.area > 0.5:
  690. with_person = True
  691. break
  692. if with_person:
  693. objs.append(head_obj)
  694. return objs
  695. def getflag(self, det, persondet,annotator, fence=0, point=None, names=None, rname=None,num=1):
  696. self.flag = False
  697. results = Results(annotator.result(),path=None,names=names,boxes=det)
  698. person_objs = []
  699. head_objs = []
  700. belt_objs = []
  701. polygon_area = strtolstl(point)[0]
  702. for result in results:
  703. boxes = result.boxes
  704. for box in boxes:
  705. left, top, right, bottom = box.xyxy.cpu().numpy().tolist()[0]
  706. polygon_box = Polygon([(left, top), (right, top), (right, bottom), (left, bottom)])
  707. intersection_areas = [polygon_area.intersection(polygon_box).area for polygon_area in self.polygon_areas ]
  708. if sum(intersection_areas) == 0:
  709. continue
  710. intersection_areas_ratio = sorted([intersection_area / polygon_box.area for intersection_area in intersection_areas])
  711. if intersection_areas_ratio[-1] < 0.9:
  712. continue
  713. conf = box.conf.cpu().numpy().tolist()[0]
  714. cls = box.cls.cpu().numpy().tolist()[0]
  715. if cls == 0:
  716. person_objs.append([left, top, right, bottom, conf, self.names[cls]])
  717. elif cls == 1:
  718. head_objs.append([left, top, right, bottom, conf, self.names[cls]])
  719. elif cls == 3:
  720. belt_objs.append([left, top, right, bottom, conf, self.names[cls]])
  721. illegal_objs = self.selectNoBeltPerson(person_objs, belt_objs) + self.selectWithPersonHead(person_objs, head_objs)
  722. if illegal_objs>0:
  723. for obj in illegal_objs:
  724. annotator.box_label(obj[:4], None, color=(0, 0, 255))
  725. self.flag = True