Zhao Dongyu's Blog

A life which is unexamined is not worth living.

0%

Crawler

背景:国内表情识别的数据集太少了 & 表情数据集基本都是外国人脸,训出来的模型实际使用效果不好。

于是,在百度上爬了一批数据下来补充到数据集里面。

[爬虫有风险,操作需谨慎]

写在前面的话

一共两个文件

  • crawler.py

    主程序:实现爬虫功能,爬取的关键词在keywords.txt里面

  • keywords.txt

    关键词:比如爬开心的图,就可以设置 happy 高兴 高兴表情 高兴的人 高兴脸 开心 开心脸 开心的人 开心表情 欢愉 愉悦 愉悦表情 欢愉表情 幸福表情 幸福的脸 幸福的人 愉悦表情 欢欣表情 欢喜表情 喜悦

    每个关键词用换行分隔

运行python文件后,需要等待一会(正在获取图片的url),然后就显示下载log了。

结果会保存到image文件夹内,后续需要进一步批处理文件以及进行人脸提取、对齐、清洗等工作。

清洗数据集还是相当繁琐的,建议不要浪费时间在数据清洗上,自己想办法找途径处理。

[爬虫有风险,操作需谨慎]

希望你能把这个代码用在正途,造福社会:)

希望你能开源数据集,减少社会整体的成本:)

python代码

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

import re
import os
import sys
import shutil
import urllib
import requests
from tqdm import tqdm

download_path = './image/'

def get_onepage_urls(onepageurl):
"""获取单个翻页的所有图片的urls+当前翻页的下一翻页的url"""
if not onepageurl:
print('已到最后一页, 结束')
return [], ''
try:
html = requests.get(onepageurl)
html.encoding = 'utf-8'
html = html.text
except Exception as e:
print(e)
pic_urls = []
fanye_url = ''
return pic_urls, fanye_url
pic_urls = re.findall('"objURL":"(.*?)",', html, re.S)
fanye_urls = re.findall(re.compile(r'<a href="(.*)" class="n">下一页</a>'), html, flags=0)
fanye_url = 'http://image.baidu.com' + fanye_urls[0] if fanye_urls else ''
return pic_urls, fanye_url


def down_pic(pic_urls,label):
"""给出图片链接列表, 下载所有图片"""
#for i, pic_url in tqdm(enumerate(pic_urls)):
for i,pic_url in enumerate(pic_urls):
try:
pic = requests.get(pic_url, timeout=15)
string = download_path + label + '_baidu/' + str(i + 1) + '.jpg'
with open(string, 'wb') as f:
f.write(pic.content)
print('成功下载第(%s/%s)张图片: %s' % (str(i + 1), str(len(pic_urls)), str(pic_url)))
except Exception as e:
print('下载第%s张图片时失败: %s' % (str(i + 1), str(pic_url)))
#print(e)
continue


if __name__ == '__main__':
with open('keywords.txt', 'r', encoding='UTF-8') as f:
lines = f.read().splitlines()
print(lines)

for line in tqdm(lines):
file_path = os.path.join(download_path, line)
file_path = file_path + '_baidu'
if not os.path.exists(file_path):
os.makedirs(file_path, 0o755)
else:
shutil.rmtree(file_path)
os.makedirs(file_path, 0o755)

url_init_first = r'http://image.baidu.com/search/flip?tn=baiduimage&ipn=r&ct=201326592&cl=2&lm=-1&st=-1&fm=result&fr=&sf=1&fmq=1497491098685_R&pv=&ic=0&nc=1&z=&se=1&showtab=0&fb=0&width=&height=&face=0&istype=2&ie=utf-8&ctd=1497491098685%5E00_1519X735&word='
url_init = url_init_first + urllib.parse.quote(line, safe='/')
all_pic_urls = []
onepage_urls, fanye_url = get_onepage_urls(url_init)
all_pic_urls.extend(onepage_urls)

fanye_count = 0 # 累计翻页数
while 1:
onepage_urls, fanye_url = get_onepage_urls(fanye_url)
fanye_count += 1
#print('第页' % str(fanye_count))
if fanye_url == '' and onepage_urls == []:
break
all_pic_urls.extend(onepage_urls)

down_pic(list(set(all_pic_urls)),line)

keywords.txt

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
鄙视脸
鄙视表情
嫌恶
恶心表情
腻烦表情
厌烦表情
憎恶表情
反感表情
不耐烦
不耐烦表情
嫌弃表情
fear
恐惧
恐惧表情
恐惧的人
恐惧脸
害怕
害怕表情
害怕脸
恐慌
恐慌表情
恐慌脸
惊吓
惊怖表情
可骇表情
怯生生表情
胆怯表情
胆怯脸
恐慌的人
惧怕表情
惧怕的人
happy
高兴
高兴表情
高兴的人
高兴脸
开心
开心脸
开心的人
开心表情
欢愉
愉悦
愉悦表情
欢愉表情
幸福表情
幸福的脸
幸福的人
愉悦表情
欢欣表情
欢喜表情
喜悦
sad
悲伤
悲伤表情
悲伤的人
悲伤脸
伤心
伤心表情
难过
难过表情
难过的人
伤心的人
伤感表情
悲痛表情
哀伤
心酸表情
悲戚表情
哀痛的人
悲痛的人
哀伤脸
心酸脸
surprise
惊讶
惊讶表情
惊讶的人
惊讶脸
诧异
惊奇
惊奇表情
惊奇脸
诧异脸
诧异表情
吃惊
吃惊表情
吃惊脸
吃惊的人
惊诧
惊愕
惊诧表情
惊叫表情
惊叫脸
Thanks for your support.