import multiprocessing
import os
import sys

import wx

import medipy.gui.image
import medipy.io.dicom
import medipy.io.dicom.misc
import medipy.io.dicom.split
import medipy.io.dicom.sort

from hierarchy_tree import HierarchyTree
from dataset_list import DataSetList

class Explorer(wx.Panel):

    def __init__(self, parent, ID=wx.ID_ANY, pos=wx.DefaultPosition,
                 size=wx.DefaultSize, style=wx.TAB_TRAVERSAL, name="explorer"):

        self._series_position = {}
        self._number_of_frames = {}
        self._loaded_datasets = {}
        self._is_multiframe_image = False

        wx.Panel.__init__(self, parent, ID, pos, size, style, name)

        # Create widgets
        main_splitter = wx.SplitterWindow(self)
        left_splitter = wx.SplitterWindow(main_splitter)
        self._hierarchy_tree = HierarchyTree(left_splitter)
        bottom_panel = wx.Panel(left_splitter)
        self._slider = wx.Slider(bottom_panel)
        self._image = medipy.gui.image.Image(bottom_panel, "axial")
        self._dataset_list = DataSetList(main_splitter)

        # Setup widgets
        self._slider.Disable()
        self._image.use_origin_and_spacing = False
        self._image.orientation_visibility = False
        left_splitter.SplitHorizontally(self._hierarchy_tree, bottom_panel)
        main_splitter.SplitVertically(left_splitter, self._dataset_list)

        # Layout
        bottom_panel.SetSizer(wx.BoxSizer(wx.VERTICAL))
        bottom_panel.GetSizer().Add(self._slider, flag=wx.EXPAND)
        bottom_panel.GetSizer().Add(self._image, 1, wx.EXPAND)
        self.SetSizer(wx.BoxSizer())
        self.GetSizer().Add(main_splitter, 1, wx.EXPAND)

        # Bind events
        self._hierarchy_tree.Bind(wx.EVT_TREE_SEL_CHANGED, self.OnHierarchy)
        self._slider.Bind(wx.EVT_SLIDER, self.OnSlider)

    def set_datasets(self, datasets):
        self._hierarchy_tree.set_datasets(datasets)

    ##############
    # Properties #
    ##############
    def _get_hierarchy_tree(self):
        return self._hierarchy_tree

    hierarchy_tree = property(_get_hierarchy_tree)

    ##################
    # Event handlers #
    ##################

    def OnHierarchy(self, dummy):
        """ Handler for hierarchy tree selection """

        if self._hierarchy_tree.selected_datasets :
            datasets = self._hierarchy_tree.selected_datasets

            self._setup_slider(datasets)

            slider_value = self._series_position.get(datasets[0].series_instance_uid.value, 0)
            self._slider.SetValue(slider_value)
            self.OnSlider(None)
        else :
            self._slider.Disable()

    def OnSlider(self, dummy):
        """ Handler for slider events """

        self.Freeze()

        datasets = self._hierarchy_tree.selected_datasets
        series_uid = datasets[0].series_instance_uid.value

        loaded_datasets = self._loaded_datasets[series_uid]
        if "number_of_frames" in loaded_datasets[0] :
            dataset = loaded_datasets[0]
            frame_number = self._slider.GetValue()
            slice = slice = dataset.pixel_array[frame_number]
        else :
            dataset = loaded_datasets[self._slider.GetValue()]
            frame_number = None
            slice = dataset.pixel_array

        slice = medipy.base.Image(data=slice.reshape((1,)+slice.shape))

        if self._image.number_of_layers == 0 :
            self._image.append_layer(slice)
            self._image.reset_view()
        else :
            self._image.delete_layer(0)
            self._image.insert_layer(0, slice)
            self._image.render()
            #self._image.set_layer_image(0, slice)
            # Contrast data

        scroll = (self._dataset_list.GetScrollPos(wx.HORIZONTAL),
                  self._dataset_list.GetScrollPos(wx.VERTICAL))
        self._dataset_list.set_dataset(dataset, frame_number)
        self._dataset_list.SetScrollPos(wx.HORIZONTAL, scroll[0])
        self._dataset_list.SetScrollPos(wx.VERTICAL, scroll[1])

        self._series_position[series_uid] = self._slider.GetValue()

        self.Thaw()

    #####################
    # Private interface #
    #####################

    def _setup_slider(self, datasets):
        """ Setup the image slider.
        """

        series_uid = datasets[0].series_instance_uid.value

        if series_uid not in self._loaded_datasets :
            # Get loadable directory records from the datasets
            records = [x for x in datasets if "directory_record_type" in x]
            file_records = []
            for dataset in datasets :
                if "directory_record_type" not in dataset :
                    continue
                file_records.extend(medipy.io.dicom.misc.get_child_file_records(dataset))

            progress_dialog = wx.ProgressDialog(
                "Loading files ...", "Loading files ...", len(file_records))

            # Spawn the tasks. Since the loading task is mostly I/O bound, use
            # more tasks than available CPUs.
            pool = multiprocessing.Pool(2*multiprocessing.cpu_count())
            manager = multiprocessing.Manager()
            queue = manager.Queue()
            tasks_count = 0
            for record in file_records :
                pool.apply_async(_load_directory_record, (record, queue))

            # Wait for the results...
            loaded_datasets = []
            while len(loaded_datasets) != len(file_records) :
                loaded_datasets.append(queue.get())
                # GUI updates must happen in the main thread
                progress_dialog.Update(len(loaded_datasets))

            # Clean-up pool and progress dialog
            pool.close()
            pool.join()
            progress_dialog.Destroy()

            # Add already-loaded datasets (i.e. non-Directory Record)
            loaded_datasets.extend([x for x in datasets if "directory_record_type" not in x])

            images = medipy.io.dicom.split.images(loaded_datasets)
            medipy.io.dicom.sort.sort(images)

            self._loaded_datasets[series_uid] = images

        loaded_datasets = self._loaded_datasets[series_uid]

        slider_max = 0
        for dataset in loaded_datasets :
            if "number_of_frames" in dataset :
                slider_max += dataset.number_of_frames.value
            else :
                slider_max += 1

        # When minValue == maxValue, SetRange seems to fail on Linux
        if slider_max == 1 :
            self._slider.Disable()
        else :
            self._slider.Enable()
            self._slider.SetRange(0, slider_max-1)

def _load_directory_record(record, queue):
    """ Worker function used in Explorer._setup_slider. This function returns
        either the loaded dataset or the traceback of any exception that
        happened.

        A "regular" (vs. class)
        function must be use, since bound methods are not pickle-able.
    """

    try :
        path = medipy.io.dicom.misc.find_dicomdir_file(
           os.path.dirname(record.path), record.referenced_file_id.value)
        dataset = medipy.io.dicom.read(path)
    except :
        result = sys.exc_info()
    else :
        result = dataset
    queue.put(result)


import re
# THIS CODE IS A FAST IMPLEMENTATION OF BIGRAMS MATCHING
#https://stackoverflow.com/questions/42742810/speed-up-millions-of-regex-replacements-in-python-3/42747503#42747503

#for testing
# sentences = ["I'm eric. Welcome here!", "Another boring sentence.",
#                  "GiraffeElephantBoat", "sfgsdg sdwerha aswertwe", 'dkdkdk sss kdkdk animal king sdf sff social sec',
#                  'dkdkdk sss kdkdk zz animal king sdf sff social sec']
#
# bigramset = set(['social sec', 'animal king', 'sss'])

def substituteBigwithMono(sentence, bigramset):
    if len(sentence.split(' ')) < 2:
        print('testo troppo corto, warning')
        return sentence
    #this regex matches couples (but not overlapping) so i need two passes of regex sub
    word_pattern = re.compile('(\w+)\s+(\w+)')

    def delete_banned_words(matchobj):
        word = matchobj.group(0)
        if word.lower() in bigramset:
            return ''.join(word.split(' '))
        else:
            return word

    #first pass matches pairs starting from token 0
    sentence = word_pattern.sub(delete_banned_words, sentence)

    #second pass matches pairs starting from token 1
    firstword, sent = sentence.split(' ', 1)
    sent = word_pattern.sub(delete_banned_words, sent)
    sentence = firstword + ' ' + sent
    return sentence


import numpy as np
import torch

from fne.data_handle import DataHandle

