有一個(gè)需求:需要大量的人臉數(shù)據(jù)
找到一個(gè)網(wǎng)站可以拿到生成人臉圖片
https://thispersondoesnotexist.com/image
import requests
from faker import Faker
import requests
import threading
import time
from datetime import datetime
faker = Faker (locale='zh_CN')
def thread_func(): # 線程函數(shù)
image_path = "image/{}.jpg".format(faker.name())
req = requests.get("https://thispersondoesnotexist.com/image")
file = open(image_path, 'wb')
file.write(req.content)
file.close()
def many_thread():
threads = []
for _ in range(50): # 循環(huán)創(chuàng)建線程
t = threading.Thread(target=thread_func)
threads.append(t)
t.setDaemon(True) # 給每個(gè)子線程添加守護(hù)線程
for t in threads: # 循環(huán)啟線程
t.start()
for t in threads:
t.join(2) # 設(shè)置子線程超時(shí)2秒
if __name__ == '__main__':
for i in range(4):
many_thread()