解決 Faster R-CNN 圖片中框不在一張圖片上顯示的問題


解決 Faster R-CNN 圖片中框不在一張圖片上顯示的問題

發現問題

在使用demo.py的時候,選取測試用的圖片,放到demo,然后修改demo.py中對應的圖片名稱,然后進行測試:

發現:圖片中被框出來的部分並沒有完全到一張圖片上去,經過多張圖片的測試,可以發現,並不是一張圖片上一個框,而是按照類別進行的划分,即:每一類一張圖片

如何解決這個問題?

原先的畫圖部分主要在這里:

def vis_detections(im, class_name, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    inds = np.where(dets[:, -1] >= thresh)[0]
    if len(inds) == 0:
        return

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(12, 12))
    ax.imshow(im, aspect='equal')
    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]

        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1], fill=False,
                          edgecolor='red', linewidth=3.5)
            )
        ax.text(bbox[0], bbox[1] - 2,
                '{:s} {:.3f}'.format(class_name, score),
                bbox=dict(facecolor='blue', alpha=0.5),
                fontsize=14, color='white')

    ax.set_title(('{} detections with '
                  'p({} | box) >= {:.1f}').format(class_name, class_name,
                                                  thresh),
                  fontsize=14)
    plt.axis('off')
    plt.tight_layout()
    plt.draw()

def demo(net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.DATA_DIR, 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()
    timer.tic()
    scores, boxes = im_detect(net, im)
    timer.toc()
    print ('Detection took {:.3f}s for '
           '{:d} object proposals').format(timer.total_time, boxes.shape[0])

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3
    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1 # because we skipped background
        cls_boxes = boxes[:, 4*cls_ind:4*(cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(im, cls, dets, thresh=CONF_THRESH)

修改為:

# 將檢測可視化
def vis_detections(ax,im, class_name, dets, thresh=0.5):
    """Draw detected bounding boxes."""
    #print("+_+")
    #print(class_name,dets,thresh)
    inds = np.where(dets[:, -1] >= thresh)[0]
    print("!!!")
    #print(inds) # 是否檢測出來東西,如果有的話為0如果沒有為空
    if len(inds) == 0:
        return
    #print(im.shape)  # 4000 6000 3
    #調整通道順序,如果不調整通道順序,圖像就不正常

    for i in inds:
        bbox = dets[i, :4]
        score = dets[i, -1]
        #print(bbox[0],bbox[1],bbox[2],bbox[3])
        print("add one patch")
        ax.add_patch(
            plt.Rectangle((bbox[0], bbox[1]),
                          bbox[2] - bbox[0],
                          bbox[3] - bbox[1], fill=False,
                          edgecolor='red', linewidth=2)
            )
        ax.text(bbox[0], bbox[1] - 2,
                '{:s} {:.3f}'.format(class_name, score),
                bbox=dict(facecolor='white', alpha=0.9),
                fontsize=8, color='black')
    ax.set_title(('{} detections with '
                  'p({} | box) >= {:.1f}').format(class_name, class_name,thresh),fontsize=12)


def demo(sess, net, image_name):
    """Detect object classes in an image using pre-computed object proposals."""

    # Load the demo image
    im_file = os.path.join(cfg.FLAGS2["data_dir"], 'demo', image_name)
    im = cv2.imread(im_file)

    # Detect all object classes and regress object bounds
    timer = Timer()

    timer.tic()
    # detect the picture to find score and boxes
    scores, boxes = im_detect(sess, net, im)
    # 檢測主體部分,在這里加上save_feature_picture
    # 這里的net內容是vgg

    timer.toc()

    print('Detection took {:.3f}s for {:d} object proposals'.format(timer.total_time, boxes.shape[0]))

    # Visualize detections for each class
    CONF_THRESH = 0.8
    NMS_THRESH = 0.3

    im = im[:, :, (2, 1, 0)]
    fig, ax = plt.subplots(figsize=(10,10))
    ax.imshow(im, aspect='equal')

    for cls_ind, cls in enumerate(CLASSES[1:]):
        cls_ind += 1  # because we skipped background
        cls_boxes = boxes[:, 4 * cls_ind:4 * (cls_ind + 1)]
        cls_scores = scores[:, cls_ind]
        dets = np.hstack((cls_boxes,
                          cls_scores[:, np.newaxis])).astype(np.float32)
        keep = nms(dets, NMS_THRESH)
        dets = dets[keep, :]
        vis_detections(ax,im, cls, dets, thresh=CONF_THRESH)
        plt.draw()

點撥:一開始的時候我也沒有發現這兩個有什么區別,后來發現圖片是按照類別進行區分的以后,就可以看出,demo函數中是按照類別進行划分的,所以只要修改plt位置,將plt從vis_detections中放到demo中,這樣所有類都會花在同一個plt中,而不會分開了,這樣就解決了這個問題。

參考issues

How detect multiple object on same picture?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM