-
Notifications
You must be signed in to change notification settings - Fork 41
/
visualize.py
73 lines (63 loc) · 2.27 KB
/
visualize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
Flask app to visualize clusters.
"""
import os
import json
import argparse
from flask import Flask, render_template, request, send_from_directory
app = Flask(__name__, static_url_path='')
def chunks(clusters, size):
"""
Split clusters into chunks of size=size
"""
for i in range(0, len(clusters), size):
yield clusters[i:i+size]
def create_idx_to_path(lfw_dir):
"""
Create a dictionary where the key is the image index and the value is the
path to the image.
"""
labels = sorted(os.listdir(lfw_dir))
idx = 0
idx_to_path = {}
for label in labels:
for img in sorted(os.listdir(os.path.join(lfw_dir, label))):
idx_to_path[idx] = os.path.join(lfw_dir, label, img)
idx += 1
return idx_to_path
@app.route('/img/<path:fpath>', methods=['GET'])
def get_img_path(fpath):
print(os.path.dirname(fpath), fpath.split('/')[-1])
return send_from_directory(os.path.dirname(fpath),fpath.split('/')[-1])
@app.route('/single_cluster', methods=["GET"])
def display_one_cluster():
"""
Method to display images in ine cluster.
"""
cluster_id = request.args.get('cluster')
return render_template("single_cluster.html",
cluster_id=cluster_id,
idx_to_path=idx_to_path,
clusters=clusters)
@app.route('/clusters', methods=["GET"])
def display_clusters():
"""
Method to display the clusters
"""
offset = int(request.args.get('offset', '0'))
limit = int(request.args.get('limit', '50'))
clusters_id_sorted = sorted(clusters, key=lambda x : -len(clusters[x]))
batches = chunks(range(len(clusters_id_sorted)), size=limit)
return render_template('clusters.html',
offset=offset, limit=limit, batches=batches,
ordered_list=clusters_id_sorted[offset:offset+limit+1],
idx_to_path=idx_to_path,
clusters=clusters)
if __name__ == "__main__":
parser = argparse.ArgumentParser("Visualize Clusters")
parser.add_argument("--lfw_dir", required=True, type=str)
parser.add_argument("--clusters", default="data/clusters.json", type=str)
args = parser.parse_args()
idx_to_path = create_idx_to_path(args.lfw_dir)
clusters = json.load(open(args.clusters))
app.run()