使用 Python 为女神挑选口红
口红对于女生来说永远不嫌多,而男生也搞不明白珊瑚红、番茄色、斩男色等等颜色有什么区别,不都是红色么?当送给女神的口红是她不适合的,那结果就是口红进入垃圾箱还算是轻的,重则拉黑处理。男生们也不用着急,我们可以用 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')
示例结果
总结
通过上面的代码,我们已经可以为女神选出一支适合的口红,祝愿小伙伴们送女神口红都可以成功
使用 Python 为女神挑选口红的更多相关文章
- python学习 -女神或者男神把微信消息撤回后好慌,有了这个妈妈再也不担心你看不到女神或者男神撤回的消息了(超详解)
简介 有时候在忙工作,女朋友发了一个消息,就撤回了,但是人天生的都有一颗好奇心,而且在当今这个时代找个女朋友不容易,一个程序猿找一个女朋友更是不容易的.人家好不容易跟你,你还不得把人家当老佛爷侍候着, ...
- PyCon大会Python主题演讲摘要
PyCon 是全国际最大的以 Python 编程言语 为主题的技能大会.大会由 Python 社区组织,每年举行一次.在大会上,来自国际各地的 Python 用户与中心开发者齐聚一堂,共同同享 Pyt ...
- “全能”选手—Django 1.10文档中文版Part3
Django 1.10官方文档的入门教程已经翻译完毕,后续的部分将不会按照顺序进行翻译,而是挑重点的先翻译. 有兴趣的可以关注我的博客. 第一部分传送门 第二部分传送门 第四部分传送门 3.2 模型和 ...
- Django 1.10文档中文版Part3
目录 2.7 第一个Django app,Part 5:测试 2.7.1 自动化测试介绍 2.7.2 基本的测试策略 2.7.3 编写我们的第一个测试程序 2.7.4 测试一个视图 2.7.5 测试越 ...
- Django 1.10中文文档-第一个应用Part5-测试
本教程上接教程Part4. 前面已经建立一个网页投票应用,现在将为它创建一些自动化测试. 自动化测试简介 什么是自动化测试 测试是检查你的代码是否正常运行的行为.测试也分为不同的级别.有些测试可能是用 ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- 向Python女神推荐这些年我追过的经典书籍
http://blog.csdn.net/yueguanghaidao/article/details/10416867 最近"瑞丽模特学Python"的热点牵动了大江南北程序员的 ...
- 利用Python完成一个小游戏:随机挑选一个单词,并对其进行乱序,玩家要猜出原始单词
一 Python的概述以及游戏的内容 Python是一种功能强大且易于使用的编程语言,更接近人类语言,以至于人们都说它是“以思考的速度编程”:Python具备现代编程语言所应具备的一切功能:Pytho ...
- python 让挑选家具更方便
原文链接:https://mp.weixin.qq.com/s/tQ6uGBrxSLfJR4kk_GKB1Q 家中想置办些家具,听朋友介绍说苏州蠡(li第二声)口的家具比较出名,因为工作在苏州,也去那 ...
随机推荐
- scrapy 源码解析 (三):启动流程源码分析(三) ExecutionEngine执行引擎
ExecutionEngine执行引擎 上一篇分析了CrawlerProcess和Crawler对象的建立过程,在最终调用CrawlerProcess.start()之前,会首先建立Execution ...
- StringTable结构以及基本调优
StringTable底层实现类似于HashTable,由数组和链表组成,数组又称为桶数组.比如有这样一段代码: public class Demo4 { public static void mai ...
- OSCP Learning Notes - Buffer Overflows(2)
Finding the Offset 1. Use the Metasploite pattern_create.rb tool to create 5900 characters. /usr/sha ...
- OSCP Learning Notes - Post Exploitation(2)
Windows Post Exploitation Target Server: IE8-Win 7 VM 1. Download and upload the fgdump, PwDump7, wc ...
- Python Ethical Hacking - MODIFYING DATA IN HTTP LAYER(3)
Recalculating Content-Length: #!/usr/bin/env python import re from netfilterqueue import NetfilterQu ...
- css :clip rect 正确的使用方法
CSS clip 是一个极少使用,但又确实存在的属性. 而且,它其实在CSS2时代就有了. w3school 上有这么一句话: clip 属性剪裁绝对定位元素. 也就是说,只有 position:ab ...
- swfupload控件文件上传大小限制设置
swfupload控件,是我在开发过程中用到的上传文件的控件,非常实用和方便.但最近碰到一些问题,解决之后进行一下整理. 因为用户上传文件的大小限制增加,导致原本上传控件时,文件的大小需要进行调整和限 ...
- WebApi部署多服务器配置Nginx负载均衡
01PARTCoreWebApi教程本地演示环境 Visual Studio2019 --- Vsersion:16.4.4 + NetCore3.1.2 02PARTNginx快速搭建配置负载均衡 ...
- mybatis sqlsession与sqlsquery、transaction、connection
sqlsession和connection 一个sqlsession一般对应一个connection,并且mybatis默认每次获取session都会开启一个事务,且不自动提交事务.如果更新操作完成后 ...
- @Autowired @Qualifier
spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProcessor完成Bean ...