Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Stage_pushup:
- className='pushup'
- depth=7
- labels=['top', 'descending', 'low', 'ascending']
- modelPath = rootPath+'/Algoritmo Contagem/Action Stage/saved models/pushup/20200221_193425__stage-model_acc0.81.h5'
- inputShape=(1,7,70,1)
- memoryMostCommon=4
- def __init__(self):
- self.lastPose=0 #saves in which pose(int) was the last stage inference made for this particular action
- self.lastStages=[] #saves the last stages(int) for this particular action
- self.model = StageModel(self.modelPath,self.labels)
- #count variables
- self.ascendingCounter=0
- self.descendingCounter=0 #when it equals ascending counter target, resets ascending counter to 0
- self.count=0
- self.ascendingTarget=3
- self.ascendingLabels=['ascending','top']
- self.descendingTarget=1
- self.descendingLabels=['descending','low'] #labels responsible for descending
- def getLaggingStages(self,currentPose):
- '''
- Returns the number of stages that are going to be predicted.
- '''
- laggingStages = currentPose-self.lastPose #time since last stage inference
- laggingStages = min(laggingStages,ActionClass.actionDepth)
- print(f"number of lagging stages: {laggingStages}")
- return laggingStages
- def removeOldStages(self,laggingStages):
- '''
- Updates lastStages (N=ActionClass.actionDepth). Removes stages that were previously predicted but are now outdated
- '''
- #updates last stages list before introducing new stages
- if laggingStages==ActionClass.actionDepth:
- self.lastStages=[]
- else:
- self.lastStages=self.lastStages[-(ActionClass.actionDepth-laggingStages):]
- def updateStageAndCount(self,currentPose,poseCoords,poseProb):
- #Get number of stages that are going to be infered
- laggingStages=self.getLaggingStages(currentPose)
- #Remove past inferred stages that are no longer needed
- self.removeOldStages(laggingStages)
- #introduces new stages to lastStages list
- for i in range(laggingStages,0,-1): #fazer updates dos lagging stages
- time_1=time.perf_counter()
- features = self.getFeatures_teste7(poseCoords[-i-self.depth:-i],poseProb[-i-self.depth:-i])
- #print(f"time1 features : "+str(np.round(time.perf_counter()-time_1,5)))
- time_2=time.perf_counter()
- stageIndex = self.model.predict(np.array(features).reshape(self.inputShape))[1]
- #print(f"time2 predict : "+str(np.round(time.perf_counter()-time_2,5)))
- time_3=time.perf_counter()
- self.lastStages.append(stageIndex)
- mostCommonIndex=self.getMostCommon(self.lastStages)
- self.updateCount(mostCommonIndex)
- #print(f"time3 count : "+str(np.round(time.perf_counter()-time_3,5)))
- print(f"pose {currentPose}: updated last {laggingStages}. {self.lastStages}")
- self.lastPose=currentPose
- def getMostCommon(self,predictions=None,memoryMostCommon=None):
- if predictions==None:
- try: #if variable already exists, mostCommon was previously calculated
- return self.mostCommon
- except:
- return None
- if memoryMostCommon==None:
- memoryMostCommon=self.memoryMostCommon
- predictions=predictions[-memoryMostCommon:]
- countLastMaxClasses=[] #array that stores the number of times that class appeared in the last processed frames
- for i in range(0,len(action.classesString)):
- countLastMaxClasses.append(0)
- maxValue=0
- mostCommonClass=0
- for indexMaxClass in predictions:
- countLastMaxClasses[indexMaxClass]+=1
- if countLastMaxClasses[indexMaxClass] >= maxValue:
- maxValue=countLastMaxClasses[indexMaxClass]
- mostCommonClass=indexMaxClass
- return mostCommonClass
- def updateCount(self,mostCommonIndex):
- predictionLabel=self.labels[mostCommonIndex]
- if predictionLabel in self.ascendingLabels:
- self.ascendingCounter+=1
- if self.ascendingCounter==self.ascendingTarget:
- self.count+=1
- self.descendingCounter=0
- elif predictionLabel in self.descendingLabels:
- self.descendingCounter+=1
- if self.descendingCounter==self.descendingTarget:
- self.ascendingCounter=0
- #x/y, r/teta sequenciais, prob newKP
- def getFeatures_teste7(self,coords,prob,iFrame=None,step=1):
- '''
- Attributes:
- @step: represents the gap of frames in @coords between consecutive keypoints ("new keypoints" and "last keypoints".
- '''
- if iFrame==None:
- iFrame=len(coords)-1
- newFeatures=[]
- for i in range(0,self.depth):
- newIndex=iFrame-step*i
- prevIndex=iFrame-step*(i+1)
- try:
- newKeyPointsCoords=coords[newIndex]
- except:
- print("len(newKeyPointsCoords): "+str(len(coords)))
- print("len(newKeyPointsCoords[0]): "+str(len(coords[0])))
- print("iFrame: "+str(iFrame))
- print("newIndex: "+str(newIndex))
- previousKeyPointsCoords=coords[prevIndex]
- newKeyPointsProb=prob[newIndex]
- #newKeyPointsCoords=PreProcessing.convertListToInt(newKeyPointsCoords)
- #previousKeyPointsCoords=PreProcessing.convertListToInt(previousKeyPointsCoords)
- if type(newKeyPointsCoords) is list:
- newKeyPointsCoords=np.array(newKeyPointsCoords).reshape(14,2)
- if type(previousKeyPointsCoords) is list:
- previousKeyPointsCoords=np.array(previousKeyPointsCoords).reshape(14,2)
- if type(newKeyPointsProb) is list:
- newKeyPointsProb=np.array(newKeyPointsProb)
- for i in range(0,StageHandler.numberKeyPoints):
- newFeatures.append(newKeyPointsCoords[i][0]/StageHandler.poseOutputShape[0])
- newFeatures.append(newKeyPointsCoords[i][1]/StageHandler.poseOutputShape[1])
- for i in range(0,StageHandler.numberKeyPoints):
- 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])
- teta = np.arctan2(newKeyPointsCoords[i][0]-0 -(previousKeyPointsCoords[i][0]-0),newKeyPointsCoords[i][1]-previousKeyPointsCoords[i][1])/math.pi
- newFeatures.append(r)
- newFeatures.append(teta)
- for i in range(0,StageHandler.numberKeyPoints):
- newFeatures.append(newKeyPointsProb[i])
- return newFeatures
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement