欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)戰(zhàn)之百度智能云使人像動(dòng)漫化

 更新時(shí)間:2024年04月08日 11:30:12   作者:Linkage interrupt  
這篇文章主要介紹了python實(shí)戰(zhàn)之百度智能云使人像動(dòng)漫化,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下

一、目標(biāo)

之前無意中看到有某位博主寫過人像動(dòng)漫化這樣的文章,看著還挺好玩,所以我也想嘗試一下。

利用百度智能云中的人工智能,對圖片進(jìn)行處理達(dá)到人像動(dòng)漫化的效果。

二、準(zhǔn)備工作

1.百度云智能賬號創(chuàng)建

2.圖像特效應(yīng)用

3.開發(fā)環(huán)境python3.7+pycharm

首先要注冊一個(gè)百度智能云賬號,并創(chuàng)建這個(gè)圖像特效應(yīng)用

在這里插入圖片描述

三、操作流程

3.1 閱讀官方文檔

當(dāng)我們要使用一個(gè)我們不太了解的東西時(shí),閱讀官方文檔無疑是最重要的,官方文檔一般都寫的特別詳細(xì),對每一個(gè)功能描述的很細(xì)節(jié),我們先來看一下

在這里插入圖片描述
在這里插入圖片描述

而且這里有案例,這里我使用的是python

3.2 開始實(shí)現(xiàn)鑒權(quán)

