Пример #1
0
def main():
    #[x,y,z] = np.shape(state_points)
    # Alaska - 0, Wyoming - 49
    start = int(sys.argv[1])
    finish = int(sys.argv[2])

    print "State " + str(start) + " to " + str(finish)
    #x = 50
    y = 2500
    heading = [0, 90, 180, 270]

    borders = location_sampler.get_borders(states_file)
    labels = location_sampler.get_labels(states_file)

    density = location_sampler.load_density(density_file)

    #location_sampler.plot_map(borders)
    nLabels = len(borders)

    # generate border map of USA
    for i in range(0, nLabels):
        plt.plot(borders[i][:, 0], borders[i][:, 1], 'r-')
    plt.show(0)
    plt.hold(True)
    # subset = [4,10,26,27,37]
    # y = [2500, 1337, 2500, 2500,2500]

    #    for states in range(1,x):
    for states in range(start, finish):

        dir = images_dir + str(labels[states]) + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)
        f = open(dir + "info.txt", "a")

        # number of images already in the path
        images_in_folder = (len([
            name for name in os.listdir(dir)
            if os.path.isfile(os.path.join(dir, name))
        ]) - 1) / 4
        images_needed = y - images_in_folder

        print str(
            labels[states]) + " needs " + str(images_needed) + " more images!"

        if images_needed < 0:
            continue  # more than 10K, moving on...

        for vals in range(0, images_needed):
            panoids = []

            while not (panoids):
                state_points = location_sampler.get_points_in_states(
                    borders, 1, density)  # long, lat
                lat = state_points[states][0][1]
                lng = state_points[states][0][0]
                # lat=-33.856f93857571269 lng=151.2144895142714
                panoids = streetview_tools.panoids(lat=lat, lon=lng)
                sys.stdout.write('.')

            print str(labels[states]) + " " + str(vals + images_in_folder)

            # plot lat,lng on map
            plt.plot(lng, lat, 'b.', markersize=2)
            plt.pause(0.05)

            for directions in heading:

                filename = streetview_tools.api_download(
                    panoids[0]['panoid'],
                    directions,
                    dir,
                    streetview_API_key,
                    width=256,
                    height=256,
                    fov=90,
                    pitch=0,
                    extension='jpg',
                    year=panoids[0]['year'])
                try:
                    A = cv2.imread(filename, 1)
                    difference = cv2.subtract(A, fail_image)
                    result1 = not np.any(difference)

                    difference = cv2.subtract(A, fail_image2)
                    result2 = not np.any(difference)

                    if result1 is True:
                        print "Street View limit has been reached!"
                        # Todo - delete image
                        os.remove(filename)
                        f.close()
                        sys.exit()
                    elif result2 is True:
                        print "Street View Signature Error!"
                        # Todo - delete image
                        os.remove(filename)
                        f.close()
                        sys.exit()

                    cv2.imshow('current image', A)
                    cv2.waitKey(1)
                    f.write("%s \r %f %f \n" % ((filename), (lat), (lng)))
                except cv2.error:
                    print "OpenCV error: moving along..."
        f.close()
Пример #2
0
def main():
    # Alaska - 0, Wyoming - 49
    start = int(sys.argv[1])
    finish = int(sys.argv[2])

    print "State " + str(start) + " to " + str(finish)

    y = 500 # 20% of training data per state
    heading = [0,90,180,270]

    borders= location_sampler.get_borders(states_file)
    labels= location_sampler.get_labels(states_file)

    density = location_sampler.load_density(density_file)

    #location_sampler.plot_map(borders)
    nLabels = len(borders)

    # generate border map of USA
    for i in range(0,nLabels):
        plt.plot(borders[i][:,0], borders[i][:,1], 'r-')
    plt.show(0)
    plt.hold(True)

