123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154 |
- # YOLOv5 �� by Ultralytics, AGPL-3.0 license
- """
- Run YOLOv5 detection inference on images, videos, directories, globs, YouTube, webcam, streams, etc.
- Usage - sources:
- $ python detect.py --weights yolov5s.pt --source 0 # webcam
- img.jpg # image
- vid.mp4 # video
- screen # screenshot
- path/ # directory
- list.txt # list of images
- list.streams # list of streams
- 'path/*.jpg' # glob
- 'https://youtu.be/Zgi9g1ksQHc' # YouTube
- 'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
- Usage - formats:
- $ python detect.py --weights yolov5s.pt # PyTorch
- yolov5s.torchscript # TorchScript
- yolov5s.onnx # ONNX Runtime or OpenCV DNN with --dnn
- yolov5s_openvino_model # OpenVINO
- yolov5s.engine # TensorRT
- yolov5s.mlmodel # CoreML (macOS-only)
- yolov5s_saved_model # TensorFlow SavedModel
- yolov5s.pb # TensorFlow GraphDef
- yolov5s.tflite # TensorFlow Lite
- yolov5s_edgetpu.tflite # TensorFlow Edge TPU
- yolov5s_paddle_model # PaddlePaddle
- """
- import matplotlib.path as mat
- import requests
- import argparse
- import os
- import platform
- import sqlite3
- import sys
- import threading
- import time
- from pathlib import Path
- import signal
- import torch
- from concurrent.futures import ThreadPoolExecutor
- from concurrent.futures import ProcessPoolExecutor
- from multiprocessing import Process,Manager,Value
- from multiprocessing import Queue
- from multiprocessing import set_start_method
- import multiprocessing
- import multiprocessing as mp
- import numpy as np
- from torchvision import transforms
- FILE = Path(__file__).resolve()
- ROOT = FILE.parents[0] # YOLOv5 root directory
- if str(ROOT) not in sys.path:
- sys.path.append(str(ROOT)) # add ROOT to PATH
- ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
- from models.common import DetectMultiBackend
- from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages,LoadStreams, LoadStreamsSQLNEWN,LoadStreamsSQL,LoadStreamsSQLNRERT,LoadStreamsVEight,LoadStreamsSQLT,LoadStreamsSQLTN
- from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
- increment_path, non_max_suppression, print_args, scale_boxes, strip_optimizer, xyxy2xywh,strtolst,apply_classifier1,apply_classifieruniform,compute_IOU,task,apply_classifierarm)
- from utils.plots import Annotator, colors, save_one_box
- from utils.torch_utils import select_device, smart_inference_mode
- from utils.renwu import newHelmet,newUniform,Fall,Personcount,Arm,Bag,Cross,Extinguisher,Persontre,Bag,Danager
- #from testpool import func1,TestA
- # def my_handler(signum, frame):
- # exit(0)
- #url = "http://36.7.84.146:18802/ai-service/open/api/operate/upload"
- url = "http://172.19.152.231/open/api/operate/upload"
- urlrtsp = "http://172.19.152.231/open/api/operate/previewURLs"
- urlt = "http://172.19.152.231/open/api/operate/taskList"
- urla = "http://172.19.152.231/open/api/operate/algorithmList"
- urlele = "http://172.19.152.231/open/api/operate/fence"
- urltime = "http://172.19.152.231/open/api/operate/getTime"
- urlperson = "http://172.19.152.231/open/api/operate/getPersonLimitNum"
- #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'}
- #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'}
- 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'}
- 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'}
- modelalgdir = {}
- personcountdir = {}
- for key,value in modelnamedir.items():
- modelalgdir[value] = key
- taskmap = {'helmet':newHelmet,'uniform':newUniform,'fall':Fall,'personcount':Personcount,'arm':Arm,'bag':Bag,'cross':Cross,'extinguisher':Extinguisher,'persontre':Persontre,'bag':Bag,'danager':Danager}
- mean, std = [0.485, 0.456, 0.406], [0.229, 0.224, 0.225]
- test = transforms.Compose([transforms.Resize((224,224)),
- #transforms.CenterCrop(224),
- transforms.ToTensor(),
- transforms.Normalize(mean=mean, std=std)
- ])
- def clapre(modelcla,claimg,clapoint):
- imgten = torch.stack(claimg,dim=0)
- clapoint = torch.stack(clapoint,dim=0)
- imgten = imgten.to(0)
- result = modelcla(imgten)
- result = F.softmax(result)
- print(result)
- index = result.argmax(1)
- index = index.cpu().numpy()
- index = np.argwhere(index<5)
- index = index.reshape(-1)
- print(index)
- if len(index)>0:
- print(clapoint[index])
- return clapoint[index]
- else:
- return None
- class YoloOpt:
- def __init__(self, weights=ROOT / 'yolov5s.pt',source=ROOT / 'data/images',data=ROOT / 'data/coco128.yaml',
- imgsz=(640,640),
- conf_thres=0.25,
- iou_thres=0.45,
- max_det=1000,
- device='',
- view_img=False,
- save_txt=False,
- save_conf=False,
- save_crop=False,
- nosave=True,
- classes=None,
- agnostic_nms=False,
- augment=False,
- visualize=False,
- update=False,
- project=ROOT / 'runs/detect',
- name='exp',
- exist_ok=False,
- line_thickness=1,
- hide_labels=False,
- hide_conf=False,
- half=False,
- dnn=False,
- vid_stride=10,
- classify=False,
- v8=False):
- self.weights = weights # 权重文件地址
- self.source = source # 待识别的图像
- self.data = data
- if imgsz is None:
- self.imgsz = (640, 640)
- self.imgsz = imgsz # 输入图片的大小,默认 (640,640)
- self.conf_thres = conf_thres # object置信度阈值 默认0.25 用在nms中
- self.iou_thres = iou_thres # 做nms的iou阈值 默认0.45 用在nms中
- self.device = device # 执行代码的设备,由于项目只能用 CPU,这里只封装了 CPU 的方法
- self.view_img = view_img # 是否展示预测之后的图片或视频 默认False
- self.classes = classes # 只保留一部分的类别,默认是全部保留
- self.agnostic_nms = agnostic_nms # 进行NMS去除不同类别之间的框, 默认False
- self.augment = augment # augmented inference TTA测试时增强/多尺度预测,可以提分
- self.update = update # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
- self.exist_ok = exist_ok # 如果为True,则对所有模型进行strip_optimizer操作,去除pt文件中的优化器等信息,默认为False
- self.project = project # 保存测试日志的参数,本程序没有用到
- self.name = name # 每次实验的名称,本程序也没有用到
- self.max_det = max_det
- self.save_txt = save_txt
- self.save_conf= save_conf
- self.save_crop= save_crop
- self.nosave = nosave
- self.visualize = visualize
- self.line_thickness = line_thickness
- self.hide_labels = hide_labels
- self.hide_conf = hide_conf
- self.half = half
- self.dnn = dnn
- self.vid_stride = vid_stride
- self.classify = classify
- self.v8 = v8
- class Detect:
- 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):
- print(f'detectweights = {weights}')
- if v8:
- from ultralytics.nn.autobackend import AutoBackend
- from ultralytics.utils.ops import non_max_suppression
- else:
- from utils.general import non_max_suppression
- self.opt = YoloOpt(weights=weights, imgsz=imgsz,source=source,classes=classes,device=device,classify=classify,conf_thres=conf_thres,v8=v8)
- self.source = str(self.opt.source)
- self.save_img = not self.opt.nosave and not source.endswith('.txt') # save inference images
- is_file = Path(self.source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
- is_url = self.source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
- self.webcam = self.source.isnumeric() or source.endswith('.db') or (is_url and not is_file)
- screenshot = self.source.lower().startswith('screen')
- if is_url and is_file:
- self.source = check_file(self.source) # download
- self.save_dir = increment_path(Path(self.opt.project) / self.opt.name, exist_ok=self.opt.exist_ok) # increment run
- #self.save_dir = self.save_dir / Path(self.opt.weights).stem
- #self.save_dir.mkdir(parents=True, exist_ok=True)
- (self.save_dir / 'labels' if self.opt.save_txt else self.save_dir).mkdir(parents=True, exist_ok=True) # make dir
- print(f'device = {self.opt.device}')
- device = select_device(self.opt.device)
- if v8:
- self.model = AutoBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- if Path(weights).stem in ['arm', 'uniform']:
- if Path(weights).stem == 'arm':
- self.personmodel = AutoBackend('yolov8m.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- elif Path(weights).stem == 'uniform':
- self.personmodel = AutoBackend('yolo11m.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- else:
- self.model = DetectMultiBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- if Path(weights).stem in ['helmet','arm']:
- self.personmodel = DetectMultiBackend('personcount.pt', device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- self.stride, self.names, self.pt = self.model.stride, self.model.names, self.model.pt
- self.classify = classify
- if self.classify:
- #if Path(weights).stem =='uniform':
- # self.classifier_model = AutoBackend(f"{Path(weights).stem}cls.pt",device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- #else:
- if Path(weights).stem != "arm":
- classifier_model = torch.load(f"{Path(weights).stem}cls.pt")
- self.classifier_model = classifier_model.to(device)
- self.classifier_model.eval()
- else:
- self.classifier_model = AutoBackend(f"{Path(weights).stem}cls.pt",device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- self.imgsz = check_img_size(self.opt.imgsz, s=self.stride)
- self.model.warmup(imgsz=(1 , 3, *self.imgsz))
- self.readpoint()
- print(self.imgsz)
- self.updatetime = time.time()
- self.updatemtime = time.time()
- self.filetime = os.path.getmtime(self.opt.weights)
- self.taskname = taskmap[Path(self.opt.weights).stem]()
- bs = 1 # batch_size
- if self.webcam:
- #self.view_img = check_imshow(warn=True)
- self.view_img = False
- # dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
- # bs = len(dataset)
- elif screenshot:
- dataset = LoadScreenshots(self.source, img_size=self.imgsz, stride=self.stride, auto=self.pt)
- else:
- dataset = LoadImages(self.source, img_size=self.imgsz, stride=self.stride, auto=self.pt, vid_stride=self.opt.vid_stride)
- t1 = threading.Thread(target=self.load,daemon=True)
- t1.start()
- @smart_inference_mode()
- def infer(self,queue,runmodel):
- pretime = time.time()
- seen, windows, self.dt = 0, [], (Profile(), Profile(), Profile())
- #
- # print ("数据库打开成功")
- while True:
- if time.localtime().tm_hour not in range(7,20):
- time.sleep(30)
- continue
- #print('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
- if time.time()-pretime>300:
- ret = self.readpoint()
- pretime = time.time()
- if not ret:
- print(f'{Path(self.opt.weights).stem} {runmodel}')
- runmodel.pop(Path(self.opt.weights).stem)
- print(f'{Path(self.opt.weights).stem} {runmodel}')
- break
- print(f'queuelen = {len(queue)}')
- for que in queue:
- if que.qsize() == 0:
- print('queuezero')
- time.sleep(0.01)
- if que.qsize() > 0:
- #if time.time()-pretime>300:
- # ret = self.readpoint()
- # pretime = time.time()
- # if not ret:
- # print(f'{Path(self.opt.weights).stem} {runmodel}')
- # runmodel.pop(Path(self.opt.weights).stem)
- # print(f'{Path(self.opt.weights).stem} {runmodel}')
- # break
- setframe = que.get()
- # print('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')
- #if setframe is not None
- path, im, im0s, vid_cap, s, videotime ,channels = setframe
- algchannel = list(self.dirmodel.keys())
- print(algchannel)
- print(path)
- algchannel = np.array(algchannel)
- channelsnp = np.array(channels)
- algindex = np.where(np.in1d(channelsnp, algchannel))[0]
- algindex = list(algindex)
- path = np.array(path)
- path = path[algindex]
- path = path.tolist()
- channels = np.array(channels)
- channels = channels[algindex]
- channels = channels.tolist()
- # print(algindex)
- if len(algindex)==0:
- continue
- #for ia in algindex:
- # print(type(im0s[ia]))
- # print(im0s[ia].shape)
- im = im[algindex]
- #for ia in algindex:
- # print(type(ia))
- try:
- im0s = np.asarray(im0s)
- except Exception:
- im0s = np.asarray(im0s,dtype=object)
- print(im0s.shape)
- im0s = im0s[algindex]
- # im0s = im0s.tolist()
- print(f'algindex = {algindex}')
- print(f'im0s ={im0s[0].shape}')
- videotime = np.array(videotime)
- videotime = videotime[algindex]
- videotime = tuple(map(tuple, videotime))
- # global tag
- # if self.contentid[0][3] == 1 :
- # dataset.close()
- # print('newstreaming=', self.contentid[0][3])
- # conn = sqlite3.connect(self.source)
- # c = conn.cursor()
- # c.execute("UPDATE CHANGESTREAM set streamimg = 0 , addstream=0,delstream=0 where modelname= (?)",(Path(self.opt.weights).stem,))
- # print(123)
- # conn.commit()
- # c.close()
- # conn.close()
- # print('opencv1')
- # cv2.destroyAllWindows()
- # print('opencv')
- # break
- # else:
- # print('nonewstreaming=', self.contentid[0][3])
- with self.dt[0]:
- im = torch.from_numpy(im).to(self.model.device)
- im = im.half() if self.model.fp16 else im.float() # uint8 to fp16/32
- im /= 255 # 0 - 255 to 0.0 - 1.0
- if len(im.shape) == 3:
- im = im[None] # expand for batch dim
- # Inference
- with self.dt[1]:
- visualize = increment_path(self.save_dir / Path(path).stem,
- mkdir=True) if self.opt.visualize else False
- #print('error')
- # print(self.model)
- pred = self.model(im, augment=self.opt.augment, visualize=visualize)
- self.postprocess(pred, path, im0s, im, s, videotime,channels)
- # print(f'predshape= {')
- # NMS
- #processlist = []
- #for i in range(3):
- # process = Process(target=self.postprocess,args=(pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s))
- # process = Process(target=self.preprocess)
- # process.start()
- # processlist.append(process)
- #for j in processlist:
- # j.join()
- #with ProcessPoolExecutor(3) as ppool:
- #for i in range(3):
- # print('hello')
- #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
- #ppool.submit(func1, '张三', i)
- #ppool.submit(self.preprocess)
- #self.postprocess(pred, path, im0s, dataset, im, s)
- #process = Process(target=self.postprocess, args=(pred, path, im0s, dataset, im, s))
- #self.postprocess(pred, path, im0s, im, s,videotime)
- #process.start()
- #def preprocess(self):
- # print('preprocess-----------------------------------------------')
- def postprocess(self, pred, path, im0s, im, s,videotime,channels):
-
- if time.time()-self.updatemtime>300:
- if self.filetime !=os.path.getmtime(self.opt.weights):
- device = select_device(self.opt.device)
- print("load new load")
- self.model = DetectMultiBackend(self.opt.weights, device=device, dnn=self.opt.dnn, data=self.opt.data, fp16=self.opt.half)
- self.stride, self.names, self.pt = self.model.stride, self.model.names, self.model.pt
- self.filetime = os.path.getmtime(self.opt.weights)
- #try:
- #if modelalgdir[Path(self.opt.weights).stem]!='0':
- print(modelalgdir[Path(self.opt.weights).stem])
- try:
- rea = requests.post(url=urla,data={'algorithmCode':modelalgdir[Path(self.opt.weights).stem]}).json()['data']
- con = rea[0]['confidence']
- self.opt.conf_thres = con
- except Exception:
- print('error')
- #else:
- # self.opt.conf_thres = 0.25
- #except Exception:
- #print('posturlaerror')
- self.updatemtime = time.time()
- seen = 0
- # dt = (Profile(), Profile(), Profile())
- print(f'senn = {seen}')
- windows = []
- if Path(self.opt.weights).stem:
- labelnamelist = []
- with self.dt[2]:
- #print(f'cropshape={pred.shape}')
- if self.opt.v8:
- from ultralytics.utils.ops import non_max_suppression
- else:
- from utils.general import non_max_suppression
- pred = non_max_suppression(pred, self.opt.conf_thres, self.opt.iou_thres, self.opt.classes,
- self.opt.agnostic_nms, max_det=self.opt.max_det)
- # Second-stage classifier (optional)
- # pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
- if self.classify and Path(self.opt.weights).stem!='persontre':
- if Path(self.opt.weights).stem == 'arm':
- pred = apply_classifierarm(pred,self.classifier_model,im,im0s,Path(self.opt.weights).stem)
- else:
- pred = apply_classifier1(pred,self.classifier_model,im,im0s,Path(self.opt.weights).stem)
- # Process predictions
- #print(f'predshape={pred.shape}')
- for i, det in enumerate(pred): # per image
- if time.time()-self.updatetime>300:
- dataele = {
- "algorithmCode": self.dirmodel[channels[i]]['classindex'],
- "algorithmIp":self.dirmodel[channels[i]]['algip'],
- "channel":self.dirmodel[channels[i]]['channel']
- }
- try:
- resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
- resultele = resultele.split(',||')
- resultele = tuple(resultele)
- point = '%s:'*len(resultele) %resultele
- if len(point[:-2])>1:
- self.dirmodel[channels[i]]['point'] = point[:-2]
- except Exception:
- print('post error')
- if Path(self.opt.weights).stem == 'personcount':
- try:
- resultper = requests.post(url=urlperson,data=dataele).json()['data']
- personcountdir[channels[i]] = int(resultper)
- except Exception:
- print('urlpersonerror')
- if Path(self.opt.weights).stem == 'sleep' or Path(self.opt.weights).stem == 'duty' :
- datatime= {
- "algorithmCode": self.dirmodel[channels[i]]['classindex'],
- "algorithmIp":self.dirmodel[channels[i]]['algip'],
- "channel":self.dirmodel[channels[i]]['channel']
- }
- try:
- resulttime = requests.post(url=urltime,data=dataele).json()['data']
- self.dirmodel[channel]['durtime'] = int(resulttime)
- except Exception:
- print('posttime error')
- self.updatetime = time.time()
- seen += 1
- if self.webcam: # batch_size >= 1
- p, im0 = path[i], im0s[i].copy()
- s += f'{i}: '
- else:
- p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
- p = Path(p) # to Path
- save_path = str(self.save_dir / p.name) # im.jpg
- #txt_path = str(self.save_dir / 'labels' / p.stem) + (
- # '' #if dataset.mode == 'image' else f'_{frame}') # im.txt
- s += '%gx%g ' % im.shape[2:] # print string
- gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh
- imc = im0.copy() # for save_crop
- annotator = Annotator(im0, line_width=self.opt.line_thickness, example=str(self.names))
- flag = False
- if len(det) and Path(self.opt.weights).stem != 'duty':
- #flag = True
- # Rescale boxes from img_size to im0 size
- det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round()
- # Print results
- for c in det[:, 5].unique():
- n = (det[:, 5] == c).sum() # detections per class
- s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
- # Write results
- if Path(self.opt.weights).stem in ['arm', 'uniform']:
- personpred = self.personmodel(im[i][None], None, None)
- personpred = non_max_suppression(personpred, 0.7, self.opt.iou_thres, 0,
- self.opt.agnostic_nms, max_det=self.opt.max_det)
- if len(personpred[0])==0:
- flag = False
- elif Path(self.opt.weights).stem == 'other':
- persondet = []
- personpred = personpred[0]
- personpred[:, :4] = scale_boxes(im.shape[2:], personpred[:, :4], im0.shape).round()
- for *perxyxy,conf,cls in reversed(personpred):
- print(perxyxy)
- x1,y1,x3,y3 = perxyxy
- x1,y1,x3,y3 = int(x1),int(y1),int(x3),int(y3)
- x2,y2 = x3,y1
- x4,y4 = x1,y3
- flag = self.taskname.getflag(det, persondet,annotator, self.dirmodel[channels[i]]['fence'],
- self.dirmodel[channels[i]]['point'], self.names,
- self.dirmodel[channels[i]]['label'])
-
- else:
- persondet = []
- personpred = personpred[0]
- personpred[:, :4] = scale_boxes(im.shape[2:], personpred[:, :4], im0.shape).round()
- for *perxyxy,conf,cls in reversed(personpred):
- print(perxyxy)
- if conf<0.8:
- continue
- x1,y1,x3,y3 = perxyxy
- x1,y1,x3,y3 = int(x1),int(y1),int(x3),int(y3)
- x2,y2 = x3,y1
- x4,y4 = x1,y3
- persondet.append([x1,y1,x2,y2,x3,y3,x4,y4])
- flag = self.taskname.getflag(det,persondet,annotator,self.dirmodel[channels[i]]['fence'],self.dirmodel[channels[i]]['point'],self.names,self.dirmodel[channels[i]]['label'])
- else:
- if Path(self.opt.weights).stem in ['personcount']:
- flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
- self.dirmodel[channels[i]]['point'], self.names,
- self.dirmodel[channels[i]]['label'],personcountdir[channels[i]])
- elif Path(self.opt.weights).stem in ['persontre']:
- flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
- self.dirmodel[channels[i]]['point'], self.names,
- self.dirmodel[channels[i]]['label'],1,imc)
- else:
- flag = self.taskname.getflag(det, None,annotator, self.dirmodel[channels[i]]['fence'],
- self.dirmodel[channels[i]]['point'], self.names,
- self.dirmodel[channels[i]]['label'])
- if flag:
- #if self.dirmodel[channels[i]]['imgtime'] != videotime[i]:
- self.dirmodel[channels[i]]['detframe'].pop(0)
- self.dirmodel[channels[i]]['detframe'].append(1)
- self.dirmodel[channels[i]]['preim'] = annotator.result()
- self.dirmodel[channels[i]]['oripreim'] = imc
- self.dirmodel[channels[i]]['posttime'] = videotime[i]
- print(self.dirmodel[channels[i]]['detframe'])
- #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
- else:
- #print(f'deti= {i}')
- #print(detframe[i])
- #if self.dirmodel[channels[i]]['imgtime'] != videotime[i]:
- self.dirmodel[channels[i]]['detframe'].pop(0)
- self.dirmodel[channels[i]]['detframe'].append(0)
- print(self.dirmodel[channels[i]]['detframe'])
- #self.dirmodel[channels[i]]['imgtime'] = videotime[i]
- #print(detframe[i])
- # Stream results
- #im0 = annotator.result()
- #print(f'i = {i}')
- #print(channels[i])
- #print(postpretime[i])
- #print(detframe[i])
- if not self.dirmodel[channels[i]]['detflag'] and self.dirmodel[channels[i]]['detframe'].count(1)>=1:
- self.dirmodel[channels[i]]['detflag'] = True
- self.dirmodel[channels[i]]['detpretime'] = time.time()
- elif self.dirmodel[channels[i]]['detframe'].count(1)==0 :
- self.dirmodel[channels[i]]['detflag'] = False
- self.dirmodel[channels[i]]['detpretime'] = float('inf')
- # Stream results
- #im0 = annotator.result()
- 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']:
- #print()
- #if time.time() - self.dirmodel[channels[i]]['postpretime'] >30 and self.dirmodel[channels[i]]['detflag']:
- #print(time.time() - self.dirmodel[channels[i]]['detpretime'])
- #print(self.dirmodel[channels[i]]['detflag'])
- print('post-------------------------------------------------------------------------')
- #time.sleep(30)
- #print(time.time() - postpretime[i])
- #print('111111111111111111111111111111111111111111111111')
- #print(dirmodel[channels[i]]['preim'].shape)
- success, encoded_image = cv2.imencode('.jpg', self.dirmodel[channels[i]]['preim'])
- content = encoded_image.tobytes()
- successori, encoded_imageori = cv2.imencode('.jpg', self.dirmodel[channels[i]]['oripreim'])
- contentori = encoded_imageori.tobytes()
- filename = f'{p.stem}_{int(time.time())}.jpg'
- filenameori = f'ori{p.stem}_{int(time.time())}.jpg'
- print(f'str(p) {p.name}')
- print(channels[i])
- payload = {'channel': self.dirmodel[channels[i]]['channel'],
- 'classIndex': self.dirmodel[channels[i]]['classindex'],
- 'ip': self.dirmodel[channels[i]]['algip'],
- 'videoTime': time.strftime('%Y-%m-%d %H:%M:%S', self.dirmodel[channels[i]]['posttime']),
- 'videoUrl': channels[i]}
- files = [
- ('file', (filename, content, 'image/jpeg')),
- ('oldFile', (filenameori, contentori, 'image/jpeg')),
- ]
- try:
- result = requests.post(url, data=payload, files=files)
- print(result)
- except Exception:
- print('posterror')
- #time.sleep(3000)
- self.dirmodel[channels[i]]['postpretime'] = time.time()
- self.dirmodel[channels[i]]['detflag'] = False
- timesave = time.strftime('%Y-%m-%d-%H:%M:%S', self.dirmodel[channels[i]]['posttime'])
- year = time.strftime('%Y',time.localtime(time.time()))
- month = time.strftime('%m',time.localtime(time.time()))
- day = time.strftime('%d',time.localtime(time.time()))
- savefold = f'/mnt/project/images/{Path(self.opt.weights).stem}/{year}/{month}/{day}'
- savefold = Path(savefold)
- savefold.mkdir(parents=True,exist_ok=True)
- detsavefold = f'/mnt/project/detimages/{Path(self.opt.weights).stem}/{year}/{month}/{day}'
- detsavefold = Path(detsavefold)
- detsavefold.mkdir(parents=True,exist_ok=True)
- cv2.imwrite(f'{savefold}/{timesave}.jpg',self.dirmodel[channels[i]]['oripreim'])
- cv2.imwrite(f'{detsavefold}/{timesave}det.jpg',self.dirmodel[channels[i]]['preim'])
- #if self.dirmodel[channels[i]]['detframe'].count(1)==0:
- # self.dirmodel[channels[i]]['detflag'] = False
- #time.sleep(1)
- self.view_img = False
- if self.view_img:
- if platform.system() == 'Linux' and p not in windows:
- windows.append(p)
- cv2.namedWindow(f'{str(p)}-{Path(self.opt.weights).stem}',
- cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
- cv2.resizeWindow(f'{str(p)}-{Path(self.opt.weights).stem}', im0.shape[1], im0.shape[0])
- im1 = cv2.resize(im0, (1280, 720))
- cv2.imshow(f'{str(p)}-{Path(self.opt.weights).stem}', im1)
- cv2.waitKey(1) # 1 millisecond
- # Save results (image with detections)
- # Print time (inference-only)
- print(f'channels[i]={channels[i]}')
- LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{self.dt[1].dt * 1E3:.1f}ms {str(p)}-{Path(self.opt.weights).stem}")
- def load(self):
- conn = sqlite3.connect(self.source)
- c = conn.cursor()
- while True:
- #
- # print ("数据库打开成功")
- cursor = c.execute(
- "SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname= (?)",(Path(self.opt.weights).stem,))
- # content = cursor.fetchall()
- # if content[0][1] ==1 or content[0][2] ==1:
- # c.execute("UPDATE CHANGESTREAM set streamimg = 1 where modelname='yolov5s'")
- # print("updata changestream")
- # conn.commit()
- # cursor = c.execute(
- # "SELECT modelname, addstream,delstream,streamimg from CHANGESTREAM WHERE modelname='yolov5s'")
- self.contentid = cursor.fetchall()
- #global tag
- #tag = Value('i', self.contentid[0][3])
- #print(tag.value==1)
- print(f'loadcontent={self.contentid[0][3]}')
- time.sleep(3)
- c.close()
- conn.close()
- def readpoint(self):
- data = {
- "algorithmCode": modelalgdir[Path(self.opt.weights).stem],
- "deviceIp":None,
- 'fwqCode':None
- }
- self.dirmodel = {}
- result = requests.post(url=urlt,data=data).json()['data']
- channell=[]
- for info in result:
- #content = cursor.fetchall()
- #self.dirmodel = {}
- #for address,fence,point,channel,classindex,ip ,algip,label,durtime in content:
- #address = f'{address[:-1]}0'
- channel = info["deviceChannel"]
- if Path(self.opt.weights).stem == "danager" and channel =='45':
- continue
- channell.append(channel)
- self.dirmodel[channel] = {}
- self.dirmodel[channel]['fence'] = 1 if len(info["electricFence"])>0 else 0
- if Path(self.opt.weights).stem == "uniform":
- self.dirmodel[channel]['fence'] = 1
- #self.dirmodel[channel]['point'] = point
- self.dirmodel[channel]['channel'] = info['deviceChannel']
- self.dirmodel[channel]['classindex'] = info['algorithmCode']
- self.dirmodel[channel]['ip'] = info['deviceIp']
- self.dirmodel[channel]['algip'] = info['deviceAlgorithmIp']
- dataele = {
- "algorithmCode": self.dirmodel[channel]['classindex'],
- "algorithmIp":self.dirmodel[channel]['algip'],
- "channel":self.dirmodel[channel]['channel']
- }
- resultele = requests.post(url=urlele,data=dataele).json()['data']['pointCollections']
- resultele = resultele.split(',||')
- resultele = tuple(resultele)
- point = '%s:'*len(resultele) %resultele
- if Path(self.opt.weights).stem == 'personcount':
- resultper = requests.post(url=urlperson,data=dataele).json()['data']
- personcountdir[channel] = int(resultper)
- if (Path(self.opt.weights).stem == "uniform" or Path(self.opt.weights).stem == "fall") and len(point[:-2])<=1:
- self.dirmodel[channel]['point'] = "150#144,1100#144,1100#550,150#550"
- else:
- self.dirmodel[channel]['point'] = point[:-2]
- self.dirmodel[channel]['preim'] = None
- self.dirmodel[channel]['oripreim'] = None
- self.dirmodel[channel]['detframe'] = [0 for _ in range(2)]
- self.dirmodel[channel]['postpretime'] = 0
- self.dirmodel[channel]['detflag'] = False
- self.dirmodel[channel]['detpretime'] = float('inf')
- self.dirmodel[channel]['label'] = modellabeldir[data['algorithmCode']]
- if Path(self.opt.weights).stem == 'sleep' or Path(self.opt.weights).stem == 'duty' :
- datatime= {
- "algorithmCode": self.dirmodel[channel]['classindex'],
- "algorithmIp":self.dirmodel[channel]['algip'],
- "channel":self.dirmodel[channel]['channel']
- }
- resulttime = requests.post(url=urltime,data=dataele).json()['data']
- self.dirmodel[channel]['durtime'] = int(resulttime)
- else:
- self.dirmodel[channel]['durtime'] = 0
- self.dirmodel[channel]['posttime'] = 0
- print(self.dirmodel)
- return sorted(channell)
- # str = str.split(":")
- # lista = []
- # for liststr in str:
- # if len(liststr) > 0:
- # li = liststr.split(',')
- # listpoint = []
- # for i, j in zip(li[::2], li[1::2]):
- # listpoint.append((i, j))
- # lista.append(listpoint)
- # return listpoint
- #def preprocess():
- # print('preprocess-----------------------------------------------')
- def getframe(queuelist,channelsl,source,tt,numworks,lock,numworkv):
- while True:
- print("dataloader")
- imgsz = [768, 768]
- print(f'source = {source}')
- dataset = LoadStreamsSQLTN(channelsl,source, img_size=832,
- auto=True, vid_stride=20, tt=tt,numworks = numworks)
- bs = len(dataset)
- vid_path, vid_writer = [None] * bs, [None] * bs
- # self.detframe = [[0 for _ in range(8)] for i in range(bs)]
- # self.postpretime = [0]*bs
- # Run inference
- #imgsz = (1 , 3, *self.imgsz)
- print(imgsz)
- #self.model.warmup(imgsz=(1 , 3, *imgsz)) # warmup
- seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
- #
- # print ("数据库打开成功")
- pretime = time.time()
- tag = 0
- sourcebase = 'project0117.db'
- for path, im, im0s, vid_cap, s, videotime,channels in dataset:
- # print('*'*21)
- # global tag
- # print('datasetvideo')
- # if time.time()-pretime > 300:
- # pretime = time.time()
- # conn = sqlite3.connect(sourcebase)
- # c = conn.cursor()
- # cursor = c.execute("SELECT modelname, addstream,delstream,streaming from CHANGESTREAM WHERE modelname= 'helmet'")
- # contentid = cursor.fetchall()
- # tag = contentid[0][3]
- # if tag == 1:
- # lock.acquire()
- # numworkv.value += 1
- # dataset.close()
- # if numworkv.value==3:
- # print('newstreaming=', tag)
- # conn = sqlite3.connect(source)
- # c = conn.cursor()
- # c.execute("UPDATE CHANGESTREAM set streaming = 0 , addstream=0,delstream=0 where modelname='helmet'")
- # print(123)
- # conn.commit()
- # c.close()
- # conn.close()
- # lock.release()
- # print('opencv1')
- # # cv2.destroyAllWindows()
- # print('opencv')
- # break
- # else:
- # print('nonewstreaming=', tag)
- if time.time()-pretime > 300:
- channellist = []
- pretime = time.time()
- data = {
- "algorithmCode": None,
- "deviceIp":None,
- "fwqCode":None
- }
- try:
- result = requests.post(url=urlt,data=data).json()['data']
- except Exception:
- result = []
- for info in result:
- data = {
- "channel": info["deviceChannel"],
- "ip": info["deviceAlgorithmIp"]
- }
- chaflag = any(info["deviceChannel"] in t for t in channellist)
- #personcountdir[channel] = num
- if not chaflag:
- address = requests.post(url=urlrtsp,data=data).json()['msg']
- channellist.append((info['deviceChannel'],address))
- channelsa = []
- sourcea = []
- channellist = set(channellist)
- channellist = sorted(channellist,key=lambda x:x[0])
- #channellist = set(channellist)
- for cha,add in channellist:
- channelsa.append(cha)
- sourcea.append(add)
- channelsl = sorted(channelsl)
- #channelsa = sorted(channelsa)
- if channelsa!=channelsl and len(channelsa)>0:
- print(f'channelsa = {channelsa}')
- print(f'channelsl = {channelsl}')
- dataset.close()
- channelsl = channelsa
- source = sourcea
- break;
- for key,value in queuelist.items():
- hour = time.localtime(time.time()).tm_hour
- if hour in range(7,18):
- value[-1].put((path, im, im0s, vid_cap, s, videotime,channels))
- value[-1].get() if value[-1].qsize() == 10 else time.sleep(0.001)
- def getmutpro(channels,source,streamlist,numworkv,lock,numworks=1,modellen=None):
- processlist = []
- queuelist = {}
- for i in range(numworks):
- for model in modellen:
- queue = Queue(maxsize=10)
- queuelist.setdefault(model,[])
- queuelist[model].append(queue)
- process = Process(target=getframe,
- args=(queuelist, channels,source, i,numworks,lock,numworkv))
- processlist.append(process)
- process.start()
- #queuelist.append(queue)
- return queuelist
- # path = []
- # im0s = []
- # vid_cap = None
- # s = ''
- # videotime = []
- # while True:
- # imlist = []
- # pathlist = []
- # im0slist = []
- # channelslist = []
- # vid_cap = None
- # s = ''
- # videotimelist = []
- # for q in queuelist:
- # if q.qsize()>0:
- # setframe = q.get()
- # path, im, im0s, vid_cap, s, videotime ,channels = setframe
- # pathlist += path
- # channelslist +=channels
- # im0slist += im0s
- # videotimelist += videotime
- # imlist.append(im)
- # if len(imlist)>0:
- # im = np.concatenate(imlist)
- # if len(pathlist)>0:
- # print(len(path),im.shape,len(im0s))
- # streamlist.append((pathlist, im, im0slist, vid_cap, s, videotimelist,channelslist))
- #print(f'streamlist = {len(streamlist)}')
- # streamlist.pop(0) if len(streamlist) > 3 else time.sleep(0.001)
- def modelfun(queue,weights,sourcedb,classes,device,classify,conf_thres,runmodel,v8=False):
- print(weights)
- detectdemo=Detect(weights=weights,source=sourcedb,classes=classes,device=device,classify=classify,conf_thres=conf_thres,v8=v8)
- detectdemo.infer(queue,runmodel)
- def parse_opt():
- parser = argparse.ArgumentParser()
- parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolov5s.pt', help='model path or triton URL')
- opt = parser.parse_args()
- return opt
- def main(opt):
- check_requirements(ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
- run(**vars(opt))
- if __name__ == '__main__':
- #torch.multiprocessing.set_start_method('spawn')
- #set_start_method('spawn')
- opt = parse_opt()
- dbpath = 'projectnew.db'
-
- conn = sqlite3.connect(dbpath)
- #
- # print ("数据库打开成功")
- c = conn.cursor()
- task(c,conn,urlt,urla)
- cursor = c.execute('select channel,algip from stream ')
- result = cursor.fetchall()
- for channel ,algip in result:
- data = {
- "channel": channel,
- "ip":algip
- }
- #personcountdir[channel] = num
- address = requests.post(url=urlrtsp,data=data).json()['msg']
- c.execute('UPDATE STREAM set address= (?) where channel =(?)',(address,channel))
- conn.commit()
- 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'")
- #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
- content = cursor.fetchall()
- cursor = c.execute("SELECT address,channel from STREAM ")
- #cursor = c.execute("SELECT address from STREAM where modelname = 'helmet'")
- contenta = cursor.fetchall()
- source = []
- modellist = []
- addcha = []
- channellist = []
- for i in contenta:
- addcha.append((i[0],i[1]))
- #modellist.append(i[1])
- addcha = set(addcha)
- addcha = sorted(addcha,key=lambda x:x[1])
- for add,cha in addcha:
- source.append(add)
- channellist.append(cha)
- #source = set(source)
- print(addcha)
- source = list(source)
- 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')")
- contentm = cursor.fetchall()
- for m in contentm:
- modellist.append(m[0])
- modellist = set(modellist)
- modellist = list(modellist)
- contentlist = []
- for i in content:
- contentlist.append(i[0])
- #source.sort()
- n = len(content)
- print(f'modelname={n}')
- print(content)
- #content.reverse()
- print(content)
- print(source)
- # main(opt)
- #processes = []
- streamqueue = Queue(maxsize=4)
- numworkv = Value('i', 0)
- manager = Manager()
- lock = multiprocessing.Lock()
- streamlist = manager.list()
- numworks = 7
- modellen = []
- for i in modellist:
- if i in contentlist:
- modellen.append(i)
- queuelist = getmutpro(channellist,source, streamlist, numworkv, lock, numworks,modellen)
- deid = 0
- #pool = ThreadPoolExecutor(max_workers=n)
- runmodel = manager.dict()
- while True:
- for i in modellist:
- if i in contentlist:
- if i not in runmodel:
- #print(i)
- #detectdemo=Detect(weights=f'/mnt/project/yolodemo/yolov5-master/{i[0]}.pt')
- c.execute('select conf,cla from changestream where modelname = (?)',(i,))
- rea = c.fetchall()
- print(f'weights ={i[0]}.pt')
- if i in ['duty','danager','inspection','cross','personcount']:
- process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,[0],0,rea[0][1],rea[0][0],runmodel,True))
- else:
- if i in ['fall','extinguisher']:
- process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,None,0,rea[0][1],rea[0][0],runmodel))
- else:
- process = Process(target=modelfun,args=(queuelist[i],f'{i}.pt',dbpath,None,0,rea[0][1],rea[0][0],runmodel,True))
- #elif i in ['helmet','fire','smoke','fall']:
- #process = Process(target=modelfun,args=(queuelist,f'{i}.pt',dbpath,None,deid%2,True))
- #else:
- #process = Process(target=modelfun,args=(queuelist,f'{i}.pt',dbpath,None,deid%2,False))
- #processes.append(process)
- #process.start()
- #detectobj = Process(target=detectdemo.infer,args=(queue,))
- # Detect(weights=f'{i[0]}.pt')
- time.sleep(3)
- process.start()
- deid = deid+1
- runmodel[i] = 1
- time.sleep(600)
- task(c,conn,urlt,urla)
- #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'")
- #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'helmet'")
- 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'")
- content = cursor.fetchall()
- contentlist = []
- for con in content:
- contentlist.append(con[0])
- #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'")
- cursor = c.execute("SELECT address,channel from STREAM ")
- contenta = cursor.fetchall()
- source = []
- modellist = []
- addcha = []
- channellist = []
- for i in contenta:
- addcha.append((i[0],i[1]))
- #modellist.append(i[1])
- addcha = set(addcha)
- addcha = sorted(addcha)
- for a,cha in addcha:
- source.append(a)
- channellist.append(cha)
- print(addcha)
- #source = set(source)
- source = list(source)
- #source.sort()
- 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')")
- contentm = cursor.fetchall()
- for m in contentm:
- modellist.append(m[0])
- modellist = set(modellist)
- n = len(content)
- print(f'modelname={n}')
- print(content)
- #content.reverse()
- print(content)
- #pool.submit(detectobj.infer)
- #cursor = c.execute("SELECT modelname from CHANGESTREAM where modelname = 'fall'")
- #content = cursor.fetchall()
- #n = len(content)
- #print(f'modelname={n}')
- #print(content)
- #content.reverse()
- #print(content)
- # main(opt)
- #processes = []
- #pool = ProcessPoolExecutor(max_workers=n)
- #for i in content:
- #print(i)
- #detectdemo=Detect(weights=f'{i[0]}.pt')
- #process = Process(target=detectdemo.infer)
- #processes.append(process)
- #process.start()
- #detectobj = Detect(weights=f'{i[0]}.pt')
- # time.sleep(3)
- #pool.submit(detectobj.infer)
- # print('111111111111111111111111111111111111111111111111111111111')
- #pool.submit(TestA().func1, '张三', i)
- #print('----------------------------------------------------------------')
- #time.sleep(3000)
- # 等待所有进程执行完毕
- #for process in processes:
- # process.join()
- #pool.submit(Detect(weights=f'{i[0]}.pt').infer)
- # if isinstance(opt.weights,list):
- # opt.weights = opt.weights[0]
- #signal.signal(signal.SIGINT, my_handler)
- #detectdemo1 = Detect(weights=f'{content[0][0]}.pt')
- #detectdemo1.infer()
- #a = Test
- #with ProcessPoolExecutor(3) as ppool:
- #for i in range(3):
- # print('hello')
- #ppool.submit(self.postprocess,pred[i::3],path[i::3],im0s[i::3],dataset,im[i::3],s)
- #ppool.submit(TestA().func1, '张三', i)
- #ta = TestA()
- #with ProcessPoolExecutor(5) as ppool: # 创建一个5个进程的进程池
- # for i in range(1, 4):
- # ppool.submit(func1, '张三', i)
- #f1= pool.submit(detectdemo1.infer)
- # print("线程1-----------------------------------------------------------------------------------")
- #detectdemo2 = Detect(weights=r"helmet.pt")
- #f2=pool.submit(detectdemo2.infer)
- # print("线程2-------------------------------------------------------------------------------------")
- #detectdemo3 = threading.Thread(target=detectdemo3.infer)
- #detectdemo3 = Detect(weights=r"fall.pt")
- #f3=pool.submit(detectdemo3.infer)
|