File size: 7,689 Bytes
5000dc5
 
 
 
 
e91a512
2327f3b
f10a868
44aae25
145a95e
2327f3b
145a95e
617f2a8
71ed6ee
44aae25
71ed6ee
 
 
 
 
 
2327f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e91a512
f10a868
6987f85
 
 
b22c9af
71ed6ee
 
cc99721
b22c9af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd35a1d
 
 
 
 
 
 
 
1a2008f
6987f85
 
71ed6ee
 
 
 
 
 
6987f85
dd35a1d
185bb56
b22c9af
6987f85
0435fcb
f10a868
1a2008f
e70b1cc
dd35a1d
e70b1cc
145a95e
f10a868
145a95e
 
2327f3b
 
 
 
 
 
 
 
 
145a95e
2327f3b
145a95e
 
 
 
2327f3b
145a95e
2327f3b
 
145a95e
2327f3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145a95e
e91a512
90a45e8
 
 
 
2327f3b
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
import os
if os.name == 'nt':
    import sys
    sys.path.append(os.path.dirname(os.path.abspath(__file__)))

import gradio as gr
from moviepy.editor import VideoFileClip
from lab_tools import spectrogram
from lab_tools import labutils
from lab_tools import analyze1f
from lab_tools import ytutil


def update_slider_range(filepath):
    timestamp = labutils.load_signal(filepath, "Timestamp")
    max_value = float(timestamp[len(timestamp) - 1])
    min_value = float(timestamp[0])

    return gr.update(minimum=min_value, maximum=max_value), gr.update(minimum=min_value, maximum=max_value, value=max_value)


def update_anlyze_setting(mode):
    if mode == "YouTube":
        return gr.update(visible=True, value=None), gr.update(visible=False, value=None), gr.update(visible=True), gr.update(value=None)
    else:
        return gr.update(visible=False, value=None), gr.update(visible=True, value=None), gr.update(visible=False), gr.update(value=None)


def update_slidar_range_youtube(video_url):
    if video_url == "":
        return gr.update(maximum=0, value=0), gr.update(maximum=0, value=0), gr.update(value=None), gr.update(value=None), gr.update(value=None)

    video_info = ytutil.get_video_info(video_url)
    duration = int(video_info["duration"]) - 1
    title = video_info["title"]

    ytutil.remove_mp4_file()

    return gr.update(maximum=duration), gr.update(maximum=duration, value=duration), gr.update(value=title), gr.update(value=None), gr.update(value=None)


def update_slidar_range_video_file(file_path):
    if file_path == "":
        return gr.update(maximum=0), gr.update(maximum=0, value=0), gr.update(value=None), gr.update(value=None)

    with VideoFileClip(file_path) as video:
        duration = video.duration

    return gr.update(maximum=duration), gr.update(maximum=duration, value=duration), gr.update(value=None), gr.update(value=None)


