调用百度人体关键点识别API

  1. 首先在百度智能云上建立对应的应用,生成AppID、API Key和Secret Key(需要申请免费资源,或者购买额度后才能使用)。也可以从百度AI开放平台-控制台,找到对应的产品服务进入。
  2. 单张调用生成结果如下:
import cv2
from aip import AipBodyAnalysis

class BaiduAIP(object):
    def __init__(self):
        APP_ID = ''
        API_KEY = ''
        SECRET_KEY = ''
        self.client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)

    def bodyAnalysis(self,img_jpg):
        etval, buffer = cv2.imencode('.jpg', img_jpg)
        result = self.client.bodyAnalysis(buffer) #内部把buffer转换为base64了
        return result

def image_pose():
    baiduapi = BaiduAIP()
    img_path = 'E:\\test.jpg'
    img = cv2.imread(img_path)
    d = baiduapi.bodyAnalysis(img)
    print(d["person_num"])
    print(d["log_id"])
    person = d["person_info"]
    for  p  in  person:
        draw_line(img, p['body_parts'], 'ok')
        draw_point(img, p['body_parts'], 'ok')
        draw_box(img, p['location'], 'beauty')

    cv2.imshow("image1.jpg", img)
    cv2.waitKey(0)

其中,画图的代码如下所示:

def draw_line(img, dic, text):

    color = (0, 255, 0)
    thickness = 2
    if (text == 'warn'):
        color = (0, 0, 255)
    
      # nose ---> neck
    if dic['nose']['score'] > 0.2 and dic['neck']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['nose']['x']), int(dic['nose']['y'])),
                 (int(dic['neck']['x']), int(dic['neck']['y'])), color, thickness)
      # neck --> left_shoulder
    if dic['neck']['score'] > 0.2 and dic['left_shoulder']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['neck']['x']), int(dic['neck']['y'])),
                 (int(dic['left_shoulder']['x']), int(dic['left_shoulder']['y'])), color, thickness)
      # neck --> right_shoulder
    if dic['neck']['score'] > 0.2 and dic['right_shoulder']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['neck']['x']), int(dic['neck']['y'])),
                 (int(dic['right_shoulder']['x']), int(dic['right_shoulder']['y'])), color, thickness)
      # left_shoulder --> left_elbow
    if dic['left_shoulder']['score'] > 0.2 and dic['left_elbow']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['left_shoulder']['x']), int(dic['left_shoulder']['y'])),
                 (int(dic['left_elbow']['x']), int(dic['left_elbow']['y'])), color, thickness)
      # left_elbow --> left_wrist
    if dic['left_elbow']['score'] > 0.2 and dic['left_wrist']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['left_elbow']['x']), int(dic['left_elbow']['y'])),
                 (int(dic['left_wrist']['x']), int(dic['left_wrist']['y'])), color, thickness)
      # right_shoulder --> right_elbow
    if dic['right_shoulder']['score'] > 0.2 and dic['right_elbow']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['right_shoulder']['x']), int(dic['right_shoulder']['y'])),
                 (int(dic['right_elbow']['x']), int(dic['right_elbow']['y'])), color, thickness)
      # right_elbow --> right_wrist
    if dic['right_elbow']['score'] > 0.2 and dic['right_wrist']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['right_elbow']['x']), int(dic['right_elbow']['y'])),
                 (int(dic['right_wrist']['x']), int(dic['right_wrist']['y'])), color, thickness)
      # neck --> left_hip
    if dic['neck']['score'] > 0.2 and dic['left_hip']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['neck']['x']), int(dic['neck']['y'])),
                 (int(dic['left_hip']['x']), int(dic['left_hip']['y'])), color, thickness)
      # neck --> right_hip
    if dic['neck']['score'] > 0.2 and dic['right_hip']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['neck']['x']), int(dic['neck']['y'])),
                 (int(dic['right_hip']['x']), int(dic['right_hip']['y'])), color, thickness)
      # left_hip --> left_knee
    if dic['left_hip']['score'] > 0.2 and dic['left_knee']['score'] > 0.2:
        cv2.line(img, (int(dic['left_hip']['x']), int(dic['left_hip']['y'])), (int(dic['left_knee']['x']), int(dic['left_knee']['y'])), color, thickness)
      # right_hip --> right_knee
    if dic['right_hip']['score'] > 0.2 and dic['right_hip']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['right_hip']['x']), int(dic['right_hip']['y'])),
                 (int(dic['right_knee']['x']), int(dic['right_knee']['y'])), color, thickness)
      # left_knee --> left_ankle
    if dic['left_knee']['score'] > 0.2 and dic['left_ankle']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['left_knee']['x']), int(dic['left_knee']['y'])),
                 (int(dic['left_ankle']['x']), int(dic['left_ankle']['y'])), color, thickness)
      # right_knee --> right_ankle
    if dic['right_knee']['score'] > 0.2 and dic['right_knee']['score'] > 0.2:
        cv2.line(img,
                 (int(dic['right_knee']['x']), int(dic['right_knee']['y'])),
                 (int(dic['right_ankle']['x']), int(dic['right_ankle']['y'])), color, thickness)

