Bläddra i källkod

更新

tags/V2.6.0^2
chenyukun 1 år sedan
förälder
incheckning
a9ac6b2953
4 ändrade filer med 490 tillägg och 148 borttagningar
  1. +3
    -7
      .idea/workspace.xml
  2. +30
    -18
      concurrency/IntelligentRecognitionProcess.py
  3. +9
    -1
      enums/ModelTypeEnum.py
  4. +448
    -122
      util/ModelUtils.py

+ 3
- 7
.idea/workspace.xml Visa fil

@@ -5,13 +5,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="4f7dccd9-8f92-4a6e-90cc-33890d102263" name="Changes" comment="Changes">
<change beforePath="$PROJECT_DIR$/.idea/deployment.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/deployment.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/sshConfigs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/sshConfigs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/webServers.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/webServers.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/concurrency/IntelligentRecognitionProcess.py" beforeDir="false" afterPath="$PROJECT_DIR$/concurrency/IntelligentRecognitionProcess.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/dsp_application.yml" beforeDir="false" afterPath="$PROJECT_DIR$/dsp_application.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/test/kafka/producer_start.py" beforeDir="false" afterPath="$PROJECT_DIR$/test/kafka/producer_start.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/enums/ModelTypeEnum.py" beforeDir="false" afterPath="$PROJECT_DIR$/enums/ModelTypeEnum.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/util/ModelUtils.py" beforeDir="false" afterPath="$PROJECT_DIR$/util/ModelUtils.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@@ -138,7 +134,7 @@
<property name="WebServerToolWindowPanel.toolwindow.show.date" value="false" />
<property name="WebServerToolWindowPanel.toolwindow.show.permissions" value="false" />
<property name="WebServerToolWindowPanel.toolwindow.show.size" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../newcode/hhz/tuoheng_hhz" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../ali/AIlib2" />
<property name="node.js.detected.package.eslint" value="true" />
<property name="node.js.detected.package.tslint" value="true" />
<property name="node.js.selected.package.eslint" value="(autodetect)" />
@@ -388,7 +384,7 @@
<workItem from="1672273700875" duration="1315000" />
<workItem from="1672295805200" duration="19000" />
<workItem from="1672709979593" duration="2445000" />
<workItem from="1672797232144" duration="593000" />
<workItem from="1672797232144" duration="20547000" />
</task>
<servers />
</component>

+ 30
- 18
concurrency/IntelligentRecognitionProcess.py Visa fil

@@ -184,7 +184,7 @@ class OnlineIntelligentRecognitionProcess(IntelligentRecognitionProcess):
# 程序开始时间
LogUtils.init_log(self.content)
self.sendhbMessage(AnalysisStatus.WAITING.value, "0.0000", AnalysisType.ONLINE.value)
mod, model_type_code = get_model((str(self.gpu_ids[0]), self.msg["models"]))
mod, model_type_code = get_model((str(self.gpu_ids[0]), self.msg["models"], self.msg.get("request_id")))
# 结果反馈进程启动
pullProcess = OnlinePullVideoStreamProcess(self.msg, self.content, self.pullQueue, self.fbQueue, self.hbQueue,
self.imageQueue)
@@ -495,7 +495,7 @@ class OfflineIntelligentRecognitionProcess(IntelligentRecognitionProcess):
# 程序开始时间
LogUtils.init_log(self.content)
self.sendhbMessage(AnalysisStatus.WAITING.value, "0.0000", AnalysisType.OFFLINE.value)
mod, model_type_code = get_model((str(self.gpu_ids[0]), self.msg["models"]))
mod, model_type_code = get_model((str(self.gpu_ids[0]), self.msg["models"], self.msg.get("request_id")))
pullProcess = OfflinePullVideoStreamProcess(self.msg, self.content, self.pullQueue, self.fbQueue, self.hbQueue,
self.imageQueue)
pullProcess.daemon = True
@@ -840,14 +840,14 @@ def epidemic_prevention(imageUrl, mod, content, orc, msg, fbQueue, model_type_co
imageUrl,
imageUrl,
model_type_code,
mod.order,
str(3),
TimeUtils.now_date_to_str(),
json.dumps(plate_result))})
if mod.img_type == 'code':
if dataBack is None or dataBack.get("type") is None:
return True
# 行程码
if dataBack.get("type") == 1:
if dataBack.get("type") == 1 and 1 in mod.allowedList:
# 手机号
if dataBack.get("phoneNumberImage") is None or len(dataBack.get("phoneNumberImage")) == 0:
phoneNumberRecognition = ''
@@ -888,7 +888,7 @@ def epidemic_prevention(imageUrl, mod, content, orc, msg, fbQueue, model_type_co
mod.order,
TimeUtils.now_date_to_str(),
json.dumps(trip_result))})
elif dataBack.get("type") == 2:
if dataBack.get("type") == 2 and 2 in mod.allowedList:
if dataBack.get("nameImage") is None or len(dataBack.get("nameImage")) == 0:
nameRecognition = ''
name_score = ''
@@ -941,7 +941,7 @@ def epidemic_prevention(imageUrl, mod, content, orc, msg, fbQueue, model_type_co
imageUrl,
imageUrl,
model_type_code,
mod.order,
str(2),
TimeUtils.now_date_to_str(),
json.dumps(healthy_result))})

