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

很多人学习python,不知道从何学起。
很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手。
很多已经做案例的人,却不知道如何去学习更加高深的知识。
那么针对这三类人,我给大家提供一个好的学习平台,免费领取视频教程,电子书籍,以及课程的源代码!
QQ群:1097524789

嘴唇识别

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

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

# 图片转 base64
pic_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_token
headers = {'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')

示例结果

总结

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

最新文章

  1. 用node搭建静态文件服务器
  2. SimpleDateFormat做成员或者静态成员多线程安全隐患
  3. IIS启动出错解决方法
  4. JSON的解析
  5. 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0[已解决]
  6. Realm
  7. 细说UI线程和Windows消息队列
  8. markdown学习经验
  9. Mysql 创建及导入表
  10. go语言生成一张正弦图
  11. [原][飞行仿真]helios与dcs world安装,详尽教程
  12. java面试题复习(一)
  13. 新建虚拟机并与XShell连接(配置网卡)
  14. Oracle Sql Loader的学习使用
  15. sqoop加载mysql数据库
  16. 潭州课堂25班:Ph201805201 WEB 之 页面编写 第四课 登录注册 (课堂笔记)
  17. [代码]--ORA-01843: 无效的月份
  18. weblogic12C出现“java.lang.ArrayIndexOutOfBoundsException: 48188”
  19. [2018HN省队集训D1T1] Tree
  20. linux命令总结之netstat命令

热门文章

  1. Scala 基础(十六):泛型、类型约束-上界(Upper Bounds)/下界(lower bounds)、视图界定(View bounds)、上下文界定(Context bounds)、协变、逆变和不变
  2. 数据可视化之powerBI基础(十六)PowerQuery的这个小功能,让你轻松发现数据质量问题
  3. Knn和K-means
  4. InnoDB表存储结构及keyring加密
  5. [Android] keytools生成jsk文件以及获取sha1码
  6. MySQL数据库使用报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  7. 使用MapReduce运行WordCount案例
  8. Python Ethical Hacking - TROJANS Analysis(4)
  9. Android 性能优化 ---- 内存优化
  10. echarts 踩坑 : 为什么效果出不来?看看有没有正确引入