-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqwe.py
More file actions
181 lines (176 loc) · 5.77 KB
/
Copy pathqwe.py
File metadata and controls
181 lines (176 loc) · 5.77 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
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
"""三码"""
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):
"""
解析服务器返回的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']
landmark=json['result']['face_list'][0]['landmark']
glasses=json['result']['face_list'][0]['glasses']
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=(beauty,face_shape,proportion,glasses)
return result
def save_data2db(keyword,y_axis):
"""
存储数据分析的结果到数据库
:param keyword: 关键字
:param y_axis: 颜值的y轴数据
"""
# 连接到SQLite
# 参数为数据库文件名称
# 返回值:数据库连接对象
conn = sqlite3.connect('data_analyze00.db')
# 创建一个游标对象,用于执行SQL语句等操作
cursor = conn.cursor()
# 执行建表语句
cursor.execute('''
CREATE TABLE IF NOT EXISTS facial_features (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
keyword TEXT,
beauty INT,
face_shape TEXT,
proportion REAL,
glasses TEXT
)
''')
# 插入数据
# 手上已有的数据
# 组装已有数据为一个元组
y_axis.insert(0, keyword)
params = tuple(y_axis)
# 多条数据可以合并为一个列表
# 准备一个预处理的SQL语句
sql = '''
INSERT INTO facial_features(keyword,beauty,face_shape,proportion,glasses)
VALUES(?,?,?,?,?);
'''
# 替换占位符并执行3条插入操作
# 参数1:预处理的SQL语句(包含占位符)
# 参数2:要替换的参数列表(本例中包含3个元素)
cursor.execute(sql, params)
# 提交事务
conn.commit()
cursor.close()
conn.close()
def classify():
"""
先爬取图片,百度检测每一张图片,在windows中按照颜值分拣成不同的文件夹
:return: TODO
"""
root_dir='bing_img'#图片根目录
file_list=os.listdir(root_dir)#把文件夹的文件名都存到列表中
print(file_list)
#遍历图片
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)
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)
if __name__ == '__main__':
keyword = input('请输入爬取的关键字')
bing_image_crawler(keyword, 10)
# base=get_file_content('./bing_img/000001.jpg')
# json=detect_face(base)
# beauty=parse_json(json)
# print(beauty)
classify()
save_data2db(keyword, count)