zoukankan      html  css  js  c++  java
  • python调用百度人像动漫化

    一、代码

    import base64
    import requests
    
    class AnimeDemo:
        def __init__(self,AK,SK):
            self.AK=AK
            self.SK=SK
            self.access_token=self.get_access_token()
    
        def get_access_token(self):
            token_host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'.format(
                ak=self.AK, sk=self.SK)
            header = {'Content-Type': 'application/json; charset=UTF-8'}
            response = requests.post(url=token_host, headers=header)
            content = response.json()
            access_token = content.get("access_token")
            return access_token
    
        def baidu_selfie_anime(self,image_path,save_path):
            request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
            # 二进制方式打开图片文件
            f = open(image_path, 'rb')
            img = base64.b64encode(f.read())
            params = {"image": img}
            request_url = request_url + "?access_token=" + self.access_token
            headers = {'content-type': 'application/x-www-form-urlencoded'}
            try:
                response = requests.post(request_url, data=params, headers=headers)
                if response.status_code==200:
                    result= response.json()
                    print(result)
                    image=result.get("image")
                    if image:
                        img_data=base64.b64decode(image)
                        with open(save_path,"wb") as f:
                            f.write(img_data)
                        return {"msg":"完成"}
                return {"msg":"失败"}
            except Exception as e:
                return {"msg":"失败:%s"%e}
    
    if __name__ == '__main__':
        AK = "*******"  # 官网获取的AK 需要你先开通和创建应用
        SK = "*******"  # 官网获取的SK
        anime_obj = AnimeDemo(AK=AK, SK=SK)
        res=anime_obj.baidu_selfie_anime(r"D:images微信图片_20200722221927.jpg",r"C:UsersAdministratorDesktop卡通.jpg")
        print(res)

    二、效果

    感觉一般般,反正有500次免费的,凑合!

    三、增加图像风格

    import base64
    
    import requests
    
    
    class AnimeDemo:
        def __init__(self, AK, SK):
            self.AK = AK
            self.SK = SK
            self.access_token = self.get_access_token()
    
        def get_access_token(self):
            token_host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={ak}&client_secret={sk}'.format(
                ak=self.AK, sk=self.SK)
            header = {'Content-Type': 'application/json; charset=UTF-8'}
            response = requests.post(url=token_host, headers=header)
            content = response.json()
            access_token = content.get("access_token")
            return access_token
    
        def baidu_selfie_anime(self, image_path, save_path):
            request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime"
            # 二进制方式打开图片文件
            f = open(image_path, 'rb')
            img = base64.b64encode(f.read())
            params = {"image": img}
            request_url = request_url + "?access_token=" + self.access_token
            headers = {'content-type': 'application/x-www-form-urlencoded'}
            try:
                response = requests.post(request_url, data=params, headers=headers)
                if response.status_code == 200:
                    result = response.json()
                    print(result)
                    image = result.get("image")
                    if image:
                        img_data = base64.b64decode(image)
                        with open(save_path, "wb") as f:
                            f.write(img_data)
                        return {"msg": "完成"}
                return {"msg": "失败"}
            except Exception as e:
                return {"msg": "失败:%s" % e}
    
        def baidu_style_trans(self, image_path, save_path):
            '''
            图像风格转换
            cartoon:卡通画风格
            pencil:铅笔风格
            color_pencil:彩色铅笔画风格
            warm:彩色糖块油画风格
            wave:神奈川冲浪里油画风格
            lavender:薰衣草油画风格
            mononoke:奇异油画风格
            scream:呐喊油画风格
            gothic:哥特油画风格
            '''
            request_url = "https://aip.baidubce.com/rest/2.0/image-process/v1/style_trans"
            # 二进制方式打开图片文件
            f = open(image_path, 'rb')
            img = base64.b64encode(f.read())
            params = {"image": img, "option": "gothic"}
            request_url = request_url + "?access_token=" + self.access_token
            headers = {'content-type': 'application/x-www-form-urlencoded'}
            try:
                response = requests.post(request_url, data=params, headers=headers)
                if response.status_code == 200:
                    result = response.json()
                    print(result)
                    image = result.get("image")
                    if image:
                        img_data = base64.b64decode(image)
                        with open(save_path, "wb") as f:
                            f.write(img_data)
                        return {"msg": "完成"}
                return {"msg": "失败"}
            except Exception as e:
                return {"msg": "失败:%s" % e}

  • 相关阅读:
    #define IOFFSETOF(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
    互联网地址处理例程
    Android系统工程模式启动过程详解
    知识填充
    git 本地回退
    理解JS中的Promise对象
    MySQL server version for the right syntax to use near 'identified
    尾递归要注意的点
    事件捕获和事件冒泡的理解
    v 2ra-y_build_a_sever_in_vltru
  • 原文地址:https://www.cnblogs.com/angelyan/p/13363565.html
Copyright © 2011-2022 走看看