def draw_point(img, dic, text):

    color = (0, 255, 0)
    thickness = 2
    if (text == 'warn'):
            color = (0, 0, 255)
    for  i  in  dic:
            cv2.circle(img, (int(dic[i]['x']), int(dic[i]['y'])), 5, color, thickness)

def draw_box(img, dic, text):

    color = (255, 0, 0)
    if (text == 'warn'):
            color = (0, 0, 255)
    
    left_top = (int(dic['left']), int(dic['top']))
    left_bottom = (int(dic['left']), int(dic['top'] + dic['height']))
    right_bottom = (int(dic['left'] + dic['width']), int(dic['top'] + dic['height']))
    right_top = (int(dic['left'] + dic['width']), int(dic['top']))
    cv2.line(img, left_top, left_bottom, color, 2)
    cv2.line(img, left_top, right_top, color, 2)
    cv2.line(img, right_bottom, left_bottom, color, 2)
    cv2.line(img, right_bottom, right_top, color, 2)
    
    cv2.putText(img, text, (int(dic['left']), int(dic['top']) + 20), cv2.FONT_HERSHEY_COMPLEX, 1, color, 1)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基于 Python 和 PyQt5 的百度aip人体关键点识别GUI代码示例: ```python import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QFileDialog from PyQt5.QtGui import QPixmap from aip import AipBodyAnalysis # 设置百度aip的参数 APP_ID = 'your_app_id' API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY) class MainWindow(QMainWindow): def __init__(self): super().__init__() # 设置窗口标题和大小 self.setWindowTitle('人体关键点识别') self.setGeometry(100, 100, 800, 600) # 添加标签和按钮 self.label = QLabel(self) self.label.setGeometry(10, 10, 500, 500) self.button = QPushButton('选择图片', self) self.button.setGeometry(550, 50, 200, 50) # 连接按钮点击事件 self.button.clicked.connect(self.select_image) def select_image(self): # 打开文件选择对话框 file_name, _ = QFileDialog.getOpenFileName(self, '选择图片', '', 'Image files (*.jpg *.png)') # 调用百度aip人体关键点识别接口 with open(file_name, 'rb') as f: image_data = f.read() result = client.bodyAnalysis(image_data) # 获取人体关键点坐标 body_parts = result['person_info'][0]['body_parts'] points = [] for part in body_parts.values(): x, y = part['x'], part['y'] points.append((x, y)) # 在图片上绘制关键点 pixmap = QPixmap(file_name) painter = pixmap.toImage().painter() pen = painter.pen() pen.setWidth(10) pen.setColor(Qt.red) painter.setPen(pen) for point in points: painter.drawPoint(QPoint(point[0], point[1])) self.label.setPixmap(pixmap) if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.show() sys.exit(app.exec_()) ``` 在运行代码之前,需要先安装以下依赖库: - PyQt5 - baidu-aip 你需要将 `your_app_id`、`your_api_key` 和 `your_secret_key` 替换为你自己的百度aip应用参数。运行程序后,点击按钮选择一张图片,程序会自动调用百度aip人体关键点识别接口,获取关键点坐标,并在图片上绘制出关键点
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值