网络知识 娱乐 使用 Python 为女神挑选口红

使用 Python 为女神挑选口红

口红对于女生来说永远不嫌多,而男生也搞不明白珊瑚红、番茄色、斩男色等等颜色有什么区别,不都是红色么?当送给女神的口红是她不适合的,那结果就是口红进入垃圾箱还算是轻的,重则拉黑处理。男生们也不用着急,我们可以用 Python 对女神照片进行人脸识别,并对嘴唇部分涂上口红。这样就可以挑选出美美哒的口红了,下面一起来看看吧。

嘴唇识别

主要是采用百度 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=' + skresponse = requests.get(host)if response: access_token = response.json()['access_token']else: raise Exception('access_token 获取失败')

获取 access_token 后,在网上找一个美女头像图片作为底片,转换成 base64 位格式当做参数请求得到人脸的 150 个关键点

# 图片转 base64pic_path = '/Users/xx/Desktop/kh/原图.png'with open (pic_path, 'rb') as f: 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_tokenheaders = {'content-type': 'application/json'}response = requests.post(request_url, data=params, headers=headers)if response: face = response.json()else: raise Exception('人脸关键点获取失败')

示例结果

取到人脸关键点后,参照人脸识别的文档可以得到嘴唇的 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']for f in face['result']['face_list']: # 上嘴唇关键点 [(x,y),(x,y),(x,y)] 元组列表 mouth_lip_upper_list = [] # 下嘴唇关键点 [(x,y),(x,y),(x,y)] 元组列表 mouth_lip_low_list = [] for point in mouth_lip_upper_point_list: p = f['landmark150'][point] mouth_lip_upper_list.append((p['x'], p['y'])) for point in mouth_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')

示例结果

总结

通过上面的代码,我们已经可以为女神选出一支适合的口红,祝愿小伙伴们送女神口红都可以成功