@@ -999,9 +999,12 @@ class PhotosIntelligentRecognitionProcess(IntelligentRecognitionProcess):
analyse_time=TimeUtils.now_date_to_str())})
# 加载模型
mod, model_type_code = get_model((str(self.gpu_ids[0]), self.msg["models"]))
# 获取所有图片信息
imageUrls = self.msg.get("image_urls")
if model_type_code == ModelType.EPIDEMIC_PREVENTION_MODEL.value[1]:
result = self.epidemicPrevention(imageUrls, mod, model_type_code)
elif model_type_code == ModelType.PLATE_MODEL.value[1]:
result = self.epidemicPrevention(imageUrls, mod, model_type_code)
else:
result = self.publicIdentification(imageUrls, mod, model_type_code)
if result:
@@ -1048,23 +1051,32 @@ class PhotosIntelligentRecognitionProcess(IntelligentRecognitionProcess):

def get_model(args):
logger.info("######################开始加载模型######################")
model_config = {
# 加载河道模型
ModelType.WATER_SURFACE_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.SZModel(x, y, z, r), ModelType.WATER_SURFACE_MODEL.value[1]),
# 加载林场模型
ModelType.FOREST_FARM_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.LCModel(x, y, z, r), ModelType.FOREST_FARM_MODEL.value[1]),
# 加载交通模型
ModelType.TRAFFIC_FARM_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.TRAFFIC_FARM_MODEL(x, y, z, r), ModelType.TRAFFIC_FARM_MODEL.value[1]),
# 加载防疫模型
ModelType.EPIDEMIC_PREVENTION_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.IMModel(x, y, z, r), ModelType.EPIDEMIC_PREVENTION_MODEL.value[1]),
# 加载车牌模型
ModelType.PLATE_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.PlateMModel(x, y, z, r), ModelType.PLATE_MODEL.value[1]),
# 加载车辆模型
ModelType.Vehicle_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.VehicleModel(x, y, z, r), ModelType.Vehicle_MODEL.value[1]),
# 加载行人模型
ModelType.Pedestrian_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.PedestrianModel(x, y, z, r), ModelType.Pedestrian_MODEL.value[1]),
# 加载烟火模型
ModelType.Smogfire_MODEL.value[1]: (lambda x, y, z, r: ModelUtils.SmogfireModel(x, y, z, r), ModelType.Smogfire_MODEL.value[1])
}
for model in args[1]:
try:
code = model.get("code")
needed_objectsIndex = [int(category.get("id")) for category in model.get("categories")]
logger.info("code:{}, 检查目标:{}, gpuId:{}", code, needed_objectsIndex, args[0])
if code == ModelType.WATER_SURFACE_MODEL.value[1]:
logger.info("######################加载河道模型######################")
return ModelUtils.SZModel(args[0], needed_objectsIndex), code
elif code == ModelType.FOREST_FARM_MODEL.value[1]:
logger.info("######################加载林场模型######################")
return ModelUtils.LCModel(args[0], needed_objectsIndex), code
elif code == ModelType.TRAFFIC_FARM_MODEL.value[1]:
logger.info("######################加载交通模型######################")
return ModelUtils.RFModel(args[0], needed_objectsIndex), code
elif code == ModelType.EPIDEMIC_PREVENTION_MODEL.value[1]:
logger.info("######################加载防疫模型######################")
return ModelUtils.IMModel(args[0], needed_objectsIndex), code
model_method = model_config.get(code)
if model_method is not None:
return model_method(args[0], needed_objectsIndex, logger, args[2])
else:
logger.error("未匹配到对应的模型")
raise ServiceException(ExceptionType.AI_MODEL_MATCH_EXCEPTION.value[0],

+ 9
- 1
enums/ModelTypeEnum.py Visa fil

@@ -5,7 +5,7 @@ from enum import Enum, unique
@unique
class ModelType(Enum):

WATER_SURFACE_MODEL = ("1", "001", "水面模型")
WATER_SURFACE_MODEL = ("1", "001", "河道模型")

FOREST_FARM_MODEL = ("2", "002", "森林模型")

@@ -13,6 +13,14 @@ class ModelType(Enum):

EPIDEMIC_PREVENTION_MODEL = ("4", "004", "防疫模型")

PLATE_MODEL = ("5", "005", "车牌模型")

Vehicle_MODEL = ("6", "006", "车辆模型")

Pedestrian_MODEL = ("7", "007", "行人模型")

Smogfire_MODEL = ("8", "008", "烟火模型")

def checkCode(code):
for model in ModelType:
if model.value[1] == code:

+ 448
- 122
util/ModelUtils.py Visa fil

@@ -1,72 +1,104 @@
# -*- coding: utf-8 -*-
import sys

import torch

from util.GPUtils import get_all_gpu_ids

sys.path.extend(['..', '../AIlib'])
sys.path.extend(['..', '../AIlib2'])
from AI import AI_process,AI_process_forest,get_postProcess_para
import cv2,os,time
from segutils.segmodel import SegModel
from segutils.segmodel_trt import SegModel_STDC_trt
from segutils.trtUtils import DetectMultiBackend
from utils.torch_utils import select_device
from models.experimental import attempt_load
from utilsK.queRiver import get_labelnames, get_label_arrays, post_process_
from AI import AI_process, AI_process_v2, AI_process_forest, get_postProcess_para
from utilsK.jkmUtils import pre_process, post_process, get_return_data
from utils.torch_utils import select_device
from utilsK.queRiver import get_labelnames,get_label_arrays,save_problem_images
import numpy as np
import torch,glob
import tensorrt as trt
from utilsK.masterUtils import get_needed_objectsIndex
from utilsK.jkmUtils import pre_process,post_process,get_return_data


class Model():
def __init__(self, device, allowedList=None):
##预先设置的参数
self.device_ = device ##选定模型,可选 cpu,'0','1'
# 河道模型
class SZModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载河道模型########################, requestId:{}", requestId)
self.allowedList = allowedList


# 水面模型
class SZModel(Model):
def __init__(self, device, allowedList=None):
super().__init__(device, allowedList)
postFile = '../AIlib/conf/para.json'
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(postFile)
self.label_arraylist = None
self.digitFont = None
labelnames = "../AIlib/weights/yolov5/class8/labelnames.json" ##对应类别表
self.names = get_labelnames(labelnames)
self.trtFlag_det = True
self.trtFlag_seg = True
self.device = select_device(self.device_)
self.half = self.device.type != 'cpu'
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
gpuname = '3090'
elif '2080' in gpu.name:
gpuname = '2080Ti'
trtFlag_det = True, # 检测模型是否采用TRT
trtFlag_seg = True, # 分割模型是否采用TRT
if trtFlag_det and trtFlag_seg:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
seg_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
seg_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
seg_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
if self.trtFlag_det:
logger = trt.Logger(trt.Logger.ERROR)

with open("../AIlib/weights/yolov5/class8/bestcao_%s_fp16.engine" % gpuname, "rb") as f, trt.Runtime(
logger) as runtime:
self.model = runtime.deserialize_cuda_engine(f.read()) # 输入trt本地文件,返回ICudaEngine对象
det_gpuname = '.pt'
seg_gpuname = '.pth'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/river/labelnames.json", # 检测类别对照表
'trtFlag_det': trtFlag_det, # 检测模型是否采用TRT
'trtFlag_seg': trtFlag_seg, # 分割模型是否采用TRT
'Detweights': "../AIlib2/weights/river/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [5, 6, 7], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 1, # 分割模型结果需要保留的等值线数目
'segPar': {
'modelSize': (640,360),
'mean': (0.485, 0.456, 0.406),
'std' : (0.229, 0.224, 0.225),
'numpy': False,
'RGB_convert_first': True
}, # 分割模型预处理参数
'Segweights': '../AIlib2/weights/river/stdc_360X640%s' % seg_gpuname, # 分割模型权重位置
'postFile': '../AIlib2/weights/river/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.names = get_labelnames(par.get('labelnames'))
half = self.device.type != 'cpu'
Detweights = par.get('Detweights') # 升级后的检测模型
if trtFlag_det:
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model = runtime.deserialize_cuda_engine(f.read())# 输入trt本地文件,返回ICudaEngine对象
print('############locad det model trt success#######')
else:
self.model = attempt_load("../AIlib/weights/yolov5/class8/bestcao.pt",
map_location=self.device) # load FP32 model
if self.half:
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
print('############locad det model pth success#######')
if half:
self.model.half()
seg_nclass = 2
self.segPar = {'modelSize': (640, 360), 'mean': (0.485, 0.456, 0.406), 'std': (0.229, 0.224, 0.225),
'numpy': False, 'RGB_convert_first': True}
if self.trtFlag_seg:
Segweights = '../AIlib/weights/STDC/model_maxmIOU75_1720_0.946_360640_%s_fp16.engine' % gpuname ##升级的分割模型
logger = trt.Logger(trt.Logger.ERROR)
with open(Segweights, "rb") as f, trt.Runtime(logger) as runtime:
seg_nclass = par.get('seg_nclass')
self.segPar = par.get('segPar')
Segweights = par.get('Segweights')
if trtFlag_seg:
log = trt.Logger(trt.Logger.ERROR)
with open(Segweights, "rb") as f, trt.Runtime(log) as runtime:
self.segmodel = runtime.deserialize_cuda_engine(f.read()) # 输入trt本地文件,返回ICudaEngine对象
print('############locad seg model trt success#######')
else:
Segweights = '../AIlib/weights/STDC/model_maxmIOU75_1720_0.946_360640.pth'
self.segmodel = SegModel(nclass=seg_nclass, weights=Segweights, device=self.device)
print('############locad seg model pth success#######')

self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.objectPar = {
'half': half,
'device': self.device,
'conf_thres': self.conf_thres,
'iou_thres': self.iou_thres,
'allowedList': self.allowedList,
'slopeIndex': par.get('slopeIndex'),
'segRegionCnt': par.get('segRegionCnt'),
'trtFlag_det': trtFlag_det,
'trtFlag_seg': trtFlag_seg
}
self.label_arraylist = None
self.digitFont = None


# names, label_arraylist, rainbows, conf_thres, iou_thres
def process(self, frame, width=1920):
@@ -86,45 +118,56 @@ class SZModel(Model):
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib/conf/platech.ttf")
fontpath="../AIlib2/conf/platech.ttf")
return AI_process([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, self.half, self.device, self.conf_thres, self.iou_thres,
self.allowedList, font=self.digitFont, trtFlag_det=self.trtFlag_det,
trtFlag_seg=self.trtFlag_seg, segPar=self.segPar)
self.rainbows, objectPar=self.objectPar, font=self.digitFont, segPar=self.segPar)


