Exemplo n.º 1
0
            t2.start()
            t1.join()
            t2.join()
            bboxes = q_detect.get()
            argmax_feats_road, argmax_feats_lane, color_map_display_road, color_map_display_lane = q_sed.get()
            argmax_feats_road[argmax_feats_road == 11] = 100
            argmax_feats_lane[argmax_feats_lane == 11] = 100
            print('inference time:{}s'.format(time.time() - st_st))

            argmax_feats_lane, argmax_feats_road = fun_intergate_seg_LaneandRoad(argmax_feats_lane, argmax_feats_road)
            decision_boxes, img_result = fun_detection_TrafficViolation(frame_np_img, bboxes, argmax_feats_lane, argmax_feats_road)
            t_start_decision = time.time()            
            
            img_fusion = frame_np_img.copy()
            for bbox in bboxes:
                img_fusion = plot_bbox(img_fusion, bbox)
            img_fusion = cv2.addWeighted(img_fusion, 1, color_map_display_road, 0.5, 0)
            imgs = np.hstack([color_map_display_lane, img_fusion, img_result])
            
            
            # RGB → BGR
            imgs = imgs[...,[2,1,0]]
            #videoWriter.write(img_result)
            videoWriter.write(cv2.resize(imgs, (int(1920), int(1080/3))))
            #videoWriter.write(imgs)
            print('decision time : {}s'.format(time.time() - t_start_decision))
            print('total time:{}s'.format(time.time() - st_st))
            success, frame_np_img = videoCapture1.read()
            frame_stack.append(frame_np_img)
            if len(frame_stack)>2:
                del frame_stack[0]
