-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyanzhi.py
173 lines (150 loc) · 5.95 KB
/
yanzhi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# coding:utf-8
# author: Livingbody
# date: 2020.05.06
import ssl
import json
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
import cv2
from PIL import Image, ImageDraw, ImageFont
import numpy
ssl._create_default_https_context = ssl._create_unverified_context
API_KEY = 'eMgcu4RUSI4Vfo0nLR5OYaXU'
SECRET_KEY = 'kc2oB7wWKF88nNhXx4p9WRnRRzVlyYCM'
FACE_DETECT = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
""" TOKEN start """
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
from flask import Flask, render_template, request, jsonify
from werkzeug.utils import secure_filename
import os
import requests
import base64
import time
from flask import Blueprint, render_template
index_yanzhi = Blueprint("yanzhi", __name__)
# 设置允许的文件格式
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'bmp', 'jpeg'])
# 当前文件所在路径
basepath = os.path.dirname(__file__)
def allowed_file(filename):
filename = filename.lower()
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
# 上传并抠图
@index_yanzhi.route('/yanzhi', methods=['POST', 'GET']) # 添加路由
def myyanzhi():
if request.method == 'POST':
try:
f = request.files['file']
if not (f and allowed_file(f.filename)):
return render_template('404.html')
sourcefile = os.path.join('static/images/source', secure_filename(f.filename))
upload_path = os.path.join(basepath, sourcefile) # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
f.save(upload_path)
filename = yanzhi_fun(upload_path)
filename = os.path.join('static/images/target', secure_filename(filename))
print(filename)
return render_template('yanzhi_ok.html', val1=time.time(), sourcefile=sourcefile, filename=filename)
except:
return render_template('404.html')
return render_template('yanzhi.html')
def yanzhi_fun(filename):
token = fetch_token()
# concat url
url = FACE_DETECT + "?access_token=" + token
file_content = read_file(filename)
response = myrequest(url, urlencode(
{
'image': base64.b64encode(file_content),
'image_type': 'BASE64',
'face_field': 'gender,age,beauty,glasses',
'max_face_num': 10
}))
data = json.loads(response)
img = cv2.imread(filename)
temp_img = img.copy()
result = data['result']
face_num = result['face_num']
targetfilename = 'yanzhi' + str(time.time()) + os.path.splitext(filename)[-1]
real_targetfilename = os.path.join(basepath, 'static/images/target', targetfilename)
print(targetfilename + 50 * '*')
if face_num > 0:
face_list = result['face_list']
for i in range(face_num):
temp_face = face_list[i]
temp_face_location = temp_face['location']
left = int(temp_face_location['left'])
top = int(temp_face_location['top'])
width = int(temp_face_location['width'])
height = int(temp_face_location['height'])
if face_list[i]["gender"]["type"] == "male":
gender = "男"
# female face
if face_list[i]["gender"]["type"] == "female":
gender = "女"
beauty = face_list[i]["beauty"]
simple_info = " 年龄: " + str(face_list[i]["age"]) + " 颜值:" + str(beauty)
cv2.rectangle(temp_img, (left, top), (left + width, top + height), (0, 255, 0), 1)
temp_img = cv2ImgAddText(temp_img, simple_info, left, top)
cv2.imwrite(real_targetfilename, temp_img, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
return targetfilename
def cv2_to_base64(image):
data = cv2.imencode('.jpg', image)[1]
return base64.b64encode(data.tostring()).decode('utf8')
def myrequest(url, data):
req = Request(url, data.encode('utf-8'))
has_error = False
try:
f = urlopen(req)
result_str = f.read()
result_str = result_str.decode()
return result_str
except URLError as err:
print(err)
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, numpy.ndarray)): # 判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式
fontStyle = ImageFont.truetype(
"Alibaba-PuHuiTi-Regular.ttf", textSize, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, textColor, font=fontStyle)
# 转换回OpenCV格式
return cv2.cvtColor(numpy.asarray(img), cv2.COLOR_RGB2BGR)
def read_file(image_path):
f = None
try:
f = open(image_path, 'rb')
return f.read()
except:
print('read image file fail')
return None
finally:
if f:
f.close()
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print('url error:' + 50 * '*')
print(err)
result_str = result_str.decode()
result = json.loads(result_str)
if ('access_token' in result.keys() and 'scope' in result.keys()):
if not 'brain_all_scope' in result['scope'].split(' '):
print('please ensure has check the ability')
exit()
return result['access_token']
else:
print('please overwrite the correct API_KEY and SECRET_KEY')
exit()