2022-08-02 14:22:06 +08:00
|
|
|
|
import sys
|
|
|
|
|
|
sys.path.extend(['..','../AIlib' ])
|
|
|
|
|
|
|
2022-12-06 09:42:40 +08:00
|
|
|
|
from AI import AI_process,AI_process_v2,AI_process_forest,get_postProcess_para
|
2022-08-02 14:22:06 +08:00
|
|
|
|
import cv2,os,time
|
|
|
|
|
|
from segutils.segmodel import SegModel
|
2022-12-06 09:42:40 +08:00
|
|
|
|
from segutils.segmodel_trt import SegModel_STDC_trt
|
|
|
|
|
|
from segutils.trtUtils import DetectMultiBackend
|
2022-08-02 14:22:06 +08:00
|
|
|
|
from models.experimental import attempt_load
|
|
|
|
|
|
from utils.torch_utils import select_device
|
|
|
|
|
|
from utilsK.queRiver import get_labelnames,get_label_arrays
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
import torch
|
2022-08-09 17:24:57 +08:00
|
|
|
|
from utilsK.masterUtils import get_needed_objectsIndex
|
2022-12-06 09:42:40 +08:00
|
|
|
|
def river_demo_v2():
|
2022-08-02 14:22:06 +08:00
|
|
|
|
##预先设置的参数
|
2022-10-11 14:36:44 +08:00
|
|
|
|
device_='0' ##选定模型,可选 cpu,'0','1'
|
2022-12-06 09:42:40 +08:00
|
|
|
|
###注意TRT模型生成时,就需要对应cuda device,下面的trt文件是cuda:0生成的,device只能是0
|
|
|
|
|
|
##以下参数目前不可改
|
|
|
|
|
|
|
2022-12-06 15:47:00 +08:00
|
|
|
|
gpuname='3090'
|
|
|
|
|
|
Detweights = "../AIlib/weights/yolov5/class8/bestcao_%s_fp16.engine"%(gpuname) ##升级后的检测模型
|
2022-12-06 09:42:40 +08:00
|
|
|
|
labelnames = "../AIlib/weights/yolov5/class8/labelnames.json" ##对应类别表
|
2022-12-06 15:47:00 +08:00
|
|
|
|
|
2022-12-06 09:42:40 +08:00
|
|
|
|
|
|
|
|
|
|
seg_nclass = 2
|
|
|
|
|
|
#Segweights = "../AIlib/weights/BiSeNet/checkpoint.pth"
|
2022-12-06 15:47:00 +08:00
|
|
|
|
Segweights = '../AIlib/weights/STDC/model_maxmIOU75_1720_0.946_360640_%s_fp16.engine'%(gpuname) ##升级的分割模型
|
2022-12-06 09:42:40 +08:00
|
|
|
|
|
|
|
|
|
|
postFile= '../AIlib/conf/para.json'
|
|
|
|
|
|
digitFont= { 'line_thickness':2,'boxLine_thickness':1, 'fontSize':1.0,'waterLineColor':(0,255,255),'waterLineWidth':3}
|
|
|
|
|
|
conf_thres,iou_thres,classes,rainbows=get_postProcess_para(postFile)
|
|
|
|
|
|
|
|
|
|
|
|
####模型选择参数用如下:
|
|
|
|
|
|
mode_paras=[
|
|
|
|
|
|
{"id":"0","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"1","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"2","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"3","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"4","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"5","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"6","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"7","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
allowedList,allowedList_string=get_needed_objectsIndex(mode_paras)
|
|
|
|
|
|
#allowedList=[0,1,2,3]
|
|
|
|
|
|
##加载模型,准备好显示字符
|
|
|
|
|
|
device = select_device(device_)
|
|
|
|
|
|
names=get_labelnames(labelnames)
|
|
|
|
|
|
|
|
|
|
|
|
label_arraylist = get_label_arrays(names,rainbows,outfontsize=40,fontpath="../AIlib/conf/platech.ttf")
|
|
|
|
|
|
half = device.type != 'cpu' # half precision only supported on CUDA
|
|
|
|
|
|
|
|
|
|
|
|
segmodel = SegModel_STDC_trt(weights=Segweights,modelsize=(640,360),std=(0.229, 0.224, 0.225),mean=(0.485, 0.456, 0.406),device=device)
|
|
|
|
|
|
model = DetectMultiBackend(Detweights, device=device)
|
|
|
|
|
|
|
|
|
|
|
|
##图像测试
|
|
|
|
|
|
#url='images/examples/20220624_响水河_12300_1621.jpg'
|
|
|
|
|
|
impth = 'images/slope/'
|
|
|
|
|
|
outpth = 'images/results/'
|
|
|
|
|
|
folders = os.listdir(impth)
|
|
|
|
|
|
for i in range(len(folders)):
|
|
|
|
|
|
imgpath = os.path.join(impth, folders[i])
|
|
|
|
|
|
im0s=[cv2.imread(imgpath)]
|
|
|
|
|
|
H,W,C = im0s[0].shape
|
|
|
|
|
|
time00 = time.time()
|
|
|
|
|
|
p_result,timeOut = AI_process_v2(im0s,model,segmodel,names,label_arraylist,rainbows,half,device,conf_thres, iou_thres,allowedList,font=digitFont)
|
|
|
|
|
|
time11 = time.time()
|
|
|
|
|
|
image_array = p_result[1]
|
|
|
|
|
|
cv2.imwrite( os.path.join( outpth,folders[i] ) ,image_array )
|
|
|
|
|
|
print('%s,%d*%d,AI-process: %.1f, %s'%(folders[i],H,W, (time11 - time00) * 1000,timeOut))
|
|
|
|
|
|
|
|
|
|
|
|
def river_demo():
|
|
|
|
|
|
##预先设置的参数
|
|
|
|
|
|
device_='1' ##选定模型,可选 cpu,'0','1'
|
2022-10-08 16:03:21 +08:00
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
##以下参数目前不可改
|
2022-08-17 16:42:20 +08:00
|
|
|
|
#Detweights = "../AIlib/weights/yolov5/class5/best_5classes.pt"
|
|
|
|
|
|
#labelnames = "../AIlib/weights/yolov5/class5/labelnames.json"
|
|
|
|
|
|
|
|
|
|
|
|
Detweights = "../AIlib/weights/yolov5/class8/bestcao.pt" ##升级后的检测模型
|
|
|
|
|
|
labelnames = "../AIlib/weights/yolov5/class8/labelnames.json" ##对应类别表
|
|
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
seg_nclass = 2
|
2022-08-17 16:42:20 +08:00
|
|
|
|
#Segweights = "../AIlib/weights/BiSeNet/checkpoint.pth"
|
2022-08-18 10:43:39 +08:00
|
|
|
|
Segweights = '../AIlib/weights/STDC/model_maxmIOU75_1720_0.946_360640.pth' ##升级的分割模型
|
2022-08-17 16:42:20 +08:00
|
|
|
|
|
2022-08-14 20:36:27 +08:00
|
|
|
|
postFile= '../AIlib/conf/para.json'
|
2022-10-19 10:54:28 +08:00
|
|
|
|
digitFont= { 'line_thickness':2,'boxLine_thickness':1, 'fontSize':1.0,'waterLineColor':(0,255,255),'waterLineWidth':3}
|
2022-08-14 20:36:27 +08:00
|
|
|
|
conf_thres,iou_thres,classes,rainbows=get_postProcess_para(postFile)
|
|
|
|
|
|
|
2022-08-09 17:24:57 +08:00
|
|
|
|
####模型选择参数用如下:
|
|
|
|
|
|
mode_paras=[
|
2022-08-17 16:42:20 +08:00
|
|
|
|
{"id":"0","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"1","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"2","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"3","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"4","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"5","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"6","config":{"k1":"v1","k2":"v2"}},
|
|
|
|
|
|
{"id":"7","config":{"k1":"v1","k2":"v2"}},
|
2022-08-09 17:24:57 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
allowedList,allowedList_string=get_needed_objectsIndex(mode_paras)
|
|
|
|
|
|
#allowedList=[0,1,2,3]
|
2022-08-02 14:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-08-09 17:24:57 +08:00
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
##加载模型,准备好显示字符
|
|
|
|
|
|
device = select_device(device_)
|
|
|
|
|
|
names=get_labelnames(labelnames)
|
2022-08-14 20:36:27 +08:00
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
label_arraylist = get_label_arrays(names,rainbows,outfontsize=40,fontpath="../AIlib/conf/platech.ttf")
|
|
|
|
|
|
half = device.type != 'cpu' # half precision only supported on CUDA
|
|
|
|
|
|
model = attempt_load(Detweights, map_location=device) # load FP32 model
|
|
|
|
|
|
if half: model.half()
|
2022-12-06 09:42:40 +08:00
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
segmodel = SegModel(nclass=seg_nclass,weights=Segweights,device=device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##图像测试
|
|
|
|
|
|
#url='images/examples/20220624_响水河_12300_1621.jpg'
|
2022-08-17 16:42:20 +08:00
|
|
|
|
impth = 'images/slope/'
|
2022-08-02 14:22:06 +08:00
|
|
|
|
outpth = 'images/results/'
|
|
|
|
|
|
folders = os.listdir(impth)
|
|
|
|
|
|
for i in range(len(folders)):
|
|
|
|
|
|
imgpath = os.path.join(impth, folders[i])
|
|
|
|
|
|
im0s=[cv2.imread(imgpath)]
|
2022-10-11 14:36:44 +08:00
|
|
|
|
H,W,C = im0s[0].shape
|
2022-08-02 14:22:06 +08:00
|
|
|
|
time00 = time.time()
|
2022-10-08 16:09:44 +08:00
|
|
|
|
p_result,timeOut = AI_process(im0s,model,segmodel,names,label_arraylist,rainbows,half,device,conf_thres, iou_thres,allowedList,font=digitFont)
|
2022-08-02 14:22:06 +08:00
|
|
|
|
time11 = time.time()
|
|
|
|
|
|
image_array = p_result[1]
|
|
|
|
|
|
cv2.imwrite( os.path.join( outpth,folders[i] ) ,image_array )
|
2022-10-11 14:36:44 +08:00
|
|
|
|
print('%s,%d*%d,AI-process: %.1f, %s'%(folders[i],H,W, (time11 - time00) * 1000,timeOut))
|
2022-08-14 20:36:27 +08:00
|
|
|
|
|
2022-09-20 16:37:37 +08:00
|
|
|
|
def road_forest_demo(business ):
|
|
|
|
|
|
|
|
|
|
|
|
##使用森林,道路模型,business 控制['forest','road']
|
2022-08-14 20:36:27 +08:00
|
|
|
|
##预先设置的参数
|
|
|
|
|
|
device_='1' ##选定模型,可选 cpu,'0','1'
|
|
|
|
|
|
|
2022-09-20 16:37:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-08-14 20:36:27 +08:00
|
|
|
|
##以下参数目前不可改
|
2022-09-20 16:37:37 +08:00
|
|
|
|
#business='forest';imageW=4916 ####森林模型
|
|
|
|
|
|
#business='road';
|
|
|
|
|
|
imageW=1536 ####道路模型
|
2022-10-08 16:03:21 +08:00
|
|
|
|
digitFont= { 'line_thickness':2, 'fontSize':1.0} ###数字显示的线宽度,大小; 如果都是None,则采用默认大小
|
|
|
|
|
|
Detweights="../AIlib/weights/%s/best.pt"%(business)
|
2022-09-20 16:37:37 +08:00
|
|
|
|
labelnames = "../AIlib/weights/%s/labelnames.json"%(business)
|
2022-10-08 16:03:21 +08:00
|
|
|
|
|
2022-08-14 20:36:27 +08:00
|
|
|
|
postFile= '../AIlib/conf/para.json'
|
2022-10-08 16:03:21 +08:00
|
|
|
|
print( Detweights,labelnames )
|
2022-08-14 20:36:27 +08:00
|
|
|
|
conf_thres,iou_thres,classes,rainbows=get_postProcess_para(postFile)
|
|
|
|
|
|
|
|
|
|
|
|
####模型选择参数用如下:
|
|
|
|
|
|
mode_paras=[
|
|
|
|
|
|
{
|
|
|
|
|
|
"id":"0",
|
|
|
|
|
|
"config":{
|
|
|
|
|
|
"k1":"v1",
|
|
|
|
|
|
"k2":"v2"
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
{
|
|
|
|
|
|
"id":"1",
|
|
|
|
|
|
"config":{
|
|
|
|
|
|
"k1":"v1",
|
|
|
|
|
|
"k2":"v2"
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
allowedList,allowedList_string=get_needed_objectsIndex(mode_paras)
|
|
|
|
|
|
#allowedList=[0,1,2,3]
|
|
|
|
|
|
|
|
|
|
|
|
print('####line108###')
|
|
|
|
|
|
|
|
|
|
|
|
##只加载检测模型,准备好显示字符
|
|
|
|
|
|
device = select_device(device_)
|
|
|
|
|
|
names=get_labelnames(labelnames)
|
2022-09-20 16:37:37 +08:00
|
|
|
|
#imageW=4915;###默认是1920,在森林巡检的高清图像中是4920
|
2022-08-14 20:36:27 +08:00
|
|
|
|
outfontsize=int(imageW/1920*40);###
|
|
|
|
|
|
label_arraylist = get_label_arrays(names,rainbows,outfontsize=outfontsize,fontpath="../AIlib/conf/platech.ttf")
|
|
|
|
|
|
half = device.type != 'cpu' # half precision only supported on CUDA
|
|
|
|
|
|
model = attempt_load(Detweights, map_location=device) # load FP32 model
|
|
|
|
|
|
if half: model.half()
|
|
|
|
|
|
segmodel = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
##图像测试
|
|
|
|
|
|
#url='images/examples/20220624_响水河_12300_1621.jpg'
|
2022-09-20 16:37:37 +08:00
|
|
|
|
impth = 'images/%s/'%(business)
|
2022-08-14 20:36:27 +08:00
|
|
|
|
outpth = 'images/results/'
|
|
|
|
|
|
folders = os.listdir(impth)
|
2022-10-11 14:36:44 +08:00
|
|
|
|
folders.sort()
|
2022-08-14 20:36:27 +08:00
|
|
|
|
for i in range(len(folders)):
|
|
|
|
|
|
#for i in range(2):
|
|
|
|
|
|
imgpath = os.path.join(impth, folders[i])
|
|
|
|
|
|
im0s=[cv2.imread(imgpath)]
|
|
|
|
|
|
time00 = time.time()
|
2022-08-14 20:56:03 +08:00
|
|
|
|
#使用不同的函数。每一个领域采用一个函数
|
2022-10-08 16:03:21 +08:00
|
|
|
|
p_result,timeOut = AI_process_forest(im0s,model,segmodel,names,label_arraylist,rainbows,half,device,conf_thres, iou_thres,allowedList,font=digitFont)
|
2022-08-14 20:36:27 +08:00
|
|
|
|
time11 = time.time()
|
|
|
|
|
|
image_array = p_result[1]
|
|
|
|
|
|
cv2.imwrite( os.path.join( outpth,folders[i] ) ,image_array )
|
|
|
|
|
|
|
|
|
|
|
|
print('----image:%s, process:%s ,save:%s, %s'%(folders[i],(time11-time00) * 1000, (time.time() - time11) * 1000,timeOut) )
|
2022-08-02 14:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
2022-11-21 10:55:37 +08:00
|
|
|
|
def jkm_demo():
|
|
|
|
|
|
from utilsK.jkmUtils import pre_process,post_process,get_return_data
|
|
|
|
|
|
img_type = 'plate' ## code,plate
|
|
|
|
|
|
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,
|
|
|
|
|
|
'iou_thres':0.45,
|
|
|
|
|
|
'device':'cuda:0',
|
|
|
|
|
|
'plate_dilate':(0.5,0.1)
|
|
|
|
|
|
}
|
|
|
|
|
|
###加载模型
|
|
|
|
|
|
device = torch.device(par['device'])
|
|
|
|
|
|
jit_weights = par['code']['weights']
|
|
|
|
|
|
model = torch.jit.load(jit_weights)
|
|
|
|
|
|
|
|
|
|
|
|
jit_weights = par['plate']['weights']
|
|
|
|
|
|
model_plate = torch.jit.load(jit_weights)
|
|
|
|
|
|
|
|
|
|
|
|
imgd='images/plate'
|
|
|
|
|
|
imgpaths = os.listdir(imgd)
|
|
|
|
|
|
for imgp in imgpaths[0:]:
|
|
|
|
|
|
#imgp = 'plate_IMG_20221030_100612.jpg'
|
|
|
|
|
|
imgpath = os.path.join(imgd,imgp)
|
|
|
|
|
|
im0 = cv2.imread(imgpath) #读取数据
|
|
|
|
|
|
img ,padInfos = pre_process(im0,device) ##预处理
|
|
|
|
|
|
if img_type=='code': pred = model(img) ##模型推理
|
|
|
|
|
|
else: pred = model_plate(img)
|
|
|
|
|
|
boxes = post_process(pred,padInfos,device,conf_thres= par['conf_thres'], iou_thres= par['iou_thres'],nc=par[img_type]['nc']) #后处理
|
|
|
|
|
|
dataBack=get_return_data(im0,boxes,modelType=img_type,plate_dilate=par['plate_dilate'])
|
|
|
|
|
|
print(imgp,boxes,dataBack['type'])
|
|
|
|
|
|
for key in dataBack.keys():
|
|
|
|
|
|
if isinstance(dataBack[key],list):
|
|
|
|
|
|
cv2.imwrite( 'images/results/%s_%s.jpg'%( imgp.replace('.jpg','').replace('.png',''),key),dataBack[key][0] ) ###返回值: dataBack
|
|
|
|
|
|
|
2022-08-02 14:22:06 +08:00
|
|
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
2022-12-06 09:42:40 +08:00
|
|
|
|
river_demo_v2()
|
2022-10-08 16:09:44 +08:00
|
|
|
|
#road_forest_demo('forest' )
|
2022-12-06 09:42:40 +08:00
|
|
|
|
#jkm_demo()
|
2022-08-14 20:36:27 +08:00
|
|
|
|
|