Exemplo n.º 2
0
def fun_detection_TrafficViolation(img, bboxes, map_seg_label_line, map_seg_label_road):
    # 物件違規判斷
    # 依據物件偵測的結果進行判斷
    # img: original image
    # bboxes: detected objects' bboxes
    
    h_img, w_img = fun_getimageWH(img)

    # th_roi_h = [int(h_img/h_img)-1, h_img]
    # th_roi_w = [int(w_img/4), w_img]
    th_roi_h = [0, h_img]
    th_roi_w = [0, w_img]
    ROI_area = (max(th_roi_h)-min(th_roi_h))*(max(th_roi_w)-min(th_roi_w)) 
    
    img_result = img.copy()
    img_result = plot_ROI(
        img_result, [th_roi_w[0],th_roi_h[0],th_roi_w[1],th_roi_h[1]])
    
    #此function用來推論"線的方程式",所以需要"線的segmentation"和"角點的segmentation"
    h_img, w_img = fun_getimageWH(map_seg_label_line)
    # image_label_line, mask_point
    # 2.1 ROI mask預測結果圖
    map_seg_label_line = fun_result_laneline_ROI_modify(map_seg_label_line)
    
    decision_boxes=[]
    for bbox in bboxes:
        object_label = bbox['label'][0]
        bbox_axis = bbox['points']
        
        # bbox左上右下的座標轉換成圖的長和寬
        bbox_x, bbox_y, bbox_w, bbox_h = fun_box2hw(bbox_axis)
        ### 刪除左下右下的carself
        #左上(Front鏡頭)排除
        if bbox_x < 500 and bbox_y > 800 :
            continue
        
        # 將detected的bbox拉大一點
        bbox_axis[0] = int(bbox_axis[0]-bbox_w*0.0)
        if bbox_axis[0]<=0: bbox_axis[0]=0
        bbox_axis[1] = int(bbox_axis[1]-bbox_h*0.1)
        if bbox_axis[1]<=0: bbox_axis[1]=0
        bbox_axis[2] = int(bbox_axis[2]+bbox_w*0.1)
        if bbox_axis[2]>=w_img: bbox_axis[2]=w_img-1
        if (object_label=='m'):
            bbox_axis[3] = int(bbox_axis[3]+bbox_h*0.1)
        if (object_label=='c'):
            bbox_axis[3] = int(bbox_axis[3]+bbox_h*0.2)
        if (object_label=='t'):
            bbox_axis[3] = int(bbox_axis[3]+bbox_h*0.2)
        if bbox_axis[3]>=h_img: bbox_axis[3]=h_img-1
        bbox_x, bbox_y, bbox_w, bbox_h = fun_box2hw(bbox_axis)
    
        bbox_h_range = [int(bbox_axis[1]),int(bbox_axis[3])]
        bbox_w_range = [int(bbox_axis[0]),int(bbox_axis[2])]
        box_area = (max(bbox_h_range)-min(bbox_h_range))*(max(bbox_w_range)-min(bbox_w_range))
        # object in ROI, object must in ROI will be classified.
        # 物件在ROI內才會進行判斷。
        if (((bbox_h_range[0]>= th_roi_h[0]) & (bbox_h_range[0] <= th_roi_h[1]))&\
            ((bbox_w_range[0]>= th_roi_w[0]) & (bbox_w_range[0] <= th_roi_w[1])))\
            &\
           (((bbox_h_range[1]>= th_roi_h[0]) & (bbox_h_range[1] <= th_roi_h[1]))&\
            ((bbox_w_range[1]>= th_roi_w[0]) & (bbox_w_range[1] <= th_roi_w[1]))):
            
            # 1. 車輛的bbox在ROI內的面積比例要大於10%才判斷
            # 2.機車在ROI內不須看比例
            if  ((box_area/ROI_area)>=0.1) & ((object_label=='b') | (object_label=='t') | (object_label=='c'))\
                | (object_label=='m'):
                decision_box = {}
                
                '''
                # 'sw':1,'dr':2,'ud':0;  map_seg_label_road: label_seg
                # 物件的框內,"可行駛道路segmentation"的結果
                # 我們要看此框的segmentation的結果左下角是不是人行道
                # 左下角是人行道的話是人行道違停的機率大。
                # 我們看框內左下(下10%和左50%的聯集區塊)的pixel數量
                '''
                box_map_seg_label_road  = map_seg_label_road[bbox_h_range[0]:bbox_h_range[1], bbox_w_range[0]:bbox_w_range[1]]
                pos_box_map_seg_label_road = np.where(box_map_seg_label_road==1)
                pos_box_map_seg_label_driven = np.where(box_map_seg_label_road==2)
                n_sw = np.sum((pos_box_map_seg_label_road[0]>(bbox_h*0.9))*(pos_box_map_seg_label_road[1]<(bbox_h*0.5))) # bbox_w
                n_da = np.sum((pos_box_map_seg_label_road[0]>(bbox_h*0.5)))
                '''
                # 物件的框內,"線的segmentation"結果
                算1. 機車停車格,2.汽車停車格,3. 紅線 的pixel數
                '''
                box_label_line = map_seg_label_line[bbox_h_range[0]:bbox_h_range[1], bbox_w_range[0]:bbox_w_range[1]]
                box_label_line_red = map_seg_label_line[bbox_h_range[0]+int(bbox_h_range[1]*0.5):bbox_h_range[1], bbox_w_range[0]:bbox_w_range[1]]
                n_motor_line = np.sum(box_label_line==0)  # 機車停車格       
                n_car_line = np.sum(box_label_line==1)  # 汽車停車格
                n_red_line = np.sum(box_label_line_red==6)  # 紅線
                n_bus_line = np.sum(box_label_line==2) #公車格
                n_white_line = np.sum(box_label_line==9)+np.sum(box_label_line==10)# 白線
                n_mstop_line = np.sum(box_label_line==3)  # 機車代轉區
                
                
                # 開始判斷準則
                if (n_sw>10) & (n_red_line<100):
                    '''
                    物件框的人行道pixel如果大於10個(預設可以調整)
                    且紅線小於100個(預設可以調整),就是人行道違停
                    '''
                    decision_box['decision'] = 'Violation_parking_in_sidewalk'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox_Violation(img_result, bbox,(0,255,255))
                elif ( n_motor_line > n_red_line) & (n_motor_line > n_car_line) & (object_label=='m'):
                    '''
                    物件為機車
                    機車線停車線的pixel數量 > 紅線pixel數量
                    機車線停車線的pixel數量 > 汽車停車線的pixel數量
                    機車停在機車停車格,pass
                    '''
                    decision_box['decision'] = 'pass'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox(img_result, bbox)
                elif (n_car_line > n_red_line) & (n_car_line > n_motor_line) & (object_label=='c'):
                    '''
                    物件為汽車
                    汽車線停車線的pixel數量 > 紅線pixel數量
                    汽車線停車線的pixel數量 > 機車停車線的pixel數量
                    汽車停在汽車停車格,pass
                    '''
                    decision_box['decision'] = 'pass'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox(img_result, bbox)
                elif (((n_red_line > n_car_line) | (n_red_line > n_motor_line)) & (n_red_line>n_white_line)&(n_red_line > n_mstop_line)) & (n_red_line>=10) &\
                     ((object_label=='b') | (object_label=='t') | (object_label=='m') | (object_label=='c')):
                    '''
                    (紅線pixel數量 > 汽車線停車線的pixel數量) 或是 (紅線pixel數量 > 機車線停車線的pixel數量)
                    且
                    物件為b(大車)或t(卡車)或m(機車上無人)或是c(汽車)
                    就是紅線違停
                    '''
                    decision_box['decision'] = 'Violation_parking_in_redline'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox_Violation(img_result, bbox,(255,0,0))
                elif ((n_bus_line > n_car_line) & (n_bus_line > n_motor_line)) & (n_bus_line>=100) &\
                     ((object_label=='b') | (object_label=='t') | (object_label=='m') | (object_label=='c')):
                    '''
                    (紅線pixel數量 > 汽車線停車線的pixel數量) 或是 (紅線pixel數量 > 汽機車線停車線的pixel數量)
                    且
                    物件為b(大車)或t(卡車)或m(機車上無人)或是c(汽車)
                    就是公車格違停
                    '''
                    decision_box['decision'] = 'Violation_parking_in_redline'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox_Violation(img_result, bbox,(255,0,255))
                
                else:
                    '''
                    沒被規範到的預設為pass
                    '''
                    decision_box['decision'] = 'pass'
                    decision_box['bbox'] = bbox
                    img_result = plot_bbox(img_result, bbox)
                decision_boxes.append(decision_box)
            else:
                '''
                沒在ROI內的
                '''
                decision_box = {}
                decision_box['decision'] = 'pass'
                decision_box['bbox'] = bbox
                img_result = plot_bbox(img_result, bbox)        
                decision_boxes.append(decision_box)
    return decision_boxes, img_result