ソースを参照

initial commit

master
Jeong Geol Kim 3年前
コミット
1e93fb07d6
5個のファイルの変更2273行の追加0行の削除
  1. 143
    0
      module/coordsi.py
  2. 34
    0
      module/wavacc.py
  3. 32
    0
      script/createhandletrace.py
  4. 1384
    0
      toysnippet/cosinegraph.ipynb
  5. 680
    0
      toysnippet/sandbox.ipynb

+ 143
- 0
module/coordsi.py ファイルの表示

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)        

+ 34
- 0
module/wavacc.py ファイルの表示

1
+import sys, os, math, time
2
+import numpy as np
3
+import pandas as pd
4
+'''
5
+wavacc.py for interpolate accelerometer data and extract feature as stroke.
6
+Denosing step is not implemented this module. See addValidDetection function in coordsi.py for denoising.
7
+'''
8
+def interpolation(df):
9
+    try:
10
+        #implement data here
11
+    except Exception as e:
12
+        print(e)
13
+        return df
14
+    
15
+def decomposition(df):
16
+    try:
17
+        #implemnet decompositino with wavelet transform here
18
+    except Exception as e:
19
+        print(e)
20
+        return df
21
+
22
+def getStroke(df):
23
+    try:
24
+        #implement get stroke here
25
+    except Exception as e:
26
+        print(e)
27
+        return df
28
+
29
+def mergeToDataFrame(df):
30
+    try:
31
+        #implement merge to dataframe here
32
+    except Exception as e:
33
+        print(e)
34
+        return df

+ 32
- 0
script/createhandletrace.py ファイルの表示

1
+import os, sys
2
+import pandas as pd
3
+import numpy as np
4
+import coordsi as csi
5
+import math
6
+import time
7
+
8
+# initialize root path
9
+root = './List_Data_Call/'
10
+
11
+summary_1 = csi.summaryOriginCoord(root, camid=1)
12
+
13
+#make a loop for each index
14
+for index, row in summary_1.iterrows():
15
+    timestamp = []
16
+    start_time = time.time()
17
+    df = csi.readCam(os.path.join(index,'cam1.csv'))
18
+    df = csi.convertToSI(df, 3.5, 1.5, 3.4)
19
+    df = csi.getVelocity(df)
20
+    df = csi.getAcceleration(df)
21
+    df = csi.getFullTrace(df,row['approx_total_frame'] )
22
+    df = csi.addValidDetection(df, 25.0)
23
+    try:
24
+        df.to_csv(os.path.join('./handletrace/', index[17:]+'.csv'), index=False)
25
+    #save spend time and index of summary_1 in timestamp list    
26
+        timestamp.append([index, time.time() - start_time])
27
+        print([index, time.time() - start_time])
28
+    except Exception as e:
29
+        print(e)
30
+        continue
31
+#save timestamp list to csv file
32
+pd.DataFrame(timestamp, columns=['index', 'time']).to_csv('./handletrace/timestamp.csv', index=False)

+ 1384
- 0
toysnippet/cosinegraph.ipynb
ファイル差分が大きすぎるため省略します
ファイルの表示


+ 680
- 0
toysnippet/sandbox.ipynb ファイルの表示

