Python爬取图片+百度人脸检测过滤高颜值美女
pexels网站提供了大量贴图,从中搜索美女图片,编写爬虫进行下载,下载后图片中除了女人外,还包含男人,风景、静物和动物,调用百度人脸检测模块识别检测,将其中颜值大于60分的美女保存到另外一个文件夹。爬取图片共计1251张,最后过滤出的美女共计287张。上代码:
想要学习Python。关注小编头条号,私信【学习资料】,即可免费领取一整套系统的板Python学习教程!
爬虫程序:
from bs4 import BeautifulSoup import requests import os import time save_path = 'F://photos/' url_path = 'https://www.pexels.com/search/' headers ={ 'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36' } searchWord = 'beauty' urls = [url_path+searchWord+'/?page={}'.format(str(i)) for i in range(1,100)] if not os.path.exists(save_path): os.mkdir(save_path) page =1 for url in urls: img_list = [] wb_data = requests.get(url,headers=headers) print("当前爬取页面链接",url) soup = BeautifulSoup(wb_data.text,'lxml') imgs = soup.select('article > a > img') for img in imgs: photo_src = img.get('src') img_list.append(photo_src) print("第{}页,共计{}张图片".format(page,len(img_list))) for item in img_list: data = requests.get(item, headers=headers) fp = open(save_path+item.split('?')[0][-10:],'wb') fp.write(data.content) fp.close() page = page+1 time.sleep(2)
颜值检测程序:
from aip import AipFace import base64 import os import time import shutil #接入百度AI人脸识别的参数 APP_ID="换成你自己的" API_KEY = "换成你自己的" SECRET_KEY = "换成你自己的" imageType = "BASE64" options = {} options["face_field"] = "gender,beauty" options["face_type"] = "LIVE" #下载图片和筛选图片的文件夹 file_path = 'F://photos/' copy_file_path = 'F://highScore/' file_lists=os.listdir(file_path) aipFace =AipFace(APP_ID,API_KEY,SECRET_KEY) #将图片转换为BASE64格式,这是百度平台的要求 def get_file_content(filePath): with open(filePath,'rb') as fp: content = base64.b64encode(fp.read()) return content.decode('utf-8') for file_list in file_lists: result = aipFace.detect(get_file_content(os.path.join(file_path,file_list)),imageType,options) error_code = result['error_code'] if error_code == 222202: #没有人脸 continue if error_code==223110: #人脸太多 continue try: sex_type = result['result']['face_list'][-1]['gender']['type'] #只要美女图片 if sex_type == 'male': continue beauty = result['result']['face_list'][-1]['beauty'] new_beauty = round(beauty/10,1) print(file_list,new_beauty) if new_beauty>=6: copy_src = os.path.join(file_path,str(new_beauty)+'_'+file_list) copy_dst = os.path.join(copy_file_path,str(new_beauty)+'_'+file_list) #重命名高分照片 os.rename(os.path.join(file_path,file_list),copy_src) #复制高分照片到另外的照片 shutil.copyfile(copy_src,copy_dst) time.sleep(1) except KeyError: pass except TypeError: pass
爬取的图片:
过滤后的图片:
看看最高颜值的美女:
较低颜值的女性:
相关推荐
85206537 2015-08-20
89266339 2015-08-01
wilylcyu 2019-06-06
赚钱生意 2015-05-26
leeyhCoding 2017-11-14
点击看视频直播 2017-03-22
专看视频直播 2017-10-25
listep 2019-03-04
TURBOTX 2017-08-28
TomRobot 2017-08-26
JamesRayMurphy 2018-10-20
katyusha 2018-09-23
minger0 2018-09-11
qiagu 2016-11-03
走过程序员的路 2018-09-06
zhuoxun 2015-08-23
teamvx 2015-08-25
WangPing 2015-04-17