-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_collector.py
156 lines (102 loc) · 2.62 KB
/
data_collector.py
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
155
156
DATA_PATH = 'data'
NAME = 'poke'
ORIGIN_DATA_PATH = 'origin/poke/poke_11.csv'
ROW_CNT = 15
import os
SAVE_PATH = os.path.join(DATA_PATH, NAME)
files = os.listdir(SAVE_PATH)
cnt = 0
for s in files:
try:
num = int(s.split('_')[1].split('.')[0])
if cnt<num:
cnt=num
except:
pass
print('data의 개수 : ', cnt)
cnt += 1
import matplotlib
import clipboard
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv(ORIGIN_DATA_PATH)
fig, ax = plt.subplots(figsize=(15, 8))
plt.grid(True)
plt.xlabel('time')
plt.ylabel('y')
plt.title('IMU data collector')
ax.set_aspect('auto', adjustable='box')
x = [i for i in range(len(df))]
ax.set_ylim(-400, 400)
for i in range(1, 4):
y = df.iloc[:, i] * 100
ax.plot(x, y, label=str(df.columns[i]), color='r')
ax.legend()
for i in range(4, 7):
y = df.iloc[:, i]
ax.plot(x, y, label=str(df.columns[i]), color='g')
ax.legend()
x_line = []
y_line = []
line, = ax.plot(x_line, y_line)
def remove_line():
x_line.clear()
y_line.clear()
line.set_data(x_line, y_line)
plt.draw()
def draw_vertical_line(x):
x_line.append(x)
y_line.append(500)
x_line.append(x)
y_line.append(-500)
x_line.append(x + ROW_CNT - 1)
y_line.append(-500)
x_line.append(x + ROW_CNT - 1)
y_line.append(500)
line.set_data(x_line, y_line)
plt.draw()
x = 0
def add_point(event):
global x
global cnt
if event.inaxes != ax:
return
# mouse left click
if event.button == 1:
x = int(event.xdata)
remove_line()
draw_vertical_line(x)
# mouse right click
if event.button == 3:
cut_df = df.loc[x:x + ROW_CNT - 1].set_index('time')
csv = cut_df.to_csv()
# print('---------------------------------------------------')
# print(csv)
# clipboard.copy(csv)
filename = NAME + '_' + str(cnt) + '.csv'
path = os.path.join(SAVE_PATH, filename)
f = open(path,mode='wt', encoding='utf-8')
for i in csv.split('\n'):
print(i)
f.write(i)
print(path)
f.close()
cnt += 1
# mouse mid click
if event.button == 2:
plt.disconnect(cid)
plt.close()
def on_press(event):
global x
if event.key == 'right':
x += 1
remove_line()
draw_vertical_line(x)
if event.key == 'left':
x -= 1
remove_line()
draw_vertical_line(x)
cid = plt.connect('button_press_event', add_point)
plt.connect('key_press_event', on_press)
plt.show()