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