#    for states in range(1,x):
    for states in range(start,finish): 

        dir = images_dir + '/' + str(labels[states]) + '/'
        check_dir = train_dir + '/' + str(labels[states]) + '/'
        if not os.path.exists(dir):
            os.makedirs(dir)
        f = open( dir + "info.txt","a")

        # number of images already in the path 
        images_in_folder = (len([name for name in os.listdir(dir) if os.path.isfile(os.path.join(dir, name))]) - 1)/4
        images_needed = y - images_in_folder

        print  str(labels[states]) + " needs " + str(images_needed) + " more images!"

        if images_needed < 0:
            continue    # more than 2K, moving on...

        for vals in range(0,images_needed):
            panoids = []
            
            while not(panoids):
                state_points = location_sampler.get_points_in_states(borders,1,density) # long, lat
                lat = state_points[states][0][1]
                lng = state_points[states][0][0]
                panoids = streetview_tools.panoids(lat=lat, lon=lng)
                sys.stdout.write('.')

            print  str(labels[states])  + " " + str(vals + images_in_folder)

            # plot lat,lng on map
            plt.plot(lng, lat, 'b.', markersize=2)
            plt.pause(0.05)

            for directions in heading:

                filename = streetview_tools.api_download(panoids[0]['panoid'], directions, dir, streetview_API_key, width=256, height=256,fov=90, pitch=0, extension='jpg', year=panoids[0]['year'])
                try:
                    A = cv2.imread(filename,1)
                    difference = cv2.subtract(A, fail_image)
                    result1 = not np.any(difference)

                    difference = cv2.subtract(A, fail_image2)
                    result2 = not np.any(difference)

                    if result1 is True:
                        print "Street View limit has been reached!"
                        # Todo - delete image 
                        os.remove(filename)
                        f.close()
                        sys.exit()
                    elif result2 is True:
                    	print "Street View Signature Error!"
                        # Todo - delete image 
                        os.remove(filename)
                        f.close()
                        sys.exit()

                    # check for duplicate 
                    test_filename = str(re.search(r'(.*)/(.*)',filename).group(2))

                    print "Checking for duplicate..."
                    duplicate_flag = 0
                    for train_filename in os.listdir(check_dir):
                        #pdb.set_trace()

                        if train_filename == test_filename:
                            print "DUPLICATE"
                            os.remove(filename)
                            duplicate_flag = 1
                            break

                    if duplicate_flag == 0: 
                        f.write("%s \r %f %f \n" % ((filename), (lat), (lng)))
                        print "UNIQUE"

                    cv2.imshow('current image',A)
                    cv2.waitKey(1)

                except cv2.error:
                    print "OpenCV error: moving along..."
        f.close()
Пример #3
0
def show_image_and_map_prob(test_labels, label_names, test_images, output,
                            test_image_path, offset):
    states_file = '../sampler/states.xml'
    borders = location_sampler.get_borders(states_file)
    labels = location_sampler.get_labels(states_file)
    #location_sampler.plot_map(borders)
    nLabels = len(borders)

    # 256 value colormap?
    # cmap = plt.cm.jet
    cmap = plt.get_cmap('YlOrRd')

    cmaplist = [cmap(i) for i in range(cmap.N)]

    output_vals = np.sort(output, axis=1)[:, :]
    output_labels = np.argsort(output, axis=1)[:, :]
    # pdb.set_trace()
    acc = 0

    for i in range(offset, output_labels.shape[0]):
        # plot the image first
        im_path = test_image_path + '/' + label_names[
            test_labels[i]] + '/' + test_images[i].rsplit('/', 1)[-1]
        print(im_path)
        img = mpimg.imread(im_path)
        plt.figure(0)
        imgplot = plt.imshow(img)
        # plt.show()

        plt.figure(1)
        # generate border map of USA
        for k in range(0, nLabels):
            plt.plot(borders[k][:, 0], borders[k][:, 1], 'r-')
            plt.hold(True)
            # plot for states

            # ground truth
            idx = labels.index(label_names[test_labels[i]])
            plt.plot(borders[idx][:, 0],
                     borders[idx][:, 1],
                     'c-',
                     linewidth=3.0)

            for l in range(0, nLabels):
                # shade each based on probability
                idx_shade = labels.index(label_names[int(output_labels[i, l])])
                color_to_shade = int(
                    round((output_vals[i, l] / np.max(output_vals[i, :])) *
                          (len(cmaplist) - 1)))
                #print(color_to_shade)
                #pdb.set_trace()
                plt.fill(borders[idx_shade][:, 0],
                         borders[idx_shade][:, 1],
                         color=cmaplist[color_to_shade])

            #draw legend
            colors = ['#00ffff']
            LABELS = ['Ground Truth']
            patches = [
                mpatches.Patch(color=color, label=label)
                for label, color in zip(LABELS, colors)
            ]
            plt.legend(patches, LABELS, loc=1, frameon=False)
        plt.show(0)
        input("Press Enter to continue...")
        plt.hold(False)
