__author__ = 'greg'
import json
from pprint import pprint
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook
import math
import numpy as np
import cv2
from sklearn.cluster import DBSCAN
import cPickle as pickle

# img = cv2.imread("/home/ggdhines/Dropbox/066e48f5-812c-4b5f-ab04-df6c35f50393.jpeg")
# print type(img)
# assert False

with open('old_weather.json') as data_file:
    data = json.load(data_file)

scale_x = 1.455
scale_y = 1.45
x_offset = 20
y_offset = 2

# image_file = cbook.get_sample_data("/home/ggdhines/Dropbox/066e48f5-812c-4b5f-ab04-df6c35f50393.jpeg")
image = plt.imread("/home/ggdhines/Dropbox/066e48f5-812c-4b5f-ab04-df6c35f50393.jpeg")

base = [198, 188, 153]

range_x = 0
range_y = 0

def med_x(pts):
    x,y = zip(*pts)
    return np.median(x)

data_c = 0

cluster_fig = None

for box in data["classifications"][0]["annotations"]:
    if "type" in box.keys():
        continue

    # fig = plt.figure()
    # axes = fig.add_subplot(1, 1, 1)
    # im = axes.imshow(image)
    x = int((box["x"]-x_offset)/scale_x)
    y = int((box["y"]-y_offset)/scale_y)

    x2 = int((box["x"] + box["width"]-x_offset)/scale_x)
    y2 = int((box["y"] + box["height"]-y_offset)/scale_y)
    #
    # # if ("type" in box.keys()) or (y < -1200):
    # #     continue
    #
    # plt.plot([x,x2,x2,x,x],[y,y,y2,y2,y])
    #
    # plt.show()

    m = []

    for y_i in range(y,y2+1):
    # for x_i in range(x,x2+1):
        m_t = []