Files
scene-actor-extraction/src/gallery/gallery_store.cpp
T
2026-06-12 17:54:23 +02:00

81 lines
2.6 KiB
C++

#include "gallery_store.hpp"
#include <nlohmann/json.hpp>
#include <chrono>
#include <fstream>
#include <iostream>
#include <stdexcept>
using json = nlohmann::json;
ActorGallery load_gallery(const std::string& path) {
std::ifstream f(path);
if (!f.is_open())
throw std::runtime_error("load_gallery: cannot open " + path);
std::cerr << "[gallery] loading " << path << "..." << std::flush;
auto t0 = std::chrono::steady_clock::now();
json j;
f >> j;
auto t1 = std::chrono::steady_clock::now();
std::cerr << " parsed JSON in "
<< std::chrono::duration<double>(t1 - t0).count() << "s\n";
ActorGallery gallery;
for (const auto& ja : j.at("actors")) {
ActorGallery::Actor actor;
actor.imdb_id = ja.value("imdb_id", "");
actor.tmdb_id = ja.value("tmdb_id", "");
// older make_jellyfin_gallery.py galleries used "jellyfin_person_id"
actor.jellyfin_id = ja.value("jellyfin_id", ja.value("jellyfin_person_id", ""));
actor.name = ja.at("name").get<std::string>();
if (ja.contains("source_images"))
actor.source_images = ja.at("source_images").get<std::vector<std::string>>();
for (const auto& je : ja.at("embeddings")) {
Embedding emb = je.get<Embedding>();
actor.embeddings.push_back(emb);
}
gallery.actors.push_back(std::move(actor));
}
size_t n_emb = 0;
for (const auto& actor : gallery.actors) n_emb += actor.embeddings.size();
auto t2 = std::chrono::steady_clock::now();
std::cerr << "[gallery] built " << gallery.actors.size() << " actors / "
<< n_emb << " embeddings in "
<< std::chrono::duration<double>(t2 - t1).count() << "s\n";
return gallery;
}
void save_gallery(const std::string& path, const ActorGallery& gallery) {
json j;
j["actors"] = json::array();
for (const auto& actor : gallery.actors) {
json ja;
ja["imdb_id"] = actor.imdb_id;
ja["tmdb_id"] = actor.tmdb_id;
ja["jellyfin_id"] = actor.jellyfin_id;
ja["name"] = actor.name;
ja["source_images"] = actor.source_images;
ja["embeddings"] = json::array();
for (const auto& emb : actor.embeddings) {
ja["embeddings"].push_back(
std::vector<float>(emb.begin(), emb.end()));
}
j["actors"].push_back(std::move(ja));
}
std::ofstream f(path);
if (!f.is_open())
throw std::runtime_error("save_gallery: cannot write " + path);
f << j.dump(2) << "\n";
}