class FeatureExtractionTorch:

    def __init__(self, model, image_paths, batch_size, target_tensors, input_reshape, data_handle: DataHandle,
                 n_crops=1, load_crops=False, reduction_factor=0.875, min_axis_size=None):
        self.target_tensors = target_tensors
        self.batch_size = batch_size
        self.image_paths = image_paths
        self.model = model
        self.data_handle = data_handle

        if n_crops not in [1, 10]:
            raise ValueError('The number of crops should be either 1 (no crops) or 10.')
        if n_crops == 10:
            if reduction_factor is None:
                raise ValueError('You must provide reduction factor or input to crop images.')
            if input_reshape is not None and min_axis_size is not None:
                raise ValueError('Input reshape and DownAxis (min_axis_size) are not compatible.')

        self.number_of_crops = n_crops
        if load_crops is False:
            self.reduction_factor = reduction_factor
        else:
            self.reduction_factor = None
        self.input_reshape = input_reshape
        self.min_axis_size = min_axis_size

        if load_crops is True and n_crops != 10:
            raise ValueError('For loading crops, number of crops should be 10.')
        self.load_crops = load_crops

    def extract_features(self):
        features = None
        for idx in range(0, len(self.image_paths), self.batch_size):
            batch_images_path = self.image_paths[idx:idx + self.batch_size]
            n_images_in_batch = len(batch_images_path) * self.number_of_crops

            max_resolution, variable_input_shape = self.data_handle.get_batch_image_array_shape(batch_images_path,
                                                                                self.input_reshape,
                                                                                self.number_of_crops,
                                                                                self.reduction_factor,
                                                                                self.min_axis_size,
                                                                                self.load_crops)
            img_batch = np.zeros((n_images_in_batch, *max_resolution, 3), dtype=np.float32)
            channels_first = False
            i = 0
            for img_path in batch_images_path:
                res = self.data_handle.load_image_and_resize(img_path, self.input_reshape, variable_input_shape,
                                                             max_resolution, self.reduction_factor, self.min_axis_size,
                                                             channels_first, self.load_crops, self.number_of_crops)
                img_batch[i:i + self.number_of_crops] = np.asarray(res)
                i += self.number_of_crops

            tensor = torch.as_tensor(img_batch, dtype=torch.float32).transpose(1, 3)
            features_batch = self.model.forward(tensor)

            # Define features array shape
            if features is None:
                len_features = 0
                for target_name in self.target_tensors:
                    t_tensor = features_batch[target_name].transpose(1, 3).detach().cpu().numpy()
                    len_features += t_tensor.shape[-1]
                features = np.empty((len(self.image_paths) * self.number_of_crops, len_features), dtype=np.float32)

            n_images_in_batch = len(batch_images_path) * self.number_of_crops
            feature_vals = [features_batch[i].transpose(1, 3).detach().numpy() for i in self.target_tensors]
            features_current = np.empty((n_images_in_batch, 0), dtype=np.float32)
            for feat in feature_vals:
                pooled_vals = np.mean(np.mean(feat, axis=2, dtype=np.float32), axis=1, dtype=np.float32)
                features_current = np.concatenate((features_current, pooled_vals), axis=1)

            idx_crops = idx * self.number_of_crops
            features[idx_crops:idx_crops + n_images_in_batch, :] = features_current.copy()

        return features


from optparse import OptionParser

from config import Config
from watch import start_watch

parser = OptionParser()
parser.add_option('-c', '--config', default='', dest='config',
                  help='path to config file')

def main():
    (options, args) = parser.parse_args()
    conf = Config.from_file(options.config)
    if not conf:
        raise SystemExit()

    start_watch(conf)