因?yàn)檎{(diào)用這么個(gè)接口api要進(jìn)行鑒權(quán),就是官方文檔說得到access_token,如何鑒權(quán)呢?

在這里插入圖片描述
在這里插入圖片描述

import requests
import pprint
def get_access_token(id,secret):
    get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
    response=requests.get(get_access_token_url)
    pprint.pprint(response.json())
id='*******************'
secret='******************'
get_access_token(id,secret)

這里的id和secret就是創(chuàng)建應(yīng)用的appkey和secretkey:

在這里插入圖片描述

上述代碼打印結(jié)果有很多,閱讀官網(wǎng)文檔得知,我們這里只需要得到access_token就OK了

在這里插入圖片描述

修改上述代碼以獲取access_token

import requests
def get_access_token(id,secret):
    get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
    response=requests.get(get_access_token_url)
    content=response.json()
    access_token=content['access_token']
    print(access_token)
id='*******************'
secret='******************'
get_access_token(id,secret)

3.3 人像動(dòng)漫化實(shí)現(xiàn)

正片開始

在這里插入圖片描述

在這里插入圖片描述

修改代碼

import requests
import pprint
import base64
def get_access_token(id,secret):
    get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
    response=requests.get(get_access_token_url)
    content=response.json()
    access_token=content['access_token']
    return access_token

def Animation(img_file,access_token):
    request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    f=open(img_file,'rb')
    image=base64.b64encode(f.read())
    params = {"image":image}
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    pprint.pprint(response.json())
def main():
    img_file = '1.jpg'#圖片地址
    id = '**************************'
    secret = '**************************'
    access_token = get_access_token(id, secret)
    Animation(img_file, access_token)
if __name__ == '__main__':
    main()

這時(shí)可以得到一系列的返回值

在這里插入圖片描述

我們這里只要image

獲取image值

修改代碼

import requests
import pprint
import base64
def get_access_token(id,secret):
    get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
    response=requests.get(get_access_token_url)
    content=response.json()
    access_token=content['access_token']
    return access_token

def Animation(img_file,access_token):
    request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    f=open(img_file,'rb')
    image=base64.b64encode(f.read())
    params = {"image":image}
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    image_content=response.json()
    image=image_content['image']
    print(image)
def main():
    img_file = '1.jpg'#圖片地址
    id = '**************************'
    secret = '**************************'
    access_token = get_access_token(id, secret)
    Animation(img_file, access_token)
if __name__ == '__main__':
    main()

獲取到一串base64編碼的圖片,這顯然快得到我們想要的東西了

 with open('result.jpg','wb') as f:
        f.write(base64.b64decode(image))

保存到本地

看一下對比

在這里插入圖片描述

呃呃呃,這。。。。還好吧,哈哈哈

四、完整代碼如下

import requests
import pprint
import base64
def get_access_token(id,secret):
    get_access_token_url='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+id+'&client_secret='+secret
    response=requests.get(get_access_token_url)
    content=response.json()
    access_token=content['access_token']
    return access_token

def Animation(img_file,access_token):
    request_url='https://aip.baidubce.com/rest/2.0/image-process/v1/selfie_anime'
    f=open(img_file,'rb')
    image=base64.b64encode(f.read())
    params = {"image":image}
    request_url = request_url + "?access_token=" + access_token
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    image_content=response.json()
    image=image_content['image']
    with open('result.jpg','wb') as f:
        f.write(base64.b64decode(image))  
def main():
    img_file = '1.jpg'#圖片地址
    id = '**************************'
    secret = '**************************'
    access_token = get_access_token(id, secret)
    Animation(img_file, access_token)
if __name__ == '__main__':
    main()

五、還能這么玩?

在這里插入圖片描述

厲害了,還能加口罩,試一下

修改代碼

params = 
{
"image":image,"type":'anime_mask',"mask_id":1
}
#mask_id 1-8的整數(shù),就用個(gè)1吧

看一下效果

在這里插入圖片描述

嘖嘖嘖

到此這篇關(guān)于python實(shí)戰(zhàn)之百度智能云使人像動(dòng)漫化的文章就介紹到這了,更多相關(guān)python人像動(dòng)漫化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 學(xué)好python基本數(shù)據(jù)類型

    學(xué)好python基本數(shù)據(jù)類型

    這篇文章主要介紹了學(xué)好python基本數(shù)據(jù)類型,學(xué)習(xí)python基本數(shù)據(jù)類型我們需要了解基本數(shù)據(jù)類型有數(shù)字int、布爾值bool、字符串str、列表list、元組tuple、字典dict等,其中包括他們的基本用法和其常用的方法,下面來看看文章的具體介紹吧
    2021-12-12
  • python 工具 字符串轉(zhuǎn)numpy浮點(diǎn)數(shù)組的實(shí)現(xiàn)

    python 工具 字符串轉(zhuǎn)numpy浮點(diǎn)數(shù)組的實(shí)現(xiàn)

    這篇文章主要介紹了python 工具 字符串轉(zhuǎn)numpy浮點(diǎn)數(shù)組的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Cpython3.9源碼解析python中的大小整數(shù)

    Cpython3.9源碼解析python中的大小整數(shù)

    這篇文章主要介紹了Cpython3.9源碼解析python中的大小整數(shù),在CPython中,小整數(shù)對象池是一種優(yōu)化機(jī)制,用于減少對常用小整數(shù)的內(nèi)存分配和銷毀開銷,需要的朋友可以參考下
    2023-04-04
  • Python進(jìn)程池Pool應(yīng)用實(shí)例分析

    Python進(jìn)程池Pool應(yīng)用實(shí)例分析

    這篇文章主要介紹了Python進(jìn)程池Pool應(yīng)用,結(jié)合實(shí)例形式分析了Python進(jìn)程池Pool功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-11-11
  • python和shell監(jiān)控linux服務(wù)器的詳細(xì)代碼

    python和shell監(jiān)控linux服務(wù)器的詳細(xì)代碼

    這篇文章主要為大家介紹了使用python和shell監(jiān)控linux服務(wù)器的詳細(xì)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 使用python制作九九乘法表的四種方法小結(jié)

    使用python制作九九乘法表的四種方法小結(jié)

    九九乘法表是初學(xué)者學(xué)習(xí)編程的必要練手題目之一,因此各種語言都有對應(yīng)的實(shí)現(xiàn)方式,而 Python 也不例外,在 Python 中,我們可以使用多種方式來生成一個(gè)簡單的九九乘法表,本文給大家總結(jié)了使用python制作九九乘法表的四種方法,需要的朋友可以參考下
    2024-03-03
  • Python安裝Imaging報(bào)錯(cuò):The _imaging C module is not installed問題解決方法

    Python安裝Imaging報(bào)錯(cuò):The _imaging C module is not installed問題解決

    這篇文章主要介紹了Python安裝Imaging報(bào)錯(cuò):The _imaging C module is not installed問題解決方法,原來是PIL庫的庫文件沒有加到系統(tǒng)中導(dǎo)致老是提示這個(gè)錯(cuò)誤,需要的朋友可以參考下
    2014-08-08
  • python爬蟲爬取某網(wǎng)站視頻的示例代碼

    python爬蟲爬取某網(wǎng)站視頻的示例代碼

    這篇文章主要介紹了python爬蟲爬取某網(wǎng)站視頻的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02
  • 詳談pandas中agg函數(shù)和apply函數(shù)的區(qū)別

    詳談pandas中agg函數(shù)和apply函數(shù)的區(qū)別

    下面小編就為大家分享一篇詳談pandas中agg函數(shù)和apply函數(shù)的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python編寫檢測數(shù)據(jù)庫SA用戶的方法

    Python編寫檢測數(shù)據(jù)庫SA用戶的方法

    這篇文章主要介紹了Python編寫檢測數(shù)據(jù)庫SA用戶的方法,需要的朋友可以參考下
    2014-07-07

最新評論