-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpersondoesnotexist_toimg.py
60 lines (50 loc) · 1.91 KB
/
persondoesnotexist_toimg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""PersonDoesNotExist_ToImg.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1uQwIDz3mqPovEVvdYRozQhVSFg4UdES5
Welcome,
this little script should make a collage from different images of persons. The data is provided
by https://thispersondoesnotexist.com/image. This is an open-source GAN and generates faces which
does not exist. The images will be downloaded and then stored together with 24 other ones. (One resulting
image has 25 different persons in it; 5x5 Matrix)
Thank you for beeing here (=
"""
from urllib.request import Request, urlopen
import time
import numpy as np
import cv2
import random
from tqdm import tqdm
url = 'https://thispersondoesnotexist.com/image'
raw_imgs = []
stacked_imgs = []
index = 1
pbar = tqdm(total=25, desc='Image container ' + str(index))
while True:
pbar.update(1)
start = time.time()
# download image
req = Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:77.0) Gecko/20190101 Firefox/77.0'})
webpage = urlopen(req).read()
# convert it to cv2 image
image = np.asarray(bytearray(urlopen(req).read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
raw_imgs.append(cv2.resize(image, (256, 256)))
if len(raw_imgs) >= 5:
# if 5 faces are avaible,
# it will put them horizontalyy together
stacked_imgs.append(np.concatenate(([i for i in raw_imgs]), axis=1))
raw_imgs = []
if len(stacked_imgs) >= 5:
# if 5 horitontally images are avaible,
# they will be stacked vertically and saved
img = np.concatenate(([i for i in stacked_imgs]), axis=0)
stacked_imgs = []
cv2.imwrite(f'img{random.randint(10000,100000)}.jpg', img)
pbar.close()
index += 1
pbar = tqdm(total=25, desc='Image container ' + str(index))
while (time.time() - start) < 3:
# wait to let the GAN generate a new one
time.sleep(0.2)