美文网首页数据蛙数据分析每周作业
【Python学习】No.4 调用百度API实现人脸识别

【Python学习】No.4 调用百度API实现人脸识别

作者: LL_路上 | 来源:发表于2019-01-20 17:20 被阅读1次

    通过调用百度AI平台的人脸识别接口,实现人脸打分,GET这个技能后,再也不怕找不到漂亮小姐姐了,以后可以先通过爬虫大批量爬取图片,然后通过智能打分。。。Emmm...真好~~~

    from aip import AipFace
    import base64
    import os
    import time
    
    
    APP_ID = '******'
    API_KEY = '*******'
    SECRET_KEY = '******* '
    
    aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
    
    #filePath = r'C:\Users\louis_lu\Desktop\dwt4.jpg'
    
    
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            content = base64.b64encode(fp.read())
            return content.decode('utf-8')
    
    
    imageType = "BASE64"
    
    options = {}
    options["face_field"] = "age,gender,beauty"
    
    #遍历文件夹中的图片
    file_path = 'work'
    file_lists = os.listdir(file_path)
    for file_list in file_lists:
        result = aipFace.detect(get_file_content(os.path.join(file_path, file_list)), imageType, options)
        error_code = result['error_code']
        #print(result)
        if error_code == 222202:
            continue
        try:
            sex_type = result['result']['face_list'][-1]['gender']['type']
            #if sex_type == 'male':
                #continue
            age = result['result']['face_list'][-1]['age']
            beauty = result['result']['face_list'][-1]['beauty']
            new_beauty = round(beauty/10, 1)
            print(file_list, new_beauty)
            if new_beauty >= 8:
                os.rename(os.path.join(file_path, file_list), os.path.join('8+', str(new_beauty) + '+' + str(age) + '+' + file_list))
            elif new_beauty >= 7:
                os.rename(os.path.join(file_path, file_list), os.path.join('7+', str(new_beauty) + '+' + str(age) + '+' + file_list))
            elif new_beauty >= 6:
                os.rename(os.path.join(file_path, file_list), os.path.join('6+', str(new_beauty) + '+' + str(age) + '+' + file_list))
            elif new_beauty >= 5:
                os.rename(os.path.join(file_path, file_list), os.path.join('5+', str(new_beauty) + '+' + str(age) + '+' + file_list))
            else:
                os.rename(os.path.join(file_path, file_list), os.path.join('other', str(new_beauty) + '+' + str(age) + '+' + file_list))
            time.sleep(1)
        except KeyError:
            pass
        except TypeError:
            pass
    

    相关文章

      网友评论

        本文标题:【Python学习】No.4 调用百度API实现人脸识别

        本文链接:https://www.haomeiwen.com/subject/nnoxjqtx.html