百度目标检测

code:
 

#https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=wbKPb60s3zoXQUvY3ETXiWfw&client_secret=rVlW4GGXqSsNlcSL2Q33jyhuaYuxDaEL&

from aip import AipBodyAnalysis
import cv2
import os
""" 你的 APPID AK SK """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

""" 调用人体关键点识别 """
def key_point(image,im):
    r2 = client.bodyAnalysis(image);
    num = r2.get('person_num')
    info = r2.get('person_info')
    for i in range(num):
        #print (info[i].get('location'))
        body_parts = info[i].get('body_parts')
        for (k,v) in  body_parts.items():
            #print(v)
            x = v.get('x')
            y = v.get('y')
            score = v.get('score')
            if(score <= 0.2):
                continue
            cv2.circle(im, (int(x), int(y)), 10, (0,0,255),-1)
    return im

""" 带参数调用人体检测与属性识别 """
def box(image,im):
    options = {}
    options["type"] = "gender"
    r1 = client.bodyAttr(image, options)
    num = r1.get('person_num')
    info = r1.get('person_info')
    for i in range(num):
        #print (info[i].get('location'))
        location = info[i].get('location')
        
        left = location.get('left')
        top = location.get('top')
        right = left+location.get('width')
        down  = top+location.get('height')
        score = location.get('score')
        if(score < 0.2):
            continue;
        #print(left,top,right,down)
        cv2.rectangle(im,(int(left),int(top)),(int(right),int(down)),(255,0,0),0)
    return im

def perform_one(path,filename):
    #path = 'sichuan/sichuan_15_1800.jpg'
    
    image = get_file_content(path+'/'+filename)
    im = cv2.imread(path+'/'+filename)
    im = box(image,im)
    im = key_point(image,im)
    cv2.imwrite("sichuan_out/"+filename,im);

def walkFile(file):
    for root, dirs, files in os.walk(file):
        # root 表示当前正在访问的文件夹路径
        # dirs 表示该文件夹下的子目录名list
        # files 表示该文件夹下的文件list
        # 遍历文件
        
        for f in files:
            print(os.path.join(root, f))
            #print (root,f)
            perform_one(root,f);
        # 遍历所有的文件夹
        #for d in dirs:
        #    print(os.path.join(root, d))
client = AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY)
walkFile("sichuan");

 

你可能感兴趣的:(技术,Python)