Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. class Stage_jumpingjack:
  2. className='jumpingjack'
  3. depth=7
  4. labels=['legs_opened', 'closing_legs', 'legs_closed', 'opening_legs']
  5. modelPath = rootPath+'/Algoritmo Contagem/Action Stage/saved models/jumpingjack/20200219_181822__stage-model_acc0.887.h5'
  6. inputShape=(1,7,70,1)
  7. memoryMostCommon=1
  8.  
  9. def __init__(self):
  10. self.lastPose=0 #saves in which pose(int) was the last stage inference made for this particular action
  11. self.lastStages=[] #saves the last stages(int) for this particular action
  12. self.model = StageModel(self.modelPath,self.labels)
  13. #count variables
  14. self.ascendingCounter=0
  15. self.descendingCounter=0 #when it equals ascending counter target, resets ascending counter to 0
  16. self.count=0
  17.  
  18. self.ascendingTarget=2
  19. self.ascendingLabels=['closing_legs','legs_closed']
  20. self.descendingTarget=1
  21. self.descendingLabels=['opening_legs','legs_opened'] #labels responsible for descending
  22.  
  23. def getLaggingStages(self,currentPose):
  24. '''
  25. Returns the number of stages that are going to be predicted.
  26. '''
  27.  
  28. laggingStages = currentPose-self.lastPose #time since last stage inference
  29. laggingStages = min(laggingStages,ActionClass.actionDepth)
  30. print(f"number of lagging stages: {laggingStages}")
  31. return laggingStages
  32.  
  33. def removeOldStages(self,laggingStages):
  34. '''
  35. Updates lastStages (N=ActionClass.actionDepth). Removes stages that were previously predicted but are now outdated
  36.  
  37. '''
  38. #updates last stages list before introducing new stages
  39. if laggingStages==ActionClass.actionDepth:
  40. self.lastStages=[]
  41. else:
  42. self.lastStages=self.lastStages[-(ActionClass.actionDepth-laggingStages):]
  43.  
  44.  
  45. def updateStageAndCount(self,currentPose,poseCoords,poseProb):
  46. #Get number of stages that are going to be infered
  47. laggingStages=self.getLaggingStages(currentPose)
  48.  
  49. #Remove past inferred stages that are no longer needed
  50. self.removeOldStages(laggingStages)
  51.  
  52.  
  53. #introduces new stages to lastStages list
  54. for i in range(laggingStages,0,-1): #fazer updates dos lagging stages
  55. time_1=time.perf_counter()
  56. features = self.getFeatures_teste7(poseCoords[-i-self.depth:-i],poseProb[-i-self.depth:-i])
  57. #print(f"time1 features : "+str(np.round(time.perf_counter()-time_1,5)))
  58. time_2=time.perf_counter()
  59. stageIndex = self.model.predict(np.array(features).reshape(self.inputShape))[1]
  60. #print(f"time2 predict : "+str(np.round(time.perf_counter()-time_2,5)))
  61. time_3=time.perf_counter()
  62. self.lastStages.append(stageIndex)
  63. mostCommonIndex=self.getMostCommon(self.lastStages)
  64. self.updateCount(mostCommonIndex)
  65. #print(f"time3 count : "+str(np.round(time.perf_counter()-time_3,5)))
  66.  
  67. print(f"pose {currentPose}: updated last {laggingStages}. {self.lastStages}")
  68.  
  69. self.lastPose=currentPose
  70.  
  71. def getMostCommon(self,predictions=None,memoryMostCommon=None):
  72. if predictions==None:
  73. try: #if variable already exists, mostCommon was previously calculated
  74. return self.mostCommon
  75. except:
  76. return None
  77.  
  78. if memoryMostCommon==None:
  79. memoryMostCommon=self.memoryMostCommon
  80.  
  81. predictions=predictions[-memoryMostCommon:]
  82. countLastMaxClasses=[] #array that stores the number of times that class appeared in the last processed frames
  83. for i in range(0,len(action.classesString)):
  84. countLastMaxClasses.append(0)
  85. maxValue=0
  86. mostCommonClass=0
  87.  
  88. for indexMaxClass in predictions:
  89. countLastMaxClasses[indexMaxClass]+=1
  90. if countLastMaxClasses[indexMaxClass] >= maxValue:
  91. maxValue=countLastMaxClasses[indexMaxClass]
  92. mostCommonClass=indexMaxClass
  93.  
  94. return mostCommonClass
  95.  
  96.  
  97. def updateCount(self,mostCommonIndex):
  98. predictionLabel=self.labels[mostCommonIndex]
  99. if predictionLabel in self.ascendingLabels:
  100. self.ascendingCounter+=1
  101. if self.ascendingCounter==self.ascendingTarget:
  102. self.count+=1
  103. self.descendingCounter=0
  104. elif predictionLabel in self.descendingLabels:
  105. self.descendingCounter+=1
  106. if self.descendingCounter==self.descendingTarget:
  107. self.ascendingCounter=0
  108.  
  109.  
  110.  
  111.  
  112. #x/y, r/teta sequenciais, prob newKP
  113. def getFeatures_teste7(self,coords,prob,iFrame=None,step=1):
  114. '''
  115. Attributes:
  116. @step: represents the gap of frames in @coords between consecutive keypoints ("new keypoints" and "last keypoints".
  117.  
  118. '''
  119. if iFrame==None:
  120. iFrame=len(coords)-1
  121.  
  122. newFeatures=[]
  123. for i in range(0,self.depth):
  124. newIndex=iFrame-step*i
  125. prevIndex=iFrame-step*(i+1)
  126. try:
  127. newKeyPointsCoords=coords[newIndex]
  128. except:
  129. print("len(newKeyPointsCoords): "+str(len(coords)))
  130. print("len(newKeyPointsCoords[0]): "+str(len(coords[0])))
  131. print("iFrame: "+str(iFrame))
  132. print("newIndex: "+str(newIndex))
  133.  
  134. previousKeyPointsCoords=coords[prevIndex]
  135. newKeyPointsProb=prob[newIndex]
  136.  
  137. #newKeyPointsCoords=PreProcessing.convertListToInt(newKeyPointsCoords)
  138. #previousKeyPointsCoords=PreProcessing.convertListToInt(previousKeyPointsCoords)
  139. if type(newKeyPointsCoords) is list:
  140. newKeyPointsCoords=np.array(newKeyPointsCoords).reshape(14,2)
  141. if type(previousKeyPointsCoords) is list:
  142. previousKeyPointsCoords=np.array(previousKeyPointsCoords).reshape(14,2)
  143. if type(newKeyPointsProb) is list:
  144. newKeyPointsProb=np.array(newKeyPointsProb)
  145.  
  146. for i in range(0,StageHandler.numberKeyPoints):
  147. newFeatures.append(newKeyPointsCoords[i][0]/StageHandler.poseOutputShape[0])
  148. newFeatures.append(newKeyPointsCoords[i][1]/StageHandler.poseOutputShape[1])
  149.  
  150. for i in range(0,StageHandler.numberKeyPoints):
  151. r = math.sqrt((newKeyPointsCoords[i][0]-previousKeyPointsCoords[i][0])**2 + (newKeyPointsCoords[i][1]-previousKeyPointsCoords[i][1])**2)/math.sqrt(2*StageHandler.poseOutputShape[0]*StageHandler.poseOutputShape[1])
  152. teta = np.arctan2(newKeyPointsCoords[i][0]-0 -(previousKeyPointsCoords[i][0]-0),newKeyPointsCoords[i][1]-previousKeyPointsCoords[i][1])/math.pi
  153.  
  154. newFeatures.append(r)
  155. newFeatures.append(teta)
  156. for i in range(0,StageHandler.numberKeyPoints):
  157. newFeatures.append(newKeyPointsProb[i])
  158.  
  159. return newFeatures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement