人脸数据集——亚洲人脸数据集

本文介绍大规模亚洲人脸数据(主要是亚洲明星人脸数据集)的制作过程。先通过Python爬虫从百度搜索栏获取明星名字列表并保存为文本文件,再用开源网络爬虫从Bing搜索爬取明星图片,最后使用face_recognition库对爬取的图片进行粗略清洗,删除非人脸图像。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 大规模亚洲人脸数据的制作

在这次大规模亚洲人脸数据制作主要是亚洲明星人脸数据集,此次我爬取了大概20万张亚洲人脸图像,可以修改爬取每位明星图片的数量来获取更多的图片,过程中主要分以下几步:

  • 获取明星名字列表

(1)、首先从百度搜索栏中搜索“明星”,显示出明星栏目,地区包括内地、香港、台湾、韩国和日本,如下图:

(2)、使用python爬虫将这些明星的名字爬取下来,代码如下所示:

import os
import time
import json
import requests

def getManyPages(pages):
    params=[]
    for i in range(0, 12*pages+12, 12):
        params.append({
            'resource_id': 28266,
            'from_mid': 1,
            'format': 'json',
            'ie': 'utf-8',
            'oe': 'utf-8',
            'query': '台湾明星',
            'sort_key': '',
            'sort_type': 1,
            'stat0': '',
            'stat1': '台湾',
            'stat2': '',
            'stat3': '',
            'pn': i,
            'rn': 12
                  })
    url = 'https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php'
#    names = []
#    img_results = []
    x = 0
    f = open('starName.txt', 'w')
    for param in params:
        try:
            res = requests.get(url, params=param)
            js = json.loads(res.text)
            results = js.get('data')[0].get('result')
        except AttributeError as e:
            print(e)
            continue
        for result in results:
            img_name = result['ename']
#            img_url = result['pic_4n_78']
#            img_result =  [img_name,img_url]
#            img_results.append(img_result)
            f.write(img_name+'\n')
    #        names.append(img_name)

        if x % 10 == 0:
            print('第%d页......'%x)
        x += 1
    f.close()

if __name__ == '__main__':
    getManyPages(400)

这里需要注意:params里的‘query’:‘台湾明星’;‘start1’:‘台湾’,对应台湾地区的明星,修改这两个值可以获得‘内地’、‘香港’、‘韩国’等地区的明星。从图一可以看出,每页有12位明星,getManyPages(400)是获取400页的明星名单结果,也就是12*400=4800位明星名单,通过修改获取页码值来获取更多明星的名单,将获取的明星名单保存成文本文件,在后续操作中将会用到,同时也能避免代码终止又要重新爬取。

  • 根据名单列表爬取明星图片

这里我将使用开源的网络爬虫来爬取明星图片,需要导入icrawler库,对该库的详细介绍,读者可以自己百度;由于一些小明星的照片在百度上有可能搜不到,并且百度上照片有时会很杂,所以这里我将在Bing搜索上爬取明星人脸图片,代码如下:

import os
from icrawler.builtin import BingImageCrawler
path = r'E:\weather_test\BingImage'
f = open('KoreaStarName.txt', 'r')
lines = f.readlines()
for i, line in enumerate(lines):
    name = line.strip('\n')
    file_path = os.path.join(path, name)
    if not os.path.exists(file_path):
        os.makedirs(file_path)
    bing_storage = {'root_dir': file_path}
    bing_crawler = BingImageCrawler(parser_threads=2, downloader_threads=4, storage=bing_storage)
    bing_crawler.crawl(keyword=name, max_num=10)
    print('第{}位明星:{}'.format(i, name))

其中,path为将要保存爬取的明星图片的路径,可以.txt文件为上一步爬取的明星名单文件,通过修改

bing_crawler.crawl()中的max_num的值来改变爬取每位明星的图像数目。

 

  • 对爬取的明星图片做粗略清洗

这里主要针对非人脸图像的清洗,由于爬取的图像里有可能出现非人像图片,需要对它进行删除,通过对图像人脸判断来确认是否包含人脸图像,代码如下:

其中,使用face_recognition库检测图像上是否有人脸,该库在windows上的安装比较麻烦,读者可以百度,有详细的windows安装教程,同时也使用了一些容错代码,避免出现错误,代码运行中断。path为爬取明星图片的文件目录,new_path为清洗后保存到的人脸图像目录。

import os
import face_recognition
from PIL import Image
from PIL import ImageFile
import threading
ImageFile.LOAD_TRUNCATED_IMAGES = True

def process_img(path, new_path):
    dirs = os.listdir(path)
    for pic_dir in dirs:
        print(pic_dir)
        dir_path = os.path.join(path, pic_dir)
        pics = os.listdir(dir_path)
        for pic in pics:
            pic_path = os.path.join(dir_path, pic)
            image = face_recognition.load_image_file(pic_path)
            face_locations = face_recognition.face_locations(image)
            if len(face_locations) == 0:
                continue
            img = Image.open(pic_path)
            new_pic_path = os.path.join(new_path, pic_dir)
            if not os.path.exists(new_pic_path):
                os.makedirs(new_pic_path)
            if len(img.split()) == 4:
                # 利用split和merge将通道从四个转换为三个
                r, g, b, a = img.split()
                toimg = Image.merge("RGB", (r, g, b))
                toimg.save(new_pic_path + '\\' + pic)
            else:
                try:
                    img.save(new_pic_path + '\\' + pic)
                except:
                    continue
        print('Finish......!')

def lock_test(path, new_path):
    mu = threading.Lock()
    if mu.acquire(True):
        process_img(path, new_path)
        mu.release()

if __name__ == '__main__':
    paths = [r'E:\weather_test\亚洲人脸4_1', r'E:\weather_test\亚洲人脸4_2', r'E:\weather_test\亚洲人脸4_3',
             r'E:\weather_test\亚洲人脸4_4', r'E:\weather_test\亚洲人脸4_5', r'E:\weather_test\亚洲人脸4_6']
    new_paths = [r'E:\weather_test\4_1', r'E:\weather_test\4_2', r'E:\weather_test\4_3', r'E:\weather_test\4_4',
                 r'E:\weather_test\4_5', r'E:\weather_test\4_6']
    for i in range(len(paths)):
        my_thread = threading.Thread(target=lock_test, args=(paths[i], new_paths[i]))
        my_thread.start()
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值