人脸检测颜值
pexels 提供了大量,从中搜索,编写爬虫进行,后图片中除了女人外,还 男人,风景、静物和动物,调用人脸检测模块识别检测,将其中颜值大于60分的美女保存到另外一个文件夹。爬取图片共计1251张,最后过滤出的美女共计287张。上代码:
爬虫程序:
from bs4 import BeautifulSoupimport requestsimport osimport timesave_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 =1for 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.closepage = page+1time.sleep(2)颜值检测程序:from aip import AipFaceimport base64import osimport timeimport 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:#没有人脸continueif error_code==223110:#人脸太多continuetry:sex_type = result[‘result’][‘face_list’][-1][‘gender’][‘type’]#只要if sex_type == ‘male’:continuebeauty = 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:passexcept TypeError:pass
爬取的图片:
过滤后的图片:
看看更高颜值的美女:
较低颜值的女性:
PS:如果觉得本篇 对您有所帮助,欢迎 、订阅!帮忙点个 再看 转发一下分享出去
*声明:本文于 络整理, 归原 所有,如 信息有误或权益,请 我们 或 事宜。