―― Tatsächlich werden Bilder wie $ wget -i urls.txt nacheinander heruntergeladen.
Anfragen herunter
--Überprüfen und konvertieren Sie die Bilddaten "Kissen"$ pip install pillow requests
DOWNLOAD_PATH herunter.
――Für Details lesen Sie bitte den vorherigen Artikel.$ cat config.py
CLASSES = [
    'Abe Otsu',
    'Satomi Ishihara',
    'Yuno Ohara',
    'Koshiba Fuka',
    'Haruna Kawaguchi',
    'Nana Mori',
    'Minami Hamabe',
    'Kaya Kiyohara',
    'Haruka Fukuhara',
    'Kuroshima Yuina'
]
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_PATH = os.path.join(BASE_PATH, 'data')
LINK_PATH = os.path.join(DATA_PATH, 'link')
DOWNLOAD_PATH = os.path.join(DATA_PATH, 'download')
$Kopf Kuroshima Yuina.txt
http://cm-watch.net/wp-content/uploads/2018/03/b22dc3193fd35ebb1bf7aa4e74c8cffb.jpg
https://www.crank-in.net/img/db/1165407_650.jpg
https://media.image.infoseek.co.jp/isnews/photos/hwchannel/hwchannel_20191107_7062003_0-small.jpg
https://i.pinimg.com/originals/3e/3c/61/3e3c61df2f426a8e4623b58d84d94b40.jpg
http://yukutaku.net/blog/wp-content/uploads/wordpress-popular-posts/253-100x100.jpg
http://gratitude8888.biz/wp-content/uploads/2017/03/cb1175590da467bef3600df48eabf770.jpg
https://www.cinemacafe.net/imgs/p/ATDRThl-6oWF9fpps9341csCOg8ODQwLCgkI/416673.jpg
https://s3-ap-northeast-1.amazonaws.com/moviche-uploads/wp-content/uploads/2019/10/IMG_2547.jpg
https://scontent-frx5-1.cdninstagram.com/vp/05d6926fed565f82247879638771ee46/5E259FCC/t51.2885-15/e35/67735702_2288175727962135_1310736136046930744_n.jpg?_nc_ht=scontent-frx5-1.cdninstagram.com&_nc_cat=103&se=7&ig_cache_key=MjEyMzM1MTc4NDkyMzQ4NzgxMg%3D%3D.2
http://moco-garden.com/wp-content/uploads/2016/05/kurosimayuina.jpg
--Lesen Sie die Datei, in der die zuletzt erstellte URL mit einem Zeilenumbruch beschrieben wird.
def download(query):
    """Daten herunterladen, Daten überprüfen, Bilder speichern."""
    linkfile = os.path.join(LINK_PATH, '{}.txt'.format(query))
    if not os.path.isfile(linkfile):
        print('no linkfile: {}'.format(linkfile))
        return
    with open(linkfile, 'r') as fin:
        link_list = fin.read().split('\n')[:-1]
image /kann jpeg`` png gif`` bmp sein.    for num, link in enumerate(link_list, start=1):
        try:
            result = requests.get(link)
            content = result.content
            content_type = result.headers['Content-Type']
        except Exception as err:
            print('err: {}, link: {}'.format(err, link))
            continue
        if not content_type.startswith('image/'):
            print('err: {}, link: {}'.format(content_type, link))
            continue
――Wenn Sie Folgendes einstellen, werden auch große Bilder gelesen.
ImageFile.LOAD_TRUNCATED_IMAGES = True
--Lesen Sie die Bilddaten mit "Kissen".
        try:
            image = Image.open(io.BytesIO(content))
        except Exception as err:
            print('err: {}, link: {}'.format(err, link))
            continue
――Wenn Sie über den Nachprozess nachdenken, ist es meiner Meinung nach schwierig, ihn zu verarbeiten, wenn Sie den Fall von ".png " und ".bmp" nacheinander betrachten.
        if image.mode != 'RGB':
            image = image.convert('RGB')
        data = io.BytesIO()
        image.save(data, 'jpeg', optimize=True, quality=95)
        content = data.getvalue()
DOWNLOAD_PATH unter einem Dateinamen wie 0001.jpeg`` 0002.jpeg.        filename = os.path.join(DOWNLOAD_PATH, query, '{:04d}.jpeg'.format(num))
        with open(filename, 'wb') as fout:
            fout.write(content)
        print('query: {}, filename: {}, link: {}'.format(query, os.path.basename(filename), link))
$ awk '{print $2}' err.txt | sort | uniq -c | sort -nr
  47 text/html;
  31 text/plain,
  30 ('Connection
  27 text/html,
  18 'content-type',
  10 cannot
   5 application/octet-stream,
   2 application/xml,
   1 images
   1 binary/octet-stream,
   1 UserWarning:
   1 HTTPSConnectionPool(host='jpnews24h.com',
   1 HTTPSConnectionPool(host='host-your-site.net',
   1 HTTPSConnectionPool(host='gamers.co.jp',
   1 HTTPConnectionPool(host='youtube.dojin.com',
   1 HTTPConnectionPool(host='nosh.media',
   1 HTTPConnectionPool(host='arukunews.jp',
   1 Exceeded
-- $ wget -i urls.txt behebt den juckenden Teil, der etwas unerreichbar ist.
――Nächstes Mal planen wir die Gesichtserkennung anhand von Bildern.
Recommended Posts