|
- # -*- coding: utf-8 -*-
- import sys
-
- from enums.ModelTypeEnum import ModelType
- from util.GPUtils import get_all_gpu_ids
-
- 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 models.experimental import attempt_load
- 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, logger=None, requestId=None, modeType=None):
- try:
- logger.info("########################加载{}########################, requestId:{}", modeType.value[2], requestId)
- self.modeType = modeType
- self.allowedList = allowedList
- self.trtFlag_det = True, # 检测模型是否采用TRT
- self.trtFlag_seg = True, # 分割模型是否采用TRT
- if self.trtFlag_det and self.trtFlag_seg:
- gpu = get_all_gpu_ids()[int(device)]
- if '3090' in gpu.name:
- detweights = "../AIlib2/weights/%s/yolov5_3090_fp16.engine" % modeType.value[3]
- segweights = '../AIlib2/weights/%s/stdc_360X640_3090_fp16.engine' % modeType.value[3]
- elif '2080' in gpu.name:
- detweights = "../AIlib2/weights/%s/yolov5_2080Ti_fp16.engine" % modeType.value[3]
- segweights = '../AIlib2/weights/%s/stdc_360X640_2080Ti_fp16.engine' % modeType.value[3]
- elif '4090' in gpu.name:
- detweights = "../AIlib2/weights/%s/yolov5_4090_fp16.engine" % modeType.value[3]
- segweights = '../AIlib2/weights/%s/stdc_360X640_4090_fp16.engine' % modeType.value[3]
- elif 'A10' in gpu.name:
- detweights = "../AIlib2/weights/%s/yolov5_A10_fp16.engine" % modeType.value[3]
- segweights = '../AIlib2/weights/%s/stdc_360X640_A10_fp16.engine' % modeType.value[3]
- else:
- raise Exception("未匹配到该GPU名称的模型, GPU: " + gpu.name)
- else:
- detweights = "../AIlib2/weights/conf/%s/yolov5.pt" % modeType.value[3]
- segweights = '../AIlib2/weights/conf/%s/stdc_360X640.pth' % modeType.value[3]
- par = {
- 'device': device, # 显卡号,如果用TRT模型,只支持0(单显卡)
- 'labelnames': "../AIlib2/weights/conf/%s/labelnames.json" % modeType.value[3], # 检测类别对照表
- 'trtFlag_det': self.trtFlag_det, # 检测模型是否采用TRT
- 'trtFlag_seg': self.trtFlag_seg, # 分割模型是否采用TRT
- 'Detweights': detweights, # 检测模型路径
- 'seg_nclass': 2, # 分割模型类别数目,默认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': segweights, # 分割模型权重位置
- 'postFile': '../AIlib2/weights/conf/%s/para.json' % modeType.value[3] # 后处理参数文件
- }
- if ModelType.WATER_SURFACE_MODEL == modeType:
- par['segRegionCnt'] = 1
- par['slopeIndex'] = [5, 6, 7]
- if ModelType.TRAFFIC_FARM_MODEL == modeType:
- par['segRegionCnt'] = 2
- par['slopeIndex'] = [5, 6, 7]
- else:
- par['segRegionCnt'] = 0
- par['slopeIndex'] = []
- par['segPar'] = None
- par['Segweights'] = None
- self.device = select_device(par.get('device'))
- self.names = get_labelnames(par.get('labelnames'))
- self.half = self.device.type != 'cpu'
- Detweights = par.get('Detweights') # 升级后的检测模型
- if self.trtFlag_det:
- with open(Detweights, "rb") as f, trt.Runtime(trt.Logger(trt.Logger.ERROR)) as runtime:
- self.model = runtime.deserialize_cuda_engine(f.read())# 输入trt本地文件,返回ICudaEngine对象
- 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()
- seg_nclass = par.get('seg_nclass')
- self.segPar = par.get('segPar')
- Segweights = par.get('Segweights')
- if Segweights is not None:
- if self.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#######')
- else:
- self.segmodel = None
- self.conf_thres, self.iou_thres, self.classes, self.rainbows = get_postProcess_para(par.get('postFile'))
- self.objectPar = {
- 'half': self.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': self.trtFlag_det,
- 'trtFlag_seg': self.trtFlag_seg
- }
- self.label_arraylist = None
- self.digitFont = None
- except Exception as ee:
- logger.exception("异常:{}, requestId:{}", ee, requestId)
- raise ee
-
-
- 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")
- if ModelType.WATER_SURFACE_MODEL == self.modeType or ModelType.TRAFFIC_FARM_MODEL == self.modeType:
- return AI_process([frame], self.model, self.segmodel, self.names, self.label_arraylist,
- self.rainbows, objectPar=self.objectPar, font=self.digitFont, segPar=self.segPar)
- else:
- 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 SZModel:
- # def __init__(self, device, allowedList=None, logger=None, requestId=None):
- # try:
- # logger.info("########################加载河道模型########################, requestId:{}", requestId)
- # self.allowedList = allowedList
- # trtFlag_det = False, # 检测模型是否采用TRT
- # trtFlag_seg = False, # 分割模型是否采用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 '4090' in gpu.name:
- # det_gpuname = '_4090_fp16.engine'
- # seg_gpuname = '_4090_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/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(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
- # except Exception as ee:
- # logger.exception("异常:{}, requestId:{}", ee, requestId)
- #
- #
- #
- # # 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([frame], self.model, self.segmodel, self.names, self.label_arraylist,
- # self.rainbows, objectPar=self.objectPar, font=self.digitFont, segPar=self.segPar)
- #
- #
- # # 森林模型
- # 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 '4090' in gpu.name:
- # det_gpuname = '_4090_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/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 = self.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="../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 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 '4090' in gpu.name:
- # det_gpuname = '_4090_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 = self.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="../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 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 '4090' in gpu.name:
- # det_gpuname = '_4090_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 = self.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="../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 '4090' in gpu.name:
- # det_gpuname = '_4090_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 = self.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="../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 '4090' in gpu.name:
- # det_gpuname = '_4090_fp16.engine'
- # seg_gpuname = '_4090_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, 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 IMModel:
-
- def __init__(self, device, allowedList=None, logger=None, requestId=None, modeType=None):
- logger.info("########################加载{}########################, requestId:{}", modeType.value[2], requestId)
- self.allowedList = allowedList
- if ModelType.EPIDEMIC_PREVENTION_MODEL == modeType:
- self.img_type = 'code' ## code,plate
- if ModelType.PLATE_MODEL == modeType:
- self.img_type = 'plate' ## code,plate
- self.par = {'code': {'weights': '../AIlib2/weights/conf/jkm/health_yolov5s_v3.jit', 'img_type': 'code', 'nc': 10},
- 'plate': {'weights': '../AIlib2/weights/conf/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
|