濮阳杆衣贸易有限公司

主頁 > 知識庫 > python實戰(zhàn)之百度智能云使人像動漫化

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

熱門標簽:315電話機器人廣告 房產(chǎn)電銷外呼系統(tǒng) 地圖標注的意義點 浙江電銷卡外呼系統(tǒng)好用嗎 南京銷售外呼系統(tǒng)軟件 上海機器人外呼系統(tǒng)哪家好 地圖制圖標注位置改變是移位嗎 地圖標注微信發(fā)送位置不顯示 蓋州市地圖標注

一、目標

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

利用百度智能云中的人工智能,對圖片進行處理達到人像動漫化的效果。

二、準備工作

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

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

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

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

三、操作流程

3.1 閱讀官方文檔

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


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

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

因為調(diào)用這么個接口api要進行鑒權(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_credentialsclient_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_credentialsclient_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 人像動漫化實現(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_credentialsclient_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()

這時可以得到一系列的返回值

我們這里只要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_credentialsclient_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_credentialsclient_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ù),就用個1吧

看一下效果

嘖嘖嘖

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

您可能感興趣的文章:
  • python實現(xiàn)人像動漫化的示例代碼
  • python爬蟲實例之獲取動漫截圖
  • python爬蟲智能翻頁批量下載文件的實例詳解
  • vscode如何安裝漢化和Python智能感知
  • python實現(xiàn)智能語音天氣預(yù)報
  • python實現(xiàn)人工智能Ai摳圖功能
  • Python人工智能之路 jieba gensim 最好別分家之最簡單的相似度實現(xiàn)
  • Python人工智能之路 之PyAudio 實現(xiàn)錄音 自動化交互實現(xiàn)問答

標簽:貴州 雙鴨山 日照 金華 臨汾 陽泉 克拉瑪依 赤峰

巨人網(wǎng)絡(luò)通訊聲明:本文標題《python實戰(zhàn)之百度智能云使人像動漫化》,本文關(guān)鍵詞  python,實戰(zhàn),之,百度,智能,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python實戰(zhàn)之百度智能云使人像動漫化》相關(guān)的同類信息!
  • 本頁收集關(guān)于python實戰(zhàn)之百度智能云使人像動漫化的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    泰来县| 尚志市| 锡林郭勒盟| 东乡族自治县| 本溪市| 霍州市| 汤原县| 通辽市| 大连市| 商南县| 英吉沙县| 桂平市| 阿拉善左旗| 保亭| 化州市| 环江| 东方市| 道真| 通榆县| 曲靖市| 台北县| 手游| 特克斯县| 治多县| 棋牌| 乌苏市| 大城县| 汾西县| 满城县| 宁强县| 诸暨市| 留坝县| 东阳市| 木里| 乐陵市| 隆尧县| 柘城县| 玉龙| 东乡族自治县| 湘潭县| 武冈市|