-
-
Save pramod-wick/7338033c8ce03285cc6e2662f746da56 to your computer and use it in GitHub Desktop.
python keypoints
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import norfair | |
import numpy as np | |
import time | |
from random import seed | |
from random import random | |
detection_threshold = 0.2 | |
keypoint_dist_threshold = 1080 / 30 | |
seed(1) | |
def keypoints_distance(detected_pose, tracked_pose): | |
distances = np.linalg.norm(detected_pose.points - tracked_pose.estimate, axis=1) | |
match_num = np.count_nonzero( (distances < keypoint_dist_threshold) | |
* (detected_pose.scores > detection_threshold) | |
* (tracked_pose.last_detection.scores > detection_threshold) | |
) | |
return 1 / (1 + match_num) | |
tracker = norfair.Tracker(distance_function=keypoints_distance, distance_threshold=0.3, detection_threshold=0.2) | |
def pose_format(keypoints,width,height): # convert keypoints into required format | |
convertkp=[] | |
pose_score = [] | |
for kp in keypoints: | |
if (kp[1]!=0 or kp[2]!=0): | |
convertkp.append([kp[2]*width,kp[1]*height]) | |
pose_score.append(1.0) | |
else: | |
convertkp.append([0,0]) | |
pose_score.append(0.0) | |
convertkp = np.array(convertkp, dtype=np.float32) | |
pose_score = np.array(pose_score, dtype=np.float32) | |
pose_results = [] | |
xmin = np.nanmin( convertkp[:, 0][convertkp[:, 0] > 0]) | |
xmax = np.nanmax(convertkp[:, 0]) | |
ymin = np.nanmin(convertkp[:, 1][convertkp[:, 1] > 0]) | |
ymax = np.nanmax(convertkp[:, 1]) | |
area = (xmax - xmin) * (ymax - ymin) | |
pose_results.append({'keypoints': convertkp[:, :3],'score': 0.8, 'area': area, 'pose_score': pose_score, }) | |
return pose_results | |
def fakekeypoints(): # generate keypoints | |
time.sleep(0.05) # fake inference time | |
# fake 18 keypoints for one person | |
kp=[(0, 0, 0), (1, 0, 0), (2, 0, 0), (3, 0.3575015366077423, 0.27122947573661804), | |
(4, 0.35617896914482117, 0.2848164737224579), (5, 0.38864418864250183, 0.2681225538253784), | |
(6, 0.3827485740184784, 0.2904510200023651), (7, 0.4303573668003082, 0.26537251472473145), | |
(8, 0.41974005103111267, 0.3006589412689209), (9, 0.45867863297462463, 0.27195972204208374), | |
(10, 0.4473567605018616, 0.3024648427963257), (11, 0.46456748247146606, 0.2804908752441406), | |
(12, 0.4614618420600891, 0.2982827126979828), (13, 0.5258688926696777, 0.29282426834106445), | |
(14, 0.5186691284179688, 0.31580889225006104), (15, 0.5790600776672363, 0.3047087490558624), | |
(16, 0.573983907699585, 0.3295579254627228), (17, 0.3861940801143646, 0.2790219783782959)] | |
idx=0 | |
for _ in kp: # add some noise | |
kp[idx]=(kp[idx][0], kp[idx][1]+(random()/10), kp[idx][2]+(random()/10)) | |
idx=idx+1 | |
width=1920 | |
height=1080 | |
results=pose_format(kp,width,height) | |
return results | |
prev_frame_time = 0 | |
new_frame_time = 0 | |
enable_tracker=False | |
if __name__ == '__main__': | |
while(1): | |
results=fakekeypoints() | |
if enable_tracker: | |
keypoint_dist_threshold = 1080 / 30 #img.shape[0] / 30 | |
detections = [norfair.Detection(p['keypoints'][:,0:2], scores=p['pose_score']) for p in results ] | |
tracked_objects = tracker.update(detections=detections) | |
#norfair.draw_tracked_objects(image, tracked_objects) | |
new_frame_time = time.time() | |
fps = 1 / (new_frame_time - prev_frame_time) | |
prev_frame_time = new_frame_time | |
print("fps is ", fps) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment