AIlib2/DrGraph/Bussiness/Bussiness_IllParking.py

119 lines
5.7 KiB
Python
Raw Normal View History

2025-09-23 09:54:48 +08:00
from loguru import logger
import cv2, time
import numpy as np
from DrGraph.util.drHelper import *
from .Bussiness import BussinessBase
class Bussiness_IllParking(BussinessBase):
def __init__(self, opt):
logger.info("create AlAlg_IllParking")
super().__init__(opt)
@staticmethod
def postProcess(pred, cvMask, pars):
#pred:直接预测结果,不要原图。预测结果[0,1,2,...],不是[车、T角点L角点]
#mask_cv:分割结果图numpy格式(H,W),结果是int[0,1,2,...]
#pars: 其它参数,dict格式
'''三个标签车、T角点L角点'''
'''输入:落水人员的结果(类别+坐标)、原图
过程将车辆识别框外扩并按contours形成区域
T角点与L角点的坐标合并为列表
判断每个车辆contours区域内有几个角点少于2个则判断违停
返回最终违停车辆标记结果图违停车辆信息坐标类别置信度
'''
#输入的是[cls,x0,y0,x1,y1,score]---> [x0,y0,x1,y1,cls,score]
#输出的也是[cls,x0,y0,x1,y1,score]
#pred = [ [ int(x[4]) ,*x[1:5], x[5] ] for x in pred]
#pred = [[ *x[1:5],x[0], x[5] ] for x in pred]
pred = [[ *x[0:4],x[5], x[4] ] for x in pred]
##统一格式
imgSize=pars['imgSize']
'''1、pred中车辆识别框形成列表T角点与L角点形成列表'''
tW1=time.time()
init_vehicle=[]
init_corner = []
for i in range(len(pred)):
#if pred[i][4]=='TCorner' or pred[i][4]=='LCorner': #vehicle、TCorner、LCorner
if pred[i][4]==1 or pred[i][4]==2: #vehicle、TCorner、LCorner
init_corner.append(pred[i])
else:
init_vehicle.append(pred[i])
'''2、init_corner中心点坐标计算并形成列表。'''
tW2 = time.time()
center_corner=[]
for i in range(len(init_corner)):
center_corner.append(mathHelper.center_coordinate(init_corner[i]))
'''3、遍历每个车辆识别框扩充矩形区域将矩形区域形成contours判断扩充区域内的。'''
tW3 = time.time()
final_weiting=[] #违停车辆列表
'''遍历车辆列表扩大矩形框形成contours'''
for i in range(len(init_vehicle)):
boundbxs1=[init_vehicle[i][0],init_vehicle[i][1],init_vehicle[i][2],init_vehicle[i][3]]
width_boundingbox=init_vehicle[i][2]-init_vehicle[i][0] #框宽度
height_boundingbox=init_vehicle[i][2] - init_vehicle[i][0] #框长度
#当框长大于宽,则是水平方向车辆;否则认为是竖向车辆
if width_boundingbox>=height_boundingbox:
ex_width=0.4*(init_vehicle[i][2]-init_vehicle[i][0]) #矩形扩充宽度取车宽0.4倍 #膨胀系数小一些。角点设成1个。
ex_height=0.2*(init_vehicle[i][2]-init_vehicle[i][0]) #矩形扩充宽度取车长0.2倍
boundbxs1 = imgHelper.expand_rectangle(boundbxs1, imgSize, ex_width, ex_height) # 扩充后矩形对角坐标
else:
ex_width=0.2*(init_vehicle[i][2]-init_vehicle[i][0]) #竖向不需要改变变量名称将系数对换下就行。坐标点顺序还是1234不变
ex_height=0.4*(init_vehicle[i][2]-init_vehicle[i][0]) #
boundbxs1 = imgHelper.expand_rectangle(boundbxs1, imgSize, ex_width, ex_height) # 扩充后矩形对角坐标
contour_temp = mathHelper.fourcorner_coordinate(boundbxs1) #得到扩充后矩形框的contour
contour_temp_=np.array(contour_temp)#contour转为array
contour_temp_=np.float32(contour_temp_)
'''遍历角点识别框中心坐标是否在contours内在则计1'''
zzz=0
for j in range(len(center_corner)):
flag = cv2.pointPolygonTest(contour_temp_, (center_corner[j][0], center_corner[j][1]), False) #若为False会找点是否在内或轮廓上(相应返回+1, -1, 0)。
if flag==+1:
zzz+=1
'''contours框内小于等于1个角点认为不在停车位内'''
# if zzz<=1:
if zzz<1:
final_weiting.append(init_vehicle[i])
#print('t7-t6',t7-t6)
#print('final_weiting',final_weiting)
'''4、绘制保存检违停车辆图像'''
tW4=time.time()
'''
colors = Colors()
if final_weiting is not None:
for i in range(len(final_weiting)):
lbl='illegal park'
xyxy=[final_weiting[i][0],final_weiting[i][1],final_weiting[i][2],final_weiting[i][3]]
c = int(5)
plot_one_box(xyxy, _img_cv, label=lbl, color=colors(c, True), line_thickness=3)
final_img=_img_cv
'''
tW5=time.time()
# cv2.imwrite('final_result.png', _img_cv)
timeStr = ' step1:%s step2:%s step3:%s save:%s'%(\
timeHelper.deltaTimeString_MS(tW2,tW1), \
timeHelper.deltaTimeString_MS(tW3,tW2), \
timeHelper.deltaTimeString_MS(tW4,tW3), \
timeHelper.deltaTimeString_MS(tW5,tW4) )
#final_weiting-----[x0,y0,x1,y1,cls,score]
#输出的也是outRe----[cls,x0,y0,x1,y1,score]
#outRes = [ [ 3 ,*x[0:4], x[5] ] for x in final_weiting]###违停用3表示
outRes = [ [ *x[0:4], x[5],3 ] for x in final_weiting]###违停用3表示
return outRes,timeStr #返回最终绘制的结果图、违停车辆(坐标、类别、置信度)