# 森林模型
class LCModel(Model):
def __init__(self, device, allowedList=None):
super().__init__(device, allowedList)
self.label_arraylist = None
labelnames = "../AIlib/weights/forest/labelnames.json"
self.names = get_labelnames(labelnames)
postFile = '../AIlib/weights/forest/para.json'
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(postFile)
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
gpuname = '3090'
elif '2080' in gpu.name:
gpuname = '2080Ti'
class LCModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载森林模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.trtFlag_det = True, # 检测模型是否采用TRT
if self.trtFlag_det:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
self.device = select_device(self.device_)
self.half = self.device.type != 'cpu' # half precision only supported on CUDA

self.trtFlag_det = True # 是否采用TRT模型加速
det_gpuname = '.pt'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/forest/labelnames.json", # 检测类别对照表
'Detweights': "../AIlib2/weights/forest/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 0, # 分割模型结果需要保留的等值线数目
'segPar': None, # 分割模型预处理参数
'Segweights': None, # 分割模型权重位置
'postFile': '../AIlib2/weights/forest/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.half = device.type != 'cpu' # half precision only supported on CUDA
Detweights = par.get('Detweights')
if self.trtFlag_det:
detweights = "../AIlib/weights/forest/best_%s_fp16.engine" % gpuname
logger = trt.Logger(trt.Logger.ERROR)
with open(detweights, "rb") as f, trt.Runtime(logger) as runtime:
self.model = runtime.deserialize_cuda_engine(f.read()) # 输入trt本地文件,返回ICudaEngine对象
print("1111111111111111111111111111111111111111111111111111")
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model=runtime.deserialize_cuda_engine(f.read())
print('####load TRT model :%s'%(Detweights))
else:
detweights = "../AIlib/weights/forest/best.pt"
self.model = attempt_load(detweights, map_location=self.device) # load FP32 model
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
if self.half:
self.model.half()
print("222222222222222222222222222222222222222222222222222222222")
self.names = get_labelnames(par.get('labelnames'))
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.digitFont = None
self.label_arraylist = None
self.segmodel = None

