未检测到汽车的盒子

时间:2017-06-15 00:57:55

标签: python-3.x opencv classification

我正在尝试运行python代码来检测图像中的汽车并在其周围绘制一个框。但不知何故,即使没有错误,我也没有收到盒子。我正在使用的代码如下:

绘制框的功能

def draw_boxes(img, bboxes, color=(0, 0, 255), thick=6):
    # Make a copy of the image
    imcopy = np.copy(img)
    # Iterate through the bounding boxes
    for bbox in bboxes:
        # Draw a rectangle given bbox coordinates
        cv2.rectangle(imcopy, bbox[0], bbox[1], color, thick)
    # Return the image copy with boxes drawn
    return imcopy

绘制框的功能

def search_windows(img, windows, model):
    #1) Create an empty list to receive positive detection windows
    #2) Iterate over all windows in the list
    test_images = []
    for window in windows:
        #3) Extract the test window from original image
        test_img = cv2.resize(img[window[0][1]:window[1][1], window[0][0]:window[1][0]], (64, 64))
        # Normalize image
        test_img = test_img/255
        # Predict and round the result
        test_images.append(test_img)
    test_images = np.array(test_images)

    prediction = np.around(model.predict(test_images))
    on_windows = [windows[i] for i in np.where(prediction==1)[0]]
    return on_windows

阅读图像并使用这些功能绘制框

img = mpimg.imread(test_images[0])

detected_windows = search_windows(img, windows, model)                       

window_img = draw_boxes(img, detected_windows, color=(0, 255, 0), thick=3)                    

plt.imshow(window_img)

感谢adanvce。

1 个答案:

答案 0 :(得分:1)

我找到了答案。我需要在代码中为prediction设置更好的限制。如果我将0.40.5设置为值,那么我会获得这些框,然后按照我的方式进行更好的预测。