Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +109 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tempfile
|
3 |
+
import shutil
|
4 |
+
|
5 |
+
import dlc2kinematics as dlc
|
6 |
+
import gradio as gr
|
7 |
+
import matplotlib.cm as cm
|
8 |
+
import pandas as pd
|
9 |
+
from dlc2kinematics import compute_speed, load_data
|
10 |
+
from matplotlib import pyplot as plt
|
11 |
+
|
12 |
+
|
13 |
+
def all_plot(bodyparts, speed_csv, tmpdir):
|
14 |
+
|
15 |
+
# speed_csvからグラフを作成。
|
16 |
+
plt.figure()
|
17 |
+
for i in bodyparts:
|
18 |
+
plt.plot(speed_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
|
19 |
+
|
20 |
+
plt.legend()
|
21 |
+
plt.xlabel('frame')
|
22 |
+
plt.ylabel('speed(px/frame)')
|
23 |
+
|
24 |
+
# 横軸の最大値を取得して設定
|
25 |
+
xmax = speed_csv.index.max()
|
26 |
+
plt.xlim(0, xmax)
|
27 |
+
plt.ylim(0, 100)
|
28 |
+
|
29 |
+
# グラフの凡例をグラフ外に表示。
|
30 |
+
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
|
31 |
+
|
32 |
+
# 凡例をすべて画像内に収める
|
33 |
+
plt.tight_layout()
|
34 |
+
plt.savefig(f'{tmpdir}/all_plot.png')
|
35 |
+
|
36 |
+
|
37 |
+
def bodyparts_plot(bodyparts, speed_csv, tmpdir):
|
38 |
+
# speed_csvからグラフを作成。
|
39 |
+
for i in bodyparts:
|
40 |
+
plt.figure()
|
41 |
+
plt.plot(speed_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
|
42 |
+
plt.legend()
|
43 |
+
plt.xlabel('frame')
|
44 |
+
plt.ylabel('speed')
|
45 |
+
|
46 |
+
# 横軸の最大値を取得して設定
|
47 |
+
xmax = speed_csv.index.max()
|
48 |
+
plt.xlim(0, xmax)
|
49 |
+
plt.ylim(0, 100)
|
50 |
+
|
51 |
+
# グラフの凡例をグラフ外に表示。
|
52 |
+
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
|
53 |
+
# 凡例がはみ出ないように
|
54 |
+
plt.tight_layout()
|
55 |
+
|
56 |
+
plt.savefig(f'{tmpdir}/{i}.png')
|
57 |
+
|
58 |
+
|
59 |
+
def likelihood_plot(bodyparts, likelihood_csv, tmpdir):
|
60 |
+
plt.figure()
|
61 |
+
for i in bodyparts:
|
62 |
+
plt.plot(likelihood_csv[i], label=i, color=cm.rainbow(int(255 * bodyparts.index(i) / len(bodyparts))))
|
63 |
+
|
64 |
+
plt.legend()
|
65 |
+
plt.xlabel('frame')
|
66 |
+
plt.ylabel('likeloohood')
|
67 |
+
|
68 |
+
# 横軸の最大値を取得して設定
|
69 |
+
xmax = likelihood_csv.index.max()
|
70 |
+
plt.xlim(0, xmax)
|
71 |
+
plt.ylim(0, 1)
|
72 |
+
|
73 |
+
# グラフの凡例をグラフ外に表示。
|
74 |
+
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left', borderaxespad=0, fontsize=10)
|
75 |
+
|
76 |
+
# 凡例をすべて画像内に収める
|
77 |
+
plt.tight_layout()
|
78 |
+
|
79 |
+
plt.savefig(f'{tmpdir}/likelihood_plot.png')
|
80 |
+
|
81 |
+
|
82 |
+
def main(h5_file):
|
83 |
+
h5_file_name = os.path.basename(h5_file.name)
|
84 |
+
h5_file_name = os.path.splitext(h5_file_name)[0]
|
85 |
+
df, bd, scorer = load_data(f'h5/{h5_file_name}.h5')
|
86 |
+
speed = compute_speed(df, ['all'])
|
87 |
+
|
88 |
+
# speedのカラムを取得
|
89 |
+
new_columns = speed.columns.droplevel([0, 2])
|
90 |
+
speed.columns = new_columns
|
91 |
+
# カラムを取得
|
92 |
+
bodyparts = df.columns.droplevel([0, 2]).to_list()
|
93 |
+
bodyparts = list(dict.fromkeys(bodyparts))
|
94 |
+
speed_csv = pd.DataFrame()
|
95 |
+
likelihood_csv = pd.DataFrame()
|
96 |
+
for i in bodyparts:
|
97 |
+
speed_csv[i] = speed[i].iloc[:, 0]
|
98 |
+
likelihood_csv[i] = speed[i].iloc[:, 1]
|
99 |
+
|
100 |
+
with tempfile.TemporaryDirectory(dir=".") as tmpdir:
|
101 |
+
bodyparts_plot(bodyparts, speed_csv, tmpdir)
|
102 |
+
all_plot(bodyparts, speed_csv, tmpdir)
|
103 |
+
likelihood_plot(bodyparts, likelihood_csv, tmpdir)
|
104 |
+
shutil.make_archive(f"{h5_file_name}", 'zip', root_dir=tmpdir)
|
105 |
+
return f"{h5_file_name}.zip"
|
106 |
+
|
107 |
+
|
108 |
+
iface = gr.Interface(fn=main, inputs="file", outputs="file")
|
109 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
dlc2kinematics
|
2 |
+
gradio
|
3 |
+
pandas
|
4 |
+
matplotlib
|