detectopencvmut0113auta.py 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154
  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. from torchvision import transforms
  49. FILE = Path(__file__).resolve()
  50. ROOT = FILE.parents[0] # YOLOv5 root directory
  51. if str(ROOT) not in sys.path:
  52. sys.path.append(str(ROOT)) # add ROOT to PATH
  53. ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
  54. from models.common import DetectMultiBackend
  55. from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages,LoadStreams, LoadStreamsSQLNEWN,LoadStreamsSQL,LoadStreamsSQLNRERT,LoadStreamsVEight,LoadStreamsSQLT,LoadStreamsSQLTN
  56. from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
  57. increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh,strtolst,apply_classifier1,apply_classifieruniform,compute_IOU,task,apply_classifierarm)
  58. from utils.plots import Annotator, colors, save_one_box
  59. from utils.torch_utils import select_device, smart_inference_mode
  60. from utils.renwu import newHelmet,newUniform,Fall,Personcount,Arm,Bag,Cross,Extinguisher,Persontre,Bag,Danager
  61. #from testpool import func1,TestA
  62. # def my_handler(signum, frame):
  63. # exit(0)
  64. #url = "http://36.7.84.146:18802/ai-service/open/api/operate/upload"
  65. url = "http://172.19.152.231/open/api/operate/upload"
  66. urlrtsp = "http://172.19.152.231/open/api/operate/previewURLs"
  67. urlt = "http://172.19.152.231/open/api/operate/taskList"
  68. urla = "http://172.19.152.231/open/api/operate/algorithmList"
  69. urlele = "http://172.19.152.231/open/api/operate/fence"
  70. urltime = "http://172.19.152.231/open/api/operate/getTime"
  71. urlperson = "http://172.19.152.231/open/api/operate/getPersonLimitNum"
  72. #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'}
  73. #algmodel = {'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'}
  74. modelnamedir = {'0':'helmet','8':'danager','10':'uniform','14':'smoke','16':'fire','21':'cross','25':'fall','29':'occupancy','30':'liquid','31':'pressure','32':'sleep','34':'personcount','37':'other','38':'duty','98':'face','55':'oil','52':'jingdian','53':'rope','54':'personcar','39':'inspection','11':'reflective','12':'phone','66':'extinguisher','67':'tizi','68':'menjin','35':'arm','36':'persontre','33':'bag'}
  75. modellabeldir = {'0':'head,person','8':'person','10':'black_work_clothes,blue_work_clothes,person','14':'smoke','16':'fire','21':'cross','25':'fall','29':'car','30':'liquid','31':'pressure','32':'sleep','34':'personcount','37':'other','38':'person','98':'face','55':'oil','52':'person,hand,ball','53':'rope','54':'person','39':'person','11':'blue,greent,whitet,bluecoat,whitebarcoat,graycoat,baoan,chenyi,other','12':'phone','66':'extinguisher','67':'person,tizi','68':'person','35':'barearm,arm','36':'person,foot,cart,bag,box','33':'handbox,handbag'}
  76. modelalgdir = {}
  77. personcountdir = {}
  78. for key,value in modelnamedir.items():
  79. modelalgdir[value] = key
  80. taskmap = {'helmet':newHelmet,'uniform':newUniform,'fall':Fall,'personcount':Personcount,'arm':Arm,'bag':Bag,'cross':Cross,'extinguisher':Extinguisher,'persontre':Persontre,'bag':Bag,'danager':Danager}
  81. mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
  82. test = transforms.Compose([transforms.Resize((224,224)),
  83. #transforms.CenterCrop(224),
  84. transforms.ToTensor(),
  85. transforms.Normalize(mean=mean, std=std)
  86. ])
  87. def clapre(modelcla,claimg,clapoint):
  88. imgten = torch.stack(claimg,dim=0)
  89. clapoint = torch.stack(clapoint,dim=0)
  90. imgten = imgten.to(0)
  91. result = modelcla(imgten)
  92. result = F.softmax(result)
  93. print(result)
  94. index = result.argmax(1)
  95. index = index.cpu().numpy()
  96. index = np.argwhere(index<5)
  97. index = index.reshape(-1)
  98. print(index)
  99. if len(index)>0:
  100. print(clapoint[index])
  101. return clapoint[index]
  102. else:
  103. return None
  104. class YoloOpt:
  105. def __init__(self, weights=ROOT / 'yolov5s.pt',source=ROOT / 'data/images',data=ROOT / 'data/coco128.yaml',
  106. imgsz=(640,640),
  107. conf_thres=0.25,
  108. iou_thres=0.45,
  109. max_det=1000,
  110. device='',
  111. view_img=False,
  112. save_txt=False,
  113. save_conf=False,
  114. save_crop=False,
  115. nosave=True,
  116. classes=None,
  117. agnostic_nms=False,
  118. augment=False,
  119. visualize=False,
  120. update=False,
  121. project=ROOT / 'runs/detect',
  122. name='exp',
  123. exist_ok=False,
  124. line_thickness=1,
  125. hide_labels=False,
  126. hide_conf=False,
  127. half=False,
  128. dnn=False,
  129. vid_stride=10,
  130. classify=False,
  131. v8=False):
  132. self.weights = weights # 权重文件地址
  133. self.source = source # 待识别的图像
  134. self.data = data
  135. if imgsz is None:
  136. self.imgsz = (640, 640)
  137. self.imgsz = imgsz # 输入图片的大小,默认 (640,640)
  138. self.conf_thres = conf_thres # object置信度阈值 默认0.25 用在nms中
  139. self.iou_thres = iou_thres # 做nms的iou阈值 默认0.45 用在nms中
  140. self.device = device # 执行代码的设备,由于项目只能用 CPU,这里只封装了 CPU 的方法
  141. self.view_img = view_img # 是否展示预测之后的图片或视频 默认False
  142. self.classes = classes # 只保留一部分的类别,默认是全部保留
  143. self.agnostic_nms = agnostic_nms # 进行NMS去除不同类别之间的框, 默认False
  144. self.augment = augment # augmented inference TTA测试时增强/多尺度预测,可以提分
  145. self.update = update # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
  146. self.exist_ok = exist_ok # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
  147. self.project = project # 保存测试日志的参数,本程序没有用到
  148. self.name = name # 每次实验的名称,本程序也没有用到
  149. self.max_det = max_det
  150. self.save_txt = save_txt
  151. self.save_conf= save_conf
  152. self.save_crop= save_crop
  153. self.nosave = nosave
  154. self.visualize = visualize
  155. self.line_thickness = line_thickness
  156. self.hide_labels = hide_labels
  157. self.hide_conf = hide_conf
  158. self.half = half
  159. self.dnn = dnn
  160. self.vid_stride = vid_stride
  161. self.classify = classify
  162. self.v8 = v8
  163. class Detect:
  164. def __init__(self, weights = ROOT / 'yolov5s.pt' , imgsz=(640,640),source="changshusql1103.db",classes=None,device=None,classify=False,conf_thres=0.25,v8=False):
  165. print(f'detectweights = {weights}')
  166. if v8:
  167. from ultralytics.nn.autobackend import AutoBackend
  168. from ultralytics.utils.ops import non_max_suppression
  169. else:
  170. from utils.general import non_max_suppression
  171. self.opt = YoloOpt(weights=weights, imgsz=imgsz,source=source,classes=classes,device=device,classify=classify,conf_thres=conf_thres,v8=v8)
  172. self.source = str(self.opt.source)
  173. self.save_img = not self.opt.nosave and not source.endswith('.txt') # save inference images
  174. is_file = Path(self.source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
  175. is_url = self.source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
  176. self.webcam = self.source.isnumeric() or source.endswith('.db') or (is_url and not is_file)
  177. screenshot = self.source.lower().startswith('screen')
  178. if is_url and is_file:
  179. self.source = check_file(self.source) # download
  180. self.save_dir = increment_path(Path(self.opt.project) / self.opt.name, exist_ok=self.opt.exist_ok) # increment run
  181. #self.save_dir = self.save_dir / Path(self.opt.weights).stem
  182. #self.save_dir.mkdir(parents=True, exist_ok=True)
  183. (self.save_dir / 'labels' if self.opt.save_txt else self.save_dir).mkdir(parents=True, exist_ok=True) # make dir
  184. print(f'device = {self.opt.device}')
  185. device = select_device(self.opt.device)
  186. if v8:
  187. self.model = AutoBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  188. if Path(weights).stem in ['arm', 'uniform']:
  189. if Path(weights).stem == 'arm':
  190. self.personmodel = AutoBackend('yolov8m.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  191. elif Path(weights).stem == 'uniform':
  192. self.personmodel = AutoBackend('yolo11m.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  193. else:
  194. self.model = DetectMultiBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  195. if Path(weights).stem in ['helmet','arm']:
  196. self.personmodel = DetectMultiBackend('personcount.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  197. self.stride, self.names, self.pt = self.model.stride, self.model.names, self.model.pt
  198. self.classify = classify
  199. if self.classify:
  200. #if Path(weights).stem =='uniform':
  201. # self.classifier_model = AutoBackend(f"{Path(weights).stem}cls.pt",device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  202. #else:
  203. if Path(weights).stem != "arm":
  204. classifier_model = torch.load(f"{Path(weights).stem}cls.pt")
  205. self.classifier_model = classifier_model.to(device)
  206. self.classifier_model.eval()
  207. else:
  208. self.classifier_model = AutoBackend(f"{Path(weights).stem}cls.pt",device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  209. self.imgsz = check_img_size(self.opt.imgsz, s=self.stride)
  210. self.model.warmup(imgsz=(1 , 3, *self.imgsz))
  211. self.readpoint()
  212. print(self.imgsz)
  213. self.updatetime = time.time()
  214. self.updatemtime = time.time()
  215. self.filetime = os.path.getmtime(self.opt.weights)
  216. self.taskname = taskmap[Path(self.opt.weights).stem]()
  217. bs = 1 # batch_size
  218. if self.webcam:
  219. #self.view_img = check_imshow(warn=True)
  220. self.view_img = False
  221. # dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
  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. dataset = LoadImages(self.source, img_size=self.imgsz, stride=self.stride, auto=self.pt, vid_stride=self.opt.vid_stride)
  227. t1 = threading.Thread(target=self.load,daemon=True)
  228. t1.start()
  229. @smart_inference_mode()
  230. def infer(self,queue,runmodel):
  231. pretime = time.time()
  232. seen, windows, self.dt = 0, [], (Profile(), Profile(), Profile())
  233. #
  234. # print ("数据库打开成功")
  235. while True:
  236. if time.localtime().tm_hour not in range(7,20):
  237. time.sleep(30)
  238. continue
  239. #print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
  240. if time.time()-pretime>300:
  241. ret = self.readpoint()
  242. pretime = time.time()
  243. if not ret:
  244. print(f'{Path(self.opt.weights).stem} {runmodel}')
  245. runmodel.pop(Path(self.opt.weights).stem)
  246. print(f'{Path(self.opt.weights).stem} {runmodel}')
  247. break
  248. print(f'queuelen = {len(queue)}')
  249. for que in queue:
  250. if que.qsize() == 0:
  251. print('queuezero')
  252. time.sleep(0.01)
  253. if que.qsize() > 0:
  254. #if time.time()-pretime>300:
  255. # ret = self.readpoint()
  256. # pretime = time.time()
  257. # if not ret:
  258. # print(f'{Path(self.opt.weights).stem} {runmodel}')
  259. # runmodel.pop(Path(self.opt.weights).stem)
  260. # print(f'{Path(self.opt.weights).stem} {runmodel}')
  261. # break
  262. setframe = que.get()
  263. # print('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
  264. #if setframe is not None
  265. path, im, im0s, vid_cap, s, videotime ,channels = setframe
  266. algchannel = list(self.dirmodel.keys())
  267. print(algchannel)
  268. print(path)
  269. algchannel = np.array(algchannel)
  270. channelsnp = np.array(channels)
  271. algindex = np.where(np.in1d(channelsnp, algchannel))[0]
  272. algindex = list(algindex)
  273. path = np.array(path)
  274. path = path[algindex]
  275. path = path.tolist()
  276. channels = np.array(channels)
  277. channels = channels[algindex]
  278. channels = channels.tolist()
  279. # print(algindex)
  280. if len(algindex)==0:
  281. continue
  282. #for ia in algindex:
  283. # print(type(im0s[ia]))
  284. # print(im0s[ia].shape)
  285. im = im[algindex]
  286. #for ia in algindex:
  287. # print(type(ia))
  288. try:
  289. im0s = np.asarray(im0s)
  290. except Exception:
  291. im0s = np.asarray(im0s,dtype=object)
  292. print(im0s.shape)
  293. im0s = im0s[algindex]
  294. # im0s = im0s.tolist()
  295. print(f'algindex = {algindex}')
  296. print(f'im0s ={im0s[0].shape}')
  297. videotime = np.array(videotime)
  298. videotime = videotime[algindex]
  299. videotime = tuple(map(tuple, videotime))
  300. # global tag
  301. # if self.contentid[0][3] == 1 :
  302. # dataset.close()
  303. # print('newstreaming=', self.contentid[0][3])
  304. # conn = sqlite3.connect(self.source)
  305. # c = conn.cursor()
  306. # c.execute("UPDATE CHANGESTREAM set streamimg = 0 , addstream=0,delstream=0 where modelname= (?)",(Path(self.opt.weights).stem,))
  307. # print(123)
  308. # conn.commit()
  309. # c.close()
  310. # conn.close()
  311. # print('opencv1')
  312. # cv2.destroyAllWindows()
  313. # print('opencv')
  314. # break
  315. # else:
  316. # print('nonewstreaming=', self.contentid[0][3])
  317. with self.dt[0]:
  318. im = torch.from_numpy(im).to(self.model.device)
  319. im = im.half() if self.model.fp16 else im.float() # uint8 to fp16/32
  320. im /= 255 # 0 - 255 to 0.0 - 1.0
  321. if len(im.shape) == 3:
  322. im = im[None] # expand for batch dim
  323. # Inference
  324. with self.dt[1]:
  325. visualize = increment_path(self.save_dir / Path(path).stem,
  326. mkdir=True) if self.opt.visualize else False
  327. #print('error')
  328. # print(self.model)
  329. pred = self.model(im, augment=self.opt.augment, visualize=visualize)
  330. self.postprocess(pred, path, im0s, im, s, videotime,channels)
  331. # print(f'predshape= {')
  332. # NMS
  333. #processlist = []
  334. #for i in range(3):
  335. # process = Process(target=self.postprocess,args=(pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s))
  336. # process = Process(target=self.preprocess)
  337. # process.start()
  338. # processlist.append(process)
  339. #for j in processlist:
  340. # j.join()
  341. #with ProcessPoolExecutor(3) as ppool:
  342. #for i in range(3):
  343. # print('hello')
  344. #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
  345. #ppool.submit(func1, '张三', i)
  346. #ppool.submit(self.preprocess)
  347. #self.postprocess(pred, path, im0s, dataset, im, s)
  348. #process = Process(target=self.postprocess, args=(pred, path, im0s, dataset, im, s))
  349. #self.postprocess(pred, path, im0s, im, s,videotime)
  350. #process.start()
  351. #def preprocess(self):
  352. # print('preprocess-----------------------------------------------')
  353. def postprocess(self, pred, path, im0s, im, s,videotime,channels):
  354. if time.time()-self.updatemtime>300:
  355. if self.filetime !=os.path.getmtime(self.opt.weights):
  356. device = select_device(self.opt.device)
  357. print("load new load")
  358. self.model = DetectMultiBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
  359. self.stride, self.names, self.pt = self.model.stride, self.model.names, self.model.pt
  360. self.filetime = os.path.getmtime(self.opt.weights)
  361. #try:
  362. #if modelalgdir[Path(self.opt.weights).stem]!='0':
  363. print(modelalgdir[Path(self.opt.weights).stem])
  364. try:
  365. rea = requests.post(url=urla,data={'algorithmCode':modelalgdir[Path(self.opt.weights).stem]}).json()['data']
  366. con = rea[0]['confidence']
  367. self.opt.conf_thres = con
  368. except Exception:
  369. print('error')
  370. #else:
  371. # self.opt.conf_thres = 0.25
  372. #except Exception:
  373. #print('posturlaerror')
  374. self.updatemtime = time.time()
  375. seen = 0
  376. # dt = (Profile(), Profile(), Profile())
  377. print(f'senn = {seen}')
  378. windows = []
  379. if Path(self.opt.weights).stem:
  380. labelnamelist = []
  381. with self.dt[2]:
  382. #print(f'cropshape={pred.shape}')
  383. if self.opt.v8:
  384. from ultralytics.utils.ops import non_max_suppression
  385. else:
  386. from utils.general import non_max_suppression
  387. pred = non_max_suppression(pred, self.opt.conf_thres, self.opt.iou_thres, self.opt.classes,
  388. self.opt.agnostic_nms, max_det=self.opt.max_det)
  389. # Second-stage classifier (optional)
  390. # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
  391. if self.classify and Path(self.opt.weights).stem!='persontre':
  392. if Path(self.opt.weights).stem == 'arm':
  393. pred = apply_classifierarm(pred,self.classifier_model,im,im0s,Path(self.opt.weights).stem)
  394. else:
  395. pred = apply_classifier1(pred,self.classifier_model,im,im0s,Path(self.opt.weights).stem)
  396. # Process predictions
  397. #print(f'predshape={pred.shape}')
  398. for i, det in enumerate(pred): # per image
  399. if time.time()-self.updatetime>300:
  400. dataele = {
  401. "algorithmCode": self.dirmodel[channels[i]]['classindex'],
  402. "algorithmIp":self.dirmodel[channels[i]]['algip'],
  403. "channel":self.dirmodel[channels[i]]['channel']
  404. }
  405. try:
  406. resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
  407. resultele = resultele.split(',||')
  408. resultele = tuple(resultele)
  409. point = '%s:'*len(resultele) %resultele
  410. if len(point[:-2])>1:
  411. self.dirmodel[channels[i]]['point'] = point[:-2]
  412. except Exception:
  413. print('post error')
  414. if Path(self.opt.weights).stem == 'personcount':
  415. try:
  416. resultper = requests.post(url=urlperson,data=dataele).json()['data']
  417. personcountdir[channels[i]] = int(resultper)
  418. except Exception:
  419. print('urlpersonerror')
  420. if Path(self.opt.weights).stem == 'sleep' or Path(self.opt.weights).stem == 'duty' :
  421. datatime= {
  422. "algorithmCode": self.dirmodel[channels[i]]['classindex'],
  423. "algorithmIp":self.dirmodel[channels[i]]['algip'],
  424. "channel":self.dirmodel[channels[i]]['channel']
  425. }
  426. try:
  427. resulttime = requests.post(url=urltime,data=dataele).json()['data']
  428. self.dirmodel[channel]['durtime'] = int(resulttime)
  429. except Exception:
  430. print('posttime error')
  431. self.updatetime = time.time()
  432. seen += 1
  433. if self.webcam: # batch_size >= 1
  434. p, im0 = path[i], im0s[i].copy()
  435. s += f'{i}: '
  436. else:
  437. p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
  438. p = Path(p) # to Path
  439. save_path = str(self.save_dir / p.name) # im.jpg
  440. #txt_path = str(self.save_dir / 'labels' / p.stem) + (
  441. # '' #if dataset.mode == 'image' else f'_{frame}') # im.txt
  442. s += '%gx%g ' % im.shape[2:] # print string
  443. gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
  444. imc = im0.copy() # for save_crop
  445. annotator = Annotator(im0, line_width=self.opt.line_thickness, example=str(self.names))
  446. flag = False
  447. if len(det) and Path(self.opt.weights).stem != 'duty':
  448. #flag = True
  449. # Rescale boxes from img_size to im0 size
  450. det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
  451. # Print results
  452. for c in det[:, 5].unique():
  453. n = (det[:, 5] == c).sum() # detections per class
  454. s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
  455. # Write results
  456. if Path(self.opt.weights).stem in ['arm', 'uniform']:
  457. personpred = self.personmodel(im[i][None], None, None)
  458. personpred = non_max_suppression(personpred, 0.7, self.opt.iou_thres, 0,
  459. self.opt.agnostic_nms, max_det=self.opt.max_det)
  460. if len(personpred[0])==0:
  461. flag = False
  462. elif Path(self.opt.weights).stem == 'other':
  463. persondet = []
  464. personpred = personpred[0]
  465. personpred[:, :4] = scale_boxes(im.shape[2:], personpred[:, :4], im0.shape).round()
  466. for *perxyxy,conf,cls in reversed(personpred):
  467. print(perxyxy)
  468. x1,y1,x3,y3 = perxyxy
  469. x1,y1,x3,y3 = int(x1),int(y1),int(x3),int(y3)
  470. x2,y2 = x3,y1
  471. x4,y4 = x1,y3
  472. flag = self.taskname.getflag(det, persondet,annotator, self.dirmodel[channels[i]]['fence'],
  473. self.dirmodel[channels[i]]['point'], self.names,
  474. self.dirmodel[channels[i]]['label'])
  475. else:
  476. persondet = []
  477. personpred = personpred[0]
  478. personpred[:, :4] = scale_boxes(im.shape[2:], personpred[:, :4], im0.shape).round()
  479. for *perxyxy,conf,cls in reversed(personpred):
  480. print(perxyxy)
  481. if conf<0.8:
  482. continue
  483. x1,y1,x3,y3 = perxyxy
  484. x1,y1,x3,y3 = int(x1),int(y1),int(x3),int(y3)
  485. x2,y2 = x3,y1
  486. x4,y4 = x1,y3
  487. persondet.append([x1,y1,x2,y2,x3,y3,x4,y4])
  488. flag = self.taskname.getflag(det,persondet,annotator,self.dirmodel[channels[i]]['fence'],self.dirmodel[channels[i]]['point'],self.names,self.dirmodel[channels[i]]['label'])
  489. else:
  490. if Path(self.opt.weights).stem in ['personcount']:
  491. flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
  492. self.dirmodel[channels[i]]['point'], self.names,
  493. self.dirmodel[channels[i]]['label'],personcountdir[channels[i]])
  494. elif Path(self.opt.weights).stem in ['persontre']:
  495. flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
  496. self.dirmodel[channels[i]]['point'], self.names,
  497. self.dirmodel[channels[i]]['label'],1,imc)
  498. else:
  499. flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
  500. self.dirmodel[channels[i]]['point'], self.names,
  501. self.dirmodel[channels[i]]['label'])
  502. if flag:
  503. #if self.dirmodel[channels[i]]['imgtime'] != videotime[i]:
  504. self.dirmodel[channels[i]]['detframe'].pop(0)
  505. self.dirmodel[channels[i]]['detframe'].append(1)
  506. self.dirmodel[channels[i]]['preim'] = annotator.result()
  507. self.dirmodel[channels[i]]['oripreim'] = imc
  508. self.dirmodel[channels[i]]['posttime'] = videotime[i]
  509. print(self.dirmodel[channels[i]]['detframe'])
  510. #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
  511. else:
  512. #print(f'deti= {i}')
  513. #print(detframe[i])
  514. #if self.dirmodel[channels[i]]['imgtime'] != videotime[i]:
  515. self.dirmodel[channels[i]]['detframe'].pop(0)
  516. self.dirmodel[channels[i]]['detframe'].append(0)
  517. print(self.dirmodel[channels[i]]['detframe'])
  518. #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
  519. #print(detframe[i])
  520. # Stream results
  521. #im0 = annotator.result()
  522. #print(f'i = {i}')
  523. #print(channels[i])
  524. #print(postpretime[i])
  525. #print(detframe[i])
  526. if not self.dirmodel[channels[i]]['detflag'] and self.dirmodel[channels[i]]['detframe'].count(1)>=1:
  527. self.dirmodel[channels[i]]['detflag'] = True
  528. self.dirmodel[channels[i]]['detpretime'] = time.time()
  529. elif self.dirmodel[channels[i]]['detframe'].count(1)==0 :
  530. self.dirmodel[channels[i]]['detflag'] = False
  531. self.dirmodel[channels[i]]['detpretime'] = float('inf')
  532. # Stream results
  533. #im0 = annotator.result()
  534. if time.time() - self.dirmodel[channels[i]]['postpretime'] >30 and time.time() - self.dirmodel[channels[i]]['detpretime'] > self.dirmodel[channels[i]]['durtime'] and self.dirmodel[channels[i]]['detflag']:
  535. #print()
  536. #if time.time() - self.dirmodel[channels[i]]['postpretime'] >30 and self.dirmodel[channels[i]]['detflag']:
  537. #print(time.time() - self.dirmodel[channels[i]]['detpretime'])
  538. #print(self.dirmodel[channels[i]]['detflag'])
  539. print('post-------------------------------------------------------------------------')
  540. #time.sleep(30)
  541. #print(time.time() - postpretime[i])
  542. #print('111111111111111111111111111111111111111111111111')
  543. #print(dirmodel[channels[i]]['preim'].shape)
  544. success, encoded_image = cv2.imencode('.jpg', self.dirmodel[channels[i]]['preim'])
  545. content = encoded_image.tobytes()
  546. successori, encoded_imageori = cv2.imencode('.jpg', self.dirmodel[channels[i]]['oripreim'])
  547. contentori = encoded_imageori.tobytes()
  548. filename = f'{p.stem}_{int(time.time())}.jpg'
  549. filenameori = f'ori{p.stem}_{int(time.time())}.jpg'
  550. print(f'str(p) {p.name}')
  551. print(channels[i])
  552. payload = {'channel': self.dirmodel[channels[i]]['channel'],
  553. 'classIndex': self.dirmodel[channels[i]]['classindex'],
  554. 'ip': self.dirmodel[channels[i]]['algip'],
  555. 'videoTime': time.strftime('%Y-%m-%d %H:%M:%S', self.dirmodel[channels[i]]['posttime']),
  556. 'videoUrl': channels[i]}
  557. files = [
  558. ('file', (filename, content, 'image/jpeg')),
  559. ('oldFile', (filenameori, contentori, 'image/jpeg')),
  560. ]
  561. try:
  562. result = requests.post(url, data=payload, files=files)
  563. print(result)
  564. except Exception:
  565. print('posterror')
  566. #time.sleep(3000)
  567. self.dirmodel[channels[i]]['postpretime'] = time.time()
  568. self.dirmodel[channels[i]]['detflag'] = False
  569. timesave = time.strftime('%Y-%m-%d-%H:%M:%S', self.dirmodel[channels[i]]['posttime'])
  570. year = time.strftime('%Y',time.localtime(time.time()))
  571. month = time.strftime('%m',time.localtime(time.time()))
  572. day = time.strftime('%d',time.localtime(time.time()))
  573. savefold = f'/mnt/project/images/{Path(self.opt.weights).stem}/{year}/{month}/{day}'
  574. savefold = Path(savefold)
  575. savefold.mkdir(parents=True,exist_ok=True)
  576. detsavefold = f'/mnt/project/detimages/{Path(self.opt.weights).stem}/{year}/{month}/{day}'
  577. detsavefold = Path(detsavefold)
  578. detsavefold.mkdir(parents=True,exist_ok=True)
  579. cv2.imwrite(f'{savefold}/{timesave}.jpg',self.dirmodel[channels[i]]['oripreim'])
  580. cv2.imwrite(f'{detsavefold}/{timesave}det.jpg',self.dirmodel[channels[i]]['preim'])
  581. #if self.dirmodel[channels[i]]['detframe'].count(1)==0:
  582. # self.dirmodel[channels[i]]['detflag'] = False
  583. #time.sleep(1)
  584. self.view_img = False
  585. if self.view_img:
  586. if platform.system() == 'Linux' and p not in windows:
  587. windows.append(p)
  588. cv2.namedWindow(f'{str(p)}-{Path(self.opt.weights).stem}',
  589. cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
  590. cv2.resizeWindow(f'{str(p)}-{Path(self.opt.weights).stem}', im0.shape[1], im0.shape[0])
  591. im1 = cv2.resize(im0, (1280, 720))
  592. cv2.imshow(f'{str(p)}-{Path(self.opt.weights).stem}', im1)
  593. cv2.waitKey(1) # 1 millisecond
  594. # Save results (image with detections)
  595. # Print time (inference-only)
  596. print(f'channels[i]={channels[i]}')
  597. LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{self.dt[1].dt * 1E3:.1f}ms {str(p)}-{Path(self.opt.weights).stem}")
  598. def load(self):
  599. conn = sqlite3.connect(self.source)
  600. c = conn.cursor()
  601. while True:
  602. #
  603. # print ("数据库打开成功")
  604. cursor = c.execute(
  605. "SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname= (?)",(Path(self.opt.weights).stem,))
  606. # content = cursor.fetchall()
  607. # if content[0][1] ==1 or content[0][2] ==1:
  608. # c.execute("UPDATE CHANGESTREAM set streamimg = 1 where modelname='yolov5s'")
  609. # print("updata changestream")
  610. # conn.commit()
  611. # cursor = c.execute(
  612. # "SELECT modelname, addstream,delstream,streamimg from CHANGESTREAM WHERE modelname='yolov5s'")
  613. self.contentid = cursor.fetchall()
  614. #global tag
  615. #tag = Value('i', self.contentid[0][3])
  616. #print(tag.value==1)
  617. print(f'loadcontent={self.contentid[0][3]}')
  618. time.sleep(3)
  619. c.close()
  620. conn.close()
  621. def readpoint(self):
  622. data = {
  623. "algorithmCode": modelalgdir[Path(self.opt.weights).stem],
  624. "deviceIp":None,
  625. 'fwqCode':None
  626. }
  627. self.dirmodel = {}
  628. result = requests.post(url=urlt,data=data).json()['data']
  629. channell=[]
  630. for info in result:
  631. #content = cursor.fetchall()
  632. #self.dirmodel = {}
  633. #for address,fence,point,channel,classindex,ip ,algip,label,durtime in content:
  634. #address = f'{address[:-1]}0'
  635. channel = info["deviceChannel"]
  636. if Path(self.opt.weights).stem == "danager" and channel =='45':
  637. continue
  638. channell.append(channel)
  639. self.dirmodel[channel] = {}
  640. self.dirmodel[channel]['fence'] = 1 if len(info["electricFence"])>0 else 0
  641. if Path(self.opt.weights).stem == "uniform":
  642. self.dirmodel[channel]['fence'] = 1
  643. #self.dirmodel[channel]['point'] = point
  644. self.dirmodel[channel]['channel'] = info['deviceChannel']
  645. self.dirmodel[channel]['classindex'] = info['algorithmCode']
  646. self.dirmodel[channel]['ip'] = info['deviceIp']
  647. self.dirmodel[channel]['algip'] = info['deviceAlgorithmIp']
  648. dataele = {
  649. "algorithmCode": self.dirmodel[channel]['classindex'],
  650. "algorithmIp":self.dirmodel[channel]['algip'],
  651. "channel":self.dirmodel[channel]['channel']
  652. }
  653. resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
  654. resultele = resultele.split(',||')
  655. resultele = tuple(resultele)
  656. point = '%s:'*len(resultele) %resultele
  657. if Path(self.opt.weights).stem == 'personcount':
  658. resultper = requests.post(url=urlperson,data=dataele).json()['data']
  659. personcountdir[channel] = int(resultper)
  660. if (Path(self.opt.weights).stem == "uniform" or Path(self.opt.weights).stem == "fall") and len(point[:-2])<=1:
  661. self.dirmodel[channel]['point'] = "150#144,1100#144,1100#550,150#550"
  662. else:
  663. self.dirmodel[channel]['point'] = point[:-2]
  664. self.dirmodel[channel]['preim'] = None
  665. self.dirmodel[channel]['oripreim'] = None
  666. self.dirmodel[channel]['detframe'] = [0 for _ in range(2)]
  667. self.dirmodel[channel]['postpretime'] = 0
  668. self.dirmodel[channel]['detflag'] = False
  669. self.dirmodel[channel]['detpretime'] = float('inf')
  670. self.dirmodel[channel]['label'] = modellabeldir[data['algorithmCode']]
  671. if Path(self.opt.weights).stem == 'sleep' or Path(self.opt.weights).stem == 'duty' :
  672. datatime= {
  673. "algorithmCode": self.dirmodel[channel]['classindex'],
  674. "algorithmIp":self.dirmodel[channel]['algip'],
  675. "channel":self.dirmodel[channel]['channel']
  676. }
  677. resulttime = requests.post(url=urltime,data=dataele).json()['data']
  678. self.dirmodel[channel]['durtime'] = int(resulttime)
  679. else:
  680. self.dirmodel[channel]['durtime'] = 0
  681. self.dirmodel[channel]['posttime'] = 0
  682. print(self.dirmodel)
  683. return sorted(channell)
  684. # str = str.split(":")
  685. # lista = []
  686. # for liststr in str:
  687. # if len(liststr) > 0:
  688. # li = liststr.split(',')
  689. # listpoint = []
  690. # for i, j in zip(li[::2], li[1::2]):
  691. # listpoint.append((i, j))
  692. # lista.append(listpoint)
  693. # return listpoint
  694. #def preprocess():
  695. # print('preprocess-----------------------------------------------')
  696. def getframe(queuelist,channelsl,source,tt,numworks,lock,numworkv):
  697. while True:
  698. print("dataloader")
  699. imgsz = [768, 768]
  700. print(f'source = {source}')
  701. dataset = LoadStreamsSQLTN(channelsl,source, img_size=832,
  702. auto=True, vid_stride=20, tt=tt,numworks = numworks)
  703. bs = len(dataset)
  704. vid_path, vid_writer = [None] * bs, [None] * bs
  705. # self.detframe = [[0 for _ in range(8)] for i in range(bs)]
  706. # self.postpretime = [0]*bs
  707. # Run inference
  708. #imgsz = (1 , 3, *self.imgsz)
  709. print(imgsz)
  710. #self.model.warmup(imgsz=(1 , 3, *imgsz)) # warmup
  711. seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
  712. #
  713. # print ("数据库打开成功")
  714. pretime = time.time()
  715. tag = 0
  716. sourcebase = 'project0117.db'
  717. for path, im, im0s, vid_cap, s, videotime,channels in dataset:
  718. # print('*'*21)
  719. # global tag
  720. # print('datasetvideo')
  721. # if time.time()-pretime > 300:
  722. # pretime = time.time()
  723. # conn = sqlite3.connect(sourcebase)
  724. # c = conn.cursor()
  725. # cursor = c.execute("SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname= 'helmet'")
  726. # contentid = cursor.fetchall()
  727. # tag = contentid[0][3]
  728. # if tag == 1:
  729. # lock.acquire()
  730. # numworkv.value += 1
  731. # dataset.close()
  732. # if numworkv.value==3:
  733. # print('newstreaming=', tag)
  734. # conn = sqlite3.connect(source)
  735. # c = conn.cursor()
  736. # c.execute("UPDATE CHANGESTREAM set streaming = 0 , addstream=0,delstream=0 where modelname='helmet'")
  737. # print(123)
  738. # conn.commit()
  739. # c.close()
  740. # conn.close()
  741. # lock.release()
  742. # print('opencv1')
  743. # # cv2.destroyAllWindows()
  744. # print('opencv')
  745. # break
  746. # else:
  747. # print('nonewstreaming=', tag)
  748. if time.time()-pretime > 300:
  749. channellist = []
  750. pretime = time.time()
  751. data = {
  752. "algorithmCode": None,
  753. "deviceIp":None,
  754. "fwqCode":None
  755. }
  756. try:
  757. result = requests.post(url=urlt,data=data).json()['data']
  758. except Exception:
  759. result = []
  760. for info in result:
  761. data = {
  762. "channel": info["deviceChannel"],
  763. "ip": info["deviceAlgorithmIp"]
  764. }
  765. chaflag = any(info["deviceChannel"] in t for t in channellist)
  766. #personcountdir[channel] = num
  767. if not chaflag:
  768. address = requests.post(url=urlrtsp,data=data).json()['msg']
  769. channellist.append((info['deviceChannel'],address))
  770. channelsa = []
  771. sourcea = []
  772. channellist = set(channellist)
  773. channellist = sorted(channellist,key=lambda x:x[0])
  774. #channellist = set(channellist)
  775. for cha,add in channellist:
  776. channelsa.append(cha)
  777. sourcea.append(add)
  778. channelsl = sorted(channelsl)
  779. #channelsa = sorted(channelsa)
  780. if channelsa!=channelsl and len(channelsa)>0:
  781. print(f'channelsa = {channelsa}')
  782. print(f'channelsl = {channelsl}')
  783. dataset.close()
  784. channelsl = channelsa
  785. source = sourcea
  786. break;
  787. for key,value in queuelist.items():
  788. hour = time.localtime(time.time()).tm_hour
  789. if hour in range(7,18):
  790. value[-1].put((path, im, im0s, vid_cap, s, videotime,channels))
  791. value[-1].get() if value[-1].qsize() == 10 else time.sleep(0.001)
  792. def getmutpro(channels,source,streamlist,numworkv,lock,numworks=1,modellen=None):
  793. processlist = []
  794. queuelist = {}
  795. for i in range(numworks):
  796. for model in modellen:
  797. queue = Queue(maxsize=10)
  798. queuelist.setdefault(model,[])
  799. queuelist[model].append(queue)
  800. process = Process(target=getframe,
  801. args=(queuelist, channels,source, i,numworks,lock,numworkv))
  802. processlist.append(process)
  803. process.start()
  804. #queuelist.append(queue)
  805. return queuelist
  806. # path = []
  807. # im0s = []
  808. # vid_cap = None
  809. # s = ''
  810. # videotime = []
  811. # while True:
  812. # imlist = []
  813. # pathlist = []
  814. # im0slist = []
  815. # channelslist = []
  816. # vid_cap = None
  817. # s = ''
  818. # videotimelist = []
  819. # for q in queuelist:
  820. # if q.qsize()>0:
  821. # setframe = q.get()
  822. # path, im, im0s, vid_cap, s, videotime ,channels = setframe
  823. # pathlist += path
  824. # channelslist +=channels
  825. # im0slist += im0s
  826. # videotimelist += videotime
  827. # imlist.append(im)
  828. # if len(imlist)>0:
  829. # im = np.concatenate(imlist)
  830. # if len(pathlist)>0:
  831. # print(len(path),im.shape,len(im0s))
  832. # streamlist.append((pathlist, im, im0slist, vid_cap, s, videotimelist,channelslist))
  833. #print(f'streamlist = {len(streamlist)}')
  834. # streamlist.pop(0) if len(streamlist) > 3 else time.sleep(0.001)
  835. def modelfun(queue,weights,sourcedb,classes,device,classify,conf_thres,runmodel,v8=False):
  836. print(weights)
  837. detectdemo=Detect(weights=weights,source=sourcedb,classes=classes,device=device,classify=classify,conf_thres=conf_thres,v8=v8)
  838. detectdemo.infer(queue,runmodel)
  839. def parse_opt():
  840. parser = argparse.ArgumentParser()
  841. parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path or triton URL')
  842. opt = parser.parse_args()
  843. return opt
  844. def main(opt):
  845. check_requirements(ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
  846. run(**vars(opt))
  847. if __name__ == '__main__':
  848. #torch.multiprocessing.set_start_method('spawn')
  849. #set_start_method('spawn')
  850. opt = parse_opt()
  851. dbpath = 'projectnew.db'
  852. conn = sqlite3.connect(dbpath)
  853. #
  854. # print ("数据库打开成功")
  855. c = conn.cursor()
  856. task(c,conn,urlt,urla)
  857. cursor = c.execute('select channel,algip from stream ')
  858. result = cursor.fetchall()
  859. for channel ,algip in result:
  860. data = {
  861. "channel": channel,
  862. "ip":algip
  863. }
  864. #personcountdir[channel] = num
  865. address = requests.post(url=urlrtsp,data=data).json()['msg']
  866. c.execute('UPDATE STREAM set address= (?) where channel =(?)',(address,channel))
  867. conn.commit()
  868. cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet' or modelname = 'smoke' or modelname = 'uniform' or modelname = 'fire' or modelname ='duty' or modelname = 'sleep' or modelname='occupancy' or modelname = 'personcar' or modelname = 'phone' or modelname = 'reflective' or modelname = 'extinguisher' or modelname = 'danager' or modelname = 'inspection' or modelname = 'cross' or modelname = 'personcount' or modelname= 'arm' or modelname = 'persontre' or modelname = 'bag'")
  869. #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
  870. content = cursor.fetchall()
  871. cursor = c.execute("SELECT address,channel from STREAM ")
  872. #cursor = c.execute("SELECT address from STREAM where modelname = 'helmet'")
  873. contenta = cursor.fetchall()
  874. source = []
  875. modellist = []
  876. addcha = []
  877. channellist = []
  878. for i in contenta:
  879. addcha.append((i[0],i[1]))
  880. #modellist.append(i[1])
  881. addcha = set(addcha)
  882. addcha = sorted(addcha,key=lambda x:x[1])
  883. for add,cha in addcha:
  884. source.append(add)
  885. channellist.append(cha)
  886. #source = set(source)
  887. print(addcha)
  888. source = list(source)
  889. cursor = c.execute("SELECT modelname from STREAM where (modelname ='helmet' or modelname = 'smoke' or modelname = 'uniform' or modelname = 'fire' or modelname = 'duty' or modelname = 'sleep' or modelname='occupancy' or modelname = 'personcar' or modelname = 'phone' or modelname = 'reflective' or modelname = 'extinguisher' or modelname = 'danager' or modelname = 'inspection' or modelname = 'cross' or modelname = 'personcount' or modelname = 'arm' or modelname = 'persontre' or modelname = 'bag')")
  890. contentm = cursor.fetchall()
  891. for m in contentm:
  892. modellist.append(m[0])
  893. modellist = set(modellist)
  894. modellist = list(modellist)
  895. contentlist = []
  896. for i in content:
  897. contentlist.append(i[0])
  898. #source.sort()
  899. n = len(content)
  900. print(f'modelname={n}')
  901. print(content)
  902. #content.reverse()
  903. print(content)
  904. print(source)
  905. # main(opt)
  906. #processes = []
  907. streamqueue = Queue(maxsize=4)
  908. numworkv = Value('i', 0)
  909. manager = Manager()
  910. lock = multiprocessing.Lock()
  911. streamlist = manager.list()
  912. numworks = 7
  913. modellen = []
  914. for i in modellist:
  915. if i in contentlist:
  916. modellen.append(i)
  917. queuelist = getmutpro(channellist,source, streamlist, numworkv, lock, numworks,modellen)
  918. deid = 0
  919. #pool = ThreadPoolExecutor(max_workers=n)
  920. runmodel = manager.dict()
  921. while True:
  922. for i in modellist:
  923. if i in contentlist:
  924. if i not in runmodel:
  925. #print(i)
  926. #detectdemo=Detect(weights=f'/mnt/project/yolodemo/yolov5-master/{i[0]}.pt')
  927. c.execute('select conf,cla from changestream where modelname = (?)',(i,))
  928. rea = c.fetchall()
  929. print(f'weights ={i[0]}.pt')
  930. if i in ['duty','danager','inspection','cross','personcount']:
  931. process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,[0],0,rea[0][1],rea[0][0],runmodel,True))
  932. else:
  933. if i in ['fall','extinguisher']:
  934. process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,None,0,rea[0][1],rea[0][0],runmodel))
  935. else:
  936. process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,None,0,rea[0][1],rea[0][0],runmodel,True))
  937. #elif i in ['helmet','fire','smoke','fall']:
  938. #process = Process(target=modelfun,args=(queuelist,f'{i}.pt',dbpath,None,deid%2,True))
  939. #else:
  940. #process = Process(target=modelfun,args=(queuelist,f'{i}.pt',dbpath,None,deid%2,False))
  941. #processes.append(process)
  942. #process.start()
  943. #detectobj = Process(target=detectdemo.infer,args=(queue,))
  944. # Detect(weights=f'{i[0]}.pt')
  945. time.sleep(3)
  946. process.start()
  947. deid = deid+1
  948. runmodel[i] = 1
  949. time.sleep(600)
  950. task(c,conn,urlt,urla)
  951. #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'")
  952. #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
  953. cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet' or modelname = 'smoke' or modelname = 'uniform' or modelname = 'fire' or modelname ='duty' or modelname = 'sleep' or modelname='occupancy' or modelname = 'personcar' or modelname = 'phone' or modelname = 'reflective' or modelname = 'extinguisher' or modelname = 'danager' or modelname = 'inspection' or modelname = 'cross' or modelname = 'personcount' or modelname = 'arm' or modelname = 'persontre' or modelname = 'bag'")
  954. content = cursor.fetchall()
  955. contentlist = []
  956. for con in content:
  957. contentlist.append(con[0])
  958. #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'")
  959. cursor = c.execute("SELECT address,channel from STREAM ")
  960. contenta = cursor.fetchall()
  961. source = []
  962. modellist = []
  963. addcha = []
  964. channellist = []
  965. for i in contenta:
  966. addcha.append((i[0],i[1]))
  967. #modellist.append(i[1])
  968. addcha = set(addcha)
  969. addcha = sorted(addcha)
  970. for a,cha in addcha:
  971. source.append(a)
  972. channellist.append(cha)
  973. print(addcha)
  974. #source = set(source)
  975. source = list(source)
  976. #source.sort()
  977. cursor = c.execute("SELECT modelname from STREAM where (modelname = 'helmet' or modelname = 'smoke' or modelname = 'uniform' or modelname = 'fire' or modelname = 'duty' or modelname = 'sleep' or modelname='occupancy' or modelname = 'personcar' or modelname = 'phone' or modelname = 'reflective' or modelname = 'extinguisher' or modelname = 'danager' or modelname = 'inspection' or modelname = 'cross' or modelname = 'personcount' or modelname = 'arm' or modelname = 'persontre' or modelname = 'bag')")
  978. contentm = cursor.fetchall()
  979. for m in contentm:
  980. modellist.append(m[0])
  981. modellist = set(modellist)
  982. n = len(content)
  983. print(f'modelname={n}')
  984. print(content)
  985. #content.reverse()
  986. print(content)
  987. #pool.submit(detectobj.infer)
  988. #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'fall'")
  989. #content = cursor.fetchall()
  990. #n = len(content)
  991. #print(f'modelname={n}')
  992. #print(content)
  993. #content.reverse()
  994. #print(content)
  995. # main(opt)
  996. #processes = []
  997. #pool = ProcessPoolExecutor(max_workers=n)
  998. #for i in content:
  999. #print(i)
  1000. #detectdemo=Detect(weights=f'{i[0]}.pt')
  1001. #process = Process(target=detectdemo.infer)
  1002. #processes.append(process)
  1003. #process.start()
  1004. #detectobj = Detect(weights=f'{i[0]}.pt')
  1005. # time.sleep(3)
  1006. #pool.submit(detectobj.infer)
  1007. # print('111111111111111111111111111111111111111111111111111111111')
  1008. #pool.submit(TestA().func1, '张三', i)
  1009. #print('----------------------------------------------------------------')
  1010. #time.sleep(3000)
  1011. # 等待所有进程执行完毕
  1012. #for process in processes:
  1013. # process.join()
  1014. #pool.submit(Detect(weights=f'{i[0]}.pt').infer)
  1015. # if isinstance(opt.weights,list):
  1016. # opt.weights = opt.weights[0]
  1017. #signal.signal(signal.SIGINT, my_handler)
  1018. #detectdemo1 = Detect(weights=f'{content[0][0]}.pt')
  1019. #detectdemo1.infer()
  1020. #a = Test
  1021. #with ProcessPoolExecutor(3) as ppool:
  1022. #for i in range(3):
  1023. # print('hello')
  1024. #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
  1025. #ppool.submit(TestA().func1, '张三', i)
  1026. #ta = TestA()
  1027. #with ProcessPoolExecutor(5) as ppool: # 创建一个5个进程的进程池
  1028. # for i in range(1, 4):
  1029. # ppool.submit(func1, '张三', i)
  1030. #f1= pool.submit(detectdemo1.infer)
  1031. # print("线程1-----------------------------------------------------------------------------------")
  1032. #detectdemo2 = Detect(weights=r"helmet.pt")
  1033. #f2=pool.submit(detectdemo2.infer)
  1034. # print("线程2-------------------------------------------------------------------------------------")
  1035. #detectdemo3 = threading.Thread(target=detectdemo3.infer)
  1036. #detectdemo3 = Detect(weights=r"fall.pt")
  1037. #f3=pool.submit(detectdemo3.infer)