1
+{
2
+ "cells": [
3
+  {
4
+   "cell_type": "code",
5
+   "execution_count": 1,
6
+   "metadata": {},
7
+   "outputs": [],
8
+   "source": [
9
+    "import os, sys\n",
10
+    "import pandas as pd\n",
11
+    "import numpy as np\n",
12
+    "import coordsi as csi\n",
13
+    "import math\n",
14
+    "import time"
15
+   ]
16
+  },
17
+  {
18
+   "attachments": {},
19
+   "cell_type": "markdown",
20
+   "metadata": {},
21
+   "source": [
22
+    "A brief summary of each original coordinate"
23
+   ]
24
+  },
25
+  {
26
+   "cell_type": "code",
27
+   "execution_count": 2,
28
+   "metadata": {},
29
+   "outputs": [
30
+    {
31
+     "name": "stdout",
32
+     "output_type": "stream",
33
+     "text": [
34
+      "Warning: ./List_Data_Call/21002290020230321/cam1.csv is not exist.\n",
35
+      "                                    approx_total_frame  x_min   x_max  y_min   \n",
36
+      "./List_Data_Call/21004038220221101            478800.0   -3.0  1900.0   -2.0  \\\n",
37
+      "./List_Data_Call/21001264420230215            219600.0   -4.0  1901.0   -6.0   \n",
38
+      "./List_Data_Call/21001555320230216            349200.0   -5.0  1884.0   -3.0   \n",
39
+      "./List_Data_Call/21004030420221004            666000.0   -4.0  1902.0   -3.0   \n",
40
+      "./List_Data_Call/21004008220221017            219600.0   -8.0  1883.0   -2.0   \n",
41
+      "\n",
42
+      "                                     y_max     z_min     z_max  \n",
43
+      "./List_Data_Call/21004038220221101  1051.0  0.923129  4.137998  \n",
44
+      "./List_Data_Call/21001264420230215  1055.0  0.953854  4.400389  \n",
45
+      "./List_Data_Call/21001555320230216  1057.0  0.840809  3.938679  \n",
46
+      "./List_Data_Call/21004030420221004  1057.0  0.933506  4.356071  \n",
47
+      "./List_Data_Call/21004008220221017  1058.0  0.897373  4.207281  \n"
48
+     ]
49
+    }
50
+   ],
51
+   "source": [
52
+    "# initialize root path\n",
53
+    "root = './List_Data_Call/'\n",
54
+    "\n",
55
+    "summary_1 = csi.summaryOriginCoord(root, camid=1)\n",
56
+    "print(summary_1.head())"
57
+   ]
58
+  },
59
+  {
60
+   "cell_type": "code",
61
+   "execution_count": 3,
62
+   "metadata": {},
63
+   "outputs": [
64
+    {
65
+     "name": "stdout",
66
+     "output_type": "stream",
67
+     "text": [
68
+      "./List_Data_Call/21004038220221101 64.0459258556366\n",
69
+      "./List_Data_Call/21001264420230215 34.23401594161987\n",
70
+      "./List_Data_Call/21001555320230216 55.52943301200867\n",
71
+      "./List_Data_Call/21004030420221004 106.67347645759583\n",
72
+      "./List_Data_Call/21004008220221017 31.72646999359131\n",
73
+      "./List_Data_Call/21004056620230120 19.501684188842773\n",
74
+      "./List_Data_Call/21001057620230126 53.51554536819458\n",
75
+      "./List_Data_Call/21003885420220721 76.24831080436707\n",
76
+      "./List_Data_Call/21004107920230313 97.09525084495544\n",
77
+      "./List_Data_Call/21003975920220723 36.387489318847656\n",
78
+      "./List_Data_Call/21004102020230228 31.510535717010498\n",
79
+      "./List_Data_Call/21004036420221013 63.18059492111206\n",
80
+      "./List_Data_Call/21003962420220712 36.284823179244995\n",
81
+      "./List_Data_Call/21004048220221029 50.87060260772705\n",
82
+      "./List_Data_Call/21003992420230119 49.36880159378052\n",
83
+      "./List_Data_Call/21001418920220921 35.04419279098511\n",
84
+      "./List_Data_Call/21004108620230225 94.45536732673645\n",
85
+      "./List_Data_Call/21004095420230303 7.877222776412964\n",
86
+      "./List_Data_Call/21003655120230207 22.74891757965088\n",
87
+      "./List_Data_Call/21003817020230107 24.885369300842285\n",
88
+      "./List_Data_Call/21003981020220923 36.27069091796875\n",
89
+      "./List_Data_Call/21004033920221007 86.56228399276733\n",
90
+      "./List_Data_Call/21004074720230104 37.52795934677124\n",
91
+      "./List_Data_Call/21004037520230324 30.00572395324707\n",
92
+      "./List_Data_Call/21004040420221103 90.27121639251709\n",
93
+      "./List_Data_Call/21004026620221007 69.01991891860962\n",
94
+      "./List_Data_Call/21001870620230315 39.85080003738403\n",
95
+      "./List_Data_Call/21004068020230102 7.841302871704102\n",
96
+      "./List_Data_Call/21004094420230206 100.37451100349426\n",
97
+      "./List_Data_Call/21004097420230304 84.17479515075684\n",
98
+      "./List_Data_Call/21004047820221028 89.44327425956726\n",
99
+      "./List_Data_Call/21004051720230228 9.743078708648682\n",
100
+      "./List_Data_Call/21004076520230204 76.26534867286682\n",
101
+      "./List_Data_Call/21002591020230218 88.18855333328247\n",
102
+      "./List_Data_Call/21004105820230311 59.186476945877075\n",
103
+      "./List_Data_Call/21004037520230317 54.6233925819397\n",
104
+      "./List_Data_Call/21003993520220917 95.81168413162231\n",
105
+      "./List_Data_Call/21003967920220714 36.98200988769531\n",
106
+      "./List_Data_Call/21004043620221025 25.73115038871765\n",
107
+      "./List_Data_Call/21004097520230208 93.56520891189575\n",
108
+      "./List_Data_Call/21004092020230127 89.9213137626648\n",
109
+      "./List_Data_Call/21004026220220928 81.38130640983582\n"
110
+     ]
111
+    },
112
+    {
113
+     "ename": "ValueError",
114
+     "evalue": "Found array with 0 sample(s) (shape=(0,)) while a minimum of 1 is required.",
115
+     "output_type": "error",
116
+     "traceback": [
117
+      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
118
+      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
119
+      "Cell \u001b[0;32mIn[3], line 6\u001b[0m\n\u001b[1;32m      4\u001b[0m start_time \u001b[39m=\u001b[39m time\u001b[39m.\u001b[39mtime()\n\u001b[1;32m      5\u001b[0m df \u001b[39m=\u001b[39m csi\u001b[39m.\u001b[39mreadCam(os\u001b[39m.\u001b[39mpath\u001b[39m.\u001b[39mjoin(index,\u001b[39m'\u001b[39m\u001b[39mcam1.csv\u001b[39m\u001b[39m'\u001b[39m))\n\u001b[0;32m----> 6\u001b[0m df \u001b[39m=\u001b[39m csi\u001b[39m.\u001b[39;49mconvertToSI(df, \u001b[39m3.5\u001b[39;49m, \u001b[39m1.5\u001b[39;49m, \u001b[39m3.4\u001b[39;49m)\n\u001b[1;32m      7\u001b[0m df \u001b[39m=\u001b[39m csi\u001b[39m.\u001b[39mgetVelocity(df)\n\u001b[1;32m      8\u001b[0m df \u001b[39m=\u001b[39m csi\u001b[39m.\u001b[39mgetAcceleration(df)\n",
120
+      "File \u001b[0;32m~/workspace/equation20/coordsi.py:62\u001b[0m, in \u001b[0;36mconvertToSI\u001b[0;34m(origin, w, l, h)\u001b[0m\n\u001b[1;32m     60\u001b[0m df \u001b[39m=\u001b[39m origin\u001b[39m.\u001b[39mcopy()\n\u001b[1;32m     61\u001b[0m \u001b[39m#normailze dataframe[0] from -1 to 1\u001b[39;00m\n\u001b[0;32m---> 62\u001b[0m df[\u001b[39m'\u001b[39m\u001b[39mx\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m preprocessing\u001b[39m.\u001b[39;49mminmax_scale(df[\u001b[39m'\u001b[39;49m\u001b[39mx\u001b[39;49m\u001b[39m'\u001b[39;49m], feature_range\u001b[39m=\u001b[39;49m(\u001b[39m-\u001b[39;49m(w\u001b[39m/\u001b[39;49m\u001b[39m2\u001b[39;49m), (w\u001b[39m/\u001b[39;49m\u001b[39m2\u001b[39;49m)), axis\u001b[39m=\u001b[39;49m\u001b[39m0\u001b[39;49m, copy\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n\u001b[1;32m     63\u001b[0m \u001b[39m#normailze dataframe[1] from -0.75 to 0.75\u001b[39;00m\n\u001b[1;32m     64\u001b[0m df[\u001b[39m'\u001b[39m\u001b[39my\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m preprocessing\u001b[39m.\u001b[39mminmax_scale(df[\u001b[39m'\u001b[39m\u001b[39my\u001b[39m\u001b[39m'\u001b[39m], feature_range\u001b[39m=\u001b[39m(\u001b[39m-\u001b[39m(l\u001b[39m/\u001b[39m\u001b[39m2\u001b[39m), (l\u001b[39m/\u001b[39m\u001b[39m2\u001b[39m)), axis\u001b[39m=\u001b[39m\u001b[39m0\u001b[39m, copy\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m)\n",
121
+      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/sklearn/preprocessing/_data.py:624\u001b[0m, in \u001b[0;36mminmax_scale\u001b[0;34m(X, feature_range, axis, copy)\u001b[0m\n\u001b[1;32m    550\u001b[0m \u001b[39m\u001b[39m\u001b[39m\"\"\"Transform features by scaling each feature to a given range.\u001b[39;00m\n\u001b[1;32m    551\u001b[0m \n\u001b[1;32m    552\u001b[0m \u001b[39mThis estimator scales and translates each feature individually such\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    620\u001b[0m \u001b[39m<sphx_glr_auto_examples_preprocessing_plot_all_scaling.py>`.\u001b[39;00m\n\u001b[1;32m    621\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m    622\u001b[0m \u001b[39m# Unlike the scaler object, this function allows 1d input.\u001b[39;00m\n\u001b[1;32m    623\u001b[0m \u001b[39m# If copy is required, it will be done inside the scaler object.\u001b[39;00m\n\u001b[0;32m--> 624\u001b[0m X \u001b[39m=\u001b[39m check_array(\n\u001b[1;32m    625\u001b[0m     X, copy\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m, ensure_2d\u001b[39m=\u001b[39;49m\u001b[39mFalse\u001b[39;49;00m, dtype\u001b[39m=\u001b[39;49mFLOAT_DTYPES, force_all_finite\u001b[39m=\u001b[39;49m\u001b[39m\"\u001b[39;49m\u001b[39mallow-nan\u001b[39;49m\u001b[39m\"\u001b[39;49m\n\u001b[1;32m    626\u001b[0m )\n\u001b[1;32m    627\u001b[0m original_ndim \u001b[39m=\u001b[39m X\u001b[39m.\u001b[39mndim\n\u001b[1;32m    629\u001b[0m \u001b[39mif\u001b[39;00m original_ndim \u001b[39m==\u001b[39m \u001b[39m1\u001b[39m:\n",
122
+      "File \u001b[0;32m~/.local/lib/python3.8/site-packages/sklearn/utils/validation.py:931\u001b[0m, in \u001b[0;36mcheck_array\u001b[0;34m(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator, input_name)\u001b[0m\n\u001b[1;32m    929\u001b[0m     n_samples \u001b[39m=\u001b[39m _num_samples(array)\n\u001b[1;32m    930\u001b[0m     \u001b[39mif\u001b[39;00m n_samples \u001b[39m<\u001b[39m ensure_min_samples:\n\u001b[0;32m--> 931\u001b[0m         \u001b[39mraise\u001b[39;00m \u001b[39mValueError\u001b[39;00m(\n\u001b[1;32m    932\u001b[0m             \u001b[39m\"\u001b[39m\u001b[39mFound array with \u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m sample(s) (shape=\u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m) while a\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m    933\u001b[0m             \u001b[39m\"\u001b[39m\u001b[39m minimum of \u001b[39m\u001b[39m%d\u001b[39;00m\u001b[39m is required\u001b[39m\u001b[39m%s\u001b[39;00m\u001b[39m.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m    934\u001b[0m             \u001b[39m%\u001b[39m (n_samples, array\u001b[39m.\u001b[39mshape, ensure_min_samples, context)\n\u001b[1;32m    935\u001b[0m         )\n\u001b[1;32m    937\u001b[0m \u001b[39mif\u001b[39;00m ensure_min_features \u001b[39m>\u001b[39m \u001b[39m0\u001b[39m \u001b[39mand\u001b[39;00m array\u001b[39m.\u001b[39mndim \u001b[39m==\u001b[39m \u001b[39m2\u001b[39m:\n\u001b[1;32m    938\u001b[0m     n_features \u001b[39m=\u001b[39m array\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m]\n",
123
+      "\u001b[0;31mValueError\u001b[0m: Found array with 0 sample(s) (shape=(0,)) while a minimum of 1 is required."
124
+     ]
125
+    }
126
+   ],
127
+   "source": [
128
+    "#make a loop for each index\n",
129
+    "for index, row in summary_1.iterrows():\n",
130
+    "    timestamp = []\n",
131
+    "    start_time = time.time()\n",
132
+    "    df = csi.readCam(os.path.join(index,'cam1.csv'))\n",
133
+    "    df = csi.convertToSI(df, 3.5, 1.5, 3.4)\n",
134
+    "    df = csi.getVelocity(df)\n",
135
+    "    df = csi.getAcceleration(df)\n",
136
+    "    df = csi.getFullTrace(df,row['approx_total_frame'] )\n",
137
+    "    df = csi.addValidDetection(df, 25.0)\n",
138
+    "    df.to_csv(os.path.join('./handletrace/', index[17:]+'.csv'), index=False)\n",
139
+    "    #save spend time and index of summary_1 in timestamp list    \n",
140
+    "    timestamp.append([index, time.time() - start_time])\n",
141
+    "#save timestamp list to csv file\n",
142
+    "pd.DataFrame(timestamp, columns=['index', 'time']).to_csv('./handletrace/timestamp.csv', index=False)\n",
143
+    "\n"
144
+   ]
145
+  },
146
+  {
147
+   "attachments": {},
148
+   "cell_type": "markdown",
149
+   "metadata": {},
150
+   "source": [
151
+    "import a origin coord and convert"
152
+   ]
153
+  },
154
+  {
155
+   "cell_type": "code",
156
+   "execution_count": 3,
157
+   "metadata": {},
158
+   "outputs": [],
159
+   "source": [
160
+    "arr_1 = csi.readCam(os.path.join(summary_1.index[0],'cam1.csv'))\n",
161
+    "si_1 = csi.convertToSI(arr_1, 3.5, 1.5, 3.4)\n",
162
+    "#cartesian coordinate system\n",
163
+    "#구면 좌표계로 변경도 해보자...나중에...\n",
164
+    "#meter/(1/30)s\n",
165
+    "si_1['positionvectorvalue'] = (si_1['x']**2 + si_1['y']**2 + si_1['z']**2)"
166
+   ]
167
+  },
168
+  {
169
+   "cell_type": "code",
170
+   "execution_count": 4,
171
+   "metadata": {},
172
+   "outputs": [],
173
+   "source": [
174
+    "cal_buffer_1, cal_buffer_2 = si_1['positionvectorvalue'].copy(),  si_1['positionvectorvalue'].copy()\n",
175
+    "#print(cal_buffer_1,'\\n',cal_buffer_2)\n",
176
+    "cal_buffer_1, cal_buffer_2= cal_buffer_1.to_frame(), cal_buffer_2.to_frame()\n",
177
+    "cal_buffer_1 = list(cal_buffer_1.loc[:, 'positionvectorvalue'])\n",
178
+    "cal_buffer_2 = list(cal_buffer_2.loc[:, 'positionvectorvalue'])\n",
179
+    "cal_buffer_3 = [0.0]\n",
180
+    "for v in range(0, len(cal_buffer_2)-1):\n",
181
+    "    cal_buffer_3.append(cal_buffer_2[v])\n",
182
+    "buffer_a = np.array(cal_buffer_1)\n",
183
+    "buffer_b = np.array(cal_buffer_3)"
184
+   ]
185
+  },
186
+  {
187
+   "cell_type": "code",
188
+   "execution_count": 5,
189
+   "metadata": {},
190
+   "outputs": [],
191
+   "source": [
192
+    "velocity = (buffer_a - buffer_b)\n",
193
+    "for x in range(0, len(velocity)):\n",
194
+    "    velocity[x] = math.sqrt(abs(velocity[x]))\n",
195
+    "velocity[0]=0.0"
196
+   ]
197
+  },
198
+  {
199
+   "cell_type": "code",
200
+   "execution_count": 6,
201
+   "metadata": {},
202
+   "outputs": [],
203
+   "source": [
204
+    "#merge each list as Series\n",
205
+    "si_1['velocity'] = velocity\n",
206
+    "#print(si_1)"
207
+   ]
208
+  },
209
+  {
210
+   "cell_type": "code",
211
+   "execution_count": 7,
212
+   "metadata": {},
213
+   "outputs": [],
214
+   "source": [
215
+    "cal_buffer_4, cal_buffer_5 = si_1['velocity'].copy(),  si_1['velocity'].copy()\n",
216
+    "#print(cal_buffer_1,'\\n',cal_buffer_2)\n",
217
+    "cal_buffer_4, cal_buffer_5= cal_buffer_4.to_frame(), cal_buffer_5.to_frame()\n",
218
+    "cal_buffer_4 = list(cal_buffer_4.loc[:, 'velocity'])\n",
219
+    "cal_buffer_5 = list(cal_buffer_5.loc[:, 'velocity'])\n",
220
+    "cal_buffer_6 = [0.0]\n",
221
+    "for v in range(0, len(cal_buffer_5)-1):\n",
222
+    "    cal_buffer_6.append(cal_buffer_5[v])\n",
223
+    "buffer_c = np.array(cal_buffer_4)\n",
224
+    "buffer_d = np.array(cal_buffer_6)"
225
+   ]
226
+  },
227
+  {
228
+   "cell_type": "code",
229
+   "execution_count": 8,
230
+   "metadata": {},
231
+   "outputs": [],
232
+   "source": [
233
+    "acceleration = buffer_c - buffer_d\n",
234
+    "acceleration[1] = 0.0\n",
235
+    "#print(acceleration)"
236
+   ]
237
+  },
238
+  {
239
+   "cell_type": "code",
240
+   "execution_count": 9,
241
+   "metadata": {},
242
+   "outputs": [],
243
+   "source": [
244
+    "si_1['acceleration'] = acceleration\n",
245
+    "#print(si_1)"
246
+   ]
247
+  },
248
+  {
249
+   "attachments": {},
250
+   "cell_type": "markdown",
251
+   "metadata": {},
252
+   "source": [
253
+    "Calculate Velocity of all detected objects"
254
+   ]
255
+  },
256
+  {
257
+   "attachments": {},
258
+   "cell_type": "markdown",
259
+   "metadata": {},
260
+   "source": [
261
+    "\n",
262
+    "TODO: approx_total_frame 개수 만큼의 빈 컬럼 생성 (나중에 전체 데이터프레임은 20등분으로 잘릴 예정) -> done\n",
263
+    "실제 값이 존재하는 프레임을 찾고, 그 프레임을 인덱스에 맞추어 x,y,z 집어넣기\n",
264
+    "존재한다면 (x^2 + y^2 + z^2)^(1/3) 을 계산하여 새 컬럼에 저장\n",
265
+    "비 등속 보간 작업을 해야 하는데, 어떤 방식으로 할지 결정하기(piecewise_polynomial?)"
266
+   ]
267
+  },
268
+  {
269
+   "cell_type": "code",
270
+   "execution_count": 11,
271
+   "metadata": {},
272
+   "outputs": [
273
+    {
274
+     "data": {
275
+      "text/html": [
276
+       "<div>\n",
277
+       "<style scoped>\n",
278
+       "    .dataframe tbody tr th:only-of-type {\n",
279
+       "        vertical-align: middle;\n",
280
+       "    }\n",
281
+       "\n",
282
+       "    .dataframe tbody tr th {\n",
283
+       "        vertical-align: top;\n",
284
+       "    }\n",
285
+       "\n",
286
+       "    .dataframe thead th {\n",
287
+       "        text-align: right;\n",
288
+       "    }\n",
289
+       "</style>\n",
290
+       "<table border=\"1\" class=\"dataframe\">\n",
291
+       "  <thead>\n",
292
+       "    <tr style=\"text-align: right;\">\n",
293
+       "      <th></th>\n",
294
+       "      <th>x</th>\n",
295
+       "      <th>y</th>\n",
296
+       "      <th>z</th>\n",
297
+       "      <th>frame</th>\n",
298
+       "      <th>positionvectorvalue</th>\n",
299
+       "      <th>velocity</th>\n",
300
+       "      <th>acceleration</th>\n",
301
+       "    </tr>\n",
302
+       "  </thead>\n",
303
+       "  <tbody>\n",
304
+       "    <tr>\n",
305
+       "      <th>count</th>\n",
306
+       "      <td>62495.000000</td>\n",
307
+       "      <td>62495.000000</td>\n",
308
+       "      <td>62495.000000</td>\n",
309
+       "      <td>62495.000000</td>\n",
310
+       "      <td>62495.000000</td>\n",
311
+       "      <td>62495.000000</td>\n",
312
+       "      <td>62495.000000</td>\n",
313
+       "    </tr>\n",
314
+       "    <tr>\n",
315
+       "      <th>mean</th>\n",
316
+       "      <td>-0.202211</td>\n",
317
+       "      <td>-0.207365</td>\n",
318
+       "      <td>1.601385</td>\n",
319
+       "      <td>42805.432355</td>\n",
320
+       "      <td>2.733538</td>\n",
321
+       "      <td>0.138262</td>\n",
322
+       "      <td>0.000002</td>\n",
323
+       "    </tr>\n",
324
+       "    <tr>\n",
325
+       "      <th>std</th>\n",
326
+       "      <td>0.162759</td>\n",
327
+       "      <td>0.238811</td>\n",
328
+       "      <td>0.041177</td>\n",
329
+       "      <td>27211.098449</td>\n",
330
+       "      <td>0.162993</td>\n",
331
+       "      <td>0.120752</td>\n",
332
+       "      <td>0.124054</td>\n",
333
+       "    </tr>\n",
334
+       "    <tr>\n",
335
+       "      <th>min</th>\n",
336
+       "      <td>-0.750000</td>\n",
337
+       "      <td>-1.750000</td>\n",
338
+       "      <td>0.930000</td>\n",
339
+       "      <td>1.000000</td>\n",
340
+       "      <td>1.109630</td>\n",
341
+       "      <td>0.000000</td>\n",
342
+       "      <td>-1.650274</td>\n",
343
+       "    </tr>\n",
344
+       "    <tr>\n",
345
+       "      <th>25%</th>\n",
346
+       "      <td>-0.328348</td>\n",
347
+       "      <td>-0.377956</td>\n",
348
+       "      <td>1.574043</td>\n",
349
+       "      <td>19851.500000</td>\n",
350
+       "      <td>2.637680</td>\n",
351
+       "      <td>0.067702</td>\n",
352
+       "      <td>-0.045567</td>\n",
353
+       "    </tr>\n",
354
+       "    <tr>\n",
355
+       "      <th>50%</th>\n",
356
+       "      <td>-0.183048</td>\n",
357
+       "      <td>-0.240016</td>\n",
358
+       "      <td>1.598190</td>\n",
359
+       "      <td>38725.000000</td>\n",
360
+       "      <td>2.706999</td>\n",
361
+       "      <td>0.111651</td>\n",
362
+       "      <td>0.000205</td>\n",
363
+       "    </tr>\n",
364
+       "    <tr>\n",
365
+       "      <th>75%</th>\n",
366
+       "      <td>-0.096154</td>\n",
367
+       "      <td>-0.032186</td>\n",
368
+       "      <td>1.625495</td>\n",
369
+       "      <td>69024.500000</td>\n",
370
+       "      <td>2.793686</td>\n",
371
+       "      <td>0.168573</td>\n",
372
+       "      <td>0.045744</td>\n",
373
+       "    </tr>\n",
374
+       "    <tr>\n",
375
+       "      <th>max</th>\n",
376
+       "      <td>0.750000</td>\n",
377
+       "      <td>1.750000</td>\n",
378
+       "      <td>1.800000</td>\n",
379
+       "      <td>233066.000000</td>\n",
380
+       "      <td>6.039652</td>\n",
381
+       "      <td>1.941675</td>\n",
382
+       "      <td>1.692079</td>\n",
383
+       "    </tr>\n",
384
+       "  </tbody>\n",
385
+       "</table>\n",
386
+       "</div>"
387
+      ],
388
+      "text/plain": [
389
+       "                  x             y             z          frame   \n",
390
+       "count  62495.000000  62495.000000  62495.000000   62495.000000  \\\n",
391
+       "mean      -0.202211     -0.207365      1.601385   42805.432355   \n",
392
+       "std        0.162759      0.238811      0.041177   27211.098449   \n",
393
+       "min       -0.750000     -1.750000      0.930000       1.000000   \n",
394
+       "25%       -0.328348     -0.377956      1.574043   19851.500000   \n",
395
+       "50%       -0.183048     -0.240016      1.598190   38725.000000   \n",
396
+       "75%       -0.096154     -0.032186      1.625495   69024.500000   \n",
397
+       "max        0.750000      1.750000      1.800000  233066.000000   \n",
398
+       "\n",
399
+       "       positionvectorvalue      velocity  acceleration  \n",
400
+       "count         62495.000000  62495.000000  62495.000000  \n",
401
+       "mean              2.733538      0.138262      0.000002  \n",
402
+       "std               0.162993      0.120752      0.124054  \n",
403
+       "min               1.109630      0.000000     -1.650274  \n",
404
+       "25%               2.637680      0.067702     -0.045567  \n",
405
+       "50%               2.706999      0.111651      0.000205  \n",
406
+       "75%               2.793686      0.168573      0.045744  \n",
407
+       "max               6.039652      1.941675      1.692079  "
408
+      ]
409
+     },
410
+     "execution_count": 11,
411
+     "metadata": {},
412
+     "output_type": "execute_result"
413
+    }
414
+   ],
415
+   "source": [
416
+    "si_1.describe()"
417
+   ]
418
+  },
419
+  {
420
+   "cell_type": "code",
421
+   "execution_count": 12,
422
+   "metadata": {},
423
+   "outputs": [
424
+    {
425
+     "name": "stdout",
426
+     "output_type": "stream",
427
+     "text": [
428
+      "          x    y    z  frame  positionvectorvalue  velocity  acceleration   \n",
429
+      "478795  0.0  0.0  0.0    0.0                  0.0       0.0           0.0  \\\n",
430
+      "478796  0.0  0.0  0.0    0.0                  0.0       0.0           0.0   \n",
431
+      "478797  0.0  0.0  0.0    0.0                  0.0       0.0           0.0   \n",
432
+      "478798  0.0  0.0  0.0    0.0                  0.0       0.0           0.0   \n",
433
+      "478799  0.0  0.0  0.0    0.0                  0.0       0.0           0.0   \n",
434
+      "\n",
435
+      "        isValid  \n",
436
+      "478795      0.0  \n",
437
+      "478796      0.0  \n",
438
+      "478797      0.0  \n",
439
+      "478798      0.0  \n",
440
+      "478799      0.0  \n"
441
+     ]
442
+    }
443
+   ],
444
+   "source": [
445
+    "# create dataframe with 6 columns and create 0 value rows as many as approx_total_frame value in summary_1\n",
446
+    "j = 0\n",
447
+    "handletrace = pd.DataFrame(columns=['x', 'y', 'z', 'frame', 'positionvectorvalue', 'velocity', 'acceleration', 'isValid'])\n",
448
+    "handletrace = pd.concat([handletrace, pd.DataFrame(np.zeros((int(summary_1['approx_total_frame'][j]),8)), columns=['x', 'y', 'z', 'frame', 'positionvectorvalue','velocity', 'acceleration', 'isValid'])], axis=0)\n",
449
+    "print(handletrace.tail())"
450
+   ]
451
+  },
452
+  {
453
+   "cell_type": "code",
454
+   "execution_count": null,
455
+   "metadata": {},
456
+   "outputs": [],
457
+   "source": [
458
+    "#print(int(si_1['frame'][2]))\n",
459
+    "#print(si_1.loc[2])"
460
+   ]
461
+  },
462
+  {
463
+   "cell_type": "code",
464
+   "execution_count": 13,
465
+   "metadata": {},
466
+   "outputs": [],
467
+   "source": [
468
+    "# frame의 값에 해당하는 것으로 변경\n",
469
+    "for x in range(0, len(si_1)):\n",
470
+    "    handletrace.loc[int(si_1['frame'][x])] = si_1.loc[x]\n",
471
+    "handletrace['isValid'] = handletrace['isValid'].fillna(0)"
472
+   ]
473
+  },
474
+  {
475
+   "cell_type": "code",
476
+   "execution_count": 14,
477
+   "metadata": {},
478
+   "outputs": [
479
+    {
480
+     "name": "stdout",
481
+     "output_type": "stream",
482
+     "text": [
483
+      "               x         y         z  frame  positionvectorvalue  velocity   \n",
484
+      "0       0.000000  0.000000  0.000000    0.0             0.000000  0.000000  \\\n",
485
+      "1      -0.400997 -0.319101  1.632182    1.0             2.926644  0.000000   \n",
486
+      "2      -0.410969 -0.333815  1.629004    2.0             2.933981  0.085661   \n",
487
+      "3      -0.412393 -0.352207  1.634730    3.0             2.966459  0.180214   \n",
488
+      "4      -0.413818 -0.357725  1.638935    4.0             2.985322  0.137342   \n",
489
+      "...          ...       ...       ...    ...                  ...       ...   \n",
490
+      "478795  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
491
+      "478796  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
492
+      "478797  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
493
+      "478798  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
494
+      "478799  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
495
+      "\n",
496
+      "        acceleration  isValid  \n",
497
+      "0           0.000000      0.0  \n",
498
+      "1           0.000000      0.0  \n",
499
+      "2           0.000000      0.0  \n",
500
+      "3           0.094553      0.0  \n",
501
+      "4          -0.042873      0.0  \n",
502
+      "...              ...      ...  \n",
503
+      "478795      0.000000      0.0  \n",
504
+      "478796      0.000000      0.0  \n",
505
+      "478797      0.000000      0.0  \n",
506
+      "478798      0.000000      0.0  \n",
507
+      "478799      0.000000      0.0  \n",
508
+      "\n",
509
+      "[478800 rows x 8 columns]\n"
510
+     ]
511
+    }
512
+   ],
513
+   "source": [
514
+    "print(handletrace)"
515
+   ]
516
+  },
517
+  {
518
+   "cell_type": "code",
519
+   "execution_count": null,
520
+   "metadata": {},
521
+   "outputs": [],
522
+   "source": [
523
+    "'''import plotly.graph_objects as go\n",
524
+    "\n",
525
+    "#Create Figure\n",
526
+    "fig = go.Figure()\n",
527
+    "fig.add_trace(go.Scatter( x=list(handletrace.index), y=list(handletrace['velocity'] * 30)))\n",
528
+    "\n",
529
+    "#Set Title\n",
530
+    "fig.update_layout(title_text='Velocity per second by frame')\n",
531
+    "\n",
532
+    "#Add range slider\n",
533
+    "fig.update_layout(\n",
534
+    "    xaxis=dict(\n",
535
+    "        rangeslider=dict(\n",
536
+    "            visible=True\n",
537
+    "        )\n",
538
+    "    )\n",
539
+    ")'''"
540
+   ]
541
+  },
542
+  {
543
+   "cell_type": "code",
544
+   "execution_count": null,
545
+   "metadata": {},
546
+   "outputs": [],
547
+   "source": [
548
+    "'''import plotly.graph_objects as go\n",
549
+    "\n",
550
+    "#Create Figure\n",
551
+    "fig = go.Figure()\n",
552
+    "fig.add_trace(go.Scatter( x=list(handletrace.index), y=list(handletrace['acceleration']*900)))\n",
553
+    "\n",
554
+    "#Set Title\n",
555
+    "fig.update_layout(title_text='acceleration per second^2 by frame')\n",
556
+    "\n",
557
+    "#Add range slider\n",
558
+    "fig.update_layout(\n",
559
+    "    xaxis=dict(\n",
560
+    "        rangeslider=dict(\n",
561
+    "            visible=True\n",
562
+    "        )\n",
563
+    "    )\n",
564
+    ")'''"
565
+   ]
566
+  },
567
+  {
568
+   "cell_type": "code",
569
+   "execution_count": 15,
570
+   "metadata": {},
571
+   "outputs": [],
572
+   "source": [
573
+    "#comapre the value of handletrace['acceleration'] and if it is between -25 and 25, then set handletrace['isValid'] to 1\n",
574
+    "for x in range(0, len(handletrace)):\n",
575
+    "    if (-25/900) < handletrace['acceleration'][x] < (25/900):\n",
576
+    "        handletrace['isValid'][x] = 1\n",
577
+    "    else:\n",
578
+    "        handletrace['isValid'][x] = 0"
579
+   ]
580
+  },
581
+  {
582
+   "cell_type": "code",
583
+   "execution_count": 16,
584
+   "metadata": {},
585
+   "outputs": [
586
+    {
587
+     "name": "stdout",
588
+     "output_type": "stream",
589
+     "text": [
590
+      "[1. 0.]\n",
591
+      "isValid\n",
592
+      "1.0    437730\n",
593
+      "0.0     41070\n",
594
+      "Name: count, dtype: int64\n"
595
+     ]
596
+    }
597
+   ],
598
+   "source": [
599
+    "print(handletrace['isValid'].unique())\n",
600
+    "print(handletrace['isValid'].value_counts())"
601
+   ]
602
+  },
603
+  {
604
+   "cell_type": "code",
605
+   "execution_count": 17,
606
+   "metadata": {},
607
+   "outputs": [
608
+    {
609
+     "name": "stdout",
610
+     "output_type": "stream",
611
+     "text": [
612
+      "               x         y         z  frame  positionvectorvalue  velocity   \n",
613
+      "0       0.000000  0.000000  0.000000    0.0             0.000000  0.000000  \\\n",
614
+      "1      -0.400997 -0.319101  1.632182    1.0             2.926644  0.000000   \n",
615
+      "2      -0.410969 -0.333815  1.629004    2.0             2.933981  0.085661   \n",
616
+      "3      -0.412393 -0.352207  1.634730    3.0             2.966459  0.180214   \n",
617
+      "4      -0.413818 -0.357725  1.638935    4.0             2.985322  0.137342   \n",
618
+      "...          ...       ...       ...    ...                  ...       ...   \n",
619
+      "478795  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
620
+      "478796  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
621
+      "478797  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
622
+      "478798  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
623
+      "478799  0.000000  0.000000  0.000000    0.0             0.000000  0.000000   \n",
624
+      "\n",
625
+      "        acceleration  isValid  \n",
626
+      "0           0.000000      1.0  \n",
627
+      "1           0.000000      1.0  \n",
628
+      "2           0.000000      1.0  \n",
629
+      "3           0.094553      0.0  \n",
630
+      "4          -0.042873      0.0  \n",
631
+      "...              ...      ...  \n",
632
+      "478795      0.000000      1.0  \n",
633
+      "478796      0.000000      1.0  \n",
634
+      "478797      0.000000      1.0  \n",
635
+      "478798      0.000000      1.0  \n",
636
+      "478799      0.000000      1.0  \n",
637
+      "\n",
638
+      "[478800 rows x 8 columns]\n"
639
+     ]
640
+    }
641
+   ],
642
+   "source": [
643
+    "print(handletrace)"
644
+   ]
645
+  },
646
+  {
647
+   "cell_type": "code",
648
+   "execution_count": null,
649
+   "metadata": {},
650
+   "outputs": [],
651
+   "source": []
652
+  }
653
+ ],
654
+ "metadata": {
655
+  "kernelspec": {
656
+   "display_name": "Python 3",
657
+   "language": "python",
658
+   "name": "python3"
659
+  },
660
+  "language_info": {
661
+   "codemirror_mode": {
662
+    "name": "ipython",
663
+    "version": 3
664
+   },
665
+   "file_extension": ".py",
666
+   "mimetype": "text/x-python",
667
+   "name": "python",
668
+   "nbconvert_exporter": "python",
669
+   "pygments_lexer": "ipython3",
670
+   "version": "3.8.0"
671
+  },
672
+  "vscode": {
673
+   "interpreter": {
674
+    "hash": "df0893f56f349688326838aaeea0de204df53a132722cbd565e54b24a8fec5f6"
675
+   }
676
+  }
677
+ },
678
+ "nbformat": 4,
679
+ "nbformat_minor": 2
680
+}

読み込み中…
キャンセル
保存