Spaces:
Sleeping
Sleeping
File size: 4,022 Bytes
4ede126 f00e27b 38e1f78 f00e27b 4ede126 38e1f78 4ede126 ab1e176 4ede126 f00e27b 4ede126 |
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 |
import os
import tempfile
import shutil
import dlc2kinematics as dlc
import gradio as gr
import matplotlib.cm as cm
import pandas as pd
from dlc2kinematics import compute_speed, load_data
from matplotlib import pyplot as plt
def all_plot(bodyparts, speed_csv, tmpdir):
# speed_csvからグラフを作成。
plt.figure()
for i in bodyparts:
plt.plot(speed_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
plt.legend()
plt.xlabel('frame')
plt.ylabel('speed(px/frame)')
# 横軸の最大値を取得して設定
xmax = speed_csv.index.max()
plt.xlim(0, xmax)
plt.ylim(0, 100)
# グラフの凡例をグラフ外に表示。
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
# 凡例をすべて画像内に収める
plt.tight_layout()
plt.savefig(f'{tmpdir}/all_plot.png')
def bodyparts_plot(bodyparts, speed_csv, tmpdir):
# speed_csvからグラフを作成。
for i in bodyparts:
plt.figure()
plt.plot(speed_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
plt.legend()
plt.xlabel('frame')
plt.ylabel('speed')
# 横軸の最大値を取得して設定
xmax = speed_csv.index.max()
plt.xlim(0, xmax)
plt.ylim(0, 100)
# グラフの凡例をグラフ外に表示。
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
# 凡例がはみ出ないように
plt.tight_layout()
plt.savefig(f'{tmpdir}/{i}.png')
def likelihood_plot(bodyparts, likelihood_csv, tmpdir):
for i in bodyparts:
plt.figure()
plt.plot(likelihood_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
plt.legend()
plt.xlabel('frame')
plt.ylabel('likeloohood')
# 横軸の最大値を取得して設定
xmax = likelihood_csv.index.max()
plt.xlim(0, xmax)
plt.ylim(0, 1)
# グラフの凡例をグラフ外に表示。
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
# 凡例をすべて画像内に収める
plt.tight_layout()
plt.savefig(f'{tmpdir}/{i}_likelihood.png')
def all_likelihood_plot(bodyparts, likelihood_csv, tmpdir):
plt.figure()
for i in bodyparts:
plt.plot(likelihood_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
plt.legend()
plt.xlabel('frame')
plt.ylabel('likeloohood')
# 横軸の最大値を取得して設定
xmax = likelihood_csv.index.max()
plt.xlim(0, xmax)
plt.ylim(0, 1)
# グラフの凡例をグラフ外に表示。
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
# 凡例をすべて画像内に収める
plt.tight_layout()
plt.savefig(f'{tmpdir}/all_likelihood_plot.png')
def main(h5_file):
h5_file_name = os.path.basename(h5_file.name)
h5_file_name = os.path.splitext(h5_file_name)[0]
df, bd, scorer = load_data(h5_file.name)
speed = compute_speed(df, ['all'])
# speedのカラムを取得
new_columns = speed.columns.droplevel([0, 2])
speed.columns = new_columns
# カラムを取得
bodyparts = df.columns.droplevel([0, 2]).to_list()
bodyparts = list(dict.fromkeys(bodyparts))
speed_csv = pd.DataFrame()
likelihood_csv = pd.DataFrame()
for i in bodyparts:
speed_csv[i] = speed[i].iloc[:, 0]
likelihood_csv[i] = speed[i].iloc[:, 1]
with tempfile.TemporaryDirectory(dir=".") as tmpdir:
bodyparts_plot(bodyparts, speed_csv, tmpdir)
all_plot(bodyparts, speed_csv, tmpdir)
all_likelihood_plot(bodyparts, likelihood_csv, tmpdir)
likelihood_plot(bodyparts, likelihood_csv, tmpdir)
shutil.make_archive(f"{h5_file_name}", 'zip', root_dir=tmpdir)
return f"{h5_file_name}.zip"
iface = gr.Interface(fn=main, inputs="file", outputs="file")
iface.launch()
|