-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
375 lines (317 loc) · 11.1 KB
/
Copy pathmain.py
File metadata and controls
375 lines (317 loc) · 11.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import os
import shutil
from flask import Flask, jsonify, render_template
import pyecharts
from pyecharts import options as opts
from pyecharts.charts import Bar, Pie, Map
import Crawler
import base64
import os
import webbrowser
import sqlite3
from pyecharts.charts import *
from pyecharts import options as opts
from pyecharts.globals import ThemeType # 主题
from aip import AipFace
import Crawler
import shutil
import os
import shutil
from icrawler.builtin import BingImageCrawler
# class Database:
# def work(self, continent="Global"):
# print(f"为 {continent} 准备数据...")
# if continent == "Asia":
# return [100, 80, 110, 130, 95]
# elif continent == "Europe":
# return [120, 110, 90, 80, 100]
# elif continent == "Africa":
# return [90, 85, 95, 105, 100]
# elif continent == "South America":
# return [50, 67, 89, 101, 120]
# elif continent == "North America":
# return [70, 56, 78, 120, 100]
# elif continent == "Oceania":
# return [78, 88, 95, 100, 110]
# else:
# return [200, 150, 180, 220, 190]
#
"""三码"""
APP_ID = '119428112'
API_KEY = 'ayVbPc13C8R90cmCGfs30ui5'
SECRET_KEY = 'ClFlNrSPYxsu3wmsFRMrYMqQtF583N7r'
from icrawler.builtin import BingImageCrawler
keyword = ''
count = [] # 存取每个分类的图片数量
def bing_image_crawler(keyword, max_num):
dic = 'bing_img'
if os.path.exists(dic):
shutil.rmtree(dic)
bing_crawler = BingImageCrawler(
feeder_threads=1,
parser_threads=2,
downloader_threads=4,
storage={'root_dir': 'bing_img'}) # 修改存储文件夹名称
# 设置筛选条件
# filters = dict(
# type='photo', # 确保是照片
# color='blackandwhite', # 黑白风格
# size='large', # 大尺寸
# layout='wide' # 宽幅图片,更可能是合照
# )
# 开始爬取
# 参数1-keyword:关键字(改为"时代少年团 合照")
# 参数2-filters:筛选器
# 参数3-max_num:最大尝试数量
bing_crawler.crawl(
keyword=keyword, # 修改关键词
# filters=filters,
max_num=max_num
)
# 创建人脸检测对象
def get_file_content(file_path):
"""
文件转BASE64编码字符串
:param file_path:文件的路径
:return: 转船之后的结果
"""
file = open(file_path, 'rb') # 使用只读模式打开文件
data = file.read() # 读取文件数据流
content = base64.b64encode(data) # BASE64编码
file.close() # 关闭文件流
base = content.decode('utf-8') # 8位的通用编码
return base
def detect_face(base):
"""
送给百度服务器进行人脸检测
:param client:百度人脸检测对象
:param base: BASE64编码之后的图片,支持PNG,JPG,JPEG,BMP
:return: 服务器返回的的JSON数据
"""
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
options = {'face_field': 'beauty,face_shape,landmark,glasses'}
json = client.detect(base, 'BASE64', options)
return json
def parse_json(json, keyword):
"""
解析服务器返回的JSON数据
:param json:
:return: 解析出的颜值,如果监测失败返回-1
"""
# 判断是否成功
code = json['error_code']
if code == 0:
# 成功检测到人脸,返回研颜值分数(十分制)
beauty = int(json['result']['face_list'][0]['beauty'] / 10) + 1
face_shape = json['result']['face_list'][0]['face_shape']['type']
landmark = json['result']['face_list'][0]['landmark']
glasses = json['result']['face_list'][0]['glasses']['type']
eyes_instance = landmark[1]['x'] - landmark[0]['x']
mouth_instance = landmark[3]['y'] - landmark[2]['y']
proportion = eyes_instance / mouth_instance
else:
beauty = -1
face_shape = 'null'
landmark = 'null'
glasses = 'null'
eyes_instance = 'null'
mouth_instance = 'null'
proportion = 'null'
result = (keyword, beauty, face_shape, proportion, glasses)
return result
def save_data2db(keyword, y_axis):
conn = sqlite3.connect('data_analyze00.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS facial_features (
id INTEGER PRIMARY KEY AUTOINCREMENT,
keyword TEXT,
beauty INT,
face_shape TEXT,
proportion REAL,
glasses TEXT
)
''')
for i in y_axis:
sql = '''
INSERT INTO facial_features(keyword,beauty,face_shape,proportion,glasses)
VALUES(?,?,?,?,?);
'''
# 替换占位符并执行3条插入操作
# 参数1:预处理的SQL语句(包含占位符)
# 参数2:要替换的参数列表(本例中包含3个元素)
cursor.execute(sql, i)
# 多条数据可以合并为一个列表
# 准备一个预处理的SQL语句
# 提交事务
conn.commit()
cursor.close()
conn.close()
def classify():
"""
先爬取图片,百度检测每一张图片,在windows中按照颜值分拣成不同的文件夹
:return: TODO
"""
root_dir = 'bing_img' # 图片根目录
file_list = os.listdir(root_dir) # 把文件夹的文件名都存到列表中
# 遍历图片
for i in file_list:
# 拼接处文件路径
path = root_dir + '/' + i
print(path)
# 判断是否是文件
if os.path.isfile(path):
base = get_file_content(path) # BASE64
json = detect_face(base) # 百度处理
global count
result = parse_json(json, keyword)
count.append(result)
# TODO把图片分类到不同目录下
dic = root_dir + '/' + str(keyword) # 文件夹路径
if not os.path.exists(dic): # 如果文件夹不存在
os.makedirs(dic) # 创建文件夹
# 移动文件到所属的文件位置
# 参数1:源文件的位置
# 参数2:目标文件的位置
os.rename(path, dic + '/' + i)
def select_data():
conn = sqlite3.connect('data_analyze00.db')
# 创建一个游标对象,用于执行SQL语句等操作
cursor = conn.cursor()
# 查询
sql = 'SELECT * FROM facial_features;'
cursor.execute(sql)
rows = cursor.fetchall() # 拿到所有查询的结果
# # 遍历输出每行数据
# for row in rows:
# print(row) # 返回值元组
# 关闭游标和连接
cursor.close()
conn.close()
return rows
app = Flask(__name__, template_folder='.')
name = ["亚洲人", "欧洲人", "非洲人", "南美洲人", "北美洲人", "大洋洲人"]
@app.route('/')
def index():
return render_template('index.html')
@app.route('/api/data/<string:continent>')
def get_data(continent):
sample_data = select_data()
continent_map = {
"Asia": "亚洲人", "Europe": "欧洲人", "Africa": "非洲人",
"South America": "南美洲人", "North America": "北美洲人",
"Oceania": "大洋洲人"
}
cnt = [0]*5
shape = [0]*5
arr = [0]*5
glass = [0]*2
if continent == "Global":
target_list = None
else:
target_list = { continent_map[continent] }
for (_id, kw, beauty, fs, prop, gls) in sample_data:
if beauty < 0:
continue
# 如果 target_list 非空,就过滤掉不在其中的
if target_list is not None and kw not in target_list:
continue
# 颜值分段
if beauty <= 2: cnt[0] += 1
elif beauty <= 4: cnt[1] += 1
elif beauty <= 6: cnt[2] += 1
elif beauty <= 8: cnt[3] += 1
else: cnt[4] += 1
# 脸型
if fs == "square": shape[0] += 1
elif fs == "triangle": shape[1] += 1
elif fs == "oval": shape[2] += 1
elif fs == "heart": shape[3] += 1
elif fs == "round": shape[4] += 1
# 五官比例
if prop <= 1.5: arr[0] += 1
elif prop <= 2.0: arr[1] += 1
elif prop <= 2.5: arr[2] += 1
elif prop <= 3.0: arr[3] += 1
else: arr[4] += 1
# 眼镜
if gls == 'none':
glass[0] += 1
else:
glass[1] += 1
print(*cnt)
print(*shape)
beauty_chart = (
Bar()
.add_xaxis(['0-2', '2-4', '4-6', '6-8', '8-10'])
.add_yaxis('人数', cnt)
.set_global_opts(
title_opts=opts.TitleOpts(title=""),
legend_opts=opts.LegendOpts(is_show=False),
)
)
faceshape_chart = (
Pie()
.add("", [("正方形", shape[0]), ("三角形", shape[1]), ("椭圆形", shape[2]),("心形",shape[3]),("圆形",shape[4])], radius=["40%", "75%"])
.set_global_opts(
legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
)
)
features_chart = (
Bar()
.add_xaxis(["1.0~1.5","1.5~2.0","2.0~2.5","2.5~3.0","<1.0 || >3.0"])
.add_yaxis("人数", arr)
.set_global_opts(
title_opts=opts.TitleOpts(title=""),
legend_opts=opts.LegendOpts(is_show=False),
xaxis_opts=opts.AxisOpts(name="五官比例", axislabel_opts=opts.LabelOpts(rotate=30)),
yaxis_opts=opts.AxisOpts(name="人数", axislabel_opts=opts.LabelOpts(formatter="{value}%")),
)
)
glass_data = [
("未佩戴", glass[0]),
("佩戴", glass[1])
]
glasses_chart = (
Pie()
.add("", glass_data, radius=["40%", "75%"])
.set_global_opts(
legend_opts=opts.LegendOpts(orient="vertical", pos_top="15%", pos_left="2%"),
)
)
world_map_chart = (
Map()
.add("", [("China", 100)], "world", is_map_symbol_show=False)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(is_show=False),
)
)
response_data = {
"viewName": "全球汇总" if continent == "Global" else continent,
"beautyScoreOptions": beauty_chart.dump_options_with_quotes(),
"faceshapeOptions": faceshape_chart.dump_options_with_quotes(),
"featuresOptions": features_chart.dump_options_with_quotes(),
"glassesOptions": glasses_chart.dump_options_with_quotes(),
"worldMapOptions": world_map_chart.dump_options_with_quotes(),
}
return jsonify(response_data)
if __name__ == '__main__':
# keyword=input("请输入关键词:")
# num=int(input("数量:"))
name=["亚洲人","欧洲人","非洲人","南美洲人","北美洲人","大洋洲人"]
# imgpath = 'imgs'
# for keyword in name:
# print("正在爬取", keyword, "数据...")
# bing_image_crawler(keyword, 10)
# classify()
# save_data2db(keyword, count)
# loc = os.path.join(imgpath, 'test',keyword)
# if os.path.exists(loc):
# shutil.rmtree(loc)
# os.makedirs(loc, exist_ok=True)
# clr = Crawler.Crawler(keyword,num,loc)
# clr.run()
sample_data=select_data()
app.run()
print("Accepted.")