欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > 百度AI人体关键点识别跑跑跑

百度AI人体关键点识别跑跑跑

2025/5/1 17:16:20 来源:https://blog.csdn.net/baixue1183/article/details/141395580  浏览:    关键词:百度AI人体关键点识别跑跑跑

百度AI人体关键点识别跑跑跑

首先,本代码基于这篇博客,感谢大佬详尽的步骤说明,他是在原来基础功能之上增加了:

  • 单图片处理时间
  • 将识别出来的关键点标注在图片上并输出

在此基础上我加了两个改动:

  • 在图像上标注好的每个点旁边增加点名称
  • 仅显示我需要的关键点
import urllib
import base64
import json
import time
from PIL import Image, ImageDraw, ImageFont
import re
import cv2 as cv
import osfrom aip import AipBodyAnalysis#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id = '你的AK'
client_secret = '你的SK'#获取token
def get_token():host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secretrequest = urllib.request.Request(host)request.add_header('Content-Type', 'application/json; charset=UTF-8')response = urllib.request.urlopen(request)token_content = response.read()if token_content:token_info = json.loads(token_content)token_key = token_info['access_token']return token_key
#画出人体识别结果
def draw_bodys(originfilename,bodys,resultfilename,pointsize):image_origin = Image.open(originfilename)draw =ImageDraw.Draw(image_origin)for body in bodys:x=0for body_part in body['body_parts'].values():#print(body_part)draw.ellipse((body_part['x']-pointsize,body_part['y']-pointsize,body_part['x']+pointsize,body_part['y']+pointsize),fill = "blue")font = ImageFont.truetype("consola.ttf", 20, encoding="unic")#设置字体draw.text((body_part['x'], body_part['y']), str(list(body['body_parts'].keys())[x]), 'fuchsia', font)x=x+1# draw.text(image_origin,str(list(body['body_parts'].keys())[x]), (body_part['x'],body_part['y']), cv.FONT_HERSHEY_SIMPLEX,1,(0,0,255), 1, cv.LINE_AA)gesture = body['location'] draw.rectangle((gesture['left'],gesture['top'],gesture['left']+gesture['width'],gesture['top']+gesture['height']),outline = "red")image_origin.save(resultfilename, "JPEG")#人体识别
#filename:原图片名(本地存储包括路径)
def body_analysis(filename,resultfilename,pointsize):request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_analysis"print(filename)# 二进制方式打开图片文件f = open(filename, 'rb')img = base64.b64encode(f.read())params = dict()params['image'] = imgparams = urllib.parse.urlencode(params).encode("utf-8")#params = json.dumps(params).encode('utf-8')access_token = get_token()begin = time.perf_counter()request_url = request_url + "?access_token=" + access_tokenrequest = urllib.request.Request(url=request_url, data=params)request.add_header('Content-Type', 'application/x-www-form-urlencoded')response = urllib.request.urlopen(request)content = response.read()end = time.perf_counter()print('处理时长:'+'%.2f'%(end-begin)+'秒')if content:#print(content)content=content.decode('utf-8')#下面这些就是把我不需要的什么鼻子啊,左右嘴角啊,脖子啊这种点给去掉了,但仅仅是不标,并没有不识别#去掉的方式就是在已经识别出来的这些点的这个content里找到这些点的名字,在他们后面一个点前面加上“www”字符串,然后识别出要删除的点的名字作为起点,“,www”作为终点,把从起点到终点(包含起点终点)的字符串删掉。这是个笨办法,因为我不知道咋删除字典里的一个键……content=add_content(content, '"right_knee"', 'www')content=remove_identifiers_and_content(content, '"nose"', ',www', )content=add_content(content, '"right_elbow"', 'www')content=remove_identifiers_and_content(content, '"left_mouth_corner"', ',www', )content=add_content(content, '"left_elbow"', 'www')content=remove_identifiers_and_content(content, '"neck"', ',www', )content=add_content(content, '"left_ankle"', 'www')content=remove_identifiers_and_content(content, '"right_mouth_corner"', ',www', )print(content)data = json.loads(content)f2 = open('new_json.json', 'w')f2.write(content)f2.close()#print(data)#print(data)result=data['person_info']draw_bodys(filename,result,resultfilename,pointsize)#下面这俩函数就是为了在content里增减一些字符
def remove_identifiers_and_content(original_string, identifier_A, identifier_B):# 将标识符A和B转换为正则表达式,以匹配它们及其之间的任何内容pattern = f"{identifier_A}.*?{identifier_B}"# 使用re.sub()函数替换掉匹配的模式modified_string = re.sub(pattern, "", original_string, flags=re.DOTALL)return modified_stringdef add_content(original_string, identifier_A, identifier_B):str_index = original_string.find(identifier_A)new_str = original_string[:str_index] + identifier_B + original_string[str_index:]return new_strbase_path = '图片路径,用/这个斜线'
files = os.listdir(base_path)
for path in files:print(path)body_analysis(base_path+path,'1'+path,2)

接着学吧,真是任重道远呢

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词