Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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