1. 程式人生 > 實用技巧 >使用 Python 為女神挑選口紅 ,成功把女神拿下,你學會了嗎

使用 Python 為女神挑選口紅 ,成功把女神拿下,你學會了嗎

口紅對於女生來說永遠不嫌多,而男生也搞不明白珊瑚紅、番茄色、斬男色等等顏色有什麼區別,不都是紅色麼?當送給女神的口紅是她不適合的,那結果就是口紅進入垃圾箱還算是輕的,重則拉黑處理。男生們也不用著急,我們可以用 Python 對女神照片進行人臉識別,並對嘴脣部分塗上口紅。這樣就可以挑選出美美噠的口紅了,下面一起來看看吧。

很多人學習python,不知道從何學起。
很多人學習python,掌握了基本語法過後,不知道在哪裡尋找案例上手。
很多已經做案例的人,卻不知道如何去學習更加高深的知識。
那麼針對這三類人,我給大家提供一個好的學習平臺,免費領取視訊教程,電子書籍,以及課程的原始碼!
QQ群:101677771

嘴脣識別

主要是採用百度 AI 開放平臺的人臉識別,它可以識別人臉的 150 個關鍵點,其中嘴巴的關鍵點就有 48 個

熟悉百度 AI 開放平臺的小夥伴都知道,需要使用百度控制檯的 AK 和 SK 才能生成 access_token 變數

ak='xxx'
sk='xxx'

host='https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+ak+'&client_secret='+sk
response=requests.get(host)
ifresponse:
access_token=response.json()['access_token']
else:
raiseException('access_token獲取失敗')

獲取 access_token 後,在網上找一個美女頭像圖片作為底片,轉換成 base64 位格式當做引數請求得到人臉的 150 個關鍵點

#圖片轉base64
pic_path='/Users/xx/Desktop/kh/原圖.png'
withopen(pic_path,'rb')asf:
base64_data=base64.b64encode(f.read())

# image:圖片,image_type:圖片格式,face_field:請求的結果,landmark150為人臉的 150個關鍵點
params='{"image":"'+base64_data.decode('utf-8')+'","image_type":"BASE64","face_field":"landmark150"}'
request_url='https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token='+access_token
headers={'content-type':'application/json'}
response=requests.post(request_url,data=params,headers=headers)

ifresponse:
face=response.json()
else:
raiseException('人臉關鍵點獲取失敗')

示例結果

取到人臉關鍵點後,參照人臉識別的文件(下圖)可以得到嘴脣的 48 個關鍵點

​#上嘴脣關鍵點,按順時針方向的順序組成一個多邊形

mouth_lip_upper_point_list=[
'mouth_corner_right_outer','mouth_lip_upper_outer_1','mouth_lip_upper_outer_2','mouth_lip_upper_outer_3',
'mouth_lip_upper_outer_4','mouth_lip_upper_outer_5','mouth_lip_upper_outer_6','mouth_lip_upper_outer_7',
'mouth_lip_upper_outer_8','mouth_lip_upper_outer_9','mouth_lip_upper_outer_10','mouth_lip_upper_outer_11',
'mouth_corner_left_outer','mouth_corner_left_inner','mouth_lip_upper_inner_11','mouth_lip_upper_inner_10',
'mouth_lip_upper_inner_9','mouth_lip_upper_inner_8','mouth_lip_upper_inner_7','mouth_lip_upper_inner_6',
'mouth_lip_upper_inner_5','mouth_lip_upper_inner_4','mouth_lip_upper_inner_3','mouth_lip_upper_inner_2',
'mouth_lip_upper_inner_1','mouth_corner_right_inner','mouth_corner_right_outer'
]

#下嘴脣關鍵點,按順時針方向的順序組成一個多邊形
mouth_lip_low_point_list=[
'mouth_corner_right_outer','mouth_corner_right_inner','mouth_lip_lower_inner_1','mouth_lip_lower_inner_2',
'mouth_lip_lower_inner_3','mouth_lip_lower_inner_4','mouth_lip_lower_inner_5','mouth_lip_lower_inner_6',
'mouth_lip_lower_inner_7','mouth_lip_lower_inner_8','mouth_lip_lower_inner_9','mouth_lip_lower_inner_10',
'mouth_lip_lower_inner_11','mouth_corner_left_outer','mouth_lip_lower_outer_11','mouth_lip_lower_outer_10',
'mouth_lip_lower_outer_9','mouth_lip_lower_outer_8','mouth_lip_lower_outer_7','mouth_lip_lower_outer_6',
'mouth_lip_lower_outer_5','mouth_lip_lower_outer_4','mouth_lip_lower_outer_3','mouth_lip_lower_outer_2',
'mouth_lip_lower_outer_1','mouth_corner_right_outer'
]

forfinface['result']['face_list']:

#上嘴脣關鍵點[(x,y),(x,y),(x,y)]元組列表
mouth_lip_upper_list=[]
#下嘴脣關鍵點[(x,y),(x,y),(x,y)]元組列表
mouth_lip_low_list=[]

forpointinmouth_lip_upper_point_list:
p=f['landmark150'][point]
mouth_lip_upper_list.append((p['x'],p['y']))

forpointinmouth_lip_low_point_list:
p=f['landmark150'][point]
mouth_lip_low_list.append((p['x'],p['y']))

塗口紅

在全網都沒有找到每種口紅所對應的 16 進位制顏色,RGBA 的顏色也沒有找到,在這裡使用笨辦法,在天貓上開啟一個口紅頁面,在開發者模式下拾取顏色並複製 16 進位制顏色,口紅圖層使用mageDraw.Draw模組的polygon函式繪製多邊形並填充顏色

#將將轉為可操作的RGBA模式
img=Image.open(pic_path)
d=ImageDraw.Draw(img,'RGBA')

#口紅顏色
hex=input('請輸入口紅的16進位制顏色:')
#16進位制顏色轉rgba模式
color=(int(hex[1:3],16),int(hex[3:5],16),int(hex[5:7],16))

#繪製多邊形並填充顏色
d.polygon(mouth_lip_upper_list,fill=color)
#繪製邊框並填充顏色
d.line(mouth_lip_upper_list,fill=color,width=1)

d.polygon(mouth_lip_low_list,fill=color)
d.line(mouth_lip_low_list,fill=color,width=1)
img.show()

img.save('/Users/xx/Desktop/kh/'+hex+'.png')

示例結果

總結

通過上面的程式碼,我們已經可以為女神選出一支適合的口紅,祝願小夥伴們送女神口紅都可以成功