代码只进行了18次循环处理俩个人18个关键点的距离。单进程耗时0.001秒,多进程耗时6.34秒。
import cv2 import numpy as np from modules.keypoints import BODY_PARTS_KPT_IDS, BODY_PARTS_PAF_IDS from modules.one_euro_filter import OneEuroFilter import pandas as pd class Pose: num_kpts = 18 kpt_names = ['nose', 'neck', 'r_sho', 'r_elb', 'r_wri', 'l_sho', 'l_elb', 'l_wri', 'r_hip', 'r_knee', 'r_ank', 'l_hip', 'l_knee', 'l_ank', 'r_eye', 'l_eye', 'r_ear', 'l_ear'] sigmas = np.array([.26, .79, .79, .72, .62, .79, .72, .62, 1.07, .87, .89, 1.07, .87, .89, .25, .25, .35, .35], dtype=np.float32) / 10.0 vars = (sigmas * 2) ** 2 last_id = -1 color = [0, 224, 255]def __init__(self, keypoints, confidence): super().__init__() self.all_save_image=0 self.keypoints = keypoints self.confidence = confidence self.bbox = Pose.get_bbox(self.keypoints) self.id = None self.filters = [[OneEuroFilter(), OneEuroFilter()] for _ in range(Pose.num_kpts)]
@staticmethod def get_bbox(keypoints): found_keypoints = np.zeros((np.count_nonzero(keypoints[:, 0] != -1), 2), dtype=np.int32) found_kpt_id = 0 for kpt_id in range(Pose.num_kpts): if keypoints[kpt_id, 0] == -1: continue found_keypoints[found_kpt_id] = keypoints[kpt_id] found_kpt_id += 1 bbox = cv2.boundingRect(found_keypoints)
return bbox
def update_id(self, id=None): self.id = id if self.id is None: self.id = Pose.last_id + 1 Pose.last_id += 1 def update_all(self, id=None): self.all_save_image = id if self.all_save_image is None: self.all_save_image = 0
def draw(self, img): assert self.keypoints.shape == (Pose.num_kpts, 2)
for part_id in range(len(BODY_PARTS_PAF_IDS) - 2): kpt_a_id = BODY_PARTS_KPT_IDS[part_id][0] global_kpt_a_id = self.keypoints[kpt_a_id, 0] if global_kpt_a_id != -1: x_a, y_a = self.keypoints[kpt_a_id] cv2.circle(img, (int(x_a), int(y_a)), 3, Pose.color, -1) kpt_b_id = BODY_PARTS_KPT_IDS[part_id][1] global_kpt_b_id = self.keypoints[kpt_b_id, 0] if global_kpt_b_id != -1: x_b, y_b = self.keypoints[kpt_b_id] cv2.circle(img, (int(x_b), int(y_b)), 3, Pose.color, -1) if global_kpt_a_id != -1 and global_kpt_b_id != -1: cv2.line(img, (int(x_a), int(y_a)), (int(x_b), int(y_b)), Pose.color, 2)
def get_similarity(a, b, threshold=0.5): num_similar_kpt = 0 for kpt_id in range(Pose.num_kpts): if a.keypoints[kpt_id, 0] != -1 and b.keypoints[kpt_id, 0] != -1: distance = np.sum((a.keypoints[kpt_id] - b.keypoints[kpt_id]) ** 2) area = max(a.bbox[2] * a.bbox[3], b.bbox[2] * b.bbox[3]) similarity = np.exp(-distance / (2 * (area + np.spacing(1)) * Pose.vars[kpt_id])) if similarity > threshold: num_similar_kpt += 1 return num_similar_kpt
import time import numpy as np from concurrent.futures import ProcessPoolExecutor
def s(kpt_id,threshold=0.5): if a.keypoints[kpt_id, 0] != -1 and b.keypoints[kpt_id, 0] != -1: distance = np.sum((a.keypoints[kpt_id] - b.keypoints[kpt_id]) ** 2) area = max(a.bbox[2] * a.bbox[3], b.bbox[2] * b.bbox[3]) similarity = np.exp(-distance / (2 * (area + np.spacing(1)) * Pose.vars[kpt_id])) if similarity > threshold: return 1 if __name__=='__main__': keypoint=np.random.random((18,2)) pose=Pose(keypoint,1) a=pose b=pose start=time.time() get_similarity(pose,pose) end=time.time() print(end-start)
ind=18 num_similar_kpt=0 start=time.time() with ProcessPoolExecutor() as pool: results=pool.map(s,[i for i in range(ind)]) end=time.time() sum1=end-start print(sum1)
标签:kpt,度展,18,self,Pose,keypoints,np,进程,id From: https://www.cnblogs.com/hahaah/p/17369235.html