|
| 1 | +import base64 |
| 2 | + |
| 3 | +import requests |
| 4 | +from PIL import Image, ImageDraw |
| 5 | + |
| 6 | + |
| 7 | +ak = 'MbDXGOrgXHqsgHKlAZLv6K93' |
| 8 | + |
| 9 | +sk = 'fzIOiK2aEAKntAY7cOEHkUCoZOawe0wR' |
| 10 | + |
| 11 | +# client_id 为官网获取的AK, client_secret 为官网获取的SK |
| 12 | +host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id='+ak+'&client_secret=' + sk |
| 13 | +response = requests.get(host) |
| 14 | +if response: |
| 15 | + access_token = response.json()['access_token'] |
| 16 | + print(access_token) |
| 17 | +else: |
| 18 | + raise Exception('access_token 获取失败') |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +# 图片转 base64 |
| 23 | +pic_path = '/Users/xx/Desktop/kh/原图.png' |
| 24 | +with open (pic_path, 'rb') as f: |
| 25 | + base64_data = base64.b64encode(f.read()) |
| 26 | + |
| 27 | +# image:图片,image_type:图片格式,face_field:请求的结果,landmark150为人脸的 150 个关键点 |
| 28 | +params = '{"image":"'+base64_data.decode('utf-8')+'","image_type":"BASE64","face_field":"landmark150"}' |
| 29 | +request_url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect?access_token=' + access_token |
| 30 | +headers = {'content-type': 'application/json'} |
| 31 | +response = requests.post(request_url, data=params, headers=headers) |
| 32 | + |
| 33 | +if response: |
| 34 | + face = response.json() |
| 35 | +else: |
| 36 | + raise Exception('人脸关键点获取失败') |
| 37 | + |
| 38 | + |
| 39 | +# 上嘴唇关键点,按顺时针方向的顺序组成一个多边形 |
| 40 | +mouth_lip_upper_point_list = [ |
| 41 | + 'mouth_corner_right_outer','mouth_lip_upper_outer_1','mouth_lip_upper_outer_2','mouth_lip_upper_outer_3', |
| 42 | + 'mouth_lip_upper_outer_4','mouth_lip_upper_outer_5','mouth_lip_upper_outer_6','mouth_lip_upper_outer_7', |
| 43 | + 'mouth_lip_upper_outer_8','mouth_lip_upper_outer_9','mouth_lip_upper_outer_10','mouth_lip_upper_outer_11', |
| 44 | + 'mouth_corner_left_outer','mouth_corner_left_inner','mouth_lip_upper_inner_11','mouth_lip_upper_inner_10', |
| 45 | + 'mouth_lip_upper_inner_9','mouth_lip_upper_inner_8','mouth_lip_upper_inner_7','mouth_lip_upper_inner_6', |
| 46 | + 'mouth_lip_upper_inner_5','mouth_lip_upper_inner_4','mouth_lip_upper_inner_3','mouth_lip_upper_inner_2', |
| 47 | + 'mouth_lip_upper_inner_1','mouth_corner_right_inner','mouth_corner_right_outer' |
| 48 | +] |
| 49 | + |
| 50 | +# 下嘴唇关键点,按顺时针方向的顺序组成一个多边形 |
| 51 | +mouth_lip_low_point_list = [ |
| 52 | + 'mouth_corner_right_outer','mouth_corner_right_inner','mouth_lip_lower_inner_1','mouth_lip_lower_inner_2', |
| 53 | + 'mouth_lip_lower_inner_3','mouth_lip_lower_inner_4','mouth_lip_lower_inner_5','mouth_lip_lower_inner_6', |
| 54 | + 'mouth_lip_lower_inner_7','mouth_lip_lower_inner_8','mouth_lip_lower_inner_9','mouth_lip_lower_inner_10', |
| 55 | + 'mouth_lip_lower_inner_11','mouth_corner_left_outer','mouth_lip_lower_outer_11','mouth_lip_lower_outer_10', |
| 56 | + 'mouth_lip_lower_outer_9','mouth_lip_lower_outer_8','mouth_lip_lower_outer_7','mouth_lip_lower_outer_6', |
| 57 | + 'mouth_lip_lower_outer_5','mouth_lip_lower_outer_4','mouth_lip_lower_outer_3','mouth_lip_lower_outer_2', |
| 58 | + 'mouth_lip_lower_outer_1','mouth_corner_right_outer' |
| 59 | +] |
| 60 | + |
| 61 | + |
| 62 | +# 将将转为可操作的 RGBA 模式 |
| 63 | +img = Image.open(pic_path) |
| 64 | +d = ImageDraw.Draw(img, 'RGBA') |
| 65 | + |
| 66 | +for f in face['result']['face_list']: |
| 67 | + # 上嘴唇关键点 [(x,y),(x,y),(x,y)] 元组列表 |
| 68 | + mouth_lip_upper_list = [] |
| 69 | + # 下嘴唇关键点 [(x,y),(x,y),(x,y)] 元组列表 |
| 70 | + mouth_lip_low_list = [] |
| 71 | + |
| 72 | + for point in mouth_lip_upper_point_list: |
| 73 | + p = f['landmark150'][point] |
| 74 | + mouth_lip_upper_list.append((p['x'], p['y'])) |
| 75 | + |
| 76 | + for point in mouth_lip_low_point_list: |
| 77 | + p = f['landmark150'][point] |
| 78 | + mouth_lip_low_list.append((p['x'], p['y'])) |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + # 口红颜色 |
| 83 | + hex = input('请输入口红的16进制颜色:') |
| 84 | + color = (int(hex[1:3], 16), int(hex[3:5], 16), int(hex[5:7], 16)) |
| 85 | + |
| 86 | + # 绘制多边形并填充颜色 |
| 87 | + d.polygon(mouth_lip_upper_list, fill=color) |
| 88 | + # 绘制边框并填充颜色 |
| 89 | + d.line(mouth_lip_upper_list, fill=color, width = 1) |
| 90 | + |
| 91 | + d.polygon(mouth_lip_low_list, fill=color) |
| 92 | + d.line(mouth_lip_low_list, fill=color, width=1) |
| 93 | + |
| 94 | +img.show() |
| 95 | +img.save('/Users/xx/Desktop/kh/' + hex + '.png') |
| 96 | + |
| 97 | + |
| 98 | + |
0 commit comments