Ejemplo n.º 1
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()
Ejemplo n.º 2
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()
Ejemplo n.º 3
0
def main():

    # -------------------------------------------------------------------------------------------------------
    # load stuff
    # output probabilities (10k * 50)
    # output = np.load(output_file)

    # test data - label_names size (50), test_labels size (100013), test_files size (100013)
    pickle_1 = open(pickle_file1, "rb")
    T1 = pickle.load(pickle_1)
    pickle_in = open(pickle_file3, "rb")
    T = pickle.load(pickle_in)

    state_centers = np.load(state_center_file)
    # keys in pickle dict
    for key in T1:
        print("key: %s size: %d" % (key, len(T1[key])))
    for key in T:
        print("key: %s size: %d" % (key, len(T[key])))

    test_labels = np.asarray(T["labels"])
    test_images = np.asarray(T["filenames"])
    output = np.asarray(T["output"])
    label_names = np.asarray(T1["label_names"])

    # -------------------------------------------------------------------------------------------------------
    # create dictionary: key = filename, value = coordinates
    locations = {}
    root = test_image_path
    dirlist = [
        item for item in os.listdir(root)
        if os.path.isdir(os.path.join(root, item))
    ]
    # for i in range(0,1):
    for i in range(0, len(dirlist)):
        infoFileName = test_image_path + '/' + dirlist[i] + '/info.txt'
        # f = open(infoFileName,'r')
        stateData = genfromtxt(infoFileName, delimiter=',', dtype=str)
        nLines = stateData.shape[0]
        for i in range(0, nLines, 2):
            # print(i,coords[0])
            label = re.sub(r'.*test_data/', 'test_data/', stateData[i])
            label = label.replace('//', '/')
            coords = [float(n) for n in stateData[i + 1].split(' ')]
            locations[label] = np.array([coords])

    # -------------------------------------------------------------------------------------------------------
    # plot if correct/incorrect
    correctCoords = np.empty((0, 2))
    incorrectCoords = np.empty((0, 2))
    missing = 0
    for i in range(0, test_images.shape[0]):
        name = str(test_images[i][0])
        name = re.sub(r'.*test_data/', 'test_data/', name)
        name = name[0:-1]
        try:
            coords = locations[name]
            if (test_labels[i] == np.argmax(output[i])):
                correctCoords = np.concatenate(
                    (correctCoords, locations[name]), axis=0)
            else:
                incorrectCoords = np.concatenate(
                    (incorrectCoords, locations[name]), axis=0)
        except KeyError:
            # print('Key does not exist')
            missing += 1
    nCorrect = np.float(correctCoords.shape[0])
    nIncorrect = np.float(incorrectCoords.shape[0])
    print('correct: ', nCorrect)
    print('incorrect: ', nIncorrect)
    print('mislabelled: ', missing)
    print(nCorrect / (nCorrect + nIncorrect))

    print(list(correctCoords[0:10, 0]))

    # -------------------------------------------------------------------------------------------------------
    # USA borders
    borders = location_sampler.get_borders(states_file)

    #location_sampler.plot_map(borders)
    nLabels = len(borders)
    print(nLabels)
    plt.figure(1)
    # generate border map of USA
    for k in range(0, nLabels):
        borders[k] = borders[k].astype(np.float)
        plt.plot(borders[k][:, 0], borders[k][:, 1], 'k-', linewidth=1.0)
        plt.hold(True)
        # print(type(borders[k][0,0]))
    # plt.scatter(correctCoords[:,1],correctCoords[:,0],s=0.1,c='g')
    plt.scatter(incorrectCoords[:, 1], incorrectCoords[:, 0], s=5.0, c='r')
    plt.scatter(correctCoords[:, 1], correctCoords[:, 0], s=5.0, c='g')
    plt.show()
    plt.axis('equal')
    plt.xlabel('lng')
    plt.ylabel('lat')
Ejemplo n.º 4
0
def main():
    x = 50
    y = 2500
    heading = [0, 90, 180, 270]
    borders = location_sampler.get_borders(states_file)
    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)

    coords = genfromtxt(images_dir + '/coords.txt',
                        delimiter=',',
                        dtype=np.float64)

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

    for i in range(0, 50):
        panoids = []

        lat = coords[i, 0]
        lng = coords[i, 1]
        #plot lat,lng on map
        # plt.plot(lng, lat, 'b.', markersize=2)
        # plt.pause(0.05)

        panoids = []
        count = 0
        while not (panoids):
            panoids = streetview_tools.panoids(lat=lat, lon=lng)
            sys.stdout.write('.')
            count += 1
            print count

        print 'index: ', i, lat, lng

        # 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()
