for_elise / visualizer /server.py
Relento
first commit.
ecd3201
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)