"""adadding to day model

Revision ID: bf8a50f67c33
Revises: 33ae564bea85
Create Date: 2023-09-27 12:12:44.413878

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'bf8a50f67c33'
down_revision = '33ae564bea85'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('days', schema=None) as batch_op:
        batch_op.add_column(sa.Column('average_day_feeling', sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column('average_weekly_feeling', sa.Integer(), nullable=True))

    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('days', schema=None) as batch_op:
        batch_op.drop_column('average_weekly_feeling')
        batch_op.drop_column('average_day_feeling')

    # ### end Alembic commands ###


import argparse
import asyncio
import base64
import json
import multiprocessing
import os
import re
import sys
import traceback
import zipfile
from collections.abc import Awaitable, Callable
from functools import lru_cache
from io import BytesIO, TextIOWrapper
from pathlib import Path
from tempfile import NamedTemporaryFile, TemporaryFile
from typing import Annotated, Literal, Optional

import soundfile
import uvicorn
from fastapi import Depends, FastAPI, Form, HTTPException, Query, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.openapi.utils import get_openapi
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
from pydantic import ValidationError, parse_obj_as
from starlette.background import BackgroundTask
from starlette.middleware.errors import ServerErrorMiddleware
from starlette.responses import FileResponse

from voicevox_engine import __version__
from voicevox_engine.cancellable_engine import CancellableEngine
from voicevox_engine.core.core_adapter import CoreAdapter
from voicevox_engine.core.core_initializer import initialize_cores
from voicevox_engine.engine_manifest.EngineManifest import EngineManifest
from voicevox_engine.engine_manifest.EngineManifestLoader import EngineManifestLoader
from voicevox_engine.library_manager import LibraryManager
from voicevox_engine.metas.Metas import StyleId
from voicevox_engine.metas.MetasStore import (
    MetasStore,
    construct_lookup,
    filter_speakers_and_styles,
)
from voicevox_engine.model import (
    AccentPhrase,
    AudioQuery,
    BaseLibraryInfo,
    DownloadableLibraryInfo,
    FrameAudioQuery,
    InstalledLibraryInfo,
    MorphableTargetInfo,
    ParseKanaBadRequest,
    ParseKanaError,
    Score,
    Speaker,
    SpeakerInfo,
    StyleIdNotFoundError,
    SupportedDevicesInfo,
    UserDictWord,
    VvlibManifest,
    WordTypes,
)
from voicevox_engine.morphing import (
    get_morphable_targets,
    is_synthesis_morphing_permitted,
    synthesis_morphing,
)
from voicevox_engine.morphing import (
    synthesis_morphing_parameter as _synthesis_morphing_parameter,
)
from voicevox_engine.part_of_speech_data import MAX_PRIORITY, MIN_PRIORITY
from voicevox_engine.preset.Preset import Preset
from voicevox_engine.preset.PresetError import PresetError
from voicevox_engine.preset.PresetManager import PresetManager
from voicevox_engine.setting.Setting import CorsPolicyMode, Setting
from voicevox_engine.setting.SettingLoader import USER_SETTING_PATH, SettingLoader
from voicevox_engine.tts_pipeline.kana_converter import create_kana, parse_kana
from voicevox_engine.tts_pipeline.tts_engine import (
    TTSEngine,
    make_tts_engines_from_cores,
)
from voicevox_engine.user_dict import (
    apply_word,
    delete_word,
    import_user_dict,
    read_dict,
    rewrite_word,
    update_dict,
)
from voicevox_engine.utility.connect_base64_waves import (
    ConnectBase64WavesException,
    connect_base64_waves,
)
from voicevox_engine.utility.core_version_utility import get_latest_core_version
from voicevox_engine.utility.path_utility import delete_file, engine_root, get_save_dir
from voicevox_engine.utility.run_utility import decide_boolean_from_env

def b64encode_str(s):
    return base64.b64encode(s).decode("utf-8")

def set_output_log_utf8() -> None:
    """
    stdout/stderrのエンコーディングをUTF-8に切り替える関数
    """
    # コンソールがない環境だとNone https://docs.python.org/ja/3/library/sys.html#sys.__stdin__
    if sys.stdout is not None:
        if isinstance(sys.stdout, TextIOWrapper):
            sys.stdout.reconfigure(encoding="utf-8")
        else:
            # バッファを全て出力する
            sys.stdout.flush()
            try:
                sys.stdout = TextIOWrapper(
                    sys.stdout.buffer, encoding="utf-8", errors="backslashreplace"
                )
            except AttributeError:
                # stdout.bufferがない場合は無視
                pass
    if sys.stderr is not None:
        if isinstance(sys.stderr, TextIOWrapper):
            sys.stderr.reconfigure(encoding="utf-8")
        else:
            sys.stderr.flush()
            try:
                sys.stderr = TextIOWrapper(
                    sys.stderr.buffer, encoding="utf-8", errors="backslashreplace"
                )
            except AttributeError:
                # stderr.bufferがない場合は無視
                pass

def generate_app(
    tts_engines: dict[str, TTSEngine],
    cores: dict[str, CoreAdapter],
    latest_core_version: str,
    setting_loader: SettingLoader,
    preset_manager: PresetManager,
    cancellable_engine: CancellableEngine | None = None,
    root_dir: Optional[Path] = None,
    cors_policy_mode: CorsPolicyMode = CorsPolicyMode.localapps,
    allow_origin: Optional[list[str]] = None,
    disable_mutable_api: bool = False,
) -> FastAPI:
    if root_dir is None:
        root_dir = engine_root()

    app = FastAPI(
        title="VOICEVOX Engine",
        description="VOICEVOXの音声合成エンジンです。",
        version=__version__,
    )

    # 未処理の例外が発生するとCORSMiddlewareが適用されない問題に対するワークアラウンド
    # ref: https://github.com/VOICEVOX/voicevox_engine/issues/91
    async def global_execution_handler(request: Request, exc: Exception) -> Response:
        return JSONResponse(
            status_code=500,
            content="Internal Server Error",
        )

    app.add_middleware(ServerErrorMiddleware, handler=global_execution_handler)

    # CORS用のヘッダを生成するミドルウェア
    localhost_regex = "^https?://(localhost|127\\.0\\.0\\.1)(:[0-9]+)?$"
    compiled_localhost_regex = re.compile(localhost_regex)
    allowed_origins = ["*"]
    if cors_policy_mode == "localapps":
        allowed_origins = ["app://."]
        if allow_origin is not None:
            allowed_origins += allow_origin
            if "*" in allow_origin:
                print(
                    'WARNING: Deprecated use of argument "*" in allow_origin. '
                    'Use option "--cors_policy_mod all" instead. See "--help" for more.',
                    file=sys.stderr,
                )

    app.add_middleware(
        CORSMiddleware,
        allow_origins=allowed_origins,
        allow_credentials=True,
        allow_origin_regex=localhost_regex,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    # 許可されていないOriginを遮断するミドルウェア
    @app.middleware("http")
    async def block_origin_middleware(
        request: Request, call_next: Callable[[Request], Awaitable[Response]]
    ) -> Response | JSONResponse:
        isValidOrigin: bool = False
        if "Origin" not in request.headers:  # Originのない純粋なリクエストの場合
            isValidOrigin = True
        elif "*" in allowed_origins:  # すべてを許可する設定の場合
            isValidOrigin = True
        elif request.headers["Origin"] in allowed_origins:  # Originが許可されている場合
            isValidOrigin = True
        elif compiled_localhost_regex.fullmatch(
            request.headers["Origin"]
        ):  # localhostの場合
            isValidOrigin = True

        if isValidOrigin:
            return await call_next(request)
        else:
            return JSONResponse(
                status_code=403, content={"detail": "Origin not allowed"}
            )

    # 許可されていないAPIを無効化する
    def check_disabled_mutable_api():
        if disable_mutable_api:
            raise HTTPException(status_code=403, detail="エンジンの静的なデータを変更するAPIは無効化されています")

    engine_manifest_data = EngineManifestLoader(
        engine_root() / "engine_manifest.json", engine_root()
    ).load_manifest()
    library_manager = LibraryManager(
        get_save_dir() / "installed_libraries",
        engine_manifest_data.supported_vvlib_manifest_version,
        engine_manifest_data.brand_name,
        engine_manifest_data.name,
        engine_manifest_data.uuid,
    )

    metas_store = MetasStore(root_dir / "speaker_info")

    setting_ui_template = Jinja2Templates(
        directory=engine_root() / "ui_template",
        variable_start_string="<JINJA_PRE>",
        variable_end_string="<JINJA_POST>",
    )

    # キャッシュを有効化
    # モジュール側でlru_cacheを指定するとキャッシュを制御しにくいため、HTTPサーバ側で指定する
    # TODO: キャッシュを管理するモジュール側API・HTTP側APIを用意する
    synthesis_morphing_parameter = lru_cache(maxsize=4)(_synthesis_morphing_parameter)

    # @app.on_event("startup")
    # async def start_catch_disconnection():
    #     if cancellable_engine is not None:
    #         loop = asyncio.get_event_loop()
    #         _ = loop.create_task(cancellable_engine.catch_disconnection())

    @app.on_event("startup")
    def apply_user_dict():
        update_dict()

    def get_engine(core_version: Optional[str]) -> TTSEngine:
        if core_version is None:
            return tts_engines[latest_core_version]
        if core_version in tts_engines:
            return tts_engines[core_version]
        raise HTTPException(status_code=422, detail="不明なバージョンです")

    def get_core(core_version: Optional[str]) -> CoreAdapter:
        """指定したバージョンのコアを取得する"""
        if core_version is None:
            return cores[latest_core_version]
        if core_version in cores:
            return cores[core_version]
        raise HTTPException(status_code=422, detail="不明なバージョンです")

    @app.post(
        "/audio_query",
        response_model=AudioQuery,
        tags=["クエリ作成"],
        summary="音声合成用のクエリを作成する",
    )
    def audio_query(
        text: str,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> AudioQuery:
        """
        音声合成用のクエリの初期値を得ます。ここで得られたクエリはそのまま音声合成に利用できます。各値の意味は`Schemas`を参照してください。
        """
        engine = get_engine(core_version)
        core = get_core(core_version)
        accent_phrases = engine.create_accent_phrases(text, style_id)
        return AudioQuery(
            accent_phrases=accent_phrases,
            speedScale=1,
            pitchScale=0,
            intonationScale=1,
            volumeScale=1,
            prePhonemeLength=0.1,
            postPhonemeLength=0.1,
            outputSamplingRate=core.default_sampling_rate,
            outputStereo=False,
            kana=create_kana(accent_phrases),
        )

    @app.post(
        "/audio_query_from_preset",
        response_model=AudioQuery,
        tags=["クエリ作成"],
        summary="音声合成用のクエリをプリセットを用いて作成する",
    )
    def audio_query_from_preset(
        text: str,
        preset_id: int,
        core_version: str | None = None,
    ) -> AudioQuery:
        """
        音声合成用のクエリの初期値を得ます。ここで得られたクエリはそのまま音声合成に利用できます。各値の意味は`Schemas`を参照してください。
        """
        engine = get_engine(core_version)
        core = get_core(core_version)
        try:
            presets = preset_manager.load_presets()
        except PresetError as err:
            raise HTTPException(status_code=422, detail=str(err))
        for preset in presets:
            if preset.id == preset_id:
                selected_preset = preset
                break
        else:
            raise HTTPException(status_code=422, detail="該当するプリセットIDが見つかりません")

        accent_phrases = engine.create_accent_phrases(text, selected_preset.style_id)
        return AudioQuery(
            accent_phrases=accent_phrases,
            speedScale=selected_preset.speedScale,
            pitchScale=selected_preset.pitchScale,
            intonationScale=selected_preset.intonationScale,
            volumeScale=selected_preset.volumeScale,
            prePhonemeLength=selected_preset.prePhonemeLength,
            postPhonemeLength=selected_preset.postPhonemeLength,
            outputSamplingRate=core.default_sampling_rate,
            outputStereo=False,
            kana=create_kana(accent_phrases),
        )

    @app.post(
        "/accent_phrases",
        response_model=list[AccentPhrase],
        tags=["クエリ編集"],
        summary="テキストからアクセント句を得る",
        responses={
            400: {
                "description": "読み仮名のパースに失敗",
                "model": ParseKanaBadRequest,
            }
        },
    )
    def accent_phrases(
        text: str,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        is_kana: bool = False,
        core_version: str | None = None,
    ) -> list[AccentPhrase]:
        """
        テキストからアクセント句を得ます。
        is_kanaが`true`のとき、テキストは次のAquesTalk 風記法で解釈されます。デフォルトは`false`です。
        * 全てのカナはカタカナで記述される
        * アクセント句は`/`または`、`で区切る。`、`で区切った場合に限り無音区間が挿入される。
        * カナの手前に`_`を入れるとそのカナは無声化される
        * アクセント位置を`'`で指定する。全てのアクセント句にはアクセント位置を1つ指定する必要がある。
        * アクセント句末に`？`(全角)を入れることにより疑問文の発音ができる。
        """
        engine = get_engine(core_version)
        if is_kana:
            try:
                return engine.create_accent_phrases_from_kana(text, style_id)
            except ParseKanaError as err:
                raise HTTPException(
                    status_code=400, detail=ParseKanaBadRequest(err).dict()
                )
        else:
            return engine.create_accent_phrases(text, style_id)

    @app.post(
        "/mora_data",
        response_model=list[AccentPhrase],
        tags=["クエリ編集"],
        summary="アクセント句から音高・音素長を得る",
    )
    def mora_data(
        accent_phrases: list[AccentPhrase],
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> list[AccentPhrase]:
        engine = get_engine(core_version)
        return engine.update_length_and_pitch(accent_phrases, style_id)

    @app.post(
        "/mora_length",
        response_model=list[AccentPhrase],
        tags=["クエリ編集"],
        summary="アクセント句から音素長を得る",
    )
    def mora_length(
        accent_phrases: list[AccentPhrase],
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> list[AccentPhrase]:
        engine = get_engine(core_version)
        return engine.update_length(accent_phrases, style_id)

    @app.post(
        "/mora_pitch",
        response_model=list[AccentPhrase],
        tags=["クエリ編集"],
        summary="アクセント句から音高を得る",
    )
    def mora_pitch(
        accent_phrases: list[AccentPhrase],
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> list[AccentPhrase]:
        engine = get_engine(core_version)
        return engine.update_pitch(accent_phrases, style_id)

    @app.post(
        "/synthesis",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "audio/wav": {"schema": {"type": "string", "format": "binary"}}
                },
            }
        },
        tags=["音声合成"],
        summary="音声合成する",
    )
    def synthesis(
        query: AudioQuery,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        enable_interrogative_upspeak: bool = Query(  # noqa: B008
            default=True,
            description="疑問系のテキストが与えられたら語尾を自動調整する",
        ),
        core_version: str | None = None,
    ) -> FileResponse:
        engine = get_engine(core_version)
        wave = engine.synthesize_wave(
            query, style_id, enable_interrogative_upspeak=enable_interrogative_upspeak
        )

        with NamedTemporaryFile(delete=False) as f:
            soundfile.write(
                file=f, data=wave, samplerate=query.outputSamplingRate, format="WAV"
            )

        return FileResponse(
            f.name,
            media_type="audio/wav",
            background=BackgroundTask(delete_file, f.name),
        )

    @app.post(
        "/cancellable_synthesis",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "audio/wav": {"schema": {"type": "string", "format": "binary"}}
                },
            }
        },
        tags=["音声合成"],
        summary="音声合成する（キャンセル可能）",
    )
    def cancellable_synthesis(
        query: AudioQuery,
        request: Request,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> FileResponse:
        if cancellable_engine is None:
            raise HTTPException(
                status_code=404,
                detail="実験的機能はデフォルトで無効になっています。使用するには引数を指定してください。",
            )
        f_name = cancellable_engine._synthesis_impl(
            query, style_id, request, core_version=core_version
        )
        if f_name == "":
            raise HTTPException(status_code=422, detail="不明なバージョンです")

        return FileResponse(
            f_name,
            media_type="audio/wav",
            background=BackgroundTask(delete_file, f_name),
        )

    @app.post(
        "/multi_synthesis",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "application/zip": {
                        "schema": {"type": "string", "format": "binary"}
                    }
                },
            }
        },
        tags=["音声合成"],
        summary="複数まとめて音声合成する",
    )
    def multi_synthesis(
        queries: list[AudioQuery],
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> FileResponse:
        engine = get_engine(core_version)
        sampling_rate = queries[0].outputSamplingRate

        with NamedTemporaryFile(delete=False) as f:
            with zipfile.ZipFile(f, mode="a") as zip_file:
                for i in range(len(queries)):
                    if queries[i].outputSamplingRate != sampling_rate:
                        raise HTTPException(
                            status_code=422, detail="サンプリングレートが異なるクエリがあります"
                        )

                    with TemporaryFile() as wav_file:
                        wave = engine.synthesize_wave(queries[i], style_id)
                        soundfile.write(
                            file=wav_file,
                            data=wave,
                            samplerate=sampling_rate,
                            format="WAV",
                        )
                        wav_file.seek(0)
                        zip_file.writestr(f"{str(i + 1).zfill(3)}.wav", wav_file.read())

        return FileResponse(
            f.name,
            media_type="application/zip",
            background=BackgroundTask(delete_file, f.name),
        )

    @app.post(
        "/morphable_targets",
        response_model=list[dict[str, MorphableTargetInfo]],
        tags=["音声合成"],
        summary="指定したスタイルに対してエンジン内の話者がモーフィングが可能か判定する",
    )
    def morphable_targets(
        base_style_ids: list[StyleId], core_version: str | None = None
    ) -> list[dict[str, MorphableTargetInfo]]:
        """
        指定されたベーススタイルに対してエンジン内の各話者がモーフィング機能を利用可能か返します。
        モーフィングの許可/禁止は`/speakers`の`speaker.supported_features.synthesis_morphing`に記載されています。
        プロパティが存在しない場合は、モーフィングが許可されているとみなします。
        返り値のスタイルIDはstring型なので注意。
        """
        core = get_core(core_version)

        try:
            speakers = metas_store.load_combined_metas(core=core)
            morphable_targets = get_morphable_targets(
                speakers=speakers, base_style_ids=base_style_ids
            )
            # jsonはint型のキーを持てないので、string型に変換する
            return [
                {str(k): v for k, v in morphable_target.items()}
                for morphable_target in morphable_targets
            ]
        except StyleIdNotFoundError as e:
            raise HTTPException(
                status_code=404, detail=f"該当するスタイル(style_id={e.style_id})が見つかりません"
            )

    @app.post(
        "/synthesis_morphing",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "audio/wav": {"schema": {"type": "string", "format": "binary"}}
                },
            }
        },
        tags=["音声合成"],
        summary="2種類のスタイルでモーフィングした音声を合成する",
    )
    def _synthesis_morphing(
        query: AudioQuery,
        base_style_id: StyleId = Query(alias="base_speaker"),  # noqa: B008
        target_style_id: StyleId = Query(alias="target_speaker"),  # noqa: B008
        morph_rate: float = Query(..., ge=0.0, le=1.0),  # noqa: B008
        core_version: str | None = None,
    ) -> FileResponse:
        """
        指定された2種類のスタイルで音声を合成、指定した割合でモーフィングした音声を得ます。
        モーフィングの割合は`morph_rate`で指定でき、0.0でベースのスタイル、1.0でターゲットのスタイルに近づきます。
        """
        engine = get_engine(core_version)
        core = get_core(core_version)

        try:
            speakers = metas_store.load_combined_metas(core=core)
            speaker_lookup = construct_lookup(speakers=speakers)
            is_permitted = is_synthesis_morphing_permitted(
                speaker_lookup, base_style_id, target_style_id
            )
            if not is_permitted:
                raise HTTPException(
                    status_code=400,
                    detail="指定されたスタイルペアでのモーフィングはできません",
                )
        except StyleIdNotFoundError as e:
            raise HTTPException(
                status_code=404, detail=f"該当するスタイル(style_id={e.style_id})が見つかりません"
            )

        # 生成したパラメータはキャッシュされる
        morph_param = synthesis_morphing_parameter(
            engine=engine,
            core=core,
            query=query,
            base_style_id=base_style_id,
            target_style_id=target_style_id,
        )

        morph_wave = synthesis_morphing(
            morph_param=morph_param,
            morph_rate=morph_rate,
            output_fs=query.outputSamplingRate,
            output_stereo=query.outputStereo,
        )

        with NamedTemporaryFile(delete=False) as f:
            soundfile.write(
                file=f,
                data=morph_wave,
                samplerate=query.outputSamplingRate,
                format="WAV",
            )

        return FileResponse(
            f.name,
            media_type="audio/wav",
            background=BackgroundTask(delete_file, f.name),
        )

    @app.post(
        "/sing_frame_audio_query",
        response_model=FrameAudioQuery,
        tags=["クエリ作成"],
        summary="歌唱音声合成用のクエリを作成する",
    )
    def sing_frame_audio_query(
        score: Score,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> FrameAudioQuery:
        """
        歌唱音声合成用のクエリの初期値を得ます。ここで得られたクエリはそのまま歌唱音声合成に利用できます。各値の意味は`Schemas`を参照してください。
        """
        engine = get_engine(core_version)
        core = get_core(core_version)
        phonemes, f0, volume = engine.create_sing_phoneme_and_f0_and_volume(
            score, style_id
        )

        return FrameAudioQuery(
            f0=f0,
            volume=volume,
            phonemes=phonemes,
            volumeScale=1,
            outputSamplingRate=core.default_sampling_rate,
            outputStereo=False,
        )

    @app.post(
        "/frame_synthesis",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "audio/wav": {"schema": {"type": "string", "format": "binary"}}
                },
            }
        },
        tags=["音声合成"],
    )
    def frame_synthesis(
        query: FrameAudioQuery,
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> FileResponse:
        """
        歌唱音声合成を行います。
        """
        engine = get_engine(core_version)
        wave = engine.frame_synthsize_wave(query, style_id)

        with NamedTemporaryFile(delete=False) as f:
            soundfile.write(
                file=f, data=wave, samplerate=query.outputSamplingRate, format="WAV"
            )

        return FileResponse(
            f.name,
            media_type="audio/wav",
            background=BackgroundTask(delete_file, f.name),
        )

    @app.post(
        "/connect_waves",
        response_class=FileResponse,
        responses={
            200: {
                "content": {
                    "audio/wav": {"schema": {"type": "string", "format": "binary"}}
                },
            }
        },
        tags=["その他"],
        summary="base64エンコードされた複数のwavデータを一つに結合する",
    )
    def connect_waves(waves: list[str]) -> FileResponse:
        """
        base64エンコードされたwavデータを一纏めにし、wavファイルで返します。
        """
        try:
            waves_nparray, sampling_rate = connect_base64_waves(waves)
        except ConnectBase64WavesException as err:
            raise HTTPException(status_code=422, detail=str(err))

        with NamedTemporaryFile(delete=False) as f:
            soundfile.write(
                file=f,
                data=waves_nparray,
                samplerate=sampling_rate,
                format="WAV",
            )

        return FileResponse(
            f.name,
            media_type="audio/wav",
            background=BackgroundTask(delete_file, f.name),
        )

    @app.get("/presets", response_model=list[Preset], tags=["その他"])
    def get_presets() -> list[Preset]:
        """
        エンジンが保持しているプリセットの設定を返します

        Returns
        -------
        presets: list[Preset]
            プリセットのリスト
        """
        try:
            presets = preset_manager.load_presets()
        except PresetError as err:
            raise HTTPException(status_code=422, detail=str(err))
        return presets

    @app.post(
        "/add_preset",
        response_model=int,
        tags=["その他"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def add_preset(preset: Preset) -> int:
        """
        新しいプリセットを追加します

        Parameters
        -------
        preset: Preset
            新しいプリセット。
            プリセットIDが既存のものと重複している場合は、新規のプリセットIDが採番されます。

        Returns
        -------
        id: int
            追加したプリセットのプリセットID
        """
        try:
            id = preset_manager.add_preset(preset)
        except PresetError as err:
            raise HTTPException(status_code=422, detail=str(err))
        return id

    @app.post(
        "/update_preset",
        response_model=int,
        tags=["その他"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def update_preset(preset: Preset) -> int:
        """
        既存のプリセットを更新します

        Parameters
        -------
        preset: Preset
            更新するプリセット。
            プリセットIDが更新対象と一致している必要があります。

        Returns
        -------
        id: int
            更新したプリセットのプリセットID
        """
        try:
            id = preset_manager.update_preset(preset)
        except PresetError as err:
            raise HTTPException(status_code=422, detail=str(err))
        return id

    @app.post(
        "/delete_preset",
        status_code=204,
        tags=["その他"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def delete_preset(id: int) -> Response:
        """
        既存のプリセットを削除します

        Parameters
        -------
        id: int
            削除するプリセットのプリセットID

        """
        try:
            preset_manager.delete_preset(id)
        except PresetError as err:
            raise HTTPException(status_code=422, detail=str(err))
        return Response(status_code=204)

    @app.get("/version", tags=["その他"])
    def version() -> str:
        return __version__

    @app.get("/core_versions", response_model=list[str], tags=["その他"])
    def core_versions() -> Response:
        return Response(
            content=json.dumps(list(cores.keys())),
            media_type="application/json",
        )

    @app.get("/speakers", response_model=list[Speaker], tags=["その他"])
    def speakers(
        core_version: str | None = None,
    ) -> list[Speaker]:
        speakers = metas_store.load_combined_metas(get_core(core_version))
        return filter_speakers_and_styles(speakers, "speaker")

    @app.get("/speaker_info", response_model=SpeakerInfo, tags=["その他"])
    def speaker_info(
        speaker_uuid: str,
        core_version: str | None = None,
    ) -> SpeakerInfo:
        """
        指定されたspeaker_uuidに関する情報をjson形式で返します。
        画像や音声はbase64エンコードされたものが返されます。
        """
        return _speaker_info(
            speaker_uuid=speaker_uuid,
            speaker_or_singer="speaker",
            core_version=core_version,
        )

    # FIXME: この関数をどこかに切り出す
    def _speaker_info(
        speaker_uuid: str,
        speaker_or_singer: Literal["speaker", "singer"],
        core_version: str | None,
    ) -> SpeakerInfo:
        # エンジンに含まれる話者メタ情報は、次のディレクトリ構造に従わなければならない：
        # {root_dir}/
        #   speaker_info/
        #       {speaker_uuid_0}/
        #           policy.md
        #           portrait.png
        #           icons/
        #               {id_0}.png
        #               {id_1}.png
        #               ...
        #           portraits/
        #               {id_0}.png
        #               {id_1}.png
        #               ...
        #           voice_samples/
        #               {id_0}_001.wav
        #               {id_0}_002.wav
        #               {id_0}_003.wav
        #               {id_1}_001.wav
        #               ...
        #       {speaker_uuid_1}/
        #           ...

        # 該当話者の検索
        speakers = parse_obj_as(
            list[Speaker], json.loads(get_core(core_version).speakers)
        )
        speakers = filter_speakers_and_styles(speakers, speaker_or_singer)
        for i in range(len(speakers)):
            if speakers[i].speaker_uuid == speaker_uuid:
                speaker = speakers[i]
                break
        else:
            raise HTTPException(status_code=404, detail="該当する話者が見つかりません")

        try:
            speaker_path = root_dir / "speaker_info" / speaker_uuid
            # 話者情報の取得
            # speaker policy
            policy_path = speaker_path / "policy.md"
            policy = policy_path.read_text("utf-8")
            # speaker portrait
            portrait_path = speaker_path / "portrait.png"
            portrait = b64encode_str(portrait_path.read_bytes())
            # スタイル情報の取得
            style_infos = []
            for style in speaker.styles:
                id = style.id
                # style icon
                style_icon_path = speaker_path / "icons" / f"{id}.png"
                icon = b64encode_str(style_icon_path.read_bytes())
                # style portrait
                style_portrait_path = speaker_path / "portraits" / f"{id}.png"
                style_portrait = None
                if style_portrait_path.exists():
                    style_portrait = b64encode_str(style_portrait_path.read_bytes())
                # voice samples
                voice_samples = [
                    b64encode_str(
                        (
                            speaker_path
                            / "voice_samples/{}_{}.wav".format(id, str(j + 1).zfill(3))
                        ).read_bytes()
                    )
                    for j in range(3)
                ]
                style_infos.append(
                    {
                        "id": id,
                        "icon": icon,
                        "portrait": style_portrait,
                        "voice_samples": voice_samples,
                    }
                )
        except FileNotFoundError:
            import traceback

            traceback.print_exc()
            raise HTTPException(status_code=500, detail="追加情報が見つかりませんでした")

        ret_data = SpeakerInfo(
            policy=policy,
            portrait=portrait,
            style_infos=style_infos,
        )
        return ret_data

    @app.get("/singers", response_model=list[Speaker], tags=["その他"])
    def singers(
        core_version: str | None = None,
    ) -> list[Speaker]:
        singers = metas_store.load_combined_metas(get_core(core_version))
        return filter_speakers_and_styles(singers, "singer")

    @app.get("/singer_info", response_model=SpeakerInfo, tags=["その他"])
    def singer_info(
        speaker_uuid: str,
        core_version: str | None = None,
    ) -> SpeakerInfo:
        """
        指定されたspeaker_uuidに関する情報をjson形式で返します。
        画像や音声はbase64エンコードされたものが返されます。
        """
        return _speaker_info(
            speaker_uuid=speaker_uuid,
            speaker_or_singer="singer",
            core_version=core_version,
        )

    if engine_manifest_data.supported_features.manage_library:

        @app.get(
            "/downloadable_libraries",
            response_model=list[DownloadableLibraryInfo],
            tags=["音声ライブラリ管理"],
        )
        def downloadable_libraries() -> list[DownloadableLibraryInfo]:
            """
            ダウンロード可能な音声ライブラリの情報を返します。

            Returns
            -------
            ret_data: list[DownloadableLibrary]
            """
            if not engine_manifest_data.supported_features.manage_library:
                raise HTTPException(status_code=404, detail="この機能は実装されていません")
            return library_manager.downloadable_libraries()

        @app.get(
            "/installed_libraries",
            response_model=dict[str, InstalledLibraryInfo],
            tags=["音声ライブラリ管理"],
        )
        def installed_libraries() -> dict[str, InstalledLibraryInfo]:
            """
            インストールした音声ライブラリの情報を返します。

            Returns
            -------
            ret_data: dict[str, InstalledLibrary]
            """
            if not engine_manifest_data.supported_features.manage_library:
                raise HTTPException(status_code=404, detail="この機能は実装されていません")
            return library_manager.installed_libraries()

        @app.post(
            "/install_library/{library_uuid}",
            status_code=204,
            tags=["音声ライブラリ管理"],
            dependencies=[Depends(check_disabled_mutable_api)],
        )
        async def install_library(
            library_uuid: str,
            request: Request,
        ) -> Response:
            """
            音声ライブラリをインストールします。
            音声ライブラリのZIPファイルをリクエストボディとして送信してください。

            Parameters
            ----------
            library_uuid: str
                音声ライブラリのID
            """
            if not engine_manifest_data.supported_features.manage_library:
                raise HTTPException(status_code=404, detail="この機能は実装されていません")
            archive = BytesIO(await request.body())
            loop = asyncio.get_event_loop()
            await loop.run_in_executor(
                None, library_manager.install_library, library_uuid, archive
            )
            return Response(status_code=204)

        @app.post(
            "/uninstall_library/{library_uuid}",
            status_code=204,
            tags=["音声ライブラリ管理"],
            dependencies=[Depends(check_disabled_mutable_api)],
        )
        def uninstall_library(library_uuid: str) -> Response:
            """
            音声ライブラリをアンインストールします。

            Parameters
            ----------
            library_uuid: str
                音声ライブラリのID
            """
            if not engine_manifest_data.supported_features.manage_library:
                raise HTTPException(status_code=404, detail="この機能は実装されていません")
            library_manager.uninstall_library(library_uuid)
            return Response(status_code=204)

    @app.post("/initialize_speaker", status_code=204, tags=["その他"])
    def initialize_speaker(
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        skip_reinit: bool = Query(  # noqa: B008
            default=False, description="既に初期化済みのスタイルの再初期化をスキップするかどうか"
        ),
        core_version: str | None = None,
    ) -> Response:
        """
        指定されたスタイルを初期化します。
        実行しなくても他のAPIは使用できますが、初回実行時に時間がかかることがあります。
        """
        core = get_core(core_version)
        core.initialize_style_id_synthesis(style_id, skip_reinit=skip_reinit)
        return Response(status_code=204)

    @app.get("/is_initialized_speaker", response_model=bool, tags=["その他"])
    def is_initialized_speaker(
        style_id: StyleId = Query(alias="speaker"),  # noqa: B008
        core_version: str | None = None,
    ) -> bool:
        """
        指定されたスタイルが初期化されているかどうかを返します。
        """
        core = get_core(core_version)
        return core.is_initialized_style_id_synthesis(style_id)

    @app.get("/user_dict", response_model=dict[str, UserDictWord], tags=["ユーザー辞書"])
    def get_user_dict_words() -> dict[str, UserDictWord]:
        """
        ユーザー辞書に登録されている単語の一覧を返します。
        単語の表層形(surface)は正規化済みの物を返します。

        Returns
        -------
        dict[str, UserDictWord]
            単語のUUIDとその詳細
        """
        try:
            return read_dict()
        except Exception:
            traceback.print_exc()
            raise HTTPException(status_code=422, detail="辞書の読み込みに失敗しました。")

    @app.post(
        "/user_dict_word",
        response_model=str,
        tags=["ユーザー辞書"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def add_user_dict_word(
        surface: str,
        pronunciation: str,
        accent_type: int,
        word_type: WordTypes | None = None,
        priority: Annotated[int | None, Query(ge=MIN_PRIORITY, le=MAX_PRIORITY)] = None,
    ) -> Response:
        """
        ユーザー辞書に言葉を追加します。

        Parameters
        ----------
        surface : str
            言葉の表層形
        pronunciation: str
            言葉の発音（カタカナ）
        accent_type: int
            アクセント型（音が下がる場所を指す）
        word_type: WordTypes, optional
            PROPER_NOUN（固有名詞）、COMMON_NOUN（普通名詞）、VERB（動詞）、ADJECTIVE（形容詞）、SUFFIX（語尾）のいずれか
        priority: int, optional
            単語の優先度（0から10までの整数）
            数字が大きいほど優先度が高くなる
            1から9までの値を指定することを推奨
        """
        try:
            word_uuid = apply_word(
                surface=surface,
                pronunciation=pronunciation,
                accent_type=accent_type,
                word_type=word_type,
                priority=priority,
            )
            return Response(content=word_uuid)
        except ValidationError as e:
            raise HTTPException(status_code=422, detail="パラメータに誤りがあります。\n" + str(e))
        except Exception:
            traceback.print_exc()
            raise HTTPException(status_code=422, detail="ユーザー辞書への追加に失敗しました。")

    @app.put(
        "/user_dict_word/{word_uuid}",
        status_code=204,
        tags=["ユーザー辞書"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def rewrite_user_dict_word(
        surface: str,
        pronunciation: str,
        accent_type: int,
        word_uuid: str,
        word_type: WordTypes | None = None,
        priority: Annotated[int | None, Query(ge=MIN_PRIORITY, le=MAX_PRIORITY)] = None,
    ) -> Response:
        """
        ユーザー辞書に登録されている言葉を更新します。

        Parameters
        ----------
        surface : str
            言葉の表層形
        pronunciation: str
            言葉の発音（カタカナ）
        accent_type: int
            アクセント型（音が下がる場所を指す）
        word_uuid: str
            更新する言葉のUUID
        word_type: WordTypes, optional
            PROPER_NOUN（固有名詞）、COMMON_NOUN（普通名詞）、VERB（動詞）、ADJECTIVE（形容詞）、SUFFIX（語尾）のいずれか
        priority: int, optional
            単語の優先度（0から10までの整数）
            数字が大きいほど優先度が高くなる
            1から9までの値を指定することを推奨
        """
        try:
            rewrite_word(
                surface=surface,
                pronunciation=pronunciation,
                accent_type=accent_type,
                word_uuid=word_uuid,
                word_type=word_type,
                priority=priority,
            )
            return Response(status_code=204)
        except HTTPException:
            raise
        except ValidationError as e:
            raise HTTPException(status_code=422, detail="パラメータに誤りがあります。\n" + str(e))
        except Exception:
            traceback.print_exc()
            raise HTTPException(status_code=422, detail="ユーザー辞書の更新に失敗しました。")

    @app.delete(
        "/user_dict_word/{word_uuid}",
        status_code=204,
        tags=["ユーザー辞書"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def delete_user_dict_word(word_uuid: str) -> Response:
        """
        ユーザー辞書に登録されている言葉を削除します。

        Parameters
        ----------
        word_uuid: str
            削除する言葉のUUID
        """
        try:
            delete_word(word_uuid=word_uuid)
            return Response(status_code=204)
        except HTTPException:
            raise
        except Exception:
            traceback.print_exc()
            raise HTTPException(status_code=422, detail="ユーザー辞書の更新に失敗しました。")

    @app.post(
        "/import_user_dict",
        status_code=204,
        tags=["ユーザー辞書"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def import_user_dict_words(
        import_dict_data: dict[str, UserDictWord],
        override: bool,
    ) -> Response:
        """
        他のユーザー辞書をインポートします。

        Parameters
        ----------
        import_dict_data: dict[str, UserDictWord]
            インポートするユーザー辞書のデータ
        override: bool
            重複したエントリがあった場合、上書きするかどうか
        """
        try:
            import_user_dict(dict_data=import_dict_data, override=override)
            return Response(status_code=204)
        except Exception:
            traceback.print_exc()
            raise HTTPException(status_code=422, detail="ユーザー辞書のインポートに失敗しました。")

    @app.get("/supported_devices", response_model=SupportedDevicesInfo, tags=["その他"])
    def supported_devices(
        core_version: str | None = None,
    ) -> Response:
        supported_devices = get_core(core_version).supported_devices
        if supported_devices is None:
            raise HTTPException(status_code=422, detail="非対応の機能です。")
        return Response(
            content=supported_devices,
            media_type="application/json",
        )

    @app.get("/engine_manifest", response_model=EngineManifest, tags=["その他"])
    def engine_manifest() -> EngineManifest:
        return engine_manifest_data

    @app.post(
        "/validate_kana",
        response_model=bool,
        tags=["その他"],
        summary="テキストがAquesTalk 風記法に従っているか判定する",
        responses={
            400: {
                "description": "テキストが不正です",
                "model": ParseKanaBadRequest,
            }
        },
    )
    def validate_kana(text: str) -> bool:
        """
        テキストがAquesTalk 風記法に従っているかどうかを判定します。
        従っていない場合はエラーが返ります。

        Parameters
        ----------
        text: str
            判定する対象の文字列
        """
        try:
            parse_kana(text)
            return True
        except ParseKanaError as err:
            raise HTTPException(
                status_code=400,
                detail=ParseKanaBadRequest(err).dict(),
            )

    @app.get("/setting", response_class=Response, tags=["設定"])
    def setting_get(request: Request) -> Response:
        """
        設定ページを返します。
        """
        settings = setting_loader.load_setting_file()

        brand_name = engine_manifest_data.brand_name
        cors_policy_mode = settings.cors_policy_mode
        allow_origin = settings.allow_origin

        if allow_origin is None:
            allow_origin = ""

        return setting_ui_template.TemplateResponse(
            "ui.html",
            {
                "request": request,
                "brand_name": brand_name,
                "cors_policy_mode": cors_policy_mode,
                "allow_origin": allow_origin,
            },
        )

    @app.post(
        "/setting",
        response_class=Response,
        tags=["設定"],
        dependencies=[Depends(check_disabled_mutable_api)],
    )
    def setting_post(
        cors_policy_mode: CorsPolicyMode = Form(),  # noqa
        allow_origin: str | None = Form(default=None),  # noqa
    ) -> Response:
        """
        設定を更新します。
        """
        settings = Setting(
            cors_policy_mode=cors_policy_mode,
            allow_origin=allow_origin,
        )

        # 更新した設定へ上書き
        setting_loader.dump_setting_file(settings)

        return Response(status_code=204)

    # BaseLibraryInfo/VvlibManifestモデルはAPIとして表には出ないが、エディタ側で利用したいので、手動で追加する
    # ref: https://fastapi.tiangolo.com/advanced/extending-openapi/#modify-the-openapi-schema
    def custom_openapi():
        if app.openapi_schema:
            return app.openapi_schema
        openapi_schema = get_openapi(
            title=app.title,
            version=app.version,
            description=app.description,
            routes=app.routes,
            tags=app.openapi_tags,
            servers=app.servers,
            terms_of_service=app.terms_of_service,
            contact=app.contact,
            license_info=app.license_info,
        )
        openapi_schema["components"]["schemas"][
            "VvlibManifest"
        ] = VvlibManifest.schema()
        # ref_templateを指定しない場合、definitionsを参照してしまうので、手動で指定する
        base_library_info = BaseLibraryInfo.schema(
            ref_template="#/components/schemas/{model}"
        )
        # definitionsは既存のモデルを重複して定義するため、不要なので削除
        del base_library_info["definitions"]
        openapi_schema["components"]["schemas"]["BaseLibraryInfo"] = base_library_info
        app.openapi_schema = openapi_schema
        return openapi_schema

    app.openapi = custom_openapi  # type: ignore[method-assign]

    return app

def main() -> None:
    multiprocessing.freeze_support()

    output_log_utf8 = decide_boolean_from_env("VV_OUTPUT_LOG_UTF8")
    if output_log_utf8:
        set_output_log_utf8()

    parser = argparse.ArgumentParser(description="VOICEVOX のエンジンです。")
    parser.add_argument(
        "--host", type=str, default="127.0.0.1", help="接続を受け付けるホストアドレスです。"
    )
    parser.add_argument("--port", type=int, default=50021, help="接続を受け付けるポート番号です。")
    parser.add_argument("--use_gpu", action="store_true", help="GPUを使って音声合成するようになります。")
    parser.add_argument(
        "--voicevox_dir", type=Path, default=None, help="VOICEVOXのディレクトリパスです。"
    )
    parser.add_argument(
        "--voicelib_dir",
        type=Path,
        default=None,
        action="append",
        help="VOICEVOX COREのディレクトリパスです。",
    )
    parser.add_argument(
        "--runtime_dir",
        type=Path,
        default=None,
        action="append",
        help="VOICEVOX COREで使用するライブラリのディレクトリパスです。",
    )
    parser.add_argument(
        "--enable_mock",
        action="store_true",
        help="VOICEVOX COREを使わずモックで音声合成を行います。",
    )
    parser.add_argument(
        "--enable_cancellable_synthesis",
        action="store_true",
        help="音声合成を途中でキャンセルできるようになります。",
    )
    parser.add_argument(
        "--init_processes",
        type=int,
        default=2,
        help="cancellable_synthesis機能の初期化時に生成するプロセス数です。",
    )
    parser.add_argument(
        "--load_all_models", action="store_true", help="起動時に全ての音声合成モデルを読み込みます。"
    )

    # 引数へcpu_num_threadsの指定がなければ、環境変数をロールします。
    # 環境変数にもない場合は、Noneのままとします。
    # VV_CPU_NUM_THREADSが空文字列でなく数値でもない場合、エラー終了します。
    parser.add_argument(
        "--cpu_num_threads",
        type=int,
        default=os.getenv("VV_CPU_NUM_THREADS") or None,
        help=(
            "音声合成を行うスレッド数です。指定しない場合、代わりに環境変数 VV_CPU_NUM_THREADS の値が使われます。"
            "VV_CPU_NUM_THREADS が空文字列でなく数値でもない場合はエラー終了します。"
        ),
    )

    parser.add_argument(
        "--output_log_utf8",
        action="store_true",
        help=(
            "ログ出力をUTF-8でおこないます。指定しない場合、代わりに環境変数 VV_OUTPUT_LOG_UTF8 の値が使われます。"
            "VV_OUTPUT_LOG_UTF8 の値が1の場合はUTF-8で、0または空文字、値がない場合は環境によって自動的に決定されます。"
        ),
    )

    parser.add_argument(
        "--cors_policy_mode",
        type=CorsPolicyMode,
        choices=list(CorsPolicyMode),
        default=None,
        help=(
            "CORSの許可モード。allまたはlocalappsが指定できます。allはすべてを許可します。"
            "localappsはオリジン間リソース共有ポリシーを、app://.とlocalhost関連に限定します。"
            "その他のオリジンはallow_originオプションで追加できます。デフォルトはlocalapps。"
            "このオプションは--setting_fileで指定される設定ファイルよりも優先されます。"
        ),
    )

    parser.add_argument(
        "--allow_origin",
        nargs="*",
        help=(
            "許可するオリジンを指定します。スペースで区切ることで複数指定できます。"
            "このオプションは--setting_fileで指定される設定ファイルよりも優先されます。"
        ),
    )

    parser.add_argument(
        "--setting_file", type=Path, default=USER_SETTING_PATH, help="設定ファイルを指定できます。"
    )

    parser.add_argument(
        "--preset_file",
        type=Path,
        default=None,
        help=(
            "プリセットファイルを指定できます。"
            "指定がない場合、環境変数 VV_PRESET_FILE、--voicevox_dirのpresets.yaml、"
            "実行ファイルのディレクトリのpresets.yamlを順に探します。"
        ),
    )

    parser.add_argument(
        "--disable_mutable_api",
        action="store_true",
        help=(
            "辞書登録や設定変更など、エンジンの静的なデータを変更するAPIを無効化します。"
            "指定しない場合、代わりに環境変数 VV_DISABLE_MUTABLE_API の値が使われます。"
            "VV_DISABLE_MUTABLE_API の値が1の場合は無効化で、0または空文字、値がない場合は無視されます。"
        ),
    )

    args = parser.parse_args()

    if args.output_log_utf8:
        set_output_log_utf8()

    # Synthesis Engine
    use_gpu: bool = args.use_gpu
    voicevox_dir: Path | None = args.voicevox_dir
    voicelib_dirs: list[Path] | None = args.voicelib_dir
    runtime_dirs: list[Path] | None = args.runtime_dir
    enable_mock: bool = args.enable_mock
    cpu_num_threads: int | None = args.cpu_num_threads
    load_all_models: bool = args.load_all_models

    cores = initialize_cores(
        use_gpu=use_gpu,
        voicelib_dirs=voicelib_dirs,
        voicevox_dir=voicevox_dir,
        runtime_dirs=runtime_dirs,
        cpu_num_threads=cpu_num_threads,
        enable_mock=enable_mock,
        load_all_models=load_all_models,
    )
    tts_engines = make_tts_engines_from_cores(cores)
    assert len(tts_engines) != 0, "音声合成エンジンがありません。"
    latest_core_version = get_latest_core_version(versions=list(tts_engines.keys()))

    # Cancellable Engine
    enable_cancellable_synthesis: bool = args.enable_cancellable_synthesis
    init_processes: int = args.init_processes

    cancellable_engine: CancellableEngine | None = None
    if enable_cancellable_synthesis:
        cancellable_engine = CancellableEngine(
            init_processes=init_processes,
            use_gpu=use_gpu,
            voicelib_dirs=voicelib_dirs,
            voicevox_dir=voicevox_dir,
            runtime_dirs=runtime_dirs,
            cpu_num_threads=cpu_num_threads,
            enable_mock=enable_mock,
        )

    root_dir: Path | None = voicevox_dir
    if root_dir is None:
        root_dir = engine_root()

    setting_loader = SettingLoader(args.setting_file)

    settings = setting_loader.load_setting_file()

    cors_policy_mode: CorsPolicyMode | None = args.cors_policy_mode
    if cors_policy_mode is None:
        cors_policy_mode = settings.cors_policy_mode

    allow_origin = None
    if args.allow_origin is not None:
        allow_origin = args.allow_origin
    elif settings.allow_origin is not None:
        allow_origin = settings.allow_origin.split(" ")

    # Preset Manager
    # preset_pathの優先順: 引数、環境変数、voicevox_dir、実行ファイルのディレクトリ
    # ファイルの存在に関わらず、優先順で最初に指定されたパスをプリセットファイルとして使用する
    preset_path: Path | None = args.preset_file
    if preset_path is None:
        # 引数 --preset_file の指定がない場合
        env_preset_path = os.getenv("VV_PRESET_FILE")
        if env_preset_path is not None and len(env_preset_path) != 0:
            # 環境変数 VV_PRESET_FILE の指定がある場合
            preset_path = Path(env_preset_path)
        else:
            # 環境変数 VV_PRESET_FILE の指定がない場合
            preset_path = root_dir / "presets.yaml"

    preset_manager = PresetManager(
        preset_path=preset_path,
    )

    disable_mutable_api: bool = args.disable_mutable_api | decide_boolean_from_env(
        "VV_DISABLE_MUTABLE_API"
    )

    uvicorn.run(
        generate_app(
            tts_engines,
            cores,
            latest_core_version,
            setting_loader,
            preset_manager=preset_manager,
            cancellable_engine=cancellable_engine,
            root_dir=root_dir,
            cors_policy_mode=cors_policy_mode,
            allow_origin=allow_origin,
            disable_mutable_api=disable_mutable_api,
        ),
        host=args.host,
        port=args.port,
    )

if __name__ == "__main__":
    main()


#!/usr/bin/python27
#coding:utf-8
from __future__ import division
import os
import time
import math
import numpy as np
import tensorflow as tf
import tensorflow.contrib.slim as slim
from data_loader import DataLoader
from nets import *
from utils import *
import PIL.Image as pil
from Getdepth import *
img_height =128
img_width =416

class SfMLearner(object):
    def __init__(self):
        pass   #pass

    def build_train_graph(self):
        opt = self.opt
        loader = DataLoader(opt.dataset_dir,
                            opt.batch_size,
                            opt.img_height,
                            opt.img_width,
                            opt.num_source,
                            opt.num_scales)
        with tf.name_scope("data_loading"):
            tgt_image, src_image_stack, intrinsics = loader.load_train_batch()
            tgt_image = self.preprocess_image(tgt_image)                        # [-1,1)
            src_image_stack = self.preprocess_image(src_image_stack)            # [-1,1)
            # tgt_image = tf.Print(tgt_image,[tgt_image],message= 'tgt_image')  #(4,128,416,3)
            # src_image_stack = tf.Print(src_image_stack, [src_image_stack], message='src_image_stack')     #(4,128,416,6)
            # # print('intrinsics:',intrinsics)                                   #shape(4,4,3,3)
            # print('tgt_image.shape', tgt_image.shape)                           # shape(4,128,416,3)
            # print('src_image_stack', src_image_stack.shape)                     # shape(4,128,416,6)

        with tf.name_scope("depth_prediction"):

            # [ disp:shape=[4,128,416,1]
            #Fix the groundtruth depth

            pred_disp = Getdepth()
            pred_disp = pred_disp.get_depth_graph()
            print('pred_disp:',pred_disp)

            pred_depth = [1./pred_disp]   # inverse depth
            # pred_depth = tf.Print(pred_depth,[pred_depth],message='pred_depth')
            print('pred_depth:',pred_depth)

        with tf.name_scope("pose_and_explainability_prediction"):
            pred_poses, pred_exp_logits, pose_exp_net_endpoints = \
                pose_exp_net(tgt_image,
                             src_image_stack,
                             do_exp=(opt.explain_reg_weight > 0),
                             is_training=True)
            print('pred_poses.shape',pred_poses.shape)  #(4,2,6)
            # pred_poses = tf.Print(pred_poses,[pred_poses],message='pred_poses')
            # print('pred_exp_logits:',pred_exp_logits)
#loss function
        with tf.name_scope("compute_loss"):
            pixel_loss = 0
            exp_loss = 0
            tgt_image_all = []
            src_image_stack_all = []
            proj_image_stack_all = []
            proj_error_stack_all = []
            exp_mask_stack_all = []
            #todo:only one scale
            s = 0
            if opt.explain_reg_weight > 0:
                # Construct a reference explainability mask (i.e. all
                # pixels are explainable)
                ref_exp_mask = self.get_reference_explain_mask(s)
                # print('ref_exp_mask:',ref_exp_mask)
            # Scale the source and target images for computing loss at the
            # according scale.
            curr_tgt_image = tf.image.resize_area(tgt_image,
                [int(opt.img_height/(2**s)), int(opt.img_width/(2**s))])   #1个scale
            # print('curr_tgt_image:',curr_tgt_image)                     # (4, 128, 416, 3)
            curr_src_image_stack = tf.image.resize_area(src_image_stack,
                [int(opt.img_height/(2**s)), int(opt.img_width/(2**s))])
            # print('curr_src_image_stack:', curr_src_image_stack)         # (4, 128, 416, 6)
            for i in range(opt.num_source):                     #num_source=2
                # Inverse warp the source image to the target image frame
                curr_proj_image = projective_inverse_warp(
                    curr_src_image_stack[:,:,:,3*i:3*(i+1)],                #shape(4,128,416,3)
                    tf.squeeze(pred_depth[s], axis=3),                      #shape(4,128,416)
                    pred_poses[:,i,:],                                      #pred_poses.shape(4,6)
                    intrinsics[:,s,:,:])                                    #shape(4,4,3,3)
                curr_proj_error = tf.abs(curr_proj_image - curr_tgt_image)  #投影光度误差
                # print('curr_proj_error.shape:',curr_proj_error.shape)
                # Cross-entropy loss as regularization for the
                # explainability prediction
                if opt.explain_reg_weight > 0:

                    curr_exp_logits = tf.slice(pred_exp_logits[s],
                                               [0, 0, 0, i*2],
                                               [-1, -1, -1, 2])
                    with tf.Session() as sess:
                        test_curr_exp_logits=sess.run(curr_exp_logits)
                        # print(test_curr_exp_logits)

                    exp_loss += opt.explain_reg_weight * \
                        self.compute_exp_reg_loss(curr_exp_logits,
                                                  ref_exp_mask)
                    curr_exp = tf.nn.softmax(curr_exp_logits)
                # Photo-consistency loss weighted by explainability
                if opt.explain_reg_weight > 0:
                    pixel_loss += tf.reduce_mean(curr_proj_error * \
                        tf.expand_dims(curr_exp[:,:,:,1], -1))
                else:
                    pixel_loss += tf.reduce_mean(curr_proj_error)
                # Prepare images for tensorboard summaries
                if i == 0:
                    proj_image_stack = curr_proj_image
                    # print('proj_image_stack.shape:',proj_image_stack.shape)
                    proj_error_stack = curr_proj_error
                    # print('proj_error_stack.shape:', proj_error_stack.shape)
                    if opt.explain_reg_weight > 0:
                        exp_mask_stack = tf.expand_dims(curr_exp[:,:,:,1], -1)
                else:
                    proj_image_stack = tf.concat([proj_image_stack,
                                                  curr_proj_image], axis=3)
                    # print('proj_image_stack.shape:',proj_image_stack.shape)
                    proj_error_stack = tf.concat([proj_error_stack,
                                                  curr_proj_error], axis=3)
                    # print('proj_error_stack.shape:', proj_error_stack.shape)
                    if opt.explain_reg_weight > 0:
                        exp_mask_stack = tf.concat([exp_mask_stack,
                            tf.expand_dims(curr_exp[:,:,:,1], -1)], axis=3)
                tgt_image_all.append(curr_tgt_image)
                src_image_stack_all.append(curr_src_image_stack)
                proj_image_stack_all.append(proj_image_stack)
                proj_error_stack_all.append(proj_error_stack)
                if opt.explain_reg_weight > 0:
                    exp_mask_stack_all.append(exp_mask_stack)
            total_loss = pixel_loss + exp_loss

        with tf.name_scope("train_op"):
            train_vars = [var for var in tf.trainable_variables()]
            optim = tf.train.AdamOptimizer(opt.learning_rate, opt.beta1)
            # self.grads_and_vars = optim.compute_gradients(total_loss,
            #                                               var_list=train_vars)
            # self.train_op = optim.apply_gradients(self.grads_and_vars)
            self.train_op = slim.learning.create_train_op(total_loss, optim)
            self.global_step = tf.Variable(0,
                                           name='global_step',
                                           trainable=False)
            self.incr_global_step = tf.assign(self.global_step,
                                              self.global_step+1)

        # Collect tensors that are useful later (e.g. tf summary)
        self.pred_depth = pred_depth
        self.pred_poses = pred_poses
        self.steps_per_epoch = loader.steps_per_epoch
        self.total_loss = total_loss
        self.pixel_loss = pixel_loss
        self.exp_loss = exp_loss
        self.tgt_image_all = tgt_image_all
        self.src_image_stack_all = src_image_stack_all
        self.proj_image_stack_all = proj_image_stack_all
        self.proj_error_stack_all = proj_error_stack_all
        self.exp_mask_stack_all = exp_mask_stack_all

    def get_reference_explain_mask(self, downscaling):
        opt = self.opt
        tmp = np.array([0,1])
        ref_exp_mask = np.tile(tmp,
                               (opt.batch_size,
                                int(opt.img_height/(2**downscaling)),
                                int(opt.img_width/(2**downscaling)),
                                1))
        ref_exp_mask = tf.constant(ref_exp_mask, dtype=tf.float32)
        return ref_exp_mask

    def compute_exp_reg_loss(self, pred, ref):
        l = tf.nn.softmax_cross_entropy_with_logits(
            labels=tf.reshape(ref, [-1, 2]),
            logits=tf.reshape(pred, [-1, 2]))
        return tf.reduce_mean(l)

    def train(self, opt):
        opt.num_source = opt.seq_length - 1    #seq_length=3 ,source_view=2 and target_view=1
        # TODO: currently fixed to 4
        opt.num_scales = 4
        self.opt = opt
        self.build_train_graph()
        with tf.name_scope("parameter_count"):
            parameter_count = tf.reduce_sum([tf.reduce_prod(tf.shape(v)) \
                                            for v in tf.trainable_variables()])
        self.saver = tf.train.Saver([var for var in tf.model_variables()] + \
                                    [self.global_step],
                                     max_to_keep=10)
        sv = tf.train.Supervisor(logdir=opt.checkpoint_dir,
                                 save_summaries_secs=0,
                                 saver=None)
        config = tf.ConfigProto()
        config.gpu_options.allow_growth = True
        with sv.managed_session(config=config) as sess:
            print('Trainable variables: ')
            for var in tf.trainable_variables():
                print(var.name)
            print("parameter_count =", sess.run(parameter_count))
            if opt.continue_train:
                if opt.init_checkpoint_file is None:
                    checkpoint = tf.train.latest_checkpoint(opt.checkpoint_dir)
                else:
                    checkpoint = opt.init_checkpoint_file
                print("Resume training from previous checkpoint: %s" % checkpoint)
                self.saver.restore(sess, checkpoint)
            start_time = time.time()
            for step in range(1, opt.max_steps):
                fetches = {
                    "train": self.train_op,
                    "global_step": self.global_step,
                    "incr_global_step": self.incr_global_step
                }

                if step % opt.summary_freq == 0:
                    fetches["loss"] = self.total_loss
                    fetches["summary"] = sv.summary_op
                results = sess.run(fetches)
                gs = results["global_step"]

                if step % opt.summary_freq == 0:
                    sv.summary_writer.add_summary(results["summary"], gs)
                    train_epoch = math.ceil(gs / self.steps_per_epoch)
                    train_step = gs - (train_epoch - 1) * self.steps_per_epoch
                    print("Epoch: [%2d] [%5d/%5d] time: %4.4f/it loss: %.3f" \
                            % (train_epoch, train_step, self.steps_per_epoch, \
                                (time.time() - start_time)/opt.summary_freq,
                                results["loss"]))
                    start_time = time.time()

                if step % opt.save_latest_freq == 0:
                    self.save(sess, opt.checkpoint_dir, 'latest')

                if step % self.steps_per_epoch == 0:
                    self.save(sess, opt.checkpoint_dir, gs)

    def build_pose_test_graph(self):
        input_uint8 = tf.placeholder(tf.uint8, [self.batch_size,
            self.img_height, self.img_width * self.seq_length, 3],
            name='raw_input')                                           #shape(1,128,416*3,3)
        input_mc = self.preprocess_image(input_uint8)                   #shape(1,128,416*3,3)
        # print('input_mc.shape:',input_mc.shape)
        # input_mc = tf.Print(input_mc,[input_mc.shape],message='input_mc')
        loader = DataLoader()
        tgt_image, src_image_stack = \
            loader.batch_unpack_image_sequence(
                input_mc, self.img_height, self.img_width, self.num_source)

        with tf.name_scope("pose_prediction"):
            pred_poses, _, _ = pose_exp_net(
                tgt_image, src_image_stack, do_exp=False, is_training=False)
            # print('pred_poses:',pred_poses)
            # tf.Print(pred_poses,[pred_poses.shape],message='pred_poses')         #shape(1,2,6)
            self.inputs = input_uint8
            self.pred_poses = pred_poses

    def preprocess_image(self, image):
        # Assuming input image is uint8
        # print('image uint8: ', image)
        # image = tf.Print(image, [image], "image uint8: ")
        image = tf.image.convert_image_dtype(image, dtype=tf.float32)  #[0,1)的float
        # image = tf.Print(image, [image], "image float32: ")
        return image * 2. -1.  #(-1,1)

    def deprocess_image(self, image):
        # Assuming input image is float32
        image = (image + 1.)/2.   #float32 to uint8
        return tf.image.convert_image_dtype(image, dtype=tf.uint8) #uint8

    def setup_inference(self,
                        img_height,
                        img_width,
                        mode,
                        seq_length=3,
                        batch_size=1):
        self.img_height = img_height
        self.img_width = img_width
        self.mode = mode
        self.batch_size = batch_size
        if self.mode == 'depth':
            self.build_depth_test_graph()
        if self.mode == 'pose':
            self.seq_length = seq_length
            self.num_source = seq_length - 1
            self.build_pose_test_graph()

    def inference(self, inputs, sess, mode='depth'):
        fetches = {}

        if mode == 'depth':
            fetches['depth'] = self.pred_depth
        if mode == 'pose':
            fetches['pose'] = self.pred_poses
        results = sess.run(fetches, feed_dict={self.inputs:inputs})
        return results

    def save(self, sess, checkpoint_dir, step):
        model_name = 'model'
        print(" [*] Saving checkpoint to %s..." % checkpoint_dir)
        if step == 'latest':
            self.saver.save(sess,
                            os.path.join(checkpoint_dir, model_name + '.latest'))
        else:
            self.saver.save(sess,
                            os.path.join(checkpoint_dir, model_name),
                            global_step=step)