Ejemplo n.º 5
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)
Ejemplo n.º 6
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)
Ejemplo n.º 7
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()
Ejemplo n.º 8
0
def main():

    # -------------------------------------------------------------------------------------------------------
    # load stuff
    # output probabilities (10k * 50)
    # output = np.load(output_file)

    # test data - label_names size (50), test_labels size (100013), test_files size (100013)
    pickle_1 = open(pickle_file1, "rb")
    T1 = pickle.load(pickle_1)
    pickle_in = open(pickle_file3, "rb")
    T = pickle.load(pickle_in)

    state_centers = np.load(state_center_file)
    # keys in pickle dict
    for key in T1:
        print("key: %s size: %d" % (key, len(T1[key])))
    for key in T:
        print("key: %s size: %d" % (key, len(T[key])))

    test_labels = np.asarray(T["labels"])
    test_images = np.asarray(T["filenames"])
    output = np.asarray(T["output"])
    label_names = np.asarray(T1["label_names"])
    # label_names = np.asarray(T["label_names"])
    # test_labels = np.asarray(T["test_labels"])
    # test_images = np.asarray(T["test_files"])
    # label_names = np.asarray(T["label_names"])

    # -------------------------------------------------------------------------------------------------------
    # create dict storing which human corresponds to each sample
    infoFileName = test_image_path + '/' + '/info.txt'
    stateData = genfromtxt(infoFileName, delimiter=',', dtype=str)
    nLines = stateData.shape[0]
    gtLabelsDict = {}
    humanLabels = np.array([
        29, 18, 46, 37, 20, 11, 10, 33, 10, 27, 49, 38, 47, 16, 30, 22, 7, 13,
        35, 27, 3, 5, 29, 30, 28, 20, 36, 13, 39, 38, 3, 27, 6, 22, 5, 6, 0,
        48, 8, 17, 29, 36, 38, 41, 29, 14, 8, 15, 14, 38
    ],
                           dtype=int)
    gtLabels = np.array([
        0, 4, 2, 4, 26, 36, 27, 8, 46, 25, 13, 35, 35, 22, 44, 5, 22, 38, 14,
        47, 28, 17, 11, 1, 28, 1, 36, 11, 49, 38, 3, 19, 17, 49, 5, 35, 39, 25,
        2, 46, 11, 6, 6, 5, 30, 17, 3, 42, 16, 38
    ],
                        dtype=int)
    gtLabels1 = gtLabels
    gtLabels = np.repeat(gtLabels, 8)
    subject = np.array([0, 1, 2, 3, 4], dtype=int)
    subject = np.repeat(subject, 80)
    subjectDict = {}
    print('N:', nLines)
    for i in range(0, nLines, 2):
        # print(i,coords[0])
        label = re.sub(r'.*Humans/', 'Humans/', stateData[i])
        label = label.replace('//', '/')
        coords = [float(n) for n in stateData[i + 1].split(' ')]
        gtLabelsDict[label] = gtLabels[i]
        subjectDict[label] = subject[i]
        # print(label,gtLabelsDict[label],subjectDict[label])

    # -------------------------------------------------------------------------------------------------------
    # display network performance for each round
    nCorrect = [0, 0, 0, 0, 0]
    for i in range(0, test_images.shape[0]):
        # print(i)
        name = str(test_images[i][0])
        name = re.sub(r'.*human_trials/', 'human_trials/', name)
        name = name.replace('human_trials', 'Humans')
        name = name[0:-1]
        if (subjectDict[name] == 0):
            print('Name: ', name, 'Guess: ', np.argmax(output[i]), 'True: ',
                  gtLabelsDict[name], 'Subject: ', subjectDict[name])
        # print(np.argmax(output[i]))
        if (np.argmax(output[i]) == gtLabelsDict[name]):
            nCorrect[subjectDict[name]] += 1
    print(nCorrect)

    borders = location_sampler.get_borders(states_file)

    # -------------------------------------------------------------------------------------------------------
    # Generate figure comparing performance on map
    #location_sampler.plot_map(borders)
    nLabels = len(borders)
    print(nLabels)
    plt.figure(1)
    # generate border map of USA
    for k in range(0, nLabels):
        borders[k] = borders[k].astype(np.float)
        plt.plot(borders[k][:, 0], borders[k][:, 1], 'k-', linewidth=1.0)
        plt.hold(True)

    #data from game 1 for generating figures
    iRound = 9
    #output labels different order from state borders
    reorder = np.array([
        11, 33, 49, 13, 46, 42, 43, 32, 23, 30, 41, 27, 10, 2, 3, 6, 5, 12, 45,
        24, 48, 38, 15, 20, 34, 22, 39, 9, 21, 28, 37, 0, 16, 44, 1, 4, 25, 47,
        31, 40, 7, 17, 26, 19, 18, 29, 36, 8, 14, 35
    ],
                       dtype=int)
    humanGuess = np.array([10, 33, 27, 46, 18, 37, 10, 20, 29, 11], dtype=int)
    netGuess = np.array([41, 41, 26, 2, 4, 4, 27, 27, 0, 36], dtype=int)
    correct = np.array([46, 8, 25, 2, 4, 4, 27, 26, 0, 36], dtype=int)
    plt.fill(borders[reorder[correct[iRound]]][:, 0],
             borders[reorder[correct[iRound]]][:, 1],
             color='#66ff33')
    plt.plot(borders[reorder[netGuess[iRound]]][:, 0],
             borders[reorder[netGuess[iRound]]][:, 1],
             'b-',
             linewidth=2.0)
    plt.plot(borders[reorder[humanGuess[iRound]]][:, 0],
             borders[reorder[humanGuess[iRound]]][:, 1],
             'r-',
             linewidth=2.0)
    plt.show()
    plt.axis('equal')
    plt.xlabel('lng')
    plt.ylabel('lat')
Ejemplo n.º 9
0
from numpy import genfromtxt
from matplotlib import pyplot as plt
from matplotlib.pyplot import draw
from matplotlib import figure as fig
import xml.etree.ElementTree as ET
import matplotlib
import matplotlib.path as mplPath

import re

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

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)

# 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, 3):
    print labels[states]
    fname = "../images/" + labels[states] + "/info.txt"