box匹配

box匹配,第1张

box匹配
def dist(point1, point2):
    return (point1[0]-point2[0])**2 + (point1[1]-point2[1])**2

def find_min_dist(center_xy, bbox_list):
    min_dist = 10000
    min_idx = -1
    for idx, bbox in enumerate(bbox_list):
        x1,y1,x2,y2 = bbox[0]
        center_x = (x1+x2)/2
        center_y = (y1+y2)/2
        dist_val = dist(center_xy, [center_x, center_y])
        if dist_val < min_dist:
            min_dist = dist_val
            min_idx = idx
    return min_idx,min_dist



def get_im_landmark_list_json(json_path, img_root_path):
    # pdb.set_trace()
    img_id_dict, img_id_box_class_dict = get_imgname_id_box_dict(json_path)
    img_name_list = os.listdir(img_root_path)
    ret_landmark_list = []
    lable_list = []
    for img_name in img_name_list:
        img_path = os.path.join(img_root_path, img_name)
        if img_name not in img_id_dict:
            print(f"img_id_dict error, img_name={img_id_dict}")
            exit(1)
        # pdb.set_trace()
        img_id = img_id_dict[img_name]
        bbox_list = img_id_box_class_dict[img_id]

        image = cv2.imread(img_path)
        results = hands.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
        image_width, image_height = image.shape[1], image.shape[0]

        
        if results.multi_hand_landmarks :
            if len(results.multi_hand_landmarks) == len(bbox_list):
                for hand_idx, hand_landmarks in enumerate(results.multi_hand_landmarks):
                    landmark_x_list = []
                    landmark_y_list = []
                    landmark_list = []
                    for _, landmark in enumerate(hand_landmarks.landmark):
                        landmark_x = min(int(landmark.x * image_width), image_width - 1)
                        landmark_y = min(int(landmark.y * image_height), image_height - 1)
                        landmark_x_list.append(landmark_x)
                        landmark_y_list.append(landmark_y)
                        landmark_list.append([landmark_x, landmark_y])
                    
                    pre_processed_landmark_list = pre_process_landmark(landmark_list)
                    mean_x = sum(landmark_x_list)/len(landmark_x_list)
                    mean_y = sum(landmark_y_list)/len(landmark_y_list)
                    min_idx, _ = util.find_min_dist([mean_x, mean_y], bbox_list)
                    class_id = bbox_list[min_idx][1]
                    if len(pre_processed_landmark_list) >0:
                        ret_landmark_list.append(pre_processed_landmark_list)
                        lable_list.append([class_id])
                    # [mean_x, mean_y, mean_x+10, mean_y+10]
                    # pdb.set_trace()
                    print(bbox_list[min_idx][0])
                    # image = draw_box([int(mean_x), int(mean_y), int(mean_x+10), int(mean_y+10)], image, class_name=f"center_{hand_idx}", color = (0, 255, 0))
                    # image = draw_box(bbox_list[min_idx][0], image, class_name=f"{hand_idx}", color = (0, 255, 0))
                    mp_drawing.draw_landmarks(image, hand_landmarks, mp_hands.HAND_CONNECTIONS)
            else:
                print(f"gt box num and keypoint box num is not eque:{len(results.multi_hand_landmarks)}-{len(bbox_list)}")
        
        cv2.imwrite(f'annssswefw.jpg', image)
    return ret_landmark_list, lable_list       

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5521588.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存