<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          五十一、結合百度API接口打造 Python小項目

          共 5162字,需瀏覽 11分鐘

           ·

          2020-12-05 17:51


          「@Author:Runsen」

          本項目圍繞圖像識別,通過調(diào)用百度 API 接口,可以實現(xiàn)很多人性化的功能,比如手勢識別、人像分割以及顏值打分等功能。

          本次Gitchat付費文章,但是因為訂閱太少了。所以只能當作公眾號文章發(fā)了。

          本項目結合人工智能和GUI圖形界面開發(fā),滿足用戶對圖像識別方面的需求。

          在本次項目中主要使用的技術點:

          • 百度API接口調(diào)用
          • Tkinker
          • opencv調(diào)用USB攝像頭

          功能需求

          本次 Python 小項目主要功能:調(diào)用電腦攝像頭實現(xiàn)拍照,并使用百度 API 接口實現(xiàn)顏值評分,手勢識別和摳圖。

          功能需求的API文檔:

          顏值評分:https://ai.baidu.com/ai-doc/FACE/yk37c1u4t

          手勢識別:https://ai.baidu.com/ai-doc/BODY/4k3cpywrv

          人像分割:https://ai.baidu.com/ai-doc/BODY/Fk3cpyxua

          功能實現(xiàn)流程接口API
          顏值評分(face_detect)調(diào)用攝像頭進行拍照識別,識別結果包括年齡,性別,人種,顏值評分https://aip.baidubce.com/rest/2.0/face/v3/detect
          手勢識別(gesture)調(diào)用攝像頭進行拍照識別,識別結果為手勢的中文大意和英文大意https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture
          人像分割(body_seg)調(diào)用攝像頭進行拍照識別,對人物進行摳圖https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg

          獲取access token值

          本次項目采用封裝類的方法進行代碼編寫,需要對Baidu_API封裝成類。

          顏值進行評分

          me.jpg

          識別結果包括年齡,性別,人種,顏值評分(0-100)

          上面圖片返回的結果:47.34 22 male yellow

          手勢識別

          gesture.jpg

          識別結果包括手勢的中文大意和英文大意

          上面圖片返回的結果:Five 五個

          人像分割摳圖

          me.jpg

          返回的結果是對人物進行摳圖

          me.png

          完整代碼

          '''
          @Author:Runsen
          @WeChat:RunsenLiu
          @微信公眾號:Python之王
          @CSDN:https://blog.csdn.net/weixin_44510615
          @Github:https://github.com/MaoliRUNsen
          @Date:2020/11/29
          '''


          import?requests
          import?base64

          class?Baidu_API():
          ????def?__init__(self):
          ????????self.headers={'Content-Type':?'application/json;?charset=UTF-8'}
          ????????self.AppID?=?'23061934'??#?百度應用賬號ID
          ????????self.APIKey?=?'0EGiM1wDAH5kmFAvhYVLYCbs'??#?針對接口訪問的授權方式
          ????????self.SecretKey?=?'KVugHxxu4uq203b111SwVY2w98Cd9D70'??#?密鑰

          ????#獲取access?token值
          ????def?get_access_token(self):
          ????????host?=?'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(self.APIKey,self.SecretKey)
          ????????response?=?requests.get(host,self.headers)
          ????????if?response:
          ????????????access_token=response.json()['access_token']
          ????????????return?access_token
          ????????else:
          ????????????print("access_token獲取失敗")

          ????#獲取轉換圖片格式為base64的圖片數(shù)據(jù)
          ????def?get_img_base64(self,img_path):
          ????????#?先以二進制的格式讀取圖片,再轉化為base64格式(使用二進制轉base64格式的函數(shù))
          ????????with?open(img_path,'rb')as?f:
          ????????????#?base64編碼
          ????????????img_data?=?base64.b64encode(f.read())
          ????????return?img_data

          ????#功能1、對顏值進行評分
          ????def?face_detect(self,img_path):
          ????????try:
          ????????????#?訪問人臉檢測api
          ????????????base_url?=?"https://aip.baidubce.com/rest/2.0/face/v3/detect"
          ????????????#?基本參數(shù)
          ????????????request_url?=?base_url?+?"?access_token="?+?self.get_access_token()

          ????????????params?=?{"image":?self.get_img_base64(img_path),
          ??????????????????????"image_type":?"BASE64",
          ??????????????????????"face_field":?"faceshape,facetype,beauty,age,beauty,glasses,gender,race"}
          ????????????#?開始訪問
          ????????????response?=?requests.post(url=request_url,
          ??????????????????????????????????????data=params,
          ??????????????????????????????????????headers=self.headers)
          ????????????re?=?response.json()
          ????????????score?=?re["result"]["face_list"][0]['beauty']
          ????????????age?=?re["result"]["face_list"][0]['age']
          ????????????gender?=?re["result"]["face_list"][0]['gender']['type']
          ????????????race?=?re["result"]["face_list"][0]['race']['type']
          ????????????#?返回數(shù)據(jù)
          ????????????print(score,age,gender,race)
          ????????????return?score,age,gender,race
          ????????except:
          ????????????return?'未能正確識別,請重試'

          ????#功能2、手勢識別
          ????def?gesture(self,img_path):
          ????????try:
          ????????????base_url?=?'https://aip.baidubce.com/rest/2.0/image-classify/v1/gesture'
          ????????????#?基本參數(shù)
          ????????????request_url?=?base_url?+?"?access_token="?+?self.get_access_token()
          ????????????params?=?{"image":?self.get_img_base64(img_path),
          ??????????????????????"image_type":?"BASE64"}
          ????????????#?開始訪問
          ????????????response?=?requests.post(url=request_url,
          ?????????????????????????????????????data=params,
          ?????????????????????????????????????headers=self.headers)??#?

          ????????????re?=?response.json()??#?
          ????????????classname_en?=?re["result"][0]['classname']
          ????????????classname_zh?=?self.translate(classname_en)
          ????????????print(classname_en,classname_zh)
          ????????????return?classname_en,classname_zh
          ????????except:
          ????????????return?'未能正確是識別,請重試'


          ????#功能3、人像分割摳圖
          ????def?body_seg(self,img_path,?out_path):
          ????????try:
          ????????????base_url?=?"https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg"
          ????????????request_url?=?base_url?+?"?access_token="?+?self.get_access_token()
          ????????????params?=?{"image":?self.get_img_base64(img_path)}
          ????????????headers?=?{'content-type':?'application/x-www-form-urlencoded'}
          ????????????response?=?requests.post(request_url,?data=params,?headers=headers)
          ????????????labelmap?=?base64.b64decode(response.json()['foreground'])
          ????????????with?open(out_path,?'wb')?as?fp:
          ????????????????fp.write(labelmap)
          ????????except:
          ????????????return?'未能正確識別,請重試'

          ????#?翻譯APi接口
          ????def?translate(self,?query):
          ????????url?=?'http://fanyi.youdao.com/translate?&doctype=json&type=AUTO&i={}'.format(query)
          ????????r?=?requests.post(url=url)
          ????????return?r.json()['translateResult'][0][0]['tgt'].strip('的')


          if?__name__?==?'__main__':
          ????Api?=?Baidu_API()
          ????Api.face_detect('me.jpg')
          ????Api.gesture('gesture.jpg')
          ????Api.body_seg('me.jpg','me.png')
          ?

          本文已收錄 GitHub,傳送門~[1] ,里面更有大廠面試完整考點,歡迎 Star。

          ?


          Reference

          [1]

          傳送門~: https://github.com/MaoliRUNsen/runsenlearnpy100


          更多的文章

          點擊下面小程序



          - END -

          瀏覽 77
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚州免费高清 | 久久人人爽人人爽人人片aV东京热 | 欧美日韩aA在线视频 | 亚洲婷婷五月综合 | 国模在线|