with gr.Blocks() as main_ui:
    with gr.Tab("Spectrogram analyze"):
        with gr.Row():
            with gr.Column():
                file_input = gr.File(label="CSVファイルをアップロードしてください。", file_count="single", file_types=["csv"])
                fmax_slider = gr.Slider(minimum=0, maximum=200, value=40, label="wavelet 最大周波数", step=10, info="単位はHz。")
                start_time = gr.Slider(minimum=0, maximum=60, value=0.0, step=0.5, label="Start Time (sec)")
                end_time = gr.Slider(minimum=0, maximum=60, value=60.0, step=0.5, label="End Time (sec)")

                with gr.Accordion(label="フィルター設定", open=False):
                    filter_setting = gr.Radio(
                        ["No Filter", "High PASS", "Low PASS"],
                        label="使用するフィルター",
                        value="High PASS",
                    )
                    fp_hp = gr.Slider(minimum=0, maximum=20, value=3, step=0.1, label="通過域端周波数 [Hz]")
                    fs_hp = gr.Slider(minimum=0, maximum=20, value=1, step=0.1, label="阻止域端周波数 [Hz]")
                    gpass = gr.Slider(minimum=0, maximum=100, value=3, step=1, label="通過域端最大損失 [dB]")
                    gstop = gr.Slider(minimum=0, maximum=100, value=40, step=1, label="阻止域端最小損失 [dB]")

                with gr.Accordion(label="詳細設定", open=False):
                    analysis_method = gr.Radio(
                        ["Short-Time Fourier Transform", "Wavelet"],
                        label="Analysis method",
                        value="Short-Time Fourier Transform",
                    )
                    fs_slider = gr.Slider(minimum=0, maximum=10000, value=1000, label="サンプリング周波数", step=10, info="単位はHz。")
                    column_dropdown = gr.Dropdown(["Fp1", "Fp2", "T7", "T8", "O1", "O2"], value="Fp2", label="使用する信号データ", allow_custom_value=True, info="使用する信号データを選んでください。デフォルトはFp2です。")
                    integration_method = gr.Radio(
                        ["Trapezoid(台形積分)", "Simpson(シンプソン法)"],
                        label="Integration Method",
                        value="Simpson(シンプソン法)",
                    )
                    segment_length = gr.Slider(minimum=0, maximum=8192, value=4096, step=1, label="STFT: セグメント長")
                    overlap = gr.Slider(minimum=0, maximum=99, value=90, step=1, label="STFT: オーバーラップ率 [%]")
                    fontsize = gr.Slider(minimum=0, maximum=20, value=12, step=1, label="グラフのフォントサイズ")

                submit_button = gr.Button("計算開始")

                file_input.change(
                    update_slider_range,
                    inputs=file_input,
                    outputs=[start_time, end_time]
                )

            with gr.Column():
                config_file = gr.File(label="Ziped Analyze File")
                wavelet_image = gr.Image(type="filepath", label="Spectrogram")
                band_intensity = gr.Image(type="filepath", label="Band Intensity")

        submit_button.click(spectrogram.generate_spectrogram_and_signal_plot, inputs=[
            file_input, analysis_method,
            fs_slider, fmax_slider, column_dropdown, start_time, end_time,
            filter_setting, fp_hp, fs_hp, gpass, gstop,
            integration_method, segment_length, overlap, fontsize],
            outputs=[wavelet_image, band_intensity, config_file])

    with gr.Tab("1f noise analyze"):
        with gr.Row():
            with gr.Column():
                mode_setting = gr.Radio(
                    ["YouTube", "動画ファイル"],
                    label="解析データ",
                    value="YouTube",
                )
                youtube_url_input = gr.Text(label="YouTubeのリンクを貼り付けてください。")
                file_input = gr.File(label="動画をアップロードしてください", visible=False, file_count="single", file_types=["mp4"])
                start_time = gr.Slider(minimum=0, maximum=3600, value=0, step=1, label="Start Time (sec)")
                end_time = gr.Slider(minimum=0, maximum=3600, value=10, step=1, label="End Time (sec)")
                submit_button = gr.Button("計算開始")
                download_button = gr.Button("ダウンロード")

            with gr.Column():
                caption = gr.Text(label="動画タイトル")
                result = gr.Image(type="filepath", label="Wavelet")
                file_result = gr.File(label="Downloaded Video")

        submit_button.click(analyze1f.analyze_1f_noise, inputs=[mode_setting, youtube_url_input, file_input, start_time, end_time], outputs=[caption, result])
        download_button.click(ytutil.download_youtube_video, inputs=[youtube_url_input], outputs=[file_result])

        mode_setting.change(
            update_anlyze_setting,
            inputs=mode_setting,
            outputs=[youtube_url_input, file_input, download_button, result]
        )
        youtube_url_input.change(
            update_slidar_range_youtube,
            inputs=youtube_url_input,
            outputs=[start_time, end_time, caption, file_result, result]
            )
        file_input.change(
            update_slidar_range_video_file,
            inputs=file_input,
            outputs=[start_time, end_time, file_result, result]
        )

if __name__ == "__main__":
    if os.name == 'nt':
        main_ui.queue().launch()
    else:
        main_ui.queue().launch(server_name="0.0.0.0", server_port=7860)
    ytutil.remove_mp4_file()