Pytorch入門與實踐——AI插畫師:生成對抗網絡數據集制作
目錄

摘要
1、用爬蟲爬取二次元妹子的圖片
2、獲取圖片中的頭像
摘要
最近想搞一搞GAN,但是發現《pytorch入門與實踐——AI插畫師:生成對抗網絡》,但是發現數據集的鏈接失效了,所以自己制作一份。
代碼來自何之源 - 知乎,我做了一些修改。
1、用爬蟲爬取二次元妹子的圖片
數據從konachan.net - Konachan.com Anime Wallpapers網站中下載的,是一個非常著名的動漫網站(不過我不知道)代碼如下:
import requests from bs4 import BeautifulSoup import os import traceback def download(url, filename): if os.path.exists(filename): print('file exists!') return try: r = requests.get(url, stream=True, timeout=60) r.raise_for_status() with open(filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter out keep-alive new chunks f.write(chunk) f.flush() return filename except KeyboardInterrupt: if os.path.exists(filename): os.remove(filename) raise KeyboardInterrupt except Exception: traceback.print_exc() if os.path.exists(filename): os.remove(filename) if os.path.exists('imgs') is False: os.makedirs('imgs') start =1 end = 8000 for i in range(start, end + 1): url = 'https://konachan.net/post?page=%d&tags=' % i html = requests.get(url).text soup = BeautifulSoup(html, 'html.parser') for img in soup.find_all('img', class_="preview"): target_url =img['src'] filename = os.path.join('imgs', target_url.split('/')[-1]) download(target_url, filename) print('%d / %d' % (i, end))
運行代碼后就能在imgs文件夾看到二次元妹子的照片,各種各樣的,目不暇接、眼花繚亂。。。。。
2、獲取圖片中的頭像
截取頭像和原文一樣,直接使用github上一個基于opencv的工具,地址:https://github.com/nagadomi/lbpcascade_animeface,將lbpcascade_animeface.xml(準確率挺高的,不過有點猥瑣,大家試一下就知道了。。。。。。)文件,放到根目錄下。
然后運行下面的代碼:
import cv2 import sys import os.path from glob import glob def detect(filename, cascade_file="lbpcascade_animeface.xml"): if not os.path.isfile(cascade_file): raise RuntimeError("%s: not found" % cascade_file) cascade = cv2.CascadeClassifier(cascade_file) image = cv2.imread(filename) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.equalizeHist(gray) faces = cascade.detectMultiScale(gray, # detector options scaleFactor=1.1, minNeighbors=5, minSize=(48, 48)) for i, (x, y, w, h) in enumerate(faces): face = image[y: y + h, x:x + w, :] face = cv2.resize(face, (96, 96)) save_filename = '%s-%d.jpg' % (os.path.basename(filename).split('.')[0], i) cv2.imwrite("faces/" + save_filename, face) if __name__ == '__main__': if os.path.exists('faces') is False: os.makedirs('faces') file_list = glob('imgs/*.jpg') for filename in file_list: detect(filename)
隨便放幾張截取后的頭像:
連接是我制作的數據:AI插畫師:生成對抗網絡數據集.zip-深度學習文檔類資源-CSDN下載
運行上面的代碼就可以截取二次元妹子的頭像了,到這里數據集制作完成了,然后我們一起GAN。如果你覺得有幫助請、,也可以打賞,多少隨意。
網站
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。