예제 #1
0
def run_prog():
    parser = OptionParser()
    parser.add_option("-m", "--model-file", action="store", default="svm_model.pkl")
    parser.add_option("-s", "--signed", action="store_true", default=False)
    parser.add_option("-r", "--random-window", action="store_true", default=False)
    parser.add_option("-n", "--num-scales", action="store", type=int, default=1)

    (options, args) = parser.parse_args()
    
    print "Loading model from file ", options.model_file
    model_file = open(options.model_file, "rb")
    scaler, svc = pickle.load(model_file)
    model_file.close()
    print "Model loaded"
    
    p_svc = pickle.dumps(svc)
    p_scaler = pickle.dumps(scaler)
    
    #imgin = Image.open("images/pedestrian1.png")
    imgin = Image.open(args[0])
    
    #imgin = imgin.resize((imgin.size[0] // 2, imgin.size[1] // 2), Image.CUBIC)
	
    humans_scale = []
    
    imgin = imgin.convert("L") # convert to greyscale (luminance)
    
    if options.num_scales == 1:
        scales = [1]
    else:
        scales = np.linspace(1, 0.2, num=5)
    
    if False:    
        for i in range(len(scales)):
            scale = scales[i]
            print "Doing scale ", scale, i, "of", len(scales)
            
            img = imgin.resize((int(imgin.size[0] * scale), int(imgin.size[1] * scale)), Image.LINEAR)
            
            img = np.array(img, dtype='d', order='C')
            
            if options.random_window:
                img = randWindowExtractor(img, options.signed)
            
            print "Image shape", img.shape
            
        
    if True:
        #img = imgin.resize((int(imgin.size[0] * 0.25), int(imgin.size[1] * 0.25)), Image.LINEAR)    
        #img = np.array(imgin, dtype='d', order='C')
        img = np.array(imgin)
        window_hits, detected_humans = detect_humans_multi(img, p_svc, p_scaler, options.signed, debug=True)
        #window_hits, detected_humans = detect_humans(img, svc, scaler, options.signed, debug=True)
        humans_scale.append((1.0, detected_humans))

    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.set_cmap(plt.cm.gray)
    ax.imshow(np.array(imgin))
    #ax.add_patch(matplotlib.patches.Rectangle(((window_hits[0] * cellsize) + window_pixel_shape[0], (window_hits[1] * cellsize) + window_pixel_shape[1]), window_pixel_shape[0], window_pixel_shape[1]))
    #for x_win_hits in range(window_hits.shape[0]):
    #    for y_win_hits in range(window_hits.shape[1]):
    #        #ax.add_patch(matplotlib.patches.Rectangle((y_win_hits * cellsize, x_win_hits * cellsize), window_pixel_shape[1], window_pixel_shape[0], ec='blue', facecolor='none'))
    #        if window_hits[x_win_hits, y_win_hits] >= 0.5:
    #            print (x_win_hits * cellsize), (y_win_hits * cellsize)
    #            ax.add_patch(matplotlib.patches.Rectangle((y_win_hits * cellsize, x_win_hits * cellsize), window_pixel_shape[1], window_pixel_shape[0], ec='red', facecolor='none', hatch="/"))
    
    for scale, detected_humans in humans_scale:
        for x, y, _ in detected_humans:
            ax.add_patch(matplotlib.patches.Rectangle((y, x), window_pixel_shape[1] / scale, window_pixel_shape[0] / scale, ec='red', facecolor='none'))
    
    #plt.figure()
    #plt.imshow(window_hits)
    plt.show()
예제 #2
0
 if len(args) < 2:
     raise "Must provide image directory and file to save to"
 
 imgs = DirectoryImagesLoader(args[0])
 
 hogs = []
 
 
 
 for i in range(len(imgs)):
     print "Running HOG on image", i, "of", len(imgs)
     img = imgs.get_image(i)
     
     if options.random_window:
         #for i in range(10):
         win = randWindowExtractor(img)
         _, normcells = HOG.HOG(win, options.signed)
         hogs.append(normcells.flatten())
     else:
         _, normcells = HOG.HOG(img, options.signed)
         hogs.append(normcells.flatten())
     
     #sPickle.s_dump_elt(normcells, f)
     
 f = open(args[1], 'wb')
 
 pickle.dump(hogs, f)
 
 f.close()
 
 print "Finished"