Пример #4
0
def show_image_and_map(test_labels, label_names, test_images, output,
                       test_image_path):
    states_file = '../sampler/states.xml'
    borders = location_sampler.get_borders(states_file)
    labels = location_sampler.get_labels(states_file)
    #location_sampler.plot_map(borders)
    nLabels = len(borders)

    output_labels = np.argsort(output, axis=1)[:, -5:]
    acc = 0

    for i in range(0, output_labels.shape[0]):
        # plot the image first
        im_path = test_image_path + '/' + label_names[
            test_labels[i]] + '/' + test_images[i].rsplit('/', 1)[-1]
        print(im_path)
        img = mpimg.imread(im_path)
        plt.figure(0)
        imgplot = plt.imshow(img)
        # plt.show()

        plt.figure(1)
        # generate border map of USA
        for k in range(0, nLabels):
            plt.plot(borders[k][:, 0], borders[k][:, 1], 'r-')
            plt.hold(True)

            # plot for states

            # ground truth
            idx = labels.index(label_names[test_labels[i]])
            plt.plot(borders[idx][:, 0],
                     borders[idx][:, 1],
                     'c-',
                     linewidth=5.0)

            idx1 = labels.index(label_names[int(output_labels[i, 0])])
            idx2 = labels.index(label_names[int(output_labels[i, 1])])
            idx3 = labels.index(label_names[int(output_labels[i, 2])])
            idx4 = labels.index(label_names[int(output_labels[i, 3])])
            idx5 = labels.index(label_names[int(output_labels[i, 4])])

            plt.fill(borders[idx1][:, 0], borders[idx1][:, 1], color='#66ff33')
            plt.fill(borders[idx2][:, 0], borders[idx2][:, 1], color='#ffff00')
            plt.fill(borders[idx3][:, 0], borders[idx3][:, 1], color='#ff9900')
            plt.fill(borders[idx4][:, 0], borders[idx4][:, 1], color='#cc3300')
            plt.fill(borders[idx5][:, 0], borders[idx5][:, 1], color='#4d1300')

            # draw legend
            colors = [
                '#66ff33', '#ffff00', '#ff9900', '#cc3300', '#4d1300',
                '#00ffff'
            ]
            LABELS = [
                '#1 prediction', '#2 prediction', '#3 prediction',
                '#4 prediction', '#5 prediction', 'Ground Truth'
            ]
            patches = [
                mpatches.Patch(color=color, label=label)
                for label, color in zip(LABELS, colors)
            ]
            plt.legend(patches, LABELS, loc=1, frameon=False)

        plt.show(0)
        input("Press Enter to continue...")
        plt.hold(False)
Пример #5
0
from geopy.distance import great_circle
import numpy as np
import googlemaps
from matplotlib import pyplot as plt
import pdb

import sys
sys.path.append('../scraper')
import location_sampler

states_file = 'states.xml'

borders = np.asarray(location_sampler.get_borders(states_file))
labels = location_sampler.get_labels(states_file)

nLabels = len(borders)

state_center = np.zeros([nLabels, 2])

for i in range(0, nLabels):
    plt.fill(borders[i][:, 0], borders[i][:, 1], 'r-')
    border_A = map(float, borders[i][:, 0])
    border_B = map(float, borders[i][:, 1])
    #pdb.set_trace()

    state_center[i, 1] = np.mean(border_A)
    state_center[i, 0] = np.mean(border_B)
    plt.plot(state_center[i, 1], state_center[i, 0], 'b.')

np.save("state_center.npy", state_center)
plt.show()