File size: 4,559 Bytes
ecd3201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from flask import (
    Flask,
    jsonify,
    Response,
    make_response,
    send_file,
    send_from_directory,
)
from flask import Response
import json
import time
import pickle
import numpy as np
import json
from pathlib import Path


def get_release_root_path():
    return Path(__file__).resolve().parents[1]

print("Loading data...")

assets_dir = get_release_root_path() / "visualizer" / "assets"
piece_id = 1
data_dir = get_release_root_path() / "dataset" / f"{piece_id:03d}"

with open(assets_dir / "mano_faces.pkl", "rb") as f:
    faces = pickle.load(f)

print("Data loaded.")
app = Flask(__name__)


@app.route("/")
def index():
    # return the index.html in the current directory
    return send_file(Path(__file__).parent / "static" / "index.html")


@app.route("/resources/<path:filename>")
def resources(filename):
    # return the index.html in the current directory
    return send_file(Path(__file__).parent / "static" / "resources" / filename)


@app.route("/js/<path:filename>")
def js(filename):
    # return the index.html in the current directory
    return send_file(Path(__file__).parent / "static" / "js" / filename)


@app.route("/css/<path:filename>")
def css(filename):
    # return the index.html in the current directory
    return send_file(Path(__file__).parent / "static" / "css" / filename)


@app.route("/vis")
def vis():
    return send_file(Path(__file__).parent / "static" / "vis.html")


@app.route("/pieces")
def pieces():
    # return the index.html in the current directory
    return send_file(Path(__file__).parent / "pieces.html")


@app.route("/pieces_metadata")
def pieces_metadata():
    # Ensure the file exists at this path
    metadata_file = assets_dir.parents[1] / "metadata.json"
    if metadata_file.exists():
        return send_file(metadata_file, mimetype="application/json")
    else:
        return jsonify({"error": "File not found"}), 404


def get_data_path(piece_id):
    return get_release_root_path() / "dataset" / f"{piece_id:03d}"


@app.route("/audio/<int:piece_id>")
def get_audio(piece_id):
    audio_path = get_data_path(piece_id) / "audio.mp3"
    return send_file(audio_path)


@app.route("/metadata/<int:piece_id>")
def get_piece_metadata(piece_id):
    return send_file(
        get_data_path(piece_id) / "vis" / "metadata.json", mimetype="application/json"
    )


@app.route("/mano_faces_data")
def mano_faces_data():
    d = {
        "left_faces": faces["left_faces"].tolist(),
        "right_faces": faces["right_faces"].tolist(),
    }
    return jsonify(d)


@app.route("/mano_vertices_data/<int:piece_id>")
def stream_mano_vertices_data(piece_id):
    data_path = get_data_path(piece_id)
    with open(data_path / "motion.pkl", "rb") as f:
        motion_data = pickle.load(f)
    with open(data_path / "vis" / "pressed_keys.pkl", "rb") as f:
        pressed_keys = pickle.load(f)
    print("motion data length", len(motion_data["left"]["mano_params"]["verts"]))
    print("pressed keys length", len(pressed_keys))
    n_frames = min(len(motion_data["left"]["mano_params"]["verts"]), len(pressed_keys))
    print("n_frames", n_frames)

    def generate_mesh_data():
        # Stream each frame of the mesh data
        for i in range(0, n_frames):
            frame_data = {
                "left_vertices": np.round(
                    motion_data["left"]["mano_params"]["verts"][i], 4
                )
                .astype(str)
                .tolist(),
                "right_vertices": np.round(
                    motion_data["right"]["mano_params"]["verts"][i], 4
                )
                .astype(str)
                .tolist(),
                "pressed_keys": pressed_keys[i].tolist(),
            }
            # Convert the frame data to a JSON string and add a newline delimiter
            yield json.dumps(
                frame_data
            ) + "\n"  # Send each frame as a complete JSON object

    response = Response(generate_mesh_data(), content_type="application/json")
    response.headers["Access-Control-Allow-Origin"] = "*"  # Allow CORS from any origin
    return response


@app.route("/piano_mesh/<path:filename>")
def get_obj_file(filename):
    piano_mesh_dir = assets_dir / "piano_meshes"
    if (piano_mesh_dir / filename).exists():
        response = send_from_directory(piano_mesh_dir, filename)
        response.headers["Access-Control-Allow-Origin"] = "*"
        return response
    else:
        return jsonify({"error": "File not found"}), 404


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8080)