detectopencvthrrun1230.py 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. # YOLOv5 🚀 by Ultralytics, AGPL-3.0 license
  2. """
  3. Run YOLOv5 detection inference on images, videos, directories, globs, YouTube, webcam, streams, etc.
  4. Usage - sources:
  5. $ python detect.py --weights yolov5s.pt --source 0 # webcam
  6. img.jpg # image
  7. vid.mp4 # video
  8. screen # screenshot
  9. path/ # directory
  10. list.txt # list of images
  11. list.streams # list of streams
  12. 'path/*.jpg' # glob
  13. 'https://youtu.be/Zgi9g1ksQHc' # YouTube
  14. 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
  15. Usage - formats:
  16. $ python detect.py --weights yolov5s.pt # PyTorch
  17. yolov5s.torchscript # TorchScript
  18. yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
  19. yolov5s_openvino_model # OpenVINO
  20. yolov5s.engine # TensorRT
  21. yolov5s.mlmodel # CoreML (macOS-only)
  22. yolov5s_saved_model # TensorFlow SavedModel
  23. yolov5s.pb # TensorFlow GraphDef
  24. yolov5s.tflite # TensorFlow Lite
  25. yolov5s_edgetpu.tflite # TensorFlow Edge TPU
  26. yolov5s_paddle_model # PaddlePaddle
  27. """
  28. import matplotlib.path as mat
  29. import requests
  30. import argparse
  31. import os
  32. import platform
  33. import sqlite3
  34. import sys
  35. import threading
  36. import time
  37. from pathlib import Path
  38. import signal
  39. import torch
  40. from concurrent.futures import ThreadPoolExecutor
  41. from concurrent.futures import ProcessPoolExecutor
  42. from multiprocessing import Process,Manager,Value
  43. from multiprocessing import Queue
  44. from multiprocessing import set_start_method
  45. import multiprocessing
  46. import multiprocessing as mp
  47. import numpy as np
  48. import platform
  49. import pathlib
  50. from collections import defaultdict, deque
  51. #import websockets
  52. import asyncio
  53. FILE = Path(__file__).resolve()
  54. ROOT = FILE.parents[0] # YOLOv5 root directory
  55. if str(ROOT) not in sys.path:
  56. sys.path.append(str(ROOT)) # add ROOT to PATH
  57. ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
  58. import math
  59. from models.common import DetectMultiBackend
  60. from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages,LoadStreams, LoadStreamsSQLNEWN,LoadStreamsSQL,LoadStreamsSQLNRERT,LoadStreamsVEight,LoadStreamsSQLTN
  61. from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
  62. increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh,strtolst,strtolstl,apply_classifier1,task)
  63. from utils.plots import Annotator, colors, save_one_box
  64. from utils.torch_utils import select_device, smart_inference_mode
  65. #from testpool import func1,TestA
  66. from ultralytics import YOLO
  67. from ultralytics.trackers.bot_sort import BOTSORT
  68. from ultralytics.utils.checks import check_yaml
  69. from ultralytics.utils import IterableSimpleNamespace, yaml_load, ops
  70. from ultralytics.nn.autobackend import AutoBackend
  71. from ultralytics.utils.ops import non_max_suppression
  72. from ultralytics.engine.results import Results
  73. # def my_handler(signum, frame):
  74. # exit(0)
  75. #url = "http://36.7.84.146:18802/ai-service/open/api/operate/upload"
  76. plt = platform.system()
  77. if plt != 'Windows':
  78. pathlib.WindowsPath = pathlib.PosixPath
  79. urlhead = "http://172.19.152.231"
  80. url = f"{urlhead}/open/api/operate/upload"
  81. urlele = f"{urlhead}/open/api/operate/fence"
  82. urlperson = f"{urlhead}/open/api/operate/getPersonLimitNum"
  83. urlt = f"{urlhead}/open/api/operate/taskList"
  84. urla = f"{urlhead}/open/api/operate/algorithmList"
  85. weburl = f"ws://36.7.84.146:28801/websocket/device"
  86. urlrtsp = f"{urlhead}/open/api/operate/previewURLs"
  87. personcountdir = {}
  88. algdir = {'0':'helmet','8':'danager','10':'uniform','14':'smoke','16':'fire','21':'cross','25':'fall','29':'occupancy','30':'liquid','31':'pressure','32':'sleep','33':'conveyor','34':'personcount','35':'gloves','36':'sit','37':'other','38':'duty','98':'face','51':'run'}
  89. modellabeldir = {'0':'head','8':'person','10':'other','14':'smoke','16':'fire','21':'cross','25':'fall','29':'car','30':'liquid','31':'pressure','32':'sleep','33':'conveyor','34':'personcount','35':'gloves','36':'sit','37':'other','38':'person','98':'face','51':'person'}
  90. modelalgdir = {'helmet': '0','danager': '8','uniform': '10','smoke': '14','fire': '16','cross': '21','fall': '25','occupancy': '29','liquid': '30','pressure': '31','sleep': '32','conveyor': '33','personcount': '34','gloves': '35','sit': '36','other': '37','duty': '38','face': '98','run': '51'}
  91. algmodel = {}
  92. for key,value in algdir.items():
  93. algmodel[value] = key
  94. def on_predict_postprocess_end(predictor: object, persist: bool = False,im0s=None) -> None:
  95. """
  96. Postprocess detected boxes and update with object tracking.
  97. Args:
  98. predictor (object): The predictor object containing the predictions.
  99. persist (bool): Whether to persist the trackers if they already exist.
  100. Examples:
  101. Postprocess predictions and update with tracking
  102. >>> predictor = YourPredictorClass()
  103. >>> on_predict_postprocess_end(predictor, persist=True)
  104. """
  105. for i in range(len(im0s)):
  106. tracker = predictor.trackers[i]
  107. det = predictor.results[i].boxes.cpu().numpy()
  108. #print(f'det = {det}')
  109. if len(det) == 0:
  110. continue
  111. #print(f'predet = {type(det)}')
  112. tracks = tracker.update(det, im0s[i])
  113. if len(tracks) == 0:
  114. continue
  115. idx = tracks[:, -1].astype(int)
  116. predictor.results[i] = predictor.results[i][idx]
  117. update_args = {"boxes": torch.as_tensor(tracks[:, :-1])}
  118. predictor.results[i].update(**update_args)
  119. def map_to_ellipse(position):
  120. x, y = position
  121. center_x = 640
  122. center_y = 360
  123. a = 580
  124. b = 280
  125. x_norm = x / 1280
  126. y_norm = y / 720
  127. d_norm = math.sqrt((x_norm - 0.5) ** 2 + (y_norm - 0.5) ** 2)
  128. theta_norm = math.atan2(y_norm - 0.5, x_norm - 0.5)
  129. f = d_norm
  130. a_new = a * f
  131. b_new = b * f
  132. bias_x = center_x + a_new * math.cos(theta_norm)
  133. bias_y = center_y + b_new * math.sin(theta_norm)
  134. return np.array([bias_x, bias_y])
  135. class YoloOpt:
  136. def __init__(self, weights=ROOT / 'yolov5s.pt',source=ROOT / 'data/images',data=ROOT / 'data/coco128.yaml',
  137. imgsz=(640,640),
  138. conf_thres=0.80,
  139. iou_thres=0.45,
  140. max_det=1000,
  141. device='',
  142. view_img=False,
  143. save_txt=False,
  144. save_conf=False,
  145. save_crop=False,
  146. nosave=True,
  147. classes=None,
  148. agnostic_nms=False,
  149. augment=False,
  150. visualize=False,
  151. update=False,
  152. project=ROOT / 'runs/detect',
  153. name='exp',
  154. exist_ok=False,
  155. line_thickness=1,
  156. hide_labels=False,
  157. hide_conf=False,
  158. half=False,
  159. dnn=False,
  160. vid_stride=10,
  161. classify=False):
  162. self.weights = weights # 权重文件地址
  163. self.source = source # 待识别的图像
  164. self.data = data
  165. if imgsz is None:
  166. self.imgsz = (640, 640)
  167. self.imgsz = imgsz # 输入图片的大小,默认 (640,640)
  168. self.conf_thres = conf_thres # object置信度阈值 默认0.25 用在nms中
  169. self.iou_thres = iou_thres # 做nms的iou阈值 默认0.45 用在nms中
  170. self.device = device # 执行代码的设备,由于项目只能用 CPU,这里只封装了 CPU 的方法
  171. self.view_img = view_img # 是否展示预测之后的图片或视频 默认False
  172. self.classes = classes # 只保留一部分的类别,默认是全部保留
  173. self.agnostic_nms = agnostic_nms # 进行NMS去除不同类别之间的框, 默认False
  174. self.augment = augment # augmented inference TTA测试时增强/多尺度预测,可以提分
  175. self.update = update # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
  176. self.exist_ok = exist_ok # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
  177. self.project = project # 保存测试日志的参数,本程序没有用到
  178. self.name = name # 每次实验的名称,本程序也没有用到
  179. self.max_det = max_det
  180. self.save_txt = save_txt
  181. self.save_conf= save_conf
  182. self.save_crop= save_crop
  183. self.nosave = nosave
  184. self.visualize = visualize
  185. self.line_thickness = line_thickness
  186. self.hide_labels = hide_labels
  187. self.hide_conf = hide_conf
  188. self.half = half
  189. self.dnn = dnn
  190. self.vid_stride = vid_stride
  191. self.classify = classify
  192. class Detect:
  193. def __init__(self, weights = ROOT / 'yolov5s.pt' , imgsz=(640,640),source="changshusql1103.db",classify=False,conf_thres=0.80,device='',channelsl=''):
  194. print(f'detectweights = {weights}')
  195. self.opt = YoloOpt(weights=weights, imgsz=imgsz,source=source,classify=classify,conf_thres=conf_thres,device=device)
  196. self.source = str(self.opt.source)
  197. self.save_img = not self.opt.nosave and not source.endswith('.txt') # save inference images
  198. is_file = Path(self.source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
  199. is_url = self.source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
  200. self.webcam = True
  201. screenshot = self.source.lower().startswith('screen')
  202. if is_url and is_file:
  203. self.source = check_file(self.source) # download
  204. self.save_dir = increment_path(Path(self.opt.project) / self.opt.name, exist_ok=self.opt.exist_ok) # increment run
  205. #self.save_dir = self.save_dir / Path(self.opt.weights).stem
  206. #self.save_dir.mkdir(parents=True, exist_ok=True)
  207. (self.save_dir / 'labels' if self.opt.save_txt else self.save_dir).mkdir(parents=True, exist_ok=True) # make dir
  208. #self.imgsz = check_img_size(self.opt.imgsz, s=self.stride)
  209. # self.readchannel = self.readpoint()
  210. #print(self.imgsz)
  211. self.updatetime = time.time()
  212. #self.filetime = os.path.getmtime(self.opt.weights)
  213. bs = 1 # batch_size
  214. if self.webcam:
  215. #self.view_img = check_imshow(warn=True)
  216. self.view_img = False
  217. # dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
  218. tt= 0
  219. numworks = 1
  220. self.dataset = LoadStreamsSQLTN(channelsl, source, img_size=640,
  221. auto=True, vid_stride=10, tt=tt, numworks=numworks)
  222. # bs = len(dataset)
  223. elif screenshot:
  224. dataset = LoadScreenshots(self.source, img_size=self.imgsz, stride=self.stride, auto=self.pt)
  225. else:
  226. self.dataset = LoadImages(self.source, img_size=self.imgsz, stride=self.stride, auto=self.pt, vid_stride=self.opt.vid_stride)
  227. self.speed_threshold = 60
  228. self.high_velocity_count_threshold = 20
  229. # t1 = threading.Thread(target=self.load,daemon=True)
  230. # t1.start()
  231. @smart_inference_mode()
  232. def infer(self,weights,classify,conf_thres,device,runmodel):
  233. tracker = check_yaml(r'/home/h3c/.local/lib/python3.8/site-packages/ultralytics/cfg/trackers/botsort.yaml')
  234. cfg = IterableSimpleNamespace(**yaml_load(tracker))
  235. device = select_device(device)
  236. print("loadmodel device{device}")
  237. model = AutoBackend(weights, device=device, dnn=self.opt.dnn, data=self.opt.data,
  238. fp16=self.opt.half)
  239. stride, names, pt = model.stride, model.names, model.pt
  240. imgsz = check_img_size(self.opt.imgsz, s=stride)
  241. model.warmup(imgsz=(1, 3, *imgsz))
  242. trackers = []
  243. for _ in range(len(self.dataset)):
  244. tracker = BOTSORT(args=cfg, frame_rate=30)
  245. trackers.append(tracker)
  246. model.trackers = trackers
  247. # stride, names, pt = model.stride, model.names, model.pt
  248. if classify:
  249. classifier_model = torch.load(f"{Path(weights).stem}cls.pt")
  250. classifier_model = classifier_model.to(device)
  251. classifier_model.eval()
  252. print('classify--------------------------------------------------------------------')
  253. #imgsz = check_img_size(self.opt.imgsz, s=stride)
  254. #model.warmup(imgsz=(1, 3, *imgsz))
  255. readchannel,dirmodel = self.readpoint(weights)
  256. #print(imgsz)
  257. updatetime = time.time()
  258. filetime = os.path.getmtime(weights)
  259. #selfreadpoint();
  260. pretime = time.time()
  261. seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
  262. #
  263. # print ("数据库打开成功")
  264. #async with websockets.connect(uri) as websocket:
  265. # for key in dirmodel.keys():
  266. # dirmodel[key]['websoc'] = await websockets.connect(dirmodel[key]['web'])
  267. for path, im, im0s, vid_cap, s, videotime,channels in self.dataset:
  268. #print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
  269. #if len(queue) == 0:
  270. # print('queuezero')
  271. # time.sleep(0.01)
  272. #print(f'timee = {time.time()-timee}')
  273. hour = time.localtime(time.time()).tm_hour
  274. if hour not in range(7,18):
  275. time.sleep(30)
  276. continue
  277. if time.time()-pretime>300:
  278. ret,_ = self.readpoint(weights)
  279. # for key in dirmodel.keys():
  280. # dirmodel[key]['websoc'] = await websockets.connect(dirmodel[key]['web'])
  281. pretime = time.time()
  282. if not ret:
  283. print(f'{Path(weights).stem} {runmodel}')
  284. runmodel.pop(Path(weights).stem)
  285. print(f'{Path(weights).stem} {runmodel}')
  286. break
  287. algchannel = list(dirmodel.keys())
  288. #print(algaddress)
  289. print(path)
  290. algchannel = np.array(algchannel)
  291. #pathnp = np.array(path)
  292. channelsnp = np.array(channels)
  293. algindex = np.where(np.in1d(channelsnp, algchannel))[0]
  294. algindex = list(algindex)
  295. if len(algindex)==0:
  296. continue
  297. path = np.array(path)
  298. path = path[algindex]
  299. path = path.tolist()
  300. channels = np.array(channels)
  301. channels = channels[algindex]
  302. channels = channels.tolist()
  303. #im = im[algindex]
  304. #print(im0s.shape)
  305. im = im[algindex]
  306. #if len(im0s)==1:
  307. # im0tmp = np.asarray(im0s)
  308. if len(im0s)==1:
  309. im0tmp = np.asarray(im0s)
  310. else:
  311. im0tmp = np.asarray(im0s,dtype = object)
  312. #print(f'im0tmplen = {len(im0tmp)}')
  313. #if len(im0tmp)>1:
  314. #except Exception:
  315. #im0tmp = np.asarray(im0s)
  316. #print('no object')
  317. im0s = im0tmp
  318. im0s = im0s[algindex]
  319. # im0s = im0s.tolist()
  320. print(f'algindex = {algindex}')
  321. print(f'im0s ={im0s[0].shape}')
  322. videotime = np.array(videotime)
  323. videotime = videotime[algindex]
  324. videotime = tuple(map(tuple, videotime))
  325. # if self.contentid[0][3] == 1 :
  326. # dataset.close()
  327. # print('newstreaming=', self.contentid[0][3])
  328. # conn = sqlite3.connect(self.source)
  329. # c = conn.cursor()
  330. # c.execute("UPDATE CHANGESTREAM set streamimg = 0 , addstream=0,delstream=0 where modelname= (?)",(Path(self.opt.weights).stem,))
  331. # print(123)
  332. # conn.commit()
  333. # c.close()
  334. # conn.close()
  335. # print('opencv1')
  336. # cv2.destroyAllWindows()
  337. # print('opencv')
  338. # break
  339. # else:
  340. # print('nonewstreaming=', self.contentid[0][3])
  341. with dt[0]:
  342. im = torch.from_numpy(im).to(model.device)
  343. im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
  344. im /= 255 # 0 - 255 to 0.0 - 1.0
  345. if len(im.shape) == 3:
  346. im = im[None] # expand for batch dim
  347. # Inference
  348. with dt[1]:
  349. visualize = False
  350. #print('error')
  351. print(im.shape)
  352. pred =model(im, augment=self.opt.augment, visualize=visualize)
  353. if classify:
  354. self.postprocess(pred, path, im0s, im, s, videotime,channels,names,dt,classify,updatetime,dirmodel,weights,filetime,classifier_model,model)
  355. #pass
  356. else:
  357. self.postprocess(pred, path, im0s, im, s, videotime, channels, names, dt, classify, updatetime,
  358. dirmodel, weights, filetime, None,model)
  359. #await self.postprocess(pred, path, im0s, im, s, videotime, channels, names, dt, classify, updatetime,
  360. #dirmodel, weights, filetime, None)
  361. # print(f'predshape= {')
  362. # NMS
  363. #processlist = []
  364. #for i in range(3):
  365. # process = Process(target=self.postprocess,args=(pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s))
  366. # process = Process(target=self.preprocess)
  367. # process.start()
  368. # processlist.append(process)
  369. #for j in processlist:
  370. # j.join()
  371. #with ProcessPoolExecutor(3) as ppool:
  372. #for i in range(3):
  373. # print('hello')
  374. #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
  375. #ppool.submit(func1, '张三', i)
  376. #ppool.submit(self.preprocess)
  377. #self.postprocess(pred, path, im0s, dataset, im, s)
  378. #process = Process(target=self.postprocess, args=(pred, path, im0s, dataset, im, s))
  379. #self.postprocess(pred, path, im0s, im, s,videotime)
  380. #process.start()
  381. #def preprocess(self):
  382. # print('preprocess-----------------------------------------------')
  383. def postprocess(self, pred, path, im0s, im, s,videotime,channels,names,dt,classify,updatetime,dirmodel,weights,filetime,classifier_model,model):
  384. seen = 0
  385. # dt = (Profile(), Profile(), Profile())
  386. print(f'senn = {seen}')
  387. windows = []
  388. with dt[2]:
  389. #print(f'cropshape={pred.shape}')
  390. pred = non_max_suppression(pred, 0.6, self.opt.iou_thres, 0,
  391. self.opt.agnostic_nms, max_det=self.opt.max_det)
  392. if classify:
  393. pred = apply_classifier1(pred, classifier_model, im, im0s,Path(weights).stem)
  394. # Second-stage classifier (optional)
  395. # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
  396. # Process predictions
  397. #print(f'predshape={pred.shape}')
  398. model.results = []
  399. for i, det in enumerate(pred): # per image
  400. current_time = time.time()
  401. if time.time()-updatetime>300:
  402. if filetime !=os.path.getmtime(weights):
  403. device = select_device(self.opt.device)
  404. print("load new load")
  405. model = YOLO(weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  406. stride, names, pt = model.stride, model.names, model.pt
  407. dataele = {
  408. "algorithmCode": dirmodel[channels[i]]['classindex'],
  409. "algorithmIp":dirmodel[channels[i]]['algip'],
  410. "channel":dirmodel[channels[i]]['channel']
  411. }
  412. try:
  413. resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
  414. resultele = resultele.split(',||')
  415. resultele = tuple(resultele)
  416. point = '%s:'*len(resultele) %resultele
  417. dirmodel[channels[i]]['point'] = point[:-2]
  418. except Exception:
  419. print('post error')
  420. if Path(weights).stem == 'personcount':
  421. try:
  422. resultper = requests.post(url=urlperson,data=dataele).json()['data']
  423. personcountdir[channels[i]] = resultper
  424. except Exception:
  425. print('urlpersonerror')
  426. #try:
  427. if modelalgdir[Path(weights).stem]!='0':
  428. print(modelalgdir[Path(weights).stem])
  429. rea = requests.post(url=urla,data={'algorithmCode':modelalgdir[Path(weights).stem]}).json()['data']
  430. con = rea[0]['confidence']
  431. conf_thres = con
  432. else:
  433. conf_thres = 0.25
  434. #except Exception:
  435. #print('posturlaerror')
  436. updatetime = time.time()
  437. seen += 1
  438. if self.webcam: # batch_size >= 1
  439. p, im0 = path[i], im0s[i].copy()
  440. im0 = np.uint8(im0)
  441. s += f'{i}: '
  442. else:
  443. p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
  444. p = Path(p) # to Path
  445. #save_path = str(self.save_dir / p.name) # im.jpg
  446. #txt_path = str(self.save_dir / 'labels' / p.stem) + (
  447. # '' #if dataset.mode == 'image' else f'_{frame}') # im.txt
  448. s += '%gx%g ' % im.shape[2:] # print string
  449. gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
  450. imc = im0.copy() # for save_crop
  451. annotator = Annotator(im0, line_width=self.opt.line_thickness, example=str(names))
  452. flag = False
  453. for c in det[:, 5].unique():
  454. n = (det[:, 5] == c).sum() # detections per class
  455. s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
  456. det[:, :4] = ops.scale_boxes(im.shape[2:], det[:, :4], im0s[i].shape)
  457. # print(pred[:,:4])
  458. model.results.append(Results(im0, path=path[i], names={0: 'person'}, boxes=det))
  459. on_predict_postprocess_end(model, True, im0s)
  460. for i,det in enumerate(model.results):
  461. flag = False
  462. print(f'++++++++++++++++++++++{det.boxes}')
  463. if det.boxes and det.boxes.id is not None:
  464. print('-----------------------------------')
  465. if Path(weights).stem == 'personcount':
  466. personnum = 0
  467. if Path(weights).stem == 'persontre':
  468. tredir = {}
  469. #flag = True
  470. # Rescale boxes from img_size to im0 size
  471. boxes = det.boxes.xywh.cpu()
  472. track_ids = det.boxes.id.int().cpu().tolist()
  473. # Print results
  474. # for c in det[:, 5].unique():
  475. # n = (det[:, 5] == c).sum() # detections per class
  476. # s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
  477. # Write results
  478. #message = '['
  479. for box, track_id in zip(boxes, track_ids):
  480. x, y, w, h = box
  481. if dirmodel[channels[i]]['fence'] == 1:
  482. c = int(cls)
  483. labelname = names[c]
  484. if labelname in dirmodel[channels[i]]['label']:
  485. point = strtolstl(dirmodel[channels[i]]['point'])
  486. for poi in point:
  487. p1 = (int(xyxy[0].cpu().item()+(xyxy[2].cpu().item()-xyxy[0].cpu().item())/3), xyxy[3].cpu().item())
  488. p2 = (int(xyxy[0].cpu().item()+(xyxy[2].cpu().item()-xyxy[0].cpu().item())/3*2), xyxy[3].cpu().item())
  489. p3 = (int(xyxy[0])+(int(xyxy[2])-int(xyxy[0]))/2,int(xyxy[1])+(int(xyxy[3])-int(xyxy[1]))/2)
  490. pt = [p1,p2]
  491. if Path(weights).stem == 'danager':
  492. pt = [p3]
  493. inflag = mat.Path(poi).contains_points(pt)
  494. if inflag.any():
  495. x, y, x2, y2 = xyxy
  496. w = x2 - x
  497. h = y2 - y
  498. flag = True
  499. #message = message + '{' + 'x:' + str(int(x)) + ',y:' + str(int(y)) + ',w:' + str(
  500. #int(w)) + ',h:' + str(int(h)) + ',c:' + "'" + str(round(float(conf), 2)) + "'},"
  501. #c = int(cls) # integer class
  502. #label = f'{self.names[c]} {conf:.2f}'
  503. label = None
  504. annotator.box_label(xyxy, label, color=colors(c, True))
  505. else:
  506. im0 = model.results[i].orig_img.copy()
  507. imc = im0.copy()
  508. cv2.rectangle(im0, (int(x - w / 2), int(y - h / 2)), (int(x + w / 2), int(y + h / 2)),
  509. (0, 255, 0), 2)
  510. bottom_left_x = int(x - w / 2)
  511. bottom_left_y = int(y + h / 2)
  512. # 计算中心点
  513. center_x = int(x)
  514. center_y = int(y)
  515. # 绘制中心点
  516. cv2.circle(im0, (center_x, center_y), 5, (255, 0, 0), -1) # 红色中心点,半径为 5
  517. dirmodel[channels[i]]['track_history'][track_id].append((bottom_left_x, bottom_left_y))
  518. if len(dirmodel[channels[i]]['track_history'][track_id]) > 100:
  519. del dirmodel[channels[i]]['track_history'][track_id][:-50] # 维持历史长度
  520. # 记录每一帧的时间
  521. dirmodel[channels[i]]['time_stamps'][track_id].append(current_time)
  522. # 计算时间间隔
  523. if len(dirmodel[channels[i]]['time_stamps'][track_id]) > 1:
  524. delta_time = dirmodel[channels[i]]['time_stamps'][track_id][-1] - dirmodel[channels[i]]['time_stamps'][track_id][-2] # 最近两帧的时间差
  525. else:
  526. delta_time = 0
  527. instantaneous_velocity = 0
  528. # 计算二维瞬时速度
  529. if len(dirmodel[channels[i]]['track_history'][track_id]) >= 2:
  530. pos1 = np.array(dirmodel[channels[i]]['track_history'][track_id][-1]) # 最新位置
  531. pos2 = np.array(dirmodel[channels[i]]['track_history'][track_id][-2]) # 前一个位置
  532. pos1 = map_to_ellipse(pos1)
  533. pos2 = map_to_ellipse(pos2)
  534. distance = np.linalg.norm(pos1 - pos2)
  535. # 使用时间间隔进行速度计算
  536. instantaneous_velocity = distance / delta_time if delta_time > 0 else np.zeros(2)
  537. instantaneous_velocity_magnitude = round(np.linalg.norm(instantaneous_velocity), 1)
  538. dirmodel[channels[i]]['instantaneous_velocities'][track_id].append(instantaneous_velocity_magnitude)
  539. else:
  540. instantaneous_velocity_magnitude = 0
  541. # 判断是否有足够数量的高速度
  542. high_velocity_count = sum(
  543. 1 for velocity in dirmodel[channels[i]]['instantaneous_velocities'][track_id] if velocity > self.speed_threshold)
  544. if high_velocity_count >= self.high_velocity_count_threshold:
  545. cv2.putText(im0, str(instantaneous_velocity_magnitude), (int(x), int(y)),
  546. cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
  547. flag = True
  548. #if self.opt.save_txt: # Write to file
  549. # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(
  550. # -1).tolist() # normalized xywh
  551. # line = (cls, *xywh, conf) if self.opt.save_conf else (cls, *xywh) # label format
  552. # with open(f'{txt_path}.txt', 'a') as f:
  553. # f.write(('%g ' * len(line)).rstrip() % line + '\n')
  554. #if self.save_img or self.opt.save_crop or self.view_img: # Add bbox to image
  555. # c = int(cls) # integer class
  556. # labelname = names[c]
  557. # if labelname in dirmodel[channels[i]]['label']:
  558. # #if Path(self.opt.weights).stem == 'arm':
  559. # #label = f'{self.names[c]} {conf:.2f}'
  560. # if labelname != 'arm':
  561. # label = None
  562. # annotator.box_label(xyxy, label, color=colors(c, True))
  563. # flag = True
  564. # x, y, x2, y2 = xyxy
  565. # w = x2 - x
  566. # h = y2 - y
  567. # #message = message + '{' + 'x:' + str(int(x)) + ',y:' + str(int(y)) + ',w:' + str(
  568. # # int(w)) + ',h:' + str(int(h)) + ',c:' + "'" + str(round(float(conf), 2)) + "'},"
  569. # if Path(weights).stem == 'personcount':
  570. # personnum = personnum+1
  571. # if Path(weights).stem == 'arm':
  572. # if labelname == 'arm':
  573. # flag = False
  574. # if Path(weights).stem == 'persontre':
  575. # if c ==4:
  576. # tredir.setdefault(0,[])
  577. # tredir[0].append(xyxy)
  578. # elif c in [0,1,2]:
  579. # tredir.setdefault(1,[])
  580. # tredir[1].append(xyxy)
  581. # print('not dence-----------------------------------------------------')
  582. # #if self.opt.save_crop:
  583. # # save_one_box1(xyxy, imc, file=self.save_dir / 'crops' / self.names[c] / f'{p.stem}.jpg',
  584. # # BGR=Truei)
  585. # #message = message[:-1] + ']'
  586. # #if len(message)>2:
  587. # #print(message)
  588. # #print(dirmodel[channels[i]]['web'])
  589. # #print(dirmodel[channels[i]]['websoc'])
  590. # # await dirmodel[channels[i]]['websoc'].send(message)
  591. # # await asyncio.sleep(0.005)
  592. # if Path(weights).stem == 'personcount':
  593. # if personnum < int(personcountdir[channels[i]]):
  594. # flag = False
  595. # if Path(weights).stem == 'persontre':
  596. # if len(list(tredir.keys()))==2:
  597. # result = judgeiou(tredir)
  598. # if result == True:
  599. # flag = True
  600. # else:
  601. # flag = False
  602. # else:
  603. # flag = False
  604. #if len(self.dirmodel[channels[i]]['framelist'])>=50:
  605. # self.dirmodel[channels[i]]['framelist'].pop(0)
  606. # self.dirmodel[channels[i]]['framelist'].append(annotator.result())
  607. #else:
  608. # self.dirmodel[channels[i]]['framelist'].append(annotator.result())
  609. if flag:
  610. #if self.dirmodel[path[i]]['imgtime'] != videotime[i]:
  611. dirmodel[channels[i]]['detframe'].pop(0)
  612. dirmodel[channels[i]]['detframe'].append(1)
  613. dirmodel[channels[i]]['preim'] = im0
  614. dirmodel[channels[i]]['oripreim'] = imc
  615. dirmodel[channels[i]]['posttime'] = videotime[i]
  616. print(dirmodel[channels[i]]['detframe'])
  617. #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
  618. else:
  619. #print(f'deti= {i}')
  620. #print(detframe[i])
  621. #if self.dirmodel[channels[i]]['imgtime'] != videotime[i]:
  622. dirmodel[channels[i]]['detframe'].pop(0)
  623. dirmodel[channels[i]]['detframe'].append(0)
  624. print(dirmodel[channels[i]]['detframe'])
  625. #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
  626. #print(detframe[i])
  627. # Stream results
  628. #im0 = annotator.result()
  629. #print(f'i = {i}')
  630. #print(channels[i])
  631. #print(postpretime[i])
  632. #print(detframe[i])
  633. if not dirmodel[channels[i]]['detflag'] and dirmodel[channels[i]]['detframe'].count(1)>=1:
  634. dirmodel[channels[i]]['detflag'] = True
  635. dirmodel[channels[i]]['detpretime'] = time.time()
  636. elif dirmodel[channels[i]]['detframe'].count(1)==0 :
  637. dirmodel[channels[i]]['detflag'] = False
  638. dirmodel[channels[i]]['detpretime'] = float('inf')
  639. # Stream results
  640. #im0 = annotator.result()
  641. if time.time() - dirmodel[channels[i]]['postpretime'] >30 and time.time() - dirmodel[channels[i]]['detpretime'] > dirmodel[channels[i]]['durtime'] and dirmodel[channels[i]]['detflag']:
  642. #print()
  643. #if time.time() - self.dirmodel[channels[i]]['postpretime'] >30 and self.dirmodel[channels[i]]['detflag']:
  644. #print(time.time() - self.dirmodel[channels[i]]['detpretime'])
  645. #print(self.dirmodel[channels[i]]['detflag'])
  646. #print(f'{Path(self.opt.weights).stem}post----{time.strftime("%Y-%m-%d %H:%M:%S", time.time()}')
  647. #time.sleep(30)
  648. #print(time.time() - postpretime[i])
  649. #print('111111111111111111111111111111111111111111111111')
  650. #print(dirmodel[channels[i]]['preim'].shape)
  651. success, encoded_image = cv2.imencode('.jpg', dirmodel[channels[i]]['preim'])
  652. content = encoded_image.tobytes()
  653. successori, encoded_imageori = cv2.imencode('.jpg', dirmodel[channels[i]]['oripreim'])
  654. contentori = encoded_imageori.tobytes()
  655. filename = f'{p.stem}_{int(time.time())}.jpg'
  656. filenameori = f'ori{p.stem}_{int(time.time())}.jpg'
  657. print(f'str(p) {p.name}')
  658. print(channels[i])
  659. payload = {'channel': dirmodel[channels[i]]['channel'],
  660. 'classIndex': dirmodel[channels[i]]['classindex'],
  661. 'ip': dirmodel[channels[i]]['algip'],
  662. 'videoTime': time.strftime('%Y-%m-%d %H:%M:%S', dirmodel[channels[i]]['posttime']),
  663. 'videoUrl': channels[i]}
  664. fourcc = cv2.VideoWriter_fourcc(*'MP4V')
  665. fps = 6
  666. height,width,_ = dirmodel[channels[i]]['preim'].shape
  667. year=time.strftime('%Y',time.localtime(time.time()))
  668. month=time.strftime('%m',time.localtime(time.time()))
  669. day=time.strftime('%d',time.localtime(time.time()))
  670. vidsavefold = f'/mnt/yolo/videos/{Path(weights).stem}/{year}/{month}/{day}'
  671. vidsaveflod = Path(vidsavefold)
  672. vidsaveflod.mkdir(parents=True, exist_ok=True)
  673. timesave = time.strftime('%Y-%m-%d-%H:%M:%S', dirmodel[channels[i]]['posttime'])
  674. #out = cv2.VideoWriter(f'{vidsaveflod}/{timesave}.mp4', fourcc, fps, (width, height))
  675. #for imgframe in self.dirmodel[channels[i]]['framelist']:
  676. #success, encoded_image = cv2.imencode('.jpg', self.dirmodel[channels[i]]['preim'])
  677. #content = encoded_image.tobytes()
  678. # out.write(imgframe)
  679. #out.release()
  680. files = [
  681. ('file', (filename, content, 'image/jpeg')),
  682. ('oldFile',(filenameori, contentori, 'image/jpeg')),
  683. #('videoFile',open(f'{vidsaveflod}/{timesave}.mp4','rb'))
  684. ]
  685. print(f'{Path(weights).stem}post----{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))}')
  686. try:
  687. # if self.dirmodel[channels[i]]['postvidpretime'] != self.dirmodel[channels[i]]['posttime'] and not self.dirmodel[channels[i]]['postvideo']:
  688. resulttext = requests.post(url, data=payload, files=files)
  689. # self.dirmodel[channels[i]]['postvidpretime'] = self.dirmodel[channels[i]]['posttime']
  690. # self.dirmodel[channels[i]]['postid'] = resulttext.json()['data']
  691. # self.dirmodel[channels[i]]['postframe'] = 0
  692. # self.dirmodel[channels[i]]['postvideo'] = True
  693. # self.dirmodel[channels[i]]['videoname'] = f'{vidsaveflod}/{timesave}.mp4'
  694. #videoqueuea.append(resulttext,f'{vidsaveflod}/{timesave}.mp4',)
  695. print(f'resulttext = {resulttext.json()["data"]}')
  696. except Exception:
  697. print("posterror")
  698. #if self.dirmodel[channels[i]]['postvideo']:
  699. #print(f'resulttext = {resulttext}')
  700. #time.sleep(3000)
  701. dirmodel[channels[i]]['postpretime'] = time.time()
  702. dirmodel[channels[i]]['detflag'] = False
  703. #timesave = time.strftime('%Y-%m-%d-%H:%M:%S', self.dirmodel[channels[i]]['posttime'])
  704. #year=time.strftime('%Y',time.localtime(time.time()))
  705. #month=time.strftime('%m',time.localtime(time.time()))
  706. #day=time.strftime('%d',time.localtime(time.time()))
  707. savefold = f'/mnt/yolo/images/{Path(weights).stem}/{year}/{month}/{day}'
  708. saveflod = Path(savefold)
  709. detsavefold = f'/mnt/yolo/detimages/{Path(weights).stem}/{year}/{month}/{day}'
  710. detsavefold = Path(detsavefold)
  711. saveflod.mkdir(parents=True, exist_ok=True)
  712. detsavefold.mkdir(parents=True, exist_ok=True)
  713. cv2.imwrite(f'{savefold}/{timesave}.jpg',dirmodel[channels[i]]['oripreim'])
  714. cv2.imwrite(f'{detsavefold}/{timesave}det.jpg',dirmodel[channels[i]]['preim'])
  715. self.view_img = False
  716. if self.view_img:
  717. if platform.system() == 'Linux' and p not in windows:
  718. windows.append(p)
  719. cv2.namedWindow(f'{str(p)}-{Path(weights).stem}',
  720. cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
  721. cv2.resizeWindow(f'{str(p)}-{Path(weights).stem}', im0.shape[1], im0.shape[0])
  722. im1 = cv2.resize(im0, (1280, 720))
  723. cv2.imshow(f'{str(p)}-{Path(weights).stem}', im1)
  724. cv2.waitKey(1) # 1 millisecond
  725. # Save results (image with detections)
  726. # Print time (inference-only)
  727. print(f'channels[i]={channels[i]}')
  728. LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms {time.strftime('%Y-%m-%d-%H:%M:%S',time.localtime(time.time()))} {Path(weights).stem}")
  729. # def load(self):
  730. # conn = sqlite3.connect(self.source)
  731. # c = conn.cursor()
  732. # while True:
  733. # #
  734. # # print ("数据库打开成功")
  735. #
  736. # cursor = c.execute(
  737. # "SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname= (?)",(Path(self.opt.weights).stem,))
  738. # # content = cursor.fetchall()
  739. # # if content[0][1] ==1 or content[0][2] ==1:
  740. # # c.execute("UPDATE CHANGESTREAM set streamimg = 1 where modelname='yolov5s'")
  741. # # print("updata changestream")
  742. # # conn.commit()
  743. # # cursor = c.execute(
  744. # # "SELECT modelname, addstream,delstream,streamimg from CHANGESTREAM WHERE modelname='yolov5s'")
  745. # self.contentid = cursor.fetchall()
  746. # #global tag
  747. # #tag = Value('i', self.contentid[0][3])
  748. # #print(tag.value==1)
  749. # print(f'loadcontent={self.contentid[0][3]}')
  750. # time.sleep(3)
  751. # c.close()
  752. # conn.close()
  753. def readpoint(self,weights):
  754. #conn = sqlite3.connect(self.source)
  755. #c = conn.cursor()
  756. #cursor = c.execute(
  757. # "SELECT address,fence,point,channel,classindex,ip ,algip,label, durtime from STREAM WHERE modelname= (?)",
  758. # (Path(self.opt.weights).stem,))
  759. data = {
  760. "algorithmCode": algmodel[Path(weights).stem],
  761. "deviceIp":None
  762. }
  763. dirmodel = {}
  764. result = requests.post(url=urlt,data=data).json()['data']
  765. channell=[]
  766. for info in result:
  767. #content = cursor.fetchall()
  768. #self.dirmodel = {}
  769. #for address,fence,point,channel,classindex,ip ,algip,label,durtime in content:
  770. #address = f'{address[:-1]}0'
  771. channel = info["deviceChannel"]
  772. channell.append(channel)
  773. dirmodel[channel] = {}
  774. dirmodel[channel]['fence'] = 1 if len(info["electricFence"])>0 else 0
  775. #self.dirmodel[channel]['point'] = point
  776. dirmodel[channel]['channel'] = info['deviceChannel']
  777. dirmodel[channel]['classindex'] = info['algorithmCode']
  778. dirmodel[channel]['ip'] = info['deviceIp']
  779. dirmodel[channel]['algip'] = info['deviceAlgorithmIp']
  780. dataele = {
  781. "algorithmCode": dirmodel[channel]['classindex'],
  782. "algorithmIp":dirmodel[channel]['algip'],
  783. "channel":dirmodel[channel]['channel']
  784. }
  785. resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
  786. resultele = resultele.split(',||')
  787. resultele = tuple(resultele)
  788. point = '%s:'*len(resultele) %resultele
  789. if Path(weights).stem == 'personcount':
  790. resultper = requests.post(url=urlperson,data=dataele).json()['data']
  791. personcountdir[channel] = resultper
  792. dirmodel[channel]['point'] = point[:-2]
  793. dirmodel[channel]['preim'] = None
  794. dirmodel[channel]['oripreim'] = None
  795. dirmodel[channel]['detframe'] = [0 for _ in range(2)]
  796. dirmodel[channel]['postpretime'] = 0
  797. dirmodel[channel]['detflag'] = False
  798. dirmodel[channel]['detpretime'] = float('inf')
  799. dirmodel[channel]['label'] = modellabeldir[data['algorithmCode']]
  800. dirmodel[channel]['durtime'] = 0
  801. dirmodel[channel]['posttime'] = 0
  802. dirmodel[channel]['track_history'] = defaultdict(lambda: [])
  803. dirmodel[channel]['time_stamps'] = defaultdict(lambda: deque(maxlen=50))
  804. dirmodel[channel]['instantaneous_velocities'] = defaultdict(lambda: deque(maxlen=30))
  805. #tmp = f'{weburl}/{channel}/{info["deviceAlgorithmIp"]}'
  806. #dirmodel[channel]['web'] = f'{weburl}/{info["deviceId"]}/{info["algorithmCode"]}'
  807. print(dirmodel)
  808. return sorted(channell),dirmodel
  809. #def strtolst(self,str):
  810. # str = str.split(":")
  811. # lista = []
  812. # for liststr in str:
  813. # if len(liststr) > 0:
  814. # li = liststr.split(',')
  815. # listpoint = []
  816. # for i, j in zip(li[::2], li[1::2]):
  817. # listpoint.append((i, j))
  818. # lista.append(listpoint)
  819. # return listpoint
  820. #def preprocess():
  821. # print('preprocess-----------------------------------------------')
  822. # def getframe(queue,channelsl,source,tt,numworks,lock,numworkv):
  823. # while True:
  824. # print("dataloader")
  825. # imgsz = [640, 640]
  826. # print(f'source = {source}')
  827. # print(f'inchannel ={channelsl}')
  828. #
  829. # bs = len(dataset)
  830. # vid_path, vid_writer = [None] * bs, [None] * bs
  831. # # self.detframe = [[0 for _ in range(8)] for i in range(bs)]
  832. # # self.postpretime = [0]*bs
  833. # # Run inference
  834. #
  835. # #imgsz = (1 , 3, *self.imgsz)
  836. # print(imgsz)
  837. # #self.model.warmup(imgsz=(1 , 3, *imgsz)) # warmup
  838. # seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
  839. # sourcebase = 'project.db'
  840. # #
  841. # # print ("数据库打开成功")
  842. # pretime = time.time()
  843. # tag = 0
  844. # for path, im, im0s, vid_cap, s, videotime,channels in dataset:
  845. # # print('*'*21)
  846. # # global tag
  847. # if time.time()-pretime > 30:
  848. # channellist = []
  849. # pretime = time.time()
  850. # data = {
  851. # "algorithmCode": None,
  852. # "deviceIp":None
  853. # }
  854. # result = requests.post(url=urlt,data=data).json()['data']
  855. # for info in result:
  856. # channellist.append((info['deviceChannel'],info['playbackAddress']))
  857. # channelsa = []
  858. # sourcea = []
  859. # channellist = set(channellist)
  860. # channellist = sorted(channellist,key=lambda x:x[0])
  861. # #channellist = set(channellist)
  862. # for cha,add in channellist:
  863. # channelsa.append(cha)
  864. # sourcea.append(add)
  865. # channelsl = sorted(channelsl)
  866. # #channelsa = sorted(channelsa)
  867. # if channelsa!=channelsl:
  868. # print(f'channelsa = {channelsa}')
  869. # print(f'channelsl = {channelsl}')
  870. # dataset.close()
  871. # channelsl = channelsa
  872. # source = sourcea
  873. # break;
  874. #
  875. # #conn = sqlite3.connect(sourcebase)
  876. # #c = conn.cursor()
  877. # #cursor = c.execute("SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname = 'stream'")
  878. # #contentid = cursor.fetchall()
  879. # #tag = contentid[0][3]
  880. # #if tag == 1:
  881. # # lock.acquire()
  882. # # numworkv.value += 1
  883. # # dataset.close()
  884. # # if numworkv.value==3:
  885. # # print('newstreaming=', tag)
  886. # # conn = sqlite3.connect(source)
  887. # # c = conn.cursor()
  888. # # c.execute("UPDATE CHANGESTREAM set streaming = 0 , addstream=0,delstream=0 where modelname= 'helmet'")
  889. # # print(123)
  890. # # conn.commit()
  891. # # c.close()
  892. # # conn.close()
  893. # # lock.release()
  894. # # print('opencv1')
  895. # # cv2.destroyAllWindows()
  896. # # print('opencv')
  897. # # break
  898. # #else:
  899. # # print('nonewstreaming=', tag)
  900. # queue.put((path, im, im0s, vid_cap, s, videotime,channels))
  901. # queue.get() if queue.qsize() > 3 else time.sleep(0.001)
  902. #
  903. # def getmutpro(channels,source,streamlist,numworkv,lock,numworks=1):
  904. # processlist = []
  905. # queuelist = []
  906. # for i in range(numworks):
  907. # queue = Queue(maxsize=4)
  908. # process = Process(target=getframe,
  909. # args=(queue, channels,source, i,numworks,lock,numworkv))
  910. # processlist.append(process)
  911. # process.start()
  912. # queuelist.append(queue)
  913. # return queuelist
  914. # #path = []
  915. # #im0s = []
  916. # #vid_cap = None
  917. # #s = ''
  918. # #videotime = []
  919. # #while True:
  920. # # imlist = []
  921. # # pathlist = []
  922. # # im0slist = []
  923. # # channelslist = []
  924. # # vid_cap = None
  925. # # s = ''
  926. # # videotimelist = []
  927. # # for q in queuelist:
  928. # # if q.qsize()>0:
  929. # # setframe = q.get()
  930. # # path, im, im0s, vid_cap, s, videotime,channels = setframe
  931. # # channelslist += channels
  932. # # pathlist += path
  933. # # im0slist += im0s
  934. # # videotimelist += videotime
  935. # # imlist.append(im)
  936. # # if len(imlist)>0:
  937. # # im = np.concatenate(imlist)
  938. # # if len(pathlist)>0:
  939. # # print(len(path),im.shape,len(im0s))
  940. # # streamlist.append((pathlist, im, im0slist, vid_cap, s, videotimelist,channelslist))
  941. # #print(f'streamlist = {len(streamlist)}')
  942. # # streamlist.pop(0) if len(streamlist) > 3 else time.sleep(0.001)
  943. def modelfun(detectdemo,weights,classify=False,conf_thres=0.80,device='',runmodel=None):
  944. print(weights)
  945. #detectdemo=Detect(weights=weights,source=sourcedb,classify=classify,conf_thres=conf_thres,device=device)
  946. detectdemo.infer(weights,classify,conf_thres,device,runmodel)
  947. #detectdemo.infer(weights, classify, conf_thres, device, runmodel)
  948. def detectmut(channellist,source,modellist,contentlist,modeladir,runmodel={},deviceid=''):
  949. detectdemo = Detect(channelsl=channellist,source=source)
  950. # while True:
  951. for modelname in modellist:
  952. if modelname in contentlist:
  953. #if modelname not in runmodel:
  954. #print(i)
  955. #detectdemo=Detect(weights=f'/mnt/project/yolodemo/yolov5-master/{i[0]}.pt')
  956. #if modelname in ['fall','helmet','bag','arm']:
  957. print(f'weights ={modelname}.pt deviceid {deviceid}')
  958. #c.execute('select conf,cla from changestream where modelname = (?)',(modelname,))
  959. #rea = c.fetchall()
  960. process = threading.Thread(target=modelfun,args=(detectdemo,f'{modelname}.pt',modeladir[modelname]['cla'],modeladir[modelname]['conf'],deviceid,runmodel))
  961. #elif modelname in ['personcount','persontre']:
  962. # process = Process(target=modelfun,args=(streamlist,videoqueue,f'{modelname}.pt',dbpath,False,0.50,'',runmodel))
  963. #elif modelname in ['uniform']:
  964. # process = Process(target=modelfun,args=(streamlist,videoqueue,f'{modelname}.pt',dbpath,True,0.50,'',runmodel))
  965. #else:
  966. # process = Process(target=modelfun,args=(streamlist,f'{modelname}.pt',dbpath))
  967. #runmodel[modelname] = 1
  968. #processes.append(process)
  969. #process.start()
  970. #detectobj = Process(target=detectdemo.infer,args=(queue,))
  971. # Detect(weights=f'{i[0]}.pt')
  972. time.sleep(3)
  973. process.start()
  974. def parse_opt():
  975. parser = argparse.ArgumentParser()
  976. parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path or triton URL')
  977. opt = parser.parse_args()
  978. return opt
  979. def main(opt):
  980. check_requirements(ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
  981. run(**vars(opt))
  982. if __name__ == '__main__':
  983. torch.multiprocessing.set_start_method('spawn')
  984. #set_start_method('spawn')
  985. #multiprocessing.set_start_method('spawn')
  986. torch.cuda.set_per_process_memory_fraction(0.6)
  987. opt = parse_opt()
  988. dbpath = 'project.db'
  989. conn = sqlite3.connect(dbpath)
  990. #
  991. # print ("数据库打开成功")
  992. c = conn.cursor()
  993. task(c,conn,urlt,urla)
  994. cursor = c.execute('select channel,algip from stream where modelname = "run"')
  995. result = cursor.fetchall()
  996. for channel ,algip in result:
  997. data = {
  998. "channel": channel,
  999. "ip":algip
  1000. }
  1001. #personcountdir[channel] = num
  1002. address = requests.post(url=urlrtsp,data=data).json()['msg']
  1003. c.execute('UPDATE STREAM set address= (?) where channel =(?)',(address,channel))
  1004. conn.commit()
  1005. cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'run'")
  1006. #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
  1007. content = cursor.fetchall()
  1008. contentlist = []
  1009. for con in content:
  1010. contentlist.append(con[0])
  1011. #cursor = c.execute("SELECT address,modelname,channel from STREAM where modelname='helmet' or modelname = 'sleep' or modelname = 'smoke' or modelname = 'danager'or modelname = 'gloves' or modelname = 'other'")
  1012. cursor = c.execute("SELECT address,modelname,channel from STREAM where modelname = 'run'")
  1013. contenta = cursor.fetchall()
  1014. source = []
  1015. modellist = []
  1016. addcha = []
  1017. channellist = []
  1018. for i in contenta:
  1019. addcha.append((i[0],i[2]))
  1020. modellist.append(i[1])
  1021. addcha = set(addcha)
  1022. addcha = sorted(addcha,key=lambda x:x[1])
  1023. for a,cha in addcha:
  1024. source.append(a)
  1025. channellist.append(cha)
  1026. print(addcha)
  1027. #source = set(source)
  1028. source = list(source)
  1029. #source.sort()
  1030. modellist = set(modellist)
  1031. n = len(content)
  1032. print(f'modelname={n}')
  1033. print(content)
  1034. #content.reverse()
  1035. print(content)
  1036. # main(opt)
  1037. #processes = []
  1038. streamqueue = Queue(maxsize=4)
  1039. numworkv = Value('i', 0)
  1040. manager = Manager()
  1041. lock = multiprocessing.Lock()
  1042. streamlist = manager.list()
  1043. numworks = 2
  1044. #queuelist = getmutpro(channellist,source, streamlist, numworkv, lock, numworks)
  1045. #thread.start()
  1046. #videoqueue = Queue(maxsize=20)
  1047. #thread1 = threading.Thread(target=postvideo, args=(videoqueue,))
  1048. #thread1.start()
  1049. #pool = ThreadPoolExecutor(max_workers=n)
  1050. #runmodel = manager.dict()
  1051. modeladir = {}
  1052. for modelname in modellist:
  1053. if modelname in contentlist:
  1054. #if modelname not in runmodel:
  1055. #print(i)
  1056. #detectdemo=Detect(weights=f'/mnt/project/yolodemo/yolov5-master/{i[0]}.pt')
  1057. #if modelname in ['fall','helmet','bag','arm']:
  1058. print(f'weights ={modelname}.pt')
  1059. c.execute('select conf,cla from changestream where modelname = (?)',(modelname,))
  1060. rea = c.fetchall()
  1061. #print(f'rea')
  1062. modeladir.setdefault(modelname,{})
  1063. modeladir[modelname]['conf'] = rea[0][0]
  1064. modeladir[modelname]['cla'] = rea[0][1]
  1065. runmodel = {}
  1066. for deviceid,num in enumerate(range(0,len(channellist),13)):
  1067. process = Process(target=detectmut,args=(channellist[num:num+13],source[num:num+13],modellist,contentlist,modeladir,{},0))
  1068. time.sleep(3)
  1069. process.start()
  1070. #deviceid = deviceid+1
  1071. # detectdemo = Detect(channelsl=channellist[0:num],source=source[0:num])
  1072. # # while True:
  1073. # for modelname in modellist:
  1074. # if modelname in contentlist:
  1075. # if modelname not in runmodel:
  1076. # #print(i)
  1077. # #detectdemo=Detect(weights=f'/mnt/project/yolodemo/yolov5-master/{i[0]}.pt')
  1078. # #if modelname in ['fall','helmet','bag','arm']:
  1079. # print(f'weights ={modelname}.pt')
  1080. # c.execute('select conf,cla from changestream where modelname = (?)',(modelname,))
  1081. # rea = c.fetchall()
  1082. # process = threading.Thread(target=modelfun,args=(detectdemo,f'{modelname}.pt',rea[0][1],rea[0][0],'',runmodel))
  1083. # #elif modelname in ['personcount','persontre']:
  1084. # # process = Process(target=modelfun,args=(streamlist,videoqueue,f'{modelname}.pt',dbpath,False,0.50,'',runmodel))
  1085. # #elif modelname in ['uniform']:
  1086. # # process = Process(target=modelfun,args=(streamlist,videoqueue,f'{modelname}.pt',dbpath,True,0.50,'',runmodel))
  1087. # #else:
  1088. # # process = Process(target=modelfun,args=(streamlist,f'{modelname}.pt',dbpath))
  1089. # runmodel[modelname] = 1
  1090. # #processes.append(process)
  1091. # #process.start()
  1092. # #detectobj = Process(target=detectdemo.infer,args=(queue,))
  1093. # # Detect(weights=f'{i[0]}.pt')
  1094. # time.sleep(3)
  1095. # process.start()
  1096. # time.sleep(900)
  1097. # task(c,conn,urlt,urla)
  1098. # cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet' or modelname = 'fall' or modelname = 'uniform' or modelname = 'personcount' or modelname = 'arm' or modelname = 'bag'")
  1099. # #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
  1100. # content = cursor.fetchall()
  1101. # contentlist = []
  1102. # for con in content:
  1103. # contentlist.append(con[0])
  1104. # #cursor = c.execute("SELECT address,modelname,channel from STREAM where modelname='helmet' or modelname = 'sleep' or modelname = 'smoke' or modelname = 'danager'or modelname = 'gloves' or modelname = 'other'")
  1105. # cursor = c.execute("SELECT address,modelname,channel from STREAM ")
  1106. # contenta = cursor.fetchall()
  1107. # source = []
  1108. # modellist = []
  1109. # addcha = []
  1110. # channellist = []
  1111. # for i in contenta:
  1112. # addcha.append((i[0],i[2]))
  1113. # modellist.append(i[1])
  1114. # addcha = set(addcha)
  1115. # addcha = sorted(addcha)
  1116. # for a,cha in addcha:
  1117. # source.append(a)
  1118. # channellist.append(cha)
  1119. # print(addcha)
  1120. # #source = set(source)
  1121. # source = list(source)
  1122. # #source.sort()
  1123. # modellist = set(modellist)
  1124. # n = len(content)
  1125. # print(f'modelname={n}')
  1126. # print(content)
  1127. # #content.reverse()
  1128. # print(content)
  1129. #pool.submit(detectobj.infer)
  1130. #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'fall'")
  1131. #content = cursor.fetchall()
  1132. #n = len(content)
  1133. #print(f'modelname={n}')
  1134. #print(content)
  1135. #content.reverse()
  1136. #print(content)
  1137. # main(opt)
  1138. #processes = []
  1139. #pool = ProcessPoolExecutor(max_workers=n)
  1140. #for i in content:
  1141. #print(i)
  1142. #detectdemo=Detect(weights=f'{i[0]}.pt')
  1143. #process = Process(target=detectdemo.infer)
  1144. #processes.append(process)
  1145. #process.start()
  1146. #detectobj = Detect(weights=f'{i[0]}.pt')
  1147. # time.sleep(3)
  1148. #pool.submit(detectobj.infer)
  1149. # print('111111111111111111111111111111111111111111111111111111111')
  1150. #pool.submit(TestA().func1, '张三', i)
  1151. #print('----------------------------------------------------------------')
  1152. #time.sleep(3000)
  1153. # 等待所有进程执行完毕
  1154. #for process in processes:
  1155. # process.join()
  1156. #pool.submit(Detect(weights=f'{i[0]}.pt').infer)
  1157. # if isinstance(opt.weights,list):
  1158. # opt.weights = opt.weights[0]
  1159. #signal.signal(signal.SIGINT, my_handler)
  1160. #detectdemo1 = Detect(weights=f'{content[0][0]}.pt')
  1161. #detectdemo1.infer()
  1162. #a = Test
  1163. #with ProcessPoolExecutor(3) as ppool:
  1164. #for i in range(3):
  1165. # print('hello')
  1166. #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
  1167. #ppool.submit(TestA().func1, '张三', i)
  1168. #ta = TestA()
  1169. #with ProcessPoolExecutor(5) as ppool: # 创建一个5个进程的进程池
  1170. # for i in range(1, 4):
  1171. # ppool.submit(func1, '张三', i)
  1172. #f1= pool.submit(detectdemo1.infer)
  1173. # print("线程1-----------------------------------------------------------------------------------")
  1174. #detectdemo2 = Detect(weights=r"helmet.pt")
  1175. #f2=pool.submit(detectdemo2.infer)
  1176. # print("线程2-------------------------------------------------------------------------------------")
  1177. #detectdemo3 = threading.Thread(target=detectdemo3.infer)
  1178. #detectdemo3 = Detect(weights=r"fall.pt")
  1179. #f3=pool.submit(detectdemo3.infer)