def assign_colors(photos, tags): """ Assigns photo color to most common tag """ used_colors = {} cluster_info = defaultdict(list) #assign color of photo to the color of the most related tag for photo in photos: min_count = len(photos) + 1 photo_tags = photo['tags'].split(' ') #for photos with tag with search term, assign initially to first item best_tag = photo_tags[0] #find most common tag for tag in photo_tags: assoc_tag = tags[tag] #found a common tag, not including one common to all photos if assoc_tag['count'] < min_count: min_count = assoc_tag['count'] best_tag = tag #generate a color for tag if it does not have one if tags[best_tag]['color'] is None: tags[best_tag]['color'] = gen_hex_color(used_colors) photo['iconcolor'] = tags[best_tag]['color'] cluster_info[best_tag].append(photo) return cluster_info
def get_centroids(photos): """ Gets the centroids used in K-means computation """ max_int = len(photos) - 1 used_colors = {} centroids = [] #get the random images selections = random.sample(range(0, max_int), CLUSTER_COUNT) #assign random unique colors for selection in selections: new_color = gen_hex_color(used_colors) photo = photos[selection] photo['iconcolor'] = new_color centroids.append({'lat':float(photo['lat']), 'lon':photo['lon'], 'color':new_color, 'photos':[]}) return centroids