# names, label_arraylist, rainbows, conf_thres, iou_thres
@@ -145,47 +188,131 @@ class LCModel(Model):
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib/conf/platech.ttf")
fontpath="../AIlib2/conf/platech.ttf")
return AI_process_forest([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, self.half, self.device, self.conf_thres, self.iou_thres,
self.allowedList, font=self.digitFont, trtFlag_det=self.trtFlag_det)


# 交通模型
class RFModel(Model):
def __init__(self, device, allowedList=None):
super().__init__(device, allowedList)
self.label_arraylist = None
labelnames = "../AIlib/weights/road/labelnames.json"
self.names = get_labelnames(labelnames)
postFile = '../AIlib/weights/road/para.json'
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(postFile)
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
gpuname = '3090'
elif '2080' in gpu.name:
gpuname = '2080Ti'
# 车辆模型
class VehicleModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载车辆模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.trtFlag_det = True, # 检测模型是否采用TRT
if self.trtFlag_det:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
det_gpuname = '.pt'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/vehicle/labelnames.json", # 检测类别对照表
'Detweights': "../AIlib2/weights/vehicle/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 0, # 分割模型结果需要保留的等值线数目
'segPar': None, # 分割模型预处理参数
'Segweights': None, # 分割模型权重位置
'postFile': '../AIlib2/weights/vehicle/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.half = device.type != 'cpu' # half precision only supported on CUDA
Detweights = par.get('Detweights')
if self.trtFlag_det:
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model=runtime.deserialize_cuda_engine(f.read())
print('####load TRT model :%s'%(Detweights))
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
if self.half:
self.model.half()
self.names = get_labelnames(par.get('labelnames'))
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.digitFont = None
self.label_arraylist = None
self.segmodel = None

# names, label_arraylist, rainbows, conf_thres, iou_thres
def process(self, frame, width=1920):
if self.label_arraylist is None:
fontsize = int(width / 1920 * 40)
line_thickness = 1
boxLine_thickness = 1
waterLineWidth = 1
if width >= 960:
line_thickness = int(round(width / 1920 * 3) - 1)
boxLine_thickness = int(round(width / 1920 * 3))
waterLineWidth = int(round(width / 1920 * 3))
numFontSize = float(format(width / 1920 * 1.1, '.1f')) # 文字大小
self.digitFont = {'line_thickness': line_thickness,
'boxLine_thickness': boxLine_thickness,
'fontSize': numFontSize,
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib2/conf/platech.ttf")
return AI_process_forest([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, self.half, self.device, self.conf_thres, self.iou_thres,
self.allowedList, font=self.digitFont, trtFlag_det=self.trtFlag_det)


self.device = select_device(self.device_)
self.half = self.device.type != 'cpu' # half precision only supported on CUDA
self.trtFlag_det = True ###是否采用TRT模型加速
# 行人模型
class PedestrianModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载行人模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.trtFlag_det = True, # 检测模型是否采用TRT
if self.trtFlag_det:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
det_gpuname = '.pt'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/pedestrian/labelnames.json", # 检测类别对照表
'Detweights': "../AIlib2/weights/pedestrian/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 0, # 分割模型结果需要保留的等值线数目
'segPar': None, # 分割模型预处理参数
'Segweights': None, # 分割模型权重位置
'postFile': '../AIlib2/weights/pedestrian/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.half = device.type != 'cpu' # half precision only supported on CUDA
Detweights = par.get('Detweights')
if self.trtFlag_det:
detweights="../AIlib/weights/road/best_%s_fp16.engine" % gpuname
logger = trt.Logger(trt.Logger.ERROR)
with open(detweights, "rb") as f, trt.Runtime(logger) as runtime:
self.model = runtime.deserialize_cuda_engine(f.read()) # 输入trt本地文件,返回ICudaEngine对象
print("1111111111111111111111111111111111111111111111111111")
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model=runtime.deserialize_cuda_engine(f.read())
print('####load TRT model :%s'%(Detweights))
else:
detweights="../AIlib/weights/road/best.pt"
self.model = attempt_load(detweights, map_location=self.device) # load FP32 model
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
if self.half:
self.model.half()
print("2222222222222222222222222222222222222222222222222222")
self.names = get_labelnames(par.get('labelnames'))
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.digitFont = None
self.label_arraylist = None
self.segmodel = None

# names, label_arraylist, rainbows, conf_thres, iou_thres
# names, label_arraylist, rainbows, conf_thres, iou_thres
def process(self, frame, width=1920):
if self.label_arraylist is None:
fontsize = int(width / 1920 * 40)
@@ -194,30 +321,229 @@ class RFModel(Model):
waterLineWidth = 1
if width >= 960:
line_thickness = int(round(width / 1920 * 3) - 1)
boxLine_thickness = int(round(width / 1920 * 2))
boxLine_thickness = int(round(width / 1920 * 3))
waterLineWidth = int(round(width / 1920 * 3))
numFontSize = float(format(width / 1920 * 1.1, '.1f'))
numFontSize = float(format(width / 1920 * 1.1, '.1f')) # 文字大小
self.digitFont = {'line_thickness': line_thickness,
'boxLine_thickness': boxLine_thickness,
'fontSize': numFontSize,
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib2/conf/platech.ttf")
return AI_process_forest([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, self.half, self.device, self.conf_thres, self.iou_thres,
self.allowedList, font=self.digitFont, trtFlag_det=self.trtFlag_det)


# 烟火模型
class SmogfireModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载烟火模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.trtFlag_det = True, # 检测模型是否采用TRT
if self.trtFlag_det:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
det_gpuname = '.pt'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/smogfire/labelnames.json", # 检测类别对照表
'Detweights': "../AIlib2/weights/smogfire/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 0, # 分割模型结果需要保留的等值线数目
'segPar': None, # 分割模型预处理参数
'Segweights': None, # 分割模型权重位置
'postFile': '../AIlib2/weights/smogfire/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.half = device.type != 'cpu' # half precision only supported on CUDA
Detweights = par.get('Detweights')
if self.trtFlag_det:
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model=runtime.deserialize_cuda_engine(f.read())
print('####load TRT model :%s'%(Detweights))
else:
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
if self.half:
self.model.half()
self.names = get_labelnames(par.get('labelnames'))
self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.digitFont = None
self.label_arraylist = None
self.segmodel = None

# names, label_arraylist, rainbows, conf_thres, iou_thres
def process(self, frame, width=1920):
if self.label_arraylist is None:
fontsize = int(width / 1920 * 40)
line_thickness = 1
boxLine_thickness = 1
waterLineWidth = 1
if width >= 960:
line_thickness = int(round(width / 1920 * 3) - 1)
boxLine_thickness = int(round(width / 1920 * 3))
waterLineWidth = int(round(width / 1920 * 3))
numFontSize = float(format(width / 1920 * 1.1, '.1f')) # 文字大小
self.digitFont = {'line_thickness': line_thickness,
'boxLine_thickness': boxLine_thickness,
'fontSize': numFontSize,
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib/conf/platech.ttf")
fontpath="../AIlib2/conf/platech.ttf")
return AI_process_forest([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, self.half, self.device, self.conf_thres, self.iou_thres,
self.allowedList, font=self.digitFont, trtFlag_det=self.trtFlag_det)

# 交通模型
class RFModel:
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载交通模型########################, requestId:{}", requestId)
self.allowedList = allowedList
trtFlag_det = True, # 检测模型是否采用TRT
trtFlag_seg = True, # 分割模型是否采用TRT
if trtFlag_det and trtFlag_seg:
gpu = get_all_gpu_ids()[int(device)]
if '3090' in gpu.name:
det_gpuname = '_3090_fp16.engine'
seg_gpuname = '_3090_fp16.engine'
elif '2080' in gpu.name:
det_gpuname = '_2080Ti_fp16.engine'
seg_gpuname = '_2080Ti_fp16.engine'
elif 'A10' in gpu.name:
det_gpuname = '_A10_fp16.engine'
seg_gpuname = '_A10_fp16.engine'
else:
raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
else:
det_gpuname = '.pt'
seg_gpuname = '.pth'
par = {
'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
'labelnames': "../AIlib2/weights/road/labelnames.json", # 检测类别对照表
'trtFlag_det': trtFlag_det, # 检测模型是否采用TRT
'trtFlag_seg': trtFlag_seg, # 分割模型是否采用TRT
'Detweights': "../AIlib2/weights/road/yolov5%s" % det_gpuname, # 检测模型路径
'slopeIndex': [], # 岸坡类别(或者其它业务里的类别),不与河道(分割的前景区域)计算交并比,即不论是否在河道内都显示。
'seg_nclass': 2, # 分割模型类别数目,默认2类
'segRegionCnt': 2, # 分割模型结果需要保留的等值线数目
'segPar': {
'modelSize': (640,360),
'mean': (0.485, 0.456, 0.406),
'std' : (0.229, 0.224, 0.225),
'numpy': False,
'RGB_convert_first': True
}, # 分割模型预处理参数
'Segweights': '../AIlib2/weights/road/stdc_360X640%s' % seg_gpuname, # 分割模型权重位置
'postFile': '../AIlib2/weights/road/para.json' # 后处理参数文件
}
self.device = select_device(par.get('device'))
self.names = get_labelnames(par.get('labelnames'))
half = self.device.type != 'cpu'
Detweights = par.get('Detweights') # 升级后的检测模型
if trtFlag_det:
log = trt.Logger(trt.Logger.ERROR)
with open(Detweights, "rb") as f, trt.Runtime(log) as runtime:
self.model = runtime.deserialize_cuda_engine(f.read())# 输入trt本地文件,返回ICudaEngine对象
print('############locad det model trt success#######')
else:
self.model = attempt_load(Detweights, map_location=self.device) # load FP32 model
print('############locad det model pth success#######')
if half:
self.model.half()
seg_nclass = par.get('seg_nclass')
self.segPar = par.get('segPar')
Segweights = par.get('Segweights')
if trtFlag_seg:
log = trt.Logger(trt.Logger.ERROR)
with open(Segweights, "rb") as f, trt.Runtime(log) as runtime:
self.segmodel = runtime.deserialize_cuda_engine(f.read()) # 输入trt本地文件,返回ICudaEngine对象
print('############locad seg model trt success#######')
else:
self.segmodel = SegModel(nclass=seg_nclass, weights=Segweights, device=self.device)
print('############locad seg model pth success#######')

self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
self.objectPar = {
'half': half,
'device': self.device,
'conf_thres': self.conf_thres,
'iou_thres': self.iou_thres,
'allowedList': self.allowedList,
'slopeIndex': par.get('slopeIndex'),
'segRegionCnt': par.get('segRegionCnt'),
'trtFlag_det': trtFlag_det,
'trtFlag_seg': trtFlag_seg
}
self.label_arraylist = None
self.digitFont = None

def process(self, frame, width=1920):
if self.label_arraylist is None:
fontsize = int(width / 1920 * 40)
line_thickness = 1
boxLine_thickness = 1
waterLineWidth = 1
if width >= 960:
line_thickness = int(round(width / 1920 * 3) - 1)
boxLine_thickness = int(round(width / 1920 * 2))
waterLineWidth = int(round(width / 1920 * 3))
numFontSize = float(format(width / 1920 * 1.1, '.1f'))

self.digitFont = {'line_thickness': line_thickness,
'boxLine_thickness': boxLine_thickness,
'fontSize': numFontSize,
'waterLineColor': (0, 255, 255),
'waterLineWidth': waterLineWidth}
self.label_arraylist = get_label_arrays(self.names, self.rainbows, outfontsize=fontsize,
fontpath="../AIlib2/conf/platech.ttf")
return AI_process([frame], self.model, self.segmodel, self.names, self.label_arraylist,
self.rainbows, objectPar=self.objectPar, font=self.digitFont, segPar=self.segPar)


# 车牌、健康码、行程码分割模型
# 健康码、行程码分割模型
class IMModel:
def __init__(self, device, allowedList=None):
self.order = allowedList[0]
if int(allowedList[0]) == 3:
self.img_type = 'plate' ## code,plate
else:
self.img_type = 'code'
def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载防疫模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.img_type = 'code'
self.par = {'code': {'weights': '../AIlib2/weights/jkm/health_yolov5s_v3.jit', 'img_type': 'code', 'nc': 10},
'plate': {'weights': '../AIlib2/weights/jkm/plate_yolov5s_v3.jit', 'img_type': 'plate', 'nc': 1},
'conf_thres': 0.4,
'iou_thres': 0.45,
'device': 'cuda:%s' % device,
'plate_dilate': (0.5, 0.3)
}
self.device = torch.device(self.par['device'])
self.model = torch.jit.load(self.par[self.img_type]['weights'])

def process(self, frame):
img, padInfos = pre_process(frame, self.device) ##预处理
pred = self.model(img) ##模型推理
boxes = post_process(pred, padInfos, self.device, conf_thres=self.par['conf_thres'],
iou_thres=self.par['iou_thres'], nc=self.par[self.img_type]['nc']) # 后处理
dataBack = get_return_data(frame, boxes, modelType=self.img_type, plate_dilate=self.par['plate_dilate'])
return dataBack


# 车牌分割模型
class PlateMModel:

def __init__(self, device, allowedList=None, logger=None, requestId=None):
logger.info("########################加载车牌模型########################, requestId:{}", requestId)
self.allowedList = allowedList
self.img_type = 'plate' ## code,plate
self.par = {'code': {'weights': '../AIlib/weights/jkm/health_yolov5s_v3.jit', 'img_type': 'code', 'nc': 10},
'plate': {'weights': '../AIlib/weights/jkm/plate_yolov5s_v3.jit', 'img_type': 'plate', 'nc': 1},
'conf_thres': 0.4,
@@ -235,4 +561,4 @@ class IMModel:
boxes = post_process(pred, padInfos, self.device, conf_thres=self.par['conf_thres'],
iou_thres=self.par['iou_thres'], nc=self.par[self.img_type]['nc']) # 后处理
dataBack = get_return_data(frame, boxes, modelType=self.img_type, plate_dilate=self.par['plate_dilate'])
return dataBack
return dataBack

Laddar…
Avbryt
Spara