|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+import os, sys
|
|
|
2
|
+import pandas as pd
|
|
|
3
|
+import numpy as np
|
|
|
4
|
+import sklearn as sk
|
|
|
5
|
+from sklearn import preprocessing
|
|
|
6
|
+import math
|
|
|
7
|
+'''
|
|
|
8
|
+coordsi.py is a module for convert coordinates to SI unit.
|
|
|
9
|
+
|
|
|
10
|
+Warning: This module convert coordinates with heuristic method.
|
|
|
11
|
+This moudle will be deprecated
|
|
|
12
|
+'''
|
|
|
13
|
+
|
|
|
14
|
+def readCam (filepath):
|
|
|
15
|
+ '''
|
|
|
16
|
+ Create a dataframe from cam*.csv file.
|
|
|
17
|
+ You must specify camera id. default is 1.
|
|
|
18
|
+ '''
|
|
|
19
|
+ try:
|
|
|
20
|
+ df = pd.read_csv(filepath, header=0, dtype={'x': np.float64, 'y': np.float64, 'z': np.float64, 'frame': np.int64})
|
|
|
21
|
+ return df
|
|
|
22
|
+ except Exception as e:
|
|
|
23
|
+ print(e)
|
|
|
24
|
+
|
|
|
25
|
+def summaryOriginCoord (root, camid=0):
|
|
|
26
|
+ '''
|
|
|
27
|
+ Show brief information of listed surgeon coordinate.
|
|
|
28
|
+ '''
|
|
|
29
|
+ d = root
|
|
|
30
|
+ try:
|
|
|
31
|
+ if camid == 1 or camid == 2 :
|
|
|
32
|
+ df = pd.DataFrame(columns=['approx_total_frame', 'x_min', 'x_max', 'y_min', 'y_max', 'z_min', 'z_max'])
|
|
|
33
|
+ subdirs = [x[0] for x in os.walk(d)]
|
|
|
34
|
+ for d in subdirs[1:]:
|
|
|
35
|
+ row = []
|
|
|
36
|
+ #get a list of files in directory
|
|
|
37
|
+ files = os.listdir(d)
|
|
|
38
|
+ count_svo = len([s for s in files if "_1.csv" in s])
|
|
|
39
|
+ filpath = os.path.join(d,'cam'+str(camid)+'.csv')
|
|
|
40
|
+ if os.path.isfile(filpath):
|
|
|
41
|
+ arr = readCam(os.path.join(d,'cam'+str(camid)+'.csv'))
|
|
|
42
|
+ row = [3600 *count_svo, arr['x'].min(), arr['x'].max(), arr['y'].min(), arr['y'].max(), arr['z'].min(), arr['z'].max()]
|
|
|
43
|
+ df.loc[d] = row
|
|
|
44
|
+ else:
|
|
|
45
|
+ print('Warning: '+filpath+' is not exist.')
|
|
|
46
|
+ continue
|
|
|
47
|
+ return df
|
|
|
48
|
+ else:
|
|
|
49
|
+ print("Error: Camera ID must be 1 or 2. type camid=1 or camid=2 after dirpath")
|
|
|
50
|
+ except Exception as e:
|
|
|
51
|
+ print(e)
|
|
|
52
|
+
|
|
|
53
|
+
|
|
|
54
|
+
|
|
|
55
|
+
|
|
|
56
|
+def convertToSI (origin, w, l, h):
|
|
|
57
|
+ '''
|
|
|
58
|
+ Waring: This function will be deprecated!
|
|
|
59
|
+ Convert coordinates to SI unit with w, l, h.
|
|
|
60
|
+ w, l, h are heuristic value. It will be changed after calibration and each surgeon room will geat unique value.
|
|
|
61
|
+ '''
|
|
|
62
|
+ try:
|
|
|
63
|
+ df = origin.copy()
|
|
|
64
|
+ #normailze dataframe[0] from -1 to 1
|
|
|
65
|
+ df['x'] = preprocessing.minmax_scale(df['x'], feature_range=(-(w/2), (w/2)), axis=0, copy=True)
|
|
|
66
|
+ #normailze dataframe[1] from -0.75 to 0.75
|
|
|
67
|
+ df['y'] = preprocessing.minmax_scale(df['y'], feature_range=(-(l/2), (l/2)), axis=0, copy=True)
|
|
|
68
|
+ #subtract dataframe[z] from int(4) and normailze from 0.93m to 1.8m (0 as floor)
|
|
|
69
|
+ df['z'] = h - df['z']
|
|
|
70
|
+ df['z'] = preprocessing.minmax_scale(df['z'], feature_range=(0.93,1.8), axis=0, copy=True)
|
|
|
71
|
+ #change column order x, y to y, x
|
|
|
72
|
+ df = df[['y', 'x', 'z', 'frame']]
|
|
|
73
|
+ #change column name y to X, x to Y
|
|
|
74
|
+ df = df.rename(columns={'y': 'x', 'x': 'y'})
|
|
|
75
|
+ return df
|
|
|
76
|
+ except Exception as e:
|
|
|
77
|
+ print(e)
|
|
|
78
|
+
|
|
|
79
|
+def getVelocity (dataframe):
|
|
|
80
|
+ try:
|
|
|
81
|
+ df = dataframe
|
|
|
82
|
+ df['positionvectorvalue'] = (df['x']**2 + df['y']**2 + df['z']**2)
|
|
|
83
|
+ buffer1, buffer2 = df['positionvectorvalue'].copy(), df['positionvectorvalue'].copy()
|
|
|
84
|
+ buffer1, buffer2 = buffer1.to_frame(), buffer2.to_frame()
|
|
|
85
|
+ buffer1, buffer2 = list(buffer1.loc[:, 'positionvectorvalue']), list(buffer2.loc[:, 'positionvectorvalue'])
|
|
|
86
|
+ buffer3 = [0.0]
|
|
|
87
|
+ for x in range(0, len(buffer2)-1):
|
|
|
88
|
+ buffer3.append(buffer2[x])
|
|
|
89
|
+ buffer3 = np.array(buffer1) - np.array(buffer3)
|
|
|
90
|
+ for x in range(0, len(buffer3)):
|
|
|
91
|
+ buffer3[x] = math.sqrt(abs(buffer3[x]))
|
|
|
92
|
+ buffer3[0] = 0.0
|
|
|
93
|
+ df['velocity'] = buffer3
|
|
|
94
|
+ return df
|
|
|
95
|
+ except Exception as e:
|
|
|
96
|
+ print(e)
|
|
|
97
|
+
|
|
|
98
|
+def getAcceleration (dataframe):
|
|
|
99
|
+ try:
|
|
|
100
|
+ df = dataframe
|
|
|
101
|
+ buffer1, buffer2 = df['velocity'].copy(), df['velocity'].copy()
|
|
|
102
|
+ buffer1, buffer2 = buffer1.to_frame(), buffer2.to_frame()
|
|
|
103
|
+ buffer1, buffer2 = list(buffer1.loc[:, 'velocity']), list(buffer2.loc[:, 'velocity'])
|
|
|
104
|
+ buffer3 = [0.0]
|
|
|
105
|
+ for x in range(0, len(buffer2)-1):
|
|
|
106
|
+ buffer3.append(buffer2[x])
|
|
|
107
|
+ buffer3 = np.array(buffer1) - np.array(buffer3)
|
|
|
108
|
+ buffer3[1] = 0.0
|
|
|
109
|
+ df['acceleration'] = buffer3
|
|
|
110
|
+ return df
|
|
|
111
|
+ except Exception as e:
|
|
|
112
|
+ print(e)
|
|
|
113
|
+
|
|
|
114
|
+def getFullTrace (dataframe, approx_total_frame):
|
|
|
115
|
+ try:
|
|
|
116
|
+ df = dataframe
|
|
|
117
|
+ total_frame = int(approx_total_frame)
|
|
|
118
|
+ handletrace = pd.DataFrame(columns=['x', 'y', 'z', 'frame', 'positionvectorvalue', 'velocity', 'acceleration', 'isValid'])
|
|
|
119
|
+ handletrace = pd.concat([handletrace, pd.DataFrame(np.zeros((total_frame,8)), columns=['x', 'y', 'z', 'frame', 'positionvectorvalue','velocity', 'acceleration', 'isValid'])], axis=0)
|
|
|
120
|
+ for x in range(0, len(df)):
|
|
|
121
|
+ handletrace.loc[df.loc[x, 'frame']] = df.loc[x]
|
|
|
122
|
+ handletrace['isValid'] = handletrace['isValid'].fillna(0)
|
|
|
123
|
+ return handletrace
|
|
|
124
|
+ except Exception as e:
|
|
|
125
|
+ print(e)
|
|
|
126
|
+
|
|
|
127
|
+def addValidDetection (dataframe, threshold = 25.0):
|
|
|
128
|
+ '''
|
|
|
129
|
+ threshold value is suitable for only 1st surgeon room(Seoul 801).
|
|
|
130
|
+ We can make threshold in every surgeon room by using epipolar geometry.(but needs more cameras with different positions)
|
|
|
131
|
+ '''
|
|
|
132
|
+ try:
|
|
|
133
|
+ df = dataframe
|
|
|
134
|
+ top = threshold
|
|
|
135
|
+ bottom = -threshold
|
|
|
136
|
+ for x in range(0, len(df)):
|
|
|
137
|
+ if bottom < df['acceleration'][x] < top:
|
|
|
138
|
+ df['isValid'][x] = 1
|
|
|
139
|
+ else:
|
|
|
140
|
+ df['isValid'][x] = 0
|
|
|
141
|
+ return df
|
|
|
142
|
+ except Exception as e:
|
|
|
